|
| 1 | +package waf |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + wafv20180125 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf/v20180125" |
| 12 | + |
| 13 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 14 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 15 | +) |
| 16 | + |
| 17 | +func ResourceTencentCloudWafBotSceneStatusConfig() *schema.Resource { |
| 18 | + return &schema.Resource{ |
| 19 | + Create: resourceTencentCloudWafBotSceneStatusConfigCreate, |
| 20 | + Read: resourceTencentCloudWafBotSceneStatusConfigRead, |
| 21 | + Update: resourceTencentCloudWafBotSceneStatusConfigUpdate, |
| 22 | + Delete: resourceTencentCloudWafBotSceneStatusConfigDelete, |
| 23 | + Importer: &schema.ResourceImporter{ |
| 24 | + State: schema.ImportStatePassthrough, |
| 25 | + }, |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "domain": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Required: true, |
| 30 | + ForceNew: true, |
| 31 | + Description: "Domain.", |
| 32 | + }, |
| 33 | + |
| 34 | + "scene_id": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Required: true, |
| 37 | + ForceNew: true, |
| 38 | + Description: "Scene ID.", |
| 39 | + }, |
| 40 | + |
| 41 | + "status": { |
| 42 | + Type: schema.TypeBool, |
| 43 | + Required: true, |
| 44 | + Description: "Bot status. true - enable; false - disable.", |
| 45 | + }, |
| 46 | + |
| 47 | + "type": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Computed: true, |
| 50 | + Description: "Scene type, default: Default scenario, custom: Non default scenario.", |
| 51 | + }, |
| 52 | + |
| 53 | + "scene_name": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Computed: true, |
| 56 | + Description: "Scene name.", |
| 57 | + }, |
| 58 | + |
| 59 | + "priority": { |
| 60 | + Type: schema.TypeInt, |
| 61 | + Computed: true, |
| 62 | + Description: "Priority.", |
| 63 | + }, |
| 64 | + }, |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func resourceTencentCloudWafBotSceneStatusConfigCreate(d *schema.ResourceData, meta interface{}) error { |
| 69 | + defer tccommon.LogElapsed("resource.tencentcloud_waf_bot_scene_status_config.create")() |
| 70 | + defer tccommon.InconsistentCheck(d, meta)() |
| 71 | + |
| 72 | + var ( |
| 73 | + domain string |
| 74 | + sceneId string |
| 75 | + ) |
| 76 | + |
| 77 | + if v, ok := d.GetOk("domain"); ok { |
| 78 | + domain = v.(string) |
| 79 | + } |
| 80 | + |
| 81 | + if v, ok := d.GetOk("scene_id"); ok { |
| 82 | + sceneId = v.(string) |
| 83 | + } |
| 84 | + |
| 85 | + d.SetId(strings.Join([]string{domain, sceneId}, tccommon.FILED_SP)) |
| 86 | + |
| 87 | + return resourceTencentCloudWafBotSceneStatusConfigUpdate(d, meta) |
| 88 | +} |
| 89 | + |
| 90 | +func resourceTencentCloudWafBotSceneStatusConfigRead(d *schema.ResourceData, meta interface{}) error { |
| 91 | + defer tccommon.LogElapsed("resource.tencentcloud_waf_bot_scene_status_config.read")() |
| 92 | + defer tccommon.InconsistentCheck(d, meta)() |
| 93 | + |
| 94 | + var ( |
| 95 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 96 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 97 | + service = WafService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 98 | + ) |
| 99 | + |
| 100 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 101 | + if len(idSplit) != 2 { |
| 102 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 103 | + } |
| 104 | + |
| 105 | + domain := idSplit[0] |
| 106 | + sceneId := idSplit[1] |
| 107 | + |
| 108 | + respData, err := service.DescribeWafBotSceneStatusConfigById(ctx, domain, sceneId) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + if respData == nil { |
| 114 | + d.SetId("") |
| 115 | + log.Printf("[WARN]%s resource `waf_bot_scene_status_config` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 116 | + return nil |
| 117 | + } |
| 118 | + |
| 119 | + _ = d.Set("domain", domain) |
| 120 | + _ = d.Set("scene_id", sceneId) |
| 121 | + |
| 122 | + if respData.SceneStatus != nil { |
| 123 | + _ = d.Set("status", respData.SceneStatus) |
| 124 | + } |
| 125 | + |
| 126 | + if respData.Type != nil { |
| 127 | + _ = d.Set("type", respData.Type) |
| 128 | + } |
| 129 | + |
| 130 | + if respData.SceneName != nil { |
| 131 | + _ = d.Set("scene_name", respData.SceneName) |
| 132 | + } |
| 133 | + |
| 134 | + if respData.Priority != nil { |
| 135 | + _ = d.Set("priority", respData.Priority) |
| 136 | + } |
| 137 | + |
| 138 | + return nil |
| 139 | +} |
| 140 | + |
| 141 | +func resourceTencentCloudWafBotSceneStatusConfigUpdate(d *schema.ResourceData, meta interface{}) error { |
| 142 | + defer tccommon.LogElapsed("resource.tencentcloud_waf_bot_scene_status_config.update")() |
| 143 | + defer tccommon.InconsistentCheck(d, meta)() |
| 144 | + |
| 145 | + var ( |
| 146 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 147 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 148 | + request = wafv20180125.NewModifyBotSceneStatusRequest() |
| 149 | + ) |
| 150 | + |
| 151 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 152 | + if len(idSplit) != 2 { |
| 153 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 154 | + } |
| 155 | + |
| 156 | + domain := idSplit[0] |
| 157 | + sceneId := idSplit[1] |
| 158 | + |
| 159 | + if v, ok := d.GetOkExists("status"); ok { |
| 160 | + request.Status = helper.Bool(v.(bool)) |
| 161 | + } |
| 162 | + |
| 163 | + request.Domain = &domain |
| 164 | + request.SceneId = &sceneId |
| 165 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 166 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseWafV20180125Client().ModifyBotSceneStatusWithContext(ctx, request) |
| 167 | + if e != nil { |
| 168 | + return tccommon.RetryError(e) |
| 169 | + } else { |
| 170 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 171 | + } |
| 172 | + |
| 173 | + return nil |
| 174 | + }) |
| 175 | + |
| 176 | + if reqErr != nil { |
| 177 | + log.Printf("[CRITAL]%s update waf bot scene status config failed, reason:%+v", logId, reqErr) |
| 178 | + return reqErr |
| 179 | + } |
| 180 | + |
| 181 | + return resourceTencentCloudWafBotSceneStatusConfigRead(d, meta) |
| 182 | +} |
| 183 | + |
| 184 | +func resourceTencentCloudWafBotSceneStatusConfigDelete(d *schema.ResourceData, meta interface{}) error { |
| 185 | + defer tccommon.LogElapsed("resource.tencentcloud_waf_bot_scene_status_config.delete")() |
| 186 | + defer tccommon.InconsistentCheck(d, meta)() |
| 187 | + |
| 188 | + return nil |
| 189 | +} |
0 commit comments