|
| 1 | +package waf |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + wafv20180125 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf/v20180125" |
| 10 | + |
| 11 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 12 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 13 | +) |
| 14 | + |
| 15 | +func ResourceTencentCloudWafDomainPostAction() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + Create: resourceTencentCloudWafDomainPostActionCreate, |
| 18 | + Read: resourceTencentCloudWafDomainPostActionRead, |
| 19 | + Update: resourceTencentCloudWafDomainPostActionUpdate, |
| 20 | + Delete: resourceTencentCloudWafDomainPostActionDelete, |
| 21 | + Importer: &schema.ResourceImporter{ |
| 22 | + State: schema.ImportStatePassthrough, |
| 23 | + }, |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + "domain": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Required: true, |
| 28 | + ForceNew: true, |
| 29 | + Description: "Domain.", |
| 30 | + }, |
| 31 | + |
| 32 | + "post_cls_action": { |
| 33 | + Type: schema.TypeInt, |
| 34 | + Required: true, |
| 35 | + ValidateFunc: tccommon.ValidateAllowedIntValue([]int{0, 1}), |
| 36 | + Description: "0- Disable shipping, 1- Enable shipping.", |
| 37 | + }, |
| 38 | + |
| 39 | + "post_ckafka_action": { |
| 40 | + Type: schema.TypeInt, |
| 41 | + Required: true, |
| 42 | + ValidateFunc: tccommon.ValidateAllowedIntValue([]int{0, 1}), |
| 43 | + Description: "0- Disable shipping, 1- Enable shipping.", |
| 44 | + }, |
| 45 | + }, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func resourceTencentCloudWafDomainPostActionCreate(d *schema.ResourceData, meta interface{}) error { |
| 50 | + defer tccommon.LogElapsed("resource.tencentcloud_waf_domain_post_action.create")() |
| 51 | + defer tccommon.InconsistentCheck(d, meta)() |
| 52 | + |
| 53 | + var domain string |
| 54 | + if v, ok := d.GetOk("domain"); ok { |
| 55 | + domain = v.(string) |
| 56 | + } |
| 57 | + |
| 58 | + d.SetId(domain) |
| 59 | + |
| 60 | + return resourceTencentCloudWafDomainPostActionUpdate(d, meta) |
| 61 | +} |
| 62 | + |
| 63 | +func resourceTencentCloudWafDomainPostActionRead(d *schema.ResourceData, meta interface{}) error { |
| 64 | + defer tccommon.LogElapsed("resource.tencentcloud_waf_domain_post_action.read")() |
| 65 | + defer tccommon.InconsistentCheck(d, meta)() |
| 66 | + |
| 67 | + var ( |
| 68 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 69 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 70 | + service = WafService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 71 | + domain = d.Id() |
| 72 | + ) |
| 73 | + |
| 74 | + respData, err := service.DescribeWafDomainPostActionById(ctx, domain) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + |
| 79 | + if respData == nil || len(respData) < 1 { |
| 80 | + d.SetId("") |
| 81 | + log.Printf("[WARN]%s resource `waf_domain_post_action` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 82 | + return nil |
| 83 | + } |
| 84 | + |
| 85 | + _ = d.Set("domain", domain) |
| 86 | + |
| 87 | + for _, item := range respData { |
| 88 | + if item.PostCLSStatus != nil { |
| 89 | + _ = d.Set("post_cls_action", item.PostCLSStatus) |
| 90 | + } |
| 91 | + |
| 92 | + if item.PostCKafkaStatus != nil { |
| 93 | + _ = d.Set("post_ckafka_action", item.PostCKafkaStatus) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return nil |
| 98 | +} |
| 99 | + |
| 100 | +func resourceTencentCloudWafDomainPostActionUpdate(d *schema.ResourceData, meta interface{}) error { |
| 101 | + defer tccommon.LogElapsed("resource.tencentcloud_waf_domain_post_action.update")() |
| 102 | + defer tccommon.InconsistentCheck(d, meta)() |
| 103 | + |
| 104 | + var ( |
| 105 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 106 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 107 | + request = wafv20180125.NewModifyDomainPostActionRequest() |
| 108 | + domain = d.Id() |
| 109 | + ) |
| 110 | + |
| 111 | + if v, ok := d.GetOkExists("post_cls_action"); ok { |
| 112 | + request.PostCLSAction = helper.IntInt64(v.(int)) |
| 113 | + } |
| 114 | + |
| 115 | + if v, ok := d.GetOkExists("post_ckafka_action"); ok { |
| 116 | + request.PostCKafkaAction = helper.IntInt64(v.(int)) |
| 117 | + } |
| 118 | + |
| 119 | + request.Domain = &domain |
| 120 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 121 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseWafV20180125Client().ModifyDomainPostActionWithContext(ctx, request) |
| 122 | + if e != nil { |
| 123 | + return tccommon.RetryError(e) |
| 124 | + } else { |
| 125 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 126 | + } |
| 127 | + |
| 128 | + return nil |
| 129 | + }) |
| 130 | + |
| 131 | + if reqErr != nil { |
| 132 | + log.Printf("[CRITAL]%s update waf domain post action failed, reason:%+v", logId, reqErr) |
| 133 | + return reqErr |
| 134 | + } |
| 135 | + |
| 136 | + return resourceTencentCloudWafDomainPostActionRead(d, meta) |
| 137 | +} |
| 138 | + |
| 139 | +func resourceTencentCloudWafDomainPostActionDelete(d *schema.ResourceData, meta interface{}) error { |
| 140 | + defer tccommon.LogElapsed("resource.tencentcloud_waf_domain_post_action.delete")() |
| 141 | + defer tccommon.InconsistentCheck(d, meta)() |
| 142 | + |
| 143 | + return nil |
| 144 | +} |
0 commit comments