Skip to content

Commit

Permalink
WAF: Improves the invoking waf api method and supports refreshing cre…
Browse files Browse the repository at this point in the history
…dential automatically
  • Loading branch information
xiaozhu36 committed Nov 13, 2024
1 parent 0ad8554 commit 0ed75c9
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 215 deletions.
7 changes: 2 additions & 5 deletions alicloud/data_source_alicloud_waf_certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,12 @@ func dataSourceAlicloudWafCertificatesRead(d *schema.ResourceData, meta interfac
}
}
var response map[string]interface{}
conn, err := client.NewWafClient()
if err != nil {
return WrapError(err)
}
var err error
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-09-10"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("waf-openapi", "2019-09-10", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
7 changes: 2 additions & 5 deletions alicloud/data_source_alicloud_waf_domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,10 @@ func dataSourceAlicloudWafDomainsRead(d *schema.ResourceData, meta interface{})
}
}
var response map[string]interface{}
conn, err := client.NewWafClient()
if err != nil {
return WrapError(err)
}
var err error
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-09-10"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("waf-openapi", "2019-09-10", action, nil, request, true)
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_waf_domains", action, AlibabaCloudSdkGoERROR)
}
Expand Down
7 changes: 2 additions & 5 deletions alicloud/data_source_alicloud_waf_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,10 @@ func dataSourceAlicloudWafInstancesRead(d *schema.ResourceData, meta interface{}
}
status, statusOk := d.GetOkExists("status")
var response map[string]interface{}
conn, err := client.NewWafClient()
if err != nil {
return WrapError(err)
}
var err error
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-09-10"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("waf-openapi", "2019-09-10", action, nil, request, true)
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_waf_instances", action, AlibabaCloudSdkGoERROR)
}
Expand Down
7 changes: 2 additions & 5 deletions alicloud/data_source_alicloud_wafv3_domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,14 @@ func dataSourceAliCloudWafv3DomainsRead(d *schema.ResourceData, meta interface{}
}

var response map[string]interface{}
conn, err := client.NewWafClient()
if err != nil {
return WrapError(err)
}
var err error

for {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2021-10-01"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("waf-openapi", "2021-10-01", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
9 changes: 2 additions & 7 deletions alicloud/data_source_alicloud_wafv3_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,26 @@ func dataSourceAlicloudWafv3Instances() *schema.Resource {

func dataSourceAlicloudWafv3InstancesRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)

request := map[string]interface{}{
"RegionId": client.RegionId,
}

conn, err := client.NewWafClient()
if err != nil {
return WrapError(err)
}
var err error
var objects []interface{}
var response map[string]interface{}
action := "DescribeInstance"
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, err := conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2021-10-01"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("waf-openapi", "2021-10-01", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
response = resp
addDebug(action, response, request)
return nil
})
Expand Down
10 changes: 3 additions & 7 deletions alicloud/resource_alicloud_waf_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"
"time"

