diff --git a/alicloud/provider.go b/alicloud/provider.go index 40aa0288b93d..f54912d7de06 100644 --- a/alicloud/provider.go +++ b/alicloud/provider.go @@ -888,6 +888,9 @@ func Provider() terraform.ResourceProvider { "alicloud_vpc_ipam_ipams": dataSourceAliCloudVpcIpamIpams(), }, ResourcesMap: map[string]*schema.Resource{ + "alicloud_esa_rewrite_url_rule": resourceAliCloudEsaRewriteUrlRule(), + "alicloud_esa_redirect_rule": resourceAliCloudEsaRedirectRule(), + "alicloud_esa_http_response_header_modification_rule": resourceAliCloudEsaHttpResponseHeaderModificationRule(), "alicloud_max_compute_tunnel_quota_timer": resourceAliCloudMaxComputeTunnelQuotaTimer(), "alicloud_max_compute_role_user_attachment": resourceAliCloudMaxComputeRoleUserAttachment(), "alicloud_max_compute_quota_schedule": resourceAliCloudMaxComputeQuotaSchedule(), diff --git a/alicloud/resource_alicloud_esa_http_response_header_modification_rule.go b/alicloud/resource_alicloud_esa_http_response_header_modification_rule.go new file mode 100644 index 000000000000..95b4037c39d8 --- /dev/null +++ b/alicloud/resource_alicloud_esa_http_response_header_modification_rule.go @@ -0,0 +1,326 @@ +// Package alicloud. This file is generated automatically. Please do not modify it manually, thank you! +package alicloud + +import ( + "encoding/json" + "fmt" + "log" + "strings" + "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" +) + +func resourceAliCloudEsaHttpResponseHeaderModificationRule() *schema.Resource { + return &schema.Resource{ + Create: resourceAliCloudEsaHttpResponseHeaderModificationRuleCreate, + Read: resourceAliCloudEsaHttpResponseHeaderModificationRuleRead, + Update: resourceAliCloudEsaHttpResponseHeaderModificationRuleUpdate, + Delete: resourceAliCloudEsaHttpResponseHeaderModificationRuleDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(5 * time.Minute), + Update: schema.DefaultTimeout(5 * time.Minute), + Delete: schema.DefaultTimeout(5 * time.Minute), + }, + Schema: map[string]*schema.Schema{ + "config_id": { + Type: schema.TypeInt, + Computed: true, + }, + "response_header_modification": { + Type: schema.TypeList, + Required: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "value": { + Type: schema.TypeString, + Optional: true, + }, + "operation": { + Type: schema.TypeString, + Required: true, + }, + "name": { + Type: schema.TypeString, + Required: true, + }, + }, + }, + }, + "rule": { + Type: schema.TypeString, + Optional: true, + }, + "rule_enable": { + Type: schema.TypeString, + Optional: true, + }, + "rule_name": { + Type: schema.TypeString, + Optional: true, + }, + "site_id": { + Type: schema.TypeInt, + Required: true, + ForceNew: true, + }, + "site_version": { + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + }, + }, + } +} + +func resourceAliCloudEsaHttpResponseHeaderModificationRuleCreate(d *schema.ResourceData, meta interface{}) error { + + client := meta.(*connectivity.AliyunClient) + + action := "CreateHttpResponseHeaderModificationRule" + var request map[string]interface{} + var response map[string]interface{} + query := make(map[string]interface{}) + conn, err := client.NewEsaClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + if v, ok := d.GetOk("site_id"); ok { + request["SiteId"] = v + } + + if v, ok := d.GetOkExists("site_version"); ok { + request["SiteVersion"] = v + } + if v, ok := d.GetOk("response_header_modification"); ok { + responseHeaderModificationMapsArray := make([]interface{}, 0) + for _, dataLoop := range v.([]interface{}) { + dataLoopTmp := dataLoop.(map[string]interface{}) + dataLoopMap := make(map[string]interface{}) + dataLoopMap["Value"] = dataLoopTmp["value"] + dataLoopMap["Name"] = dataLoopTmp["name"] + dataLoopMap["Operation"] = dataLoopTmp["operation"] + responseHeaderModificationMapsArray = append(responseHeaderModificationMapsArray, dataLoopMap) + } + responseHeaderModificationMapsJson, err := json.Marshal(responseHeaderModificationMapsArray) + if err != nil { + return WrapError(err) + } + request["ResponseHeaderModification"] = string(responseHeaderModificationMapsJson) + } + + if v, ok := d.GetOk("rule_enable"); ok { + request["RuleEnable"] = v + } + if v, ok := d.GetOk("rule_name"); ok { + request["RuleName"] = v + } + if v, ok := d.GetOk("rule"); ok { + request["Rule"] = v + } + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2024-09-10"), StringPointer("AK"), query, request, &runtime) + if err != nil { + if NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + return nil + }) + addDebug(action, response, request) + + if err != nil { + return WrapErrorf(err, DefaultErrorMsg, "alicloud_esa_http_response_header_modification_rule", action, AlibabaCloudSdkGoERROR) + } + + d.SetId(fmt.Sprintf("%v:%v", request["SiteId"], response["ConfigId"])) + + return resourceAliCloudEsaHttpResponseHeaderModificationRuleRead(d, meta) +} + +func resourceAliCloudEsaHttpResponseHeaderModificationRuleRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*connectivity.AliyunClient) + esaServiceV2 := EsaServiceV2{client} + + objectRaw, err := esaServiceV2.DescribeEsaHttpResponseHeaderModificationRule(d.Id()) + if err != nil { + if !d.IsNewResource() && NotFoundError(err) { + log.Printf("[DEBUG] Resource alicloud_esa_http_response_header_modification_rule DescribeEsaHttpResponseHeaderModificationRule Failed!!! %s", err) + d.SetId("") + return nil + } + return WrapError(err) + } + + if objectRaw["Rule"] != nil { + d.Set("rule", objectRaw["Rule"]) + } + if objectRaw["RuleEnable"] != nil { + d.Set("rule_enable", objectRaw["RuleEnable"]) + } + if objectRaw["RuleName"] != nil { + d.Set("rule_name", objectRaw["RuleName"]) + } + if objectRaw["SiteVersion"] != nil { + d.Set("site_version", objectRaw["SiteVersion"]) + } + if objectRaw["ConfigId"] != nil { + d.Set("config_id", objectRaw["ConfigId"]) + } + + responseHeaderModification2Raw := objectRaw["ResponseHeaderModification"] + responseHeaderModificationMaps := make([]map[string]interface{}, 0) + if responseHeaderModification2Raw != nil { + for _, responseHeaderModificationChild2Raw := range responseHeaderModification2Raw.([]interface{}) { + responseHeaderModificationMap := make(map[string]interface{}) + responseHeaderModificationChild2Raw := responseHeaderModificationChild2Raw.(map[string]interface{}) + responseHeaderModificationMap["name"] = responseHeaderModificationChild2Raw["Name"] + responseHeaderModificationMap["operation"] = responseHeaderModificationChild2Raw["Operation"] + responseHeaderModificationMap["value"] = responseHeaderModificationChild2Raw["Value"] + + responseHeaderModificationMaps = append(responseHeaderModificationMaps, responseHeaderModificationMap) + } + } + if objectRaw["ResponseHeaderModification"] != nil { + if err := d.Set("response_header_modification", responseHeaderModificationMaps); err != nil { + return err + } + } + + parts := strings.Split(d.Id(), ":") + d.Set("site_id", formatInt(parts[0])) + + return nil +} + +func resourceAliCloudEsaHttpResponseHeaderModificationRuleUpdate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*connectivity.AliyunClient) + var request map[string]interface{} + var response map[string]interface{} + var query map[string]interface{} + update := false + + parts := strings.Split(d.Id(), ":") + action := "UpdateHttpResponseHeaderModificationRule" + conn, err := client.NewEsaClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + request["ConfigId"] = parts[1] + request["SiteId"] = parts[0] + + if d.HasChange("response_header_modification") { + update = true + } + if v, ok := d.GetOk("response_header_modification"); ok || d.HasChange("response_header_modification") { + responseHeaderModificationMapsArray := make([]interface{}, 0) + for _, dataLoop := range v.([]interface{}) { + dataLoopTmp := dataLoop.(map[string]interface{}) + dataLoopMap := make(map[string]interface{}) + dataLoopMap["Value"] = dataLoopTmp["value"] + dataLoopMap["Name"] = dataLoopTmp["name"] + dataLoopMap["Operation"] = dataLoopTmp["operation"] + responseHeaderModificationMapsArray = append(responseHeaderModificationMapsArray, dataLoopMap) + } + responseHeaderModificationMapsJson, err := json.Marshal(responseHeaderModificationMapsArray) + if err != nil { + return WrapError(err) + } + request["ResponseHeaderModification"] = string(responseHeaderModificationMapsJson) + } + + if d.HasChange("rule_enable") { + update = true + request["RuleEnable"] = d.Get("rule_enable") + } + + if d.HasChange("rule_name") { + update = true + request["RuleName"] = d.Get("rule_name") + } + + if d.HasChange("rule") { + update = true + request["Rule"] = d.Get("rule") + } + + if update { + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2024-09-10"), StringPointer("AK"), query, request, &runtime) + if err != nil { + if NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + return nil + }) + addDebug(action, response, request) + if err != nil { + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) + } + } + + return resourceAliCloudEsaHttpResponseHeaderModificationRuleRead(d, meta) +} + +func resourceAliCloudEsaHttpResponseHeaderModificationRuleDelete(d *schema.ResourceData, meta interface{}) error { + + client := meta.(*connectivity.AliyunClient) + parts := strings.Split(d.Id(), ":") + action := "DeleteHttpResponseHeaderModificationRule" + var request map[string]interface{} + var response map[string]interface{} + query := make(map[string]interface{}) + conn, err := client.NewEsaClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + request["ConfigId"] = parts[1] + request["SiteId"] = parts[0] + + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2024-09-10"), StringPointer("AK"), query, request, &runtime) + + if err != nil { + if NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + return nil + }) + addDebug(action, response, request) + + if err != nil { + if NotFoundError(err) { + return nil + } + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) + } + + return nil +} diff --git a/alicloud/resource_alicloud_esa_http_response_header_modification_rule_test.go b/alicloud/resource_alicloud_esa_http_response_header_modification_rule_test.go new file mode 100644 index 000000000000..195f5643d442 --- /dev/null +++ b/alicloud/resource_alicloud_esa_http_response_header_modification_rule_test.go @@ -0,0 +1,162 @@ +package alicloud + +import ( + "fmt" + "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "testing" +) + +// Test ESA HttpResponseHeaderModificationRule. >>> Resource test cases, automatically generated. +// Case httpResponseHeaderModificationRule_test +func TestAccAliCloudESAHttpResponseHeaderModificationRulehttpResponseHeaderModificationRule_test(t *testing.T) { + var v map[string]interface{} + resourceId := "alicloud_esa_http_response_header_modification_rule.default" + ra := resourceAttrInit(resourceId, AliCloudESAHttpResponseHeaderModificationRulehttpResponseHeaderModificationRule_testMap) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &EsaServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeEsaHttpResponseHeaderModificationRule") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testacc%sESAHttpResponseHeaderModificationRule%d", defaultRegionToTest, rand) + + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AliCloudESAHttpResponseHeaderModificationRulehttpResponseHeaderModificationRule_testBasicDependence) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckWithRegions(t, true, []connectivity.Region{"cn-hangzhou"}) + }, + IDRefreshName: resourceId, + Providers: testAccProviders, + CheckDestroy: rac.checkResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAccConfig(map[string]interface{}{ + "site_id": "${alicloud_esa_site.resource_Site_HttpResponseHeaderModificationRule_test.id}", + "rule_enable": "on", + "response_header_modification": []map[string]interface{}{ + + { + "value": "add", + "operation": "add", + "name": "testadd", + }, + + { + "operation": "del", + "name": "testdel", + }, + + { + "value": "modify", + "operation": "modify", + "name": "testmodify", + }, + }, + "rule": "(http.host eq \\\"video.example.com\\\")", + "site_version": "0", + "rule_name": "testResponseHeader", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "rule_name": "testResponseHeader_modify", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "rule_enable": "off", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "rule": "(http.request.uri eq \\\"/content?page=1234\\\")", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "response_header_modification": []map[string]interface{}{ + + { + "value": "add1", + "operation": "add", + "name": "testadd1", + }, + }, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "rule_enable": "on", + "rule": "(http.host eq \\\"api.example.com\\\")", + "rule_name": "test_httpResponseHeader_last", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{}), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{}, + }, + }, + }) +} + +var AliCloudESAHttpResponseHeaderModificationRulehttpResponseHeaderModificationRule_testMap = map[string]string{ + "id": CHECKSET, +} + +func AliCloudESAHttpResponseHeaderModificationRulehttpResponseHeaderModificationRule_testBasicDependence(name string) string { + return fmt.Sprintf(` +variable "name" { + default = "%s" +} + + +resource "alicloud_esa_rate_plan_instance" "resource_HttpResponseHeaderModificationRule_test" { + type = "NS" + auto_renew = "false" + period = "1" + payment_type = "Subscription" + coverage = "overseas" + auto_pay = "true" + plan_name = "high" +} + +resource "alicloud_esa_site" "resource_Site_HttpResponseHeaderModificationRule_test" { + site_name = "gositecdn.cn" + instance_id = alicloud_esa_rate_plan_instance.resource_HttpResponseHeaderModificationRule_test.id + coverage = "overseas" + access_type = "NS" +} + +`, name) +} + +// Test ESA HttpResponseHeaderModificationRule. <<< Resource test cases, automatically generated. diff --git a/alicloud/resource_alicloud_esa_redirect_rule.go b/alicloud/resource_alicloud_esa_redirect_rule.go new file mode 100644 index 000000000000..4f8a3e1c0f24 --- /dev/null +++ b/alicloud/resource_alicloud_esa_redirect_rule.go @@ -0,0 +1,300 @@ +// Package alicloud. This file is generated automatically. Please do not modify it manually, thank you! +package alicloud + +import ( + "fmt" + "log" + "strings" + "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" +) + +func resourceAliCloudEsaRedirectRule() *schema.Resource { + return &schema.Resource{ + Create: resourceAliCloudEsaRedirectRuleCreate, + Read: resourceAliCloudEsaRedirectRuleRead, + Update: resourceAliCloudEsaRedirectRuleUpdate, + Delete: resourceAliCloudEsaRedirectRuleDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(5 * time.Minute), + Update: schema.DefaultTimeout(5 * time.Minute), + Delete: schema.DefaultTimeout(5 * time.Minute), + }, + Schema: map[string]*schema.Schema{ + "config_id": { + Type: schema.TypeInt, + Computed: true, + }, + "reserve_query_string": { + Type: schema.TypeString, + Required: true, + ValidateFunc: StringInSlice([]string{"on", "off"}, false), + }, + "rule": { + Type: schema.TypeString, + Optional: true, + }, + "rule_enable": { + Type: schema.TypeString, + Optional: true, + }, + "rule_name": { + Type: schema.TypeString, + Optional: true, + }, + "site_id": { + Type: schema.TypeInt, + Required: true, + ForceNew: true, + }, + "site_version": { + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + }, + "status_code": { + Type: schema.TypeString, + Required: true, + ValidateFunc: StringInSlice([]string{"301", "302"}, false), + }, + "target_url": { + Type: schema.TypeString, + Required: true, + }, + "type": { + Type: schema.TypeString, + Required: true, + ValidateFunc: StringInSlice([]string{"static"}, false), + }, + }, + } +} + +func resourceAliCloudEsaRedirectRuleCreate(d *schema.ResourceData, meta interface{}) error { + + client := meta.(*connectivity.AliyunClient) + + action := "CreateRedirectRule" + var request map[string]interface{} + var response map[string]interface{} + query := make(map[string]interface{}) + conn, err := client.NewEsaClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + if v, ok := d.GetOk("site_id"); ok { + request["SiteId"] = v + } + + if v, ok := d.GetOkExists("site_version"); ok { + request["SiteVersion"] = v + } + request["ReserveQueryString"] = d.Get("reserve_query_string") + request["TargetUrl"] = d.Get("target_url") + if v, ok := d.GetOk("rule_enable"); ok { + request["RuleEnable"] = v + } + if v, ok := d.GetOk("rule_name"); ok { + request["RuleName"] = v + } + request["Type"] = d.Get("type") + if v, ok := d.GetOk("rule"); ok { + request["Rule"] = v + } + request["StatusCode"] = d.Get("status_code") + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2024-09-10"), StringPointer("AK"), query, request, &runtime) + if err != nil { + if NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + return nil + }) + addDebug(action, response, request) + + if err != nil { + return WrapErrorf(err, DefaultErrorMsg, "alicloud_esa_redirect_rule", action, AlibabaCloudSdkGoERROR) + } + + d.SetId(fmt.Sprintf("%v:%v", request["SiteId"], response["ConfigId"])) + + return resourceAliCloudEsaRedirectRuleRead(d, meta) +} + +func resourceAliCloudEsaRedirectRuleRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*connectivity.AliyunClient) + esaServiceV2 := EsaServiceV2{client} + + objectRaw, err := esaServiceV2.DescribeEsaRedirectRule(d.Id()) + if err != nil { + if !d.IsNewResource() && NotFoundError(err) { + log.Printf("[DEBUG] Resource alicloud_esa_redirect_rule DescribeEsaRedirectRule Failed!!! %s", err) + d.SetId("") + return nil + } + return WrapError(err) + } + + if objectRaw["ReserveQueryString"] != nil { + d.Set("reserve_query_string", objectRaw["ReserveQueryString"]) + } + if objectRaw["Rule"] != nil { + d.Set("rule", objectRaw["Rule"]) + } + if objectRaw["RuleEnable"] != nil { + d.Set("rule_enable", objectRaw["RuleEnable"]) + } + if objectRaw["RuleName"] != nil { + d.Set("rule_name", objectRaw["RuleName"]) + } + if objectRaw["SiteVersion"] != nil { + d.Set("site_version", objectRaw["SiteVersion"]) + } + if objectRaw["StatusCode"] != nil { + d.Set("status_code", objectRaw["StatusCode"]) + } + if objectRaw["TargetUrl"] != nil { + d.Set("target_url", objectRaw["TargetUrl"]) + } + if objectRaw["Type"] != nil { + d.Set("type", objectRaw["Type"]) + } + if objectRaw["ConfigId"] != nil { + d.Set("config_id", objectRaw["ConfigId"]) + } + + parts := strings.Split(d.Id(), ":") + d.Set("site_id", formatInt(parts[0])) + + return nil +} + +func resourceAliCloudEsaRedirectRuleUpdate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*connectivity.AliyunClient) + var request map[string]interface{} + var response map[string]interface{} + var query map[string]interface{} + update := false + + parts := strings.Split(d.Id(), ":") + action := "UpdateRedirectRule" + conn, err := client.NewEsaClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + request["ConfigId"] = parts[1] + request["SiteId"] = parts[0] + + if d.HasChange("reserve_query_string") { + update = true + } + request["ReserveQueryString"] = d.Get("reserve_query_string") + if d.HasChange("target_url") { + update = true + } + request["TargetUrl"] = d.Get("target_url") + if d.HasChange("rule_enable") { + update = true + request["RuleEnable"] = d.Get("rule_enable") + } + + if d.HasChange("rule_name") { + update = true + request["RuleName"] = d.Get("rule_name") + } + + if d.HasChange("type") { + update = true + } + request["Type"] = d.Get("type") + if d.HasChange("rule") { + update = true + request["Rule"] = d.Get("rule") + } + + if d.HasChange("status_code") { + update = true + } + request["StatusCode"] = d.Get("status_code") + if update { + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2024-09-10"), StringPointer("AK"), query, request, &runtime) + if err != nil { + if NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + return nil + }) + addDebug(action, response, request) + if err != nil { + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) + } + } + + return resourceAliCloudEsaRedirectRuleRead(d, meta) +} + +func resourceAliCloudEsaRedirectRuleDelete(d *schema.ResourceData, meta interface{}) error { + + client := meta.(*connectivity.AliyunClient) + parts := strings.Split(d.Id(), ":") + action := "DeleteRedirectRule" + var request map[string]interface{} + var response map[string]interface{} + query := make(map[string]interface{}) + conn, err := client.NewEsaClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + request["ConfigId"] = parts[1] + request["SiteId"] = parts[0] + + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2024-09-10"), StringPointer("AK"), query, request, &runtime) + + if err != nil { + if NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + return nil + }) + addDebug(action, response, request) + + if err != nil { + if NotFoundError(err) { + return nil + } + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) + } + + return nil +} diff --git a/alicloud/resource_alicloud_esa_redirect_rule_test.go b/alicloud/resource_alicloud_esa_redirect_rule_test.go new file mode 100644 index 000000000000..07e9e4d2ce1c --- /dev/null +++ b/alicloud/resource_alicloud_esa_redirect_rule_test.go @@ -0,0 +1,154 @@ +package alicloud + +import ( + "fmt" + "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "testing" +) + +// Test ESA RedirectRule. >>> Resource test cases, automatically generated. +// Case redirectrule_test +func TestAccAliCloudESARedirectRuleredirectrule_test(t *testing.T) { + var v map[string]interface{} + resourceId := "alicloud_esa_redirect_rule.default" + ra := resourceAttrInit(resourceId, AliCloudESARedirectRuleredirectrule_testMap) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &EsaServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeEsaRedirectRule") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testacc%sESARedirectRule%d", defaultRegionToTest, rand) + + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AliCloudESARedirectRuleredirectrule_testBasicDependence) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckWithRegions(t, true, []connectivity.Region{"cn-hangzhou"}) + }, + IDRefreshName: resourceId, + Providers: testAccProviders, + CheckDestroy: rac.checkResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAccConfig(map[string]interface{}{ + "site_id": "${alicloud_esa_site.resource_Site_RedirectRule_test.id}", + "type": "static", + "rule_enable": "on", + "reserve_query_string": "on", + "rule": "(http.host eq \\\"video.example.com\\\")", + "target_url": "http://www.exapmle.com/index.html", + "site_version": "0", + "status_code": "301", + "rule_name": "test", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "rule_name": "test_modify", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "rule_enable": "off", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "rule": "(http.request.uri eq \\\"/content?page=1234\\\")", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "type": "static", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "target_url": "http://www.exapmle.com/index.html", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "status_code": "302", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "reserve_query_string": "off", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{}), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{}, + }, + }, + }) +} + +var AliCloudESARedirectRuleredirectrule_testMap = map[string]string{ + "id": CHECKSET, +} + +func AliCloudESARedirectRuleredirectrule_testBasicDependence(name string) string { + return fmt.Sprintf(` +variable "name" { + default = "%s" +} + + +resource "alicloud_esa_rate_plan_instance" "resource_RedirectRule_test" { + type = "NS" + auto_renew = "false" + period = "1" + payment_type = "Subscription" + coverage = "overseas" + auto_pay = "true" + plan_name = "high" +} + +resource "alicloud_esa_site" "resource_Site_RedirectRule_test" { + site_name = "gositecdn.cn" + instance_id = alicloud_esa_rate_plan_instance.resource_RedirectRule_test.id + coverage = "overseas" + access_type = "NS" +} + +`, name) +} + +// Test ESA RedirectRule. <<< Resource test cases, automatically generated. diff --git a/alicloud/resource_alicloud_esa_rewrite_url_rule.go b/alicloud/resource_alicloud_esa_rewrite_url_rule.go new file mode 100644 index 000000000000..955e53a38fc6 --- /dev/null +++ b/alicloud/resource_alicloud_esa_rewrite_url_rule.go @@ -0,0 +1,312 @@ +// Package alicloud. This file is generated automatically. Please do not modify it manually, thank you! +package alicloud + +import ( + "fmt" + "log" + "strings" + "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" +) + +func resourceAliCloudEsaRewriteUrlRule() *schema.Resource { + return &schema.Resource{ + Create: resourceAliCloudEsaRewriteUrlRuleCreate, + Read: resourceAliCloudEsaRewriteUrlRuleRead, + Update: resourceAliCloudEsaRewriteUrlRuleUpdate, + Delete: resourceAliCloudEsaRewriteUrlRuleDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(5 * time.Minute), + Update: schema.DefaultTimeout(5 * time.Minute), + Delete: schema.DefaultTimeout(5 * time.Minute), + }, + Schema: map[string]*schema.Schema{ + "config_id": { + Type: schema.TypeInt, + Computed: true, + }, + "query_string": { + Type: schema.TypeString, + Optional: true, + }, + "rewrite_query_string_type": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: StringInSlice([]string{"static"}, false), + }, + "rewrite_uri_type": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: StringInSlice([]string{"static"}, false), + }, + "rule": { + Type: schema.TypeString, + Optional: true, + }, + "rule_enable": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: StringInSlice([]string{"on", "off"}, false), + }, + "rule_name": { + Type: schema.TypeString, + Optional: true, + }, + "site_id": { + Type: schema.TypeInt, + Required: true, + ForceNew: true, + }, + "site_version": { + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + }, + "uri": { + Type: schema.TypeString, + Optional: true, + }, + }, + } +} + +func resourceAliCloudEsaRewriteUrlRuleCreate(d *schema.ResourceData, meta interface{}) error { + + client := meta.(*connectivity.AliyunClient) + + action := "CreateRewriteUrlRule" + var request map[string]interface{} + var response map[string]interface{} + query := make(map[string]interface{}) + conn, err := client.NewEsaClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + if v, ok := d.GetOk("site_id"); ok { + request["SiteId"] = v + } + + if v, ok := d.GetOk("rewrite_query_string_type"); ok { + request["RewriteQueryStringType"] = v + } + if v, ok := d.GetOkExists("site_version"); ok { + request["SiteVersion"] = v + } + if v, ok := d.GetOk("uri"); ok { + request["Uri"] = v + } + if v, ok := d.GetOk("rule_enable"); ok { + request["RuleEnable"] = v + } + if v, ok := d.GetOk("query_string"); ok { + request["QueryString"] = v + } + if v, ok := d.GetOk("rule_name"); ok { + request["RuleName"] = v + } + if v, ok := d.GetOk("rule"); ok { + request["Rule"] = v + } + if v, ok := d.GetOk("rewrite_uri_type"); ok { + request["RewriteUriType"] = v + } + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2024-09-10"), StringPointer("AK"), query, request, &runtime) + if err != nil { + if NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + return nil + }) + addDebug(action, response, request) + + if err != nil { + return WrapErrorf(err, DefaultErrorMsg, "alicloud_esa_rewrite_url_rule", action, AlibabaCloudSdkGoERROR) + } + + d.SetId(fmt.Sprintf("%v:%v", request["SiteId"], response["ConfigId"])) + + return resourceAliCloudEsaRewriteUrlRuleRead(d, meta) +} + +func resourceAliCloudEsaRewriteUrlRuleRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*connectivity.AliyunClient) + esaServiceV2 := EsaServiceV2{client} + + objectRaw, err := esaServiceV2.DescribeEsaRewriteUrlRule(d.Id()) + if err != nil { + if !d.IsNewResource() && NotFoundError(err) { + log.Printf("[DEBUG] Resource alicloud_esa_rewrite_url_rule DescribeEsaRewriteUrlRule Failed!!! %s", err) + d.SetId("") + return nil + } + return WrapError(err) + } + + if objectRaw["QueryString"] != nil { + d.Set("query_string", objectRaw["QueryString"]) + } + if objectRaw["RewriteQueryStringType"] != nil { + d.Set("rewrite_query_string_type", objectRaw["RewriteQueryStringType"]) + } + if objectRaw["RewriteUriType"] != nil { + d.Set("rewrite_uri_type", objectRaw["RewriteUriType"]) + } + if objectRaw["Rule"] != nil { + d.Set("rule", objectRaw["Rule"]) + } + if objectRaw["RuleEnable"] != nil { + d.Set("rule_enable", objectRaw["RuleEnable"]) + } + if objectRaw["RuleName"] != nil { + d.Set("rule_name", objectRaw["RuleName"]) + } + if objectRaw["SiteVersion"] != nil { + d.Set("site_version", objectRaw["SiteVersion"]) + } + if objectRaw["Uri"] != nil { + d.Set("uri", objectRaw["Uri"]) + } + if objectRaw["ConfigId"] != nil { + d.Set("config_id", objectRaw["ConfigId"]) + } + + parts := strings.Split(d.Id(), ":") + d.Set("site_id", formatInt(parts[0])) + + return nil +} + +func resourceAliCloudEsaRewriteUrlRuleUpdate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*connectivity.AliyunClient) + var request map[string]interface{} + var response map[string]interface{} + var query map[string]interface{} + update := false + + parts := strings.Split(d.Id(), ":") + action := "UpdateRewriteUrlRule" + conn, err := client.NewEsaClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + request["ConfigId"] = parts[1] + request["SiteId"] = parts[0] + + if d.HasChange("rewrite_query_string_type") { + update = true + request["RewriteQueryStringType"] = d.Get("rewrite_query_string_type") + } + + if d.HasChange("uri") { + update = true + request["Uri"] = d.Get("uri") + } + + if d.HasChange("rule_enable") { + update = true + request["RuleEnable"] = d.Get("rule_enable") + } + + if d.HasChange("query_string") { + update = true + request["QueryString"] = d.Get("query_string") + } + + if d.HasChange("rule_name") { + update = true + request["RuleName"] = d.Get("rule_name") + } + + if d.HasChange("rule") { + update = true + request["Rule"] = d.Get("rule") + } + + if d.HasChange("rewrite_uri_type") { + update = true + request["RewriteUriType"] = d.Get("rewrite_uri_type") + } + + if update { + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2024-09-10"), StringPointer("AK"), query, request, &runtime) + if err != nil { + if IsExpectedErrors(err, []string{"LockFailed"}) || NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + return nil + }) + addDebug(action, response, request) + if err != nil { + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) + } + } + + return resourceAliCloudEsaRewriteUrlRuleRead(d, meta) +} + +func resourceAliCloudEsaRewriteUrlRuleDelete(d *schema.ResourceData, meta interface{}) error { + + client := meta.(*connectivity.AliyunClient) + parts := strings.Split(d.Id(), ":") + action := "DeleteRewriteUrlRule" + var request map[string]interface{} + var response map[string]interface{} + query := make(map[string]interface{}) + conn, err := client.NewEsaClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + request["ConfigId"] = parts[1] + request["SiteId"] = parts[0] + + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2024-09-10"), StringPointer("AK"), query, request, &runtime) + + if err != nil { + if IsExpectedErrors(err, []string{"InternalException"}) || NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + return nil + }) + addDebug(action, response, request) + + if err != nil { + if NotFoundError(err) { + return nil + } + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) + } + + return nil +} diff --git a/alicloud/resource_alicloud_esa_rewrite_url_rule_test.go b/alicloud/resource_alicloud_esa_rewrite_url_rule_test.go new file mode 100644 index 000000000000..cf35dc2a47ad --- /dev/null +++ b/alicloud/resource_alicloud_esa_rewrite_url_rule_test.go @@ -0,0 +1,168 @@ +package alicloud + +import ( + "fmt" + "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "testing" +) + +// Test ESA RewriteUrlRule. >>> Resource test cases, automatically generated. +// Case resource_RewriteUrlRule_test +func TestAccAliCloudESARewriteUrlRuleresource_RewriteUrlRule_test(t *testing.T) { + var v map[string]interface{} + resourceId := "alicloud_esa_rewrite_url_rule.default" + ra := resourceAttrInit(resourceId, AliCloudESARewriteUrlRuleresource_RewriteUrlRule_testMap) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &EsaServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeEsaRewriteUrlRule") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testacc%sESARewriteUrlRule%d", defaultRegionToTest, rand) + + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AliCloudESARewriteUrlRuleresource_RewriteUrlRule_testBasicDependence) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckWithRegions(t, true, []connectivity.Region{"cn-hangzhou"}) + }, + IDRefreshName: resourceId, + Providers: testAccProviders, + CheckDestroy: rac.checkResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAccConfig(map[string]interface{}{ + "site_id": "${alicloud_esa_site.resource_RewriteUrlRule_Site_test.id}", + "rewrite_uri_type": "static", + "rule_enable": "on", + "rewrite_query_string_type": "static", + "query_string": "example=123", + "rule": "http.host eq \\\"video.example.com\\\"", + "site_version": "0", + "uri": "/image/example.jpg", + "rule_name": "test", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "rule": "http.host eq \\\"video.example.com\\\"", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "rule_name": "重写URL规则名称示例", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "rule_enable": "on", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "rewrite_uri_type": "static", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "uri": "/rewritten/target-uri", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "rewrite_query_string_type": "static", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "query_string": "重写后的查询字符串示例", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "rewrite_uri_type": "static", + "rule_enable": "off", + "rewrite_query_string_type": "static", + "query_string": "新的查询字符串示例", + "rule": "http.host eq \\\"video.example.com\\\"", + "uri": "/rewritten/new-target-uri", + "rule_name": "新规则名称示例", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{}), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{}, + }, + }, + }) +} + +var AliCloudESARewriteUrlRuleresource_RewriteUrlRule_testMap = map[string]string{ + "id": CHECKSET, +} + +func AliCloudESARewriteUrlRuleresource_RewriteUrlRule_testBasicDependence(name string) string { + return fmt.Sprintf(` +variable "name" { + default = "%s" +} + + +resource "alicloud_esa_rate_plan_instance" "resource_RewriteUrlRule_RatePlanInstance_test" { + type = "NS" + auto_renew = "false" + period = "1" + payment_type = "Subscription" + coverage = "overseas" + auto_pay = "true" + plan_name = "high" +} + +resource "alicloud_esa_site" "resource_RewriteUrlRule_Site_test" { + site_name = "gositecdn.cn" + instance_id = alicloud_esa_rate_plan_instance.resource_RewriteUrlRule_RatePlanInstance_test.id + coverage = "overseas" + access_type = "NS" +} + +`, name) +} + +// Test ESA RewriteUrlRule. <<< Resource test cases, automatically generated. diff --git a/alicloud/service_alicloud_esa_v2.go b/alicloud/service_alicloud_esa_v2.go index e4a1ea4b6790..aae0b9a0929e 100644 --- a/alicloud/service_alicloud_esa_v2.go +++ b/alicloud/service_alicloud_esa_v2.go @@ -681,3 +681,311 @@ func (s *EsaServiceV2) EsaHttpRequestHeaderModificationRuleStateRefreshFunc(id s } // DescribeEsaHttpRequestHeaderModificationRule >>> Encapsulated. + +// DescribeEsaRewriteUrlRule <<< Encapsulated get interface for Esa RewriteUrlRule. + +func (s *EsaServiceV2) DescribeEsaRewriteUrlRule(id string) (object map[string]interface{}, err error) { + client := s.client + var request map[string]interface{} + var response map[string]interface{} + var query map[string]interface{} + parts := strings.Split(id, ":") + if len(parts) != 2 { + err = WrapError(fmt.Errorf("invalid Resource Id %s. Expected parts' length %d, got %d", id, 2, len(parts))) + } + conn, err := client.NewEsaClient() + if err != nil { + return object, WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + query["ConfigId"] = parts[1] + query["SiteId"] = parts[0] + + action := "GetRewriteUrlRule" + + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(1*time.Minute, func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("GET"), StringPointer("2024-09-10"), StringPointer("AK"), query, nil, &runtime) + + if err != nil { + if IsExpectedErrors(err, []string{"InternalException"}) || NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + return nil + }) + addDebug(action, response, request) + if IsExpectedErrors(err, []string{"SiteNotFound.NotFound"}) { + return object, WrapErrorf(Error(GetNotFoundMessage("RewriteUrlRule", id)), NotFoundMsg, response) + } + code, _ := jsonpath.Get("$.Code", response) + if InArray(fmt.Sprint(code), []string{"0"}) { + return object, WrapErrorf(Error(GetNotFoundMessage("RewriteUrlRule", id)), NotFoundMsg, response) + } + + return response, nil +} + +func (s *EsaServiceV2) EsaRewriteUrlRuleStateRefreshFunc(id string, field string, failStates []string) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + object, err := s.DescribeEsaRewriteUrlRule(id) + if err != nil { + if NotFoundError(err) { + return object, "", nil + } + return nil, "", WrapError(err) + } + + v, err := jsonpath.Get(field, object) + currentStatus := fmt.Sprint(v) + + if strings.HasPrefix(field, "#") { + v, _ := jsonpath.Get(strings.TrimPrefix(field, "#"), object) + if v != nil { + currentStatus = "#CHECKSET" + } + } + + for _, failState := range failStates { + if currentStatus == failState { + return object, currentStatus, WrapError(Error(FailedToReachTargetStatus, currentStatus)) + } + } + return object, currentStatus, nil + } +} + +// DescribeEsaRewriteUrlRule >>> Encapsulated. + +// DescribeEsaRedirectRule <<< Encapsulated get interface for Esa RedirectRule. + +func (s *EsaServiceV2) DescribeEsaRedirectRule(id string) (object map[string]interface{}, err error) { + client := s.client + var request map[string]interface{} + var response map[string]interface{} + var query map[string]interface{} + parts := strings.Split(id, ":") + if len(parts) != 2 { + err = WrapError(fmt.Errorf("invalid Resource Id %s. Expected parts' length %d, got %d", id, 2, len(parts))) + } + conn, err := client.NewEsaClient() + if err != nil { + return object, WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + query["ConfigId"] = parts[1] + query["SiteId"] = parts[0] + + action := "GetRedirectRule" + + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(1*time.Minute, func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("GET"), StringPointer("2024-09-10"), StringPointer("AK"), query, nil, &runtime) + + if err != nil { + if NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + return nil + }) + addDebug(action, response, request) + if err != nil { + return object, WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR) + } + + return response, nil +} + +func (s *EsaServiceV2) EsaRedirectRuleStateRefreshFunc(id string, field string, failStates []string) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + object, err := s.DescribeEsaRedirectRule(id) + if err != nil { + if NotFoundError(err) { + return object, "", nil + } + return nil, "", WrapError(err) + } + + v, err := jsonpath.Get(field, object) + currentStatus := fmt.Sprint(v) + + if strings.HasPrefix(field, "#") { + v, _ := jsonpath.Get(strings.TrimPrefix(field, "#"), object) + if v != nil { + currentStatus = "#CHECKSET" + } + } + + for _, failState := range failStates { + if currentStatus == failState { + return object, currentStatus, WrapError(Error(FailedToReachTargetStatus, currentStatus)) + } + } + return object, currentStatus, nil + } +} + +// DescribeEsaRedirectRule >>> Encapsulated. + +// DescribeEsaHttpResponseHeaderModificationRule <<< Encapsulated get interface for Esa HttpResponseHeaderModificationRule. + +func (s *EsaServiceV2) DescribeEsaHttpResponseHeaderModificationRule(id string) (object map[string]interface{}, err error) { + client := s.client + var request map[string]interface{} + var response map[string]interface{} + var query map[string]interface{} + parts := strings.Split(id, ":") + if len(parts) != 2 { + err = WrapError(fmt.Errorf("invalid Resource Id %s. Expected parts' length %d, got %d", id, 2, len(parts))) + } + conn, err := client.NewEsaClient() + if err != nil { + return object, WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + query["ConfigId"] = parts[1] + query["SiteId"] = parts[0] + + action := "GetHttpResponseHeaderModificationRule" + + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(1*time.Minute, func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("GET"), StringPointer("2024-09-10"), StringPointer("AK"), query, nil, &runtime) + + if err != nil { + if NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + return nil + }) + addDebug(action, response, request) + if err != nil { + return object, WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR) + } + + return response, nil +} + +func (s *EsaServiceV2) EsaHttpResponseHeaderModificationRuleStateRefreshFunc(id string, field string, failStates []string) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + object, err := s.DescribeEsaHttpResponseHeaderModificationRule(id) + if err != nil { + if NotFoundError(err) { + return object, "", nil + } + return nil, "", WrapError(err) + } + + v, err := jsonpath.Get(field, object) + currentStatus := fmt.Sprint(v) + + if strings.HasPrefix(field, "#") { + v, _ := jsonpath.Get(strings.TrimPrefix(field, "#"), object) + if v != nil { + currentStatus = "#CHECKSET" + } + } + + for _, failState := range failStates { + if currentStatus == failState { + return object, currentStatus, WrapError(Error(FailedToReachTargetStatus, currentStatus)) + } + } + return object, currentStatus, nil + } +} + +// DescribeEsaHttpResponseHeaderModificationRule >>> Encapsulated. + +// DescribeEsaHttpsBasicConfiguration <<< Encapsulated get interface for Esa HttpsBasicConfiguration. + +func (s *EsaServiceV2) DescribeEsaHttpsBasicConfiguration(id string) (object map[string]interface{}, err error) { + client := s.client + var request map[string]interface{} + var response map[string]interface{} + var query map[string]interface{} + parts := strings.Split(id, ":") + if len(parts) != 2 { + err = WrapError(fmt.Errorf("invalid Resource Id %s. Expected parts' length %d, got %d", id, 2, len(parts))) + } + conn, err := client.NewEsaClient() + if err != nil { + return object, WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + query["ConfigId"] = parts[1] + query["SiteId"] = parts[0] + query["RegionId"] = client.RegionId + action := "GetHttpsBasicConfiguration" + + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(1*time.Minute, func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("GET"), StringPointer("2024-09-10"), StringPointer("AK"), query, nil, &runtime) + + if err != nil { + if NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + return nil + }) + addDebug(action, response, request) + if err != nil { + return object, WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR) + } + + return response, nil +} + +func (s *EsaServiceV2) EsaHttpsBasicConfigurationStateRefreshFunc(id string, field string, failStates []string) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + object, err := s.DescribeEsaHttpsBasicConfiguration(id) + if err != nil { + if NotFoundError(err) { + return object, "", nil + } + return nil, "", WrapError(err) + } + + v, err := jsonpath.Get(field, object) + currentStatus := fmt.Sprint(v) + + if strings.HasPrefix(field, "#") { + v, _ := jsonpath.Get(strings.TrimPrefix(field, "#"), object) + if v != nil { + currentStatus = "#CHECKSET" + } + } + + for _, failState := range failStates { + if currentStatus == failState { + return object, currentStatus, WrapError(Error(FailedToReachTargetStatus, currentStatus)) + } + } + return object, currentStatus, nil + } +} + +// DescribeEsaHttpsBasicConfiguration >>> Encapsulated. diff --git a/website/docs/r/esa_http_response_header_modification_rule.html.markdown b/website/docs/r/esa_http_response_header_modification_rule.html.markdown new file mode 100644 index 000000000000..b9203dc55fca --- /dev/null +++ b/website/docs/r/esa_http_response_header_modification_rule.html.markdown @@ -0,0 +1,108 @@ +--- +subcategory: "ESA" +layout: "alicloud" +page_title: "Alicloud: alicloud_esa_http_response_header_modification_rule" +description: |- + Provides a Alicloud ESA Http Response Header Modification Rule resource. +--- + +# alicloud_esa_http_response_header_modification_rule + +Provides a ESA Http Response Header Modification Rule resource. + + + +For information about ESA Http Response Header Modification Rule and how to use it, see [What is Http Response Header Modification Rule](https://www.alibabacloud.com/help/en/). + +-> **NOTE:** Available since v1.243.0. + +## Example Usage + +Basic Usage + +```terraform +variable "name" { + default = "terraform-example" +} + +resource "alicloud_esa_rate_plan_instance" "resource_HttpResponseHeaderModificationRule_example" { + type = "NS" + auto_renew = "false" + period = "1" + payment_type = "Subscription" + coverage = "overseas" + auto_pay = "true" + plan_name = "high" +} + +resource "alicloud_esa_site" "resource_Site_HttpResponseHeaderModificationRule_example" { + site_name = "gositecdn.cn" + instance_id = alicloud_esa_rate_plan_instance.resource_HttpResponseHeaderModificationRule_example.id + coverage = "overseas" + access_type = "NS" +} + +resource "alicloud_esa_http_response_header_modification_rule" "default" { + rule_enable = "on" + response_header_modification { + value = "add" + operation = "add" + name = "exampleadd" + } + response_header_modification { + operation = "del" + name = "exampledel" + } + response_header_modification { + operation = "modify" + name = "examplemodify" + value = "modify" + } + + rule = "(http.host eq \"video.example.com\")" + site_version = "0" + rule_name = "exampleResponseHeader" + site_id = alicloud_esa_site.resource_Site_HttpResponseHeaderModificationRule_example.id +} +``` + +## Argument Reference + +The following arguments are supported: +* `response_header_modification` - (Required, List) The configurations of modifying response headers. You can add, delete, or modify a response header. See [`response_header_modification`](#response_header_modification) below. +* `rule` - (Optional) The rule content. +* `rule_enable` - (Optional) Indicates whether the rule is enabled. Valid values: + + - on + - off +* `rule_name` - (Optional) The rule name. +* `site_id` - (Required, ForceNew, Int) The site ID, which can be obtained by calling the ListSites API. +* `site_version` - (Optional, ForceNew, Int) The version number of the website configurations. + +### `response_header_modification` + +The response_header_modification supports the following: +* `name` - (Required) The response header name. +* `operation` - (Required) Mode of operation. +* `value` - (Optional) The response header value. + +## Attributes Reference + +The following attributes are exported: +* `id` - The ID of the resource supplied above.The value is formulated as `:`. +* `config_id` - Config Id + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration-0-11/resources.html#timeouts) for certain actions: +* `create` - (Defaults to 5 mins) Used when create the Http Response Header Modification Rule. +* `delete` - (Defaults to 5 mins) Used when delete the Http Response Header Modification Rule. +* `update` - (Defaults to 5 mins) Used when update the Http Response Header Modification Rule. + +## Import + +ESA Http Response Header Modification Rule can be imported using the id, e.g. + +```shell +$ terraform import alicloud_esa_http_response_header_modification_rule.example : +``` \ No newline at end of file diff --git a/website/docs/r/esa_redirect_rule.html.markdown b/website/docs/r/esa_redirect_rule.html.markdown new file mode 100644 index 000000000000..77bc68c2516f --- /dev/null +++ b/website/docs/r/esa_redirect_rule.html.markdown @@ -0,0 +1,108 @@ +--- +subcategory: "ESA" +layout: "alicloud" +page_title: "Alicloud: alicloud_esa_redirect_rule" +description: |- + Provides a Alicloud ESA Redirect Rule resource. +--- + +# alicloud_esa_redirect_rule + +Provides a ESA Redirect Rule resource. + + + +For information about ESA Redirect Rule and how to use it, see [What is Redirect Rule](https://www.alibabacloud.com/help/en/). + +-> **NOTE:** Available since v1.243.0. + +## Example Usage + +Basic Usage + +```terraform +variable "name" { + default = "terraform-example" +} + +provider "alicloud" { + region = "cn-hangzhou" +} + +resource "alicloud_esa_rate_plan_instance" "resource_RedirectRule_example" { + type = "NS" + auto_renew = "false" + period = "1" + payment_type = "Subscription" + coverage = "overseas" + auto_pay = "true" + plan_name = "high" +} + +resource "alicloud_esa_site" "resource_Site_RedirectRule_example" { + site_name = "gositecdn.cn" + instance_id = alicloud_esa_rate_plan_instance.resource_RedirectRule_example.id + coverage = "overseas" + access_type = "NS" +} + +resource "alicloud_esa_redirect_rule" "default" { + status_code = "301" + rule_name = "example" + site_id = alicloud_esa_site.resource_Site_RedirectRule_example.id + type = "static" + reserve_query_string = "on" + target_url = "http://www.exapmle.com/index.html" + rule_enable = "on" + site_version = "0" + rule = "(http.host eq \"video.example.com\")" +} +``` + +## Argument Reference + +The following arguments are supported: +* `reserve_query_string` - (Required) Indicates whether the feature of retaining the query string is enabled. Valid values: + + - on + - off +* `rule` - (Optional) The rule content. +* `rule_enable` - (Optional) Indicates whether the rule is enabled. Valid values: + + - `on` + - `off` +* `rule_name` - (Optional) 规则名,可以查出规则名为所传字段的那条规则,只有传了functionName才生效 +* `site_id` - (Required, ForceNew, Int) The website ID, which can be obtained by calling the [ListSites](https://www.alibabacloud.com/help/en/doc-detail/2850189.html) operation. +* `site_version` - (Optional, ForceNew, Int) The version of the website configurations. +* `status_code` - (Required) The response code that you want to use to indicate URL redirection. Valid values: + + - 301 + - 302 + - 303 + - 307 + - 308 +* `target_url` - (Required) The destination URL to which requests are redirected. +* `type` - (Required) The redirect type. Valid value: + + - static + +## Attributes Reference + +The following attributes are exported: +* `id` - The ID of the resource supplied above.The value is formulated as `:`. +* `config_id` - Config Id + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration-0-11/resources.html#timeouts) for certain actions: +* `create` - (Defaults to 5 mins) Used when create the Redirect Rule. +* `delete` - (Defaults to 5 mins) Used when delete the Redirect Rule. +* `update` - (Defaults to 5 mins) Used when update the Redirect Rule. + +## Import + +ESA Redirect Rule can be imported using the id, e.g. + +```shell +$ terraform import alicloud_esa_redirect_rule.example : +``` \ No newline at end of file diff --git a/website/docs/r/esa_rewrite_url_rule.html.markdown b/website/docs/r/esa_rewrite_url_rule.html.markdown new file mode 100644 index 000000000000..bdbaf9ab7f61 --- /dev/null +++ b/website/docs/r/esa_rewrite_url_rule.html.markdown @@ -0,0 +1,102 @@ +--- +subcategory: "ESA" +layout: "alicloud" +page_title: "Alicloud: alicloud_esa_rewrite_url_rule" +description: |- + Provides a Alicloud ESA Rewrite Url Rule resource. +--- + +# alicloud_esa_rewrite_url_rule + +Provides a ESA Rewrite Url Rule resource. + + + +For information about ESA Rewrite Url Rule and how to use it, see [What is Rewrite Url Rule](https://www.alibabacloud.com/help/en/). + +-> **NOTE:** Available since v1.243.0. + +## Example Usage + +Basic Usage + +```terraform +variable "name" { + default = "terraform-example" +} + +provider "alicloud" { + region = "cn-hangzhou" +} + +resource "alicloud_esa_rate_plan_instance" "resource_RewriteUrlRule_RatePlanInstance_example" { + type = "NS" + auto_renew = "false" + period = "1" + payment_type = "Subscription" + coverage = "overseas" + auto_pay = "true" + plan_name = "high" +} + +resource "alicloud_esa_site" "resource_RewriteUrlRule_Site_example" { + site_name = "gositecdn.cn" + instance_id = alicloud_esa_rate_plan_instance.resource_RewriteUrlRule_RatePlanInstance_example.id + coverage = "overseas" + access_type = "NS" +} + +resource "alicloud_esa_rewrite_url_rule" "default" { + rewrite_uri_type = "static" + rewrite_query_string_type = "static" + site_id = alicloud_esa_site.resource_RewriteUrlRule_Site_example.id + rule_name = "example" + rule_enable = "on" + query_string = "example=123" + site_version = "0" + rule = "http.host eq \"video.example.com\"" + uri = "/image/example.jpg" +} +``` + +## Argument Reference + +The following arguments are supported: +* `query_string` - (Optional) The desired query string to which you want to rewrite the query string in the original request. +* `rewrite_query_string_type` - (Optional) The query string rewrite method. Valid value: + + - static +* `rewrite_uri_type` - (Optional) The path rewrite method. Valid value: + + - static +* `rule` - (Optional) The rule content. +* `rule_enable` - (Optional) Indicates whether the rule is enabled. Valid values: + + - on + - off +* `rule_name` - (Optional) Rule name. You can find the rule whose field is passed by the rule name. The rule takes effect only if functionName is passed. +* `site_id` - (Required, ForceNew, Int) The website ID, which can be obtained by calling the [ListSites](https://www.alibabacloud.com/help/en/doc-detail/2850189.html) operation. + +* `site_version` - (Optional, ForceNew, Int) The version number of the website configurations. +* `uri` - (Optional) The desired URI to which you want to rewrite the path in the original request. + +## Attributes Reference + +The following attributes are exported: +* `id` - The ID of the resource supplied above.The value is formulated as `:`. +* `config_id` - ConfigId + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration-0-11/resources.html#timeouts) for certain actions: +* `create` - (Defaults to 5 mins) Used when create the Rewrite Url Rule. +* `delete` - (Defaults to 5 mins) Used when delete the Rewrite Url Rule. +* `update` - (Defaults to 5 mins) Used when update the Rewrite Url Rule. + +## Import + +ESA Rewrite Url Rule can be imported using the id, e.g. + +```shell +$ terraform import alicloud_esa_rewrite_url_rule.example : +``` \ No newline at end of file