|
| 1 | +/* |
| 2 | +Provides a resource to create a waf cc_auto_status |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_waf_cc_auto_status" "example" { |
| 8 | + domain = "www.demo.com" |
| 9 | + edition = "sparta-waf" |
| 10 | +} |
| 11 | +``` |
| 12 | +
|
| 13 | +Import |
| 14 | +
|
| 15 | +waf cc_auto_status can be imported using the id, e.g. |
| 16 | +
|
| 17 | +``` |
| 18 | +terraform import tencentcloud_waf_cc_auto_status.example www.demo.com#sparta-waf |
| 19 | +``` |
| 20 | +*/ |
| 21 | +package tencentcloud |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "fmt" |
| 26 | + "log" |
| 27 | + "strings" |
| 28 | + |
| 29 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 30 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 31 | + waf "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf/v20180125" |
| 32 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 33 | +) |
| 34 | + |
| 35 | +func resourceTencentCloudWafCcAutoStatus() *schema.Resource { |
| 36 | + return &schema.Resource{ |
| 37 | + Create: resourceTencentCloudWafCcAutoStatusCreate, |
| 38 | + Read: resourceTencentCloudWafCcAutoStatusRead, |
| 39 | + Delete: resourceTencentCloudWafCcAutoStatusDelete, |
| 40 | + Importer: &schema.ResourceImporter{ |
| 41 | + State: schema.ImportStatePassthrough, |
| 42 | + }, |
| 43 | + Schema: map[string]*schema.Schema{ |
| 44 | + "domain": { |
| 45 | + Required: true, |
| 46 | + ForceNew: true, |
| 47 | + Type: schema.TypeString, |
| 48 | + Description: "Domain.", |
| 49 | + }, |
| 50 | + "edition": { |
| 51 | + Required: true, |
| 52 | + ForceNew: true, |
| 53 | + Type: schema.TypeString, |
| 54 | + ValidateFunc: validateAllowedStringValue(EDITION_TYPE), |
| 55 | + Description: "Waf edition. clb-waf means clb-waf, sparta-waf means saas-waf.", |
| 56 | + }, |
| 57 | + "status": { |
| 58 | + Computed: true, |
| 59 | + Type: schema.TypeInt, |
| 60 | + Description: "cc auto status, 1 means open, 0 means close.", |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func resourceTencentCloudWafCcAutoStatusCreate(d *schema.ResourceData, meta interface{}) error { |
| 67 | + defer logElapsed("resource.tencentcloud_waf_cc_auto_status.create")() |
| 68 | + defer inconsistentCheck(d, meta)() |
| 69 | + |
| 70 | + var ( |
| 71 | + logId = getLogId(contextNil) |
| 72 | + request = waf.NewUpsertCCAutoStatusRequest() |
| 73 | + domain string |
| 74 | + edition string |
| 75 | + ) |
| 76 | + |
| 77 | + if v, ok := d.GetOk("domain"); ok { |
| 78 | + request.Domain = helper.String(v.(string)) |
| 79 | + domain = v.(string) |
| 80 | + } |
| 81 | + |
| 82 | + if v, ok := d.GetOk("edition"); ok { |
| 83 | + request.Edition = helper.String(v.(string)) |
| 84 | + edition = v.(string) |
| 85 | + } |
| 86 | + |
| 87 | + request.Value = helper.IntInt64(1) |
| 88 | + |
| 89 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 90 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseWafClient().UpsertCCAutoStatus(request) |
| 91 | + if e != nil { |
| 92 | + return retryError(e) |
| 93 | + } else { |
| 94 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 95 | + } |
| 96 | + |
| 97 | + return nil |
| 98 | + }) |
| 99 | + |
| 100 | + if err != nil { |
| 101 | + log.Printf("[CRITAL]%s create waf CcAutoStatus failed, reason:%+v", logId, err) |
| 102 | + return err |
| 103 | + } |
| 104 | + |
| 105 | + d.SetId(strings.Join([]string{domain, edition}, FILED_SP)) |
| 106 | + |
| 107 | + return resourceTencentCloudWafCcAutoStatusRead(d, meta) |
| 108 | +} |
| 109 | + |
| 110 | +func resourceTencentCloudWafCcAutoStatusRead(d *schema.ResourceData, meta interface{}) error { |
| 111 | + defer logElapsed("resource.tencentcloud_waf_cc_auto_status.read")() |
| 112 | + defer inconsistentCheck(d, meta)() |
| 113 | + |
| 114 | + var ( |
| 115 | + logId = getLogId(contextNil) |
| 116 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 117 | + service = WafService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 118 | + ) |
| 119 | + |
| 120 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 121 | + if len(idSplit) != 2 { |
| 122 | + return fmt.Errorf("id is broken,%s", idSplit) |
| 123 | + } |
| 124 | + domain := idSplit[0] |
| 125 | + edition := idSplit[1] |
| 126 | + |
| 127 | + CcAutoStatus, err := service.DescribeWafCcAutoStatusById(ctx, domain) |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + |
| 132 | + if CcAutoStatus == nil { |
| 133 | + d.SetId("") |
| 134 | + log.Printf("[WARN]%s resource `WafCcAutoStatus` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 135 | + return nil |
| 136 | + } |
| 137 | + |
| 138 | + _ = d.Set("domain", domain) |
| 139 | + _ = d.Set("edition", edition) |
| 140 | + |
| 141 | + if CcAutoStatus.AutoCCSwitch != nil { |
| 142 | + _ = d.Set("status", CcAutoStatus.AutoCCSwitch) |
| 143 | + } |
| 144 | + |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +func resourceTencentCloudWafCcAutoStatusDelete(d *schema.ResourceData, meta interface{}) error { |
| 149 | + defer logElapsed("resource.tencentcloud_waf_cc_auto_status.delete")() |
| 150 | + defer inconsistentCheck(d, meta)() |
| 151 | + |
| 152 | + var ( |
| 153 | + logId = getLogId(contextNil) |
| 154 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 155 | + service = WafService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 156 | + ) |
| 157 | + |
| 158 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 159 | + if len(idSplit) != 2 { |
| 160 | + return fmt.Errorf("id is broken,%s", idSplit) |
| 161 | + } |
| 162 | + domain := idSplit[0] |
| 163 | + edition := idSplit[1] |
| 164 | + |
| 165 | + if err := service.DeleteWafCcAutoStatusById(ctx, domain, edition); err != nil { |
| 166 | + return err |
| 167 | + } |
| 168 | + |
| 169 | + return nil |
| 170 | +} |
0 commit comments