util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -63,10 +62,7 @@ func resourceAlicloudWafCertificate() *schema.Resource {
func resourceAlicloudWafCertificateCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
request := make(map[string]interface{})
conn, err := client.NewWafClient()
if err != nil {
return WrapError(err)
}
var err error
var response map[string]interface{}
if sslId, ok := d.GetOk("certificate_id"); ok {
action := "CreateCertificateByCertificateId"
Expand All @@ -77,7 +73,7 @@ func resourceAlicloudWafCertificateCreate(d *schema.ResourceData, meta interface
}
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-09-10"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPost("waf-openapi", "2019-09-10", action, nil, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -117,7 +113,7 @@ func resourceAlicloudWafCertificateCreate(d *schema.ResourceData, meta interface

wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-09-10"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPost("waf-openapi", "2019-09-10", action, nil, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
26 changes: 5 additions & 21 deletions alicloud/resource_alicloud_waf_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"

util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
Expand Down Expand Up @@ -151,10 +150,7 @@ func resourceAlicloudWafDomainCreate(d *schema.ResourceData, meta interface{}) e
var response map[string]interface{}
action := "CreateDomain"
request := make(map[string]interface{})
conn, err := client.NewWafClient()
if err != nil {
return WrapError(err)
}
var err error
if v, ok := d.GetOk("cluster_type"); ok {
request["ClusterType"] = convertClusterTypeRequest(v.(string))
}
Expand Down Expand Up @@ -224,7 +220,7 @@ func resourceAlicloudWafDomainCreate(d *schema.ResourceData, meta interface{}) e

wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(3*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-09-10"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPost("waf-openapi", "2019-09-10", action, nil, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -309,13 +305,9 @@ func resourceAlicloudWafDomainUpdate(d *schema.ResourceData, meta interface{}) e
}
request["ClusterType"] = convertClusterTypeRequest(d.Get("cluster_type").(string))
action := "ModifyDomainClusterType"
conn, err := client.NewWafClient()
if err != nil {
return WrapError(err)
}
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(3*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-09-10"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPost("waf-openapi", "2019-09-10", action, nil, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -390,13 +382,9 @@ func resourceAlicloudWafDomainUpdate(d *schema.ResourceData, meta interface{}) e
request["WriteTime"] = d.Get("write_time")
if update {
action := "ModifyDomain"
conn, err := client.NewWafClient()
if err != nil {
return WrapError(err)
}
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(3*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-09-10"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPost("waf-openapi", "2019-09-10", action, nil, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -434,18 +422,14 @@ func resourceAlicloudWafDomainDelete(d *schema.ResourceData, meta interface{}) e
}
action := "DeleteDomain"
var response map[string]interface{}
conn, err := client.NewWafClient()
if err != nil {
return WrapError(err)
}
request := map[string]interface{}{
"Domain": parts[1],
"InstanceId": parts[0],
}

wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(3*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-09-10"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPost("waf-openapi", "2019-09-10", action, nil, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
9 changes: 3 additions & 6 deletions alicloud/resource_alicloud_waf_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ func testSweepWafDomains(region string) error {
wafInstanceIds := make([]string, 0)
domainIds := make([]string, 0)
request := make(map[string]interface{})
conn, err := client.NewWafClient()
if err != nil {
return WrapError(err)
}
var err error
action := "DescribeInstanceInfos"

response, err := conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-09-10"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
Expand All @@ -64,7 +61,7 @@ func testSweepWafDomains(region string) error {
action = "DescribeDomainNames"
request = make(map[string]interface{})
request["InstanceId"] = instanceId
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-09-10"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPost("waf-openapi", "2019-09-10", action, nil, request, false)
if err != nil {
log.Printf("[ERROR] Failed to retrieve waf domain in service list: %s", err)
}
Expand Down Expand Up @@ -95,7 +92,7 @@ func testSweepWafDomains(region string) error {
action = "DeleteDomain"
request["InstanceId"] = part[0]
request["Domain"] = name
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-09-10"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPost("waf-openapi", "2019-09-10", action, nil, request, false)
if err != nil {
log.Printf("[ERROR] Failed to delete WAF domain (%s): %s", id, err)
}
Expand Down
28 changes: 10 additions & 18 deletions alicloud/resource_alicloud_waf_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"

util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
Expand Down Expand Up @@ -100,12 +99,10 @@ func resourceAlicloudWafInstance() *schema.Resource {
func resourceAlicloudWafInstanceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
var response map[string]interface{}
var err error
var endpoint string
action := "CreateInstance"
request := make(map[string]interface{})
conn, err := client.NewBssopenapiClient()
if err != nil {
return WrapError(err)
}
if v, ok := d.GetOk("period"); ok {
request["Period"] = v
}
Expand Down Expand Up @@ -169,14 +166,14 @@ func resourceAlicloudWafInstanceCreate(d *schema.ResourceData, meta interface{})
}
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(3*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2017-12-14"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPostWithEndpoint("BssOpenApi", "2017-12-14", action, nil, request, true, endpoint)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
if IsExpectedErrors(err, []string{"NotApplicable"}) {
conn.Endpoint = String(connectivity.BssOpenAPIEndpointInternational)
endpoint = connectivity.BssOpenAPIEndpointInternational
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
Expand Down Expand Up @@ -213,11 +210,9 @@ func resourceAlicloudWafInstanceRead(d *schema.ResourceData, meta interface{}) e
}
func resourceAlicloudWafInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
conn, err := client.NewBssopenapiClient()
if err != nil {
return WrapError(err)
}
var response map[string]interface{}
var err error
var endpoint string
update := false
request := map[string]interface{}{
"InstanceId": d.Id(),
Expand Down Expand Up @@ -271,14 +266,14 @@ func resourceAlicloudWafInstanceUpdate(d *schema.ResourceData, meta interface{})
action := "ModifyInstance"
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(3*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2017-12-14"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPostWithEndpoint("BssOpenApi", "2017-12-14", action, nil, request, true, endpoint)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
if IsExpectedErrors(err, []string{"NotApplicable"}) {
conn.Endpoint = String(connectivity.BssOpenAPIEndpointInternational)
endpoint = connectivity.BssOpenAPIEndpointInternational
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
Expand All @@ -300,10 +295,7 @@ func resourceAlicloudWafInstanceDelete(d *schema.ResourceData, meta interface{})
client := meta.(*connectivity.AliyunClient)
action := "DeleteInstance"
var response map[string]interface{}
conn, err := client.NewWafClient()
if err != nil {
return WrapError(err)
}
var err error
request := map[string]interface{}{
"InstanceId": d.Id(),
}
Expand All @@ -313,7 +305,7 @@ func resourceAlicloudWafInstanceDelete(d *schema.ResourceData, meta interface{})
}
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(3*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-09-10"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPost("waf-openapi", "2019-09-10", action, nil, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
Loading

0 comments on commit 0ed75c9

Please sign in to comment.