|
| 1 | +package teo |
| 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 | + teov20220901 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo/v20220901" |
| 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 ResourceTencentCloudTeoDdosProtectionConfig() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + Create: resourceTencentCloudTeoDdosProtectionConfigCreate, |
| 18 | + Read: resourceTencentCloudTeoDdosProtectionConfigRead, |
| 19 | + Update: resourceTencentCloudTeoDdosProtectionConfigUpdate, |
| 20 | + Delete: resourceTencentCloudTeoDdosProtectionConfigDelete, |
| 21 | + Importer: &schema.ResourceImporter{ |
| 22 | + State: schema.ImportStatePassthrough, |
| 23 | + }, |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + "zone_id": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Required: true, |
| 28 | + ForceNew: true, |
| 29 | + Description: "Zone ID.", |
| 30 | + }, |
| 31 | + |
| 32 | + "ddos_protection": { |
| 33 | + Type: schema.TypeList, |
| 34 | + Required: true, |
| 35 | + MaxItems: 1, |
| 36 | + Description: "Specifies the exclusive Anti-DDoS configuration.", |
| 37 | + Elem: &schema.Resource{ |
| 38 | + Schema: map[string]*schema.Schema{ |
| 39 | + "protection_option": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Required: true, |
| 42 | + Description: "Specifies the protection scope of standalone DDoS. valid values:.\n<li>protect_all_domains: specifies exclusive Anti-DDoS protection for all domain names in the site. newly added domain names automatically enable exclusive Anti-DDoS protection. when this parameter is specified, DomainDDoSProtections will not be processed.</li>.\n<li>protect_specified_domains: only applicable to specified domains. specific scope can be set via DomainDDoSProtection parameter.</li>.", |
| 43 | + }, |
| 44 | + "domain_ddos_protections": { |
| 45 | + Type: schema.TypeSet, |
| 46 | + Optional: true, |
| 47 | + Computed: true, |
| 48 | + Description: "Anti-DDoS configuration of the domain. specifies the exclusive ddos protection settings for the domain in request parameters.\n<li>When ProtectionOption remains protect_specified_domains, the domain names not filled in keep their exclusive Anti-DDoS protection configuration unchanged, while explicitly specified domain names are updated according to the input parameters.</li>.\n<li>When ProtectionOption switches from protect_all_domains to protect_specified_domains: if DomainDDoSProtections is empty, disable exclusive DDoS protection for all domains under the site; if DomainDDoSProtections is not empty, disable or maintain exclusive DDoS protection for the domain names specified in the parameter, and disable exclusive DDoS protection for other unlisted domain names.</li>.", |
| 49 | + Elem: &schema.Resource{ |
| 50 | + Schema: map[string]*schema.Schema{ |
| 51 | + "domain": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Required: true, |
| 54 | + Description: "Domain name.", |
| 55 | + }, |
| 56 | + "switch": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Required: true, |
| 59 | + Description: "Standalone DDoS switch of the domain. valid values:.\n<li>on: enabled;</li>.\n<li>off: closed.</li>.", |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + "shared_cname_ddos_protections": { |
| 65 | + Type: schema.TypeList, |
| 66 | + Computed: true, |
| 67 | + Description: "Specifies the exclusive DDoS protection configuration of a shared CNAME. used as an output parameter.", |
| 68 | + Elem: &schema.Resource{ |
| 69 | + Schema: map[string]*schema.Schema{ |
| 70 | + "domain": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Computed: true, |
| 73 | + Description: "Domain name.", |
| 74 | + }, |
| 75 | + "switch": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Computed: true, |
| 78 | + Description: "Standalone DDoS switch of the domain. valid values:.\n<li>on: enabled;</li>.\n<li>off: closed.</li>.", |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func resourceTencentCloudTeoDdosProtectionConfigCreate(d *schema.ResourceData, meta interface{}) error { |
| 91 | + defer tccommon.LogElapsed("resource.tencentcloud_teo_ddos_protection_config.create")() |
| 92 | + defer tccommon.InconsistentCheck(d, meta)() |
| 93 | + |
| 94 | + var zoneId string |
| 95 | + if v, ok := d.GetOk("zone_id"); ok { |
| 96 | + zoneId = v.(string) |
| 97 | + } |
| 98 | + |
| 99 | + d.SetId(zoneId) |
| 100 | + return resourceTencentCloudTeoDdosProtectionConfigUpdate(d, meta) |
| 101 | +} |
| 102 | + |
| 103 | +func resourceTencentCloudTeoDdosProtectionConfigRead(d *schema.ResourceData, meta interface{}) error { |
| 104 | + defer tccommon.LogElapsed("resource.tencentcloud_teo_ddos_protection_config.read")() |
| 105 | + defer tccommon.InconsistentCheck(d, meta)() |
| 106 | + |
| 107 | + var ( |
| 108 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 109 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 110 | + service = TeoService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 111 | + zoneId = d.Id() |
| 112 | + ) |
| 113 | + |
| 114 | + respData, err := service.DescribeTeoDdosProtectionConfigById(ctx, zoneId) |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + if respData == nil { |
| 120 | + log.Printf("[WARN]%s resource `tencentcloud_teo_ddos_protection_config` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 121 | + d.SetId("") |
| 122 | + return nil |
| 123 | + } |
| 124 | + |
| 125 | + _ = d.Set("zone_id", zoneId) |
| 126 | + |
| 127 | + dMap := make(map[string]interface{}, 0) |
| 128 | + if respData.ProtectionOption != nil { |
| 129 | + dMap["protection_option"] = respData.ProtectionOption |
| 130 | + } |
| 131 | + |
| 132 | + if respData.DomainDDoSProtections != nil { |
| 133 | + domainDDoSProtectionsList := make([]map[string]interface{}, 0, len(respData.DomainDDoSProtections)) |
| 134 | + for _, domainDDoSProtections := range respData.DomainDDoSProtections { |
| 135 | + domainDDoSProtectionsMap := map[string]interface{}{} |
| 136 | + if domainDDoSProtections.Domain != nil { |
| 137 | + domainDDoSProtectionsMap["domain"] = domainDDoSProtections.Domain |
| 138 | + } |
| 139 | + |
| 140 | + if domainDDoSProtections.Switch != nil { |
| 141 | + domainDDoSProtectionsMap["switch"] = domainDDoSProtections.Switch |
| 142 | + } |
| 143 | + |
| 144 | + domainDDoSProtectionsList = append(domainDDoSProtectionsList, domainDDoSProtectionsMap) |
| 145 | + } |
| 146 | + |
| 147 | + dMap["domain_ddos_protections"] = domainDDoSProtectionsList |
| 148 | + } |
| 149 | + |
| 150 | + if respData.SharedCNAMEDDoSProtections != nil { |
| 151 | + sharedCNAMEDDoSProtectionsList := make([]map[string]interface{}, 0, len(respData.SharedCNAMEDDoSProtections)) |
| 152 | + for _, sharedCNAMEDDoSProtections := range respData.SharedCNAMEDDoSProtections { |
| 153 | + sharedCNAMEDDoSProtectionsMap := map[string]interface{}{} |
| 154 | + if sharedCNAMEDDoSProtections.Domain != nil { |
| 155 | + sharedCNAMEDDoSProtectionsMap["domain"] = sharedCNAMEDDoSProtections.Domain |
| 156 | + } |
| 157 | + |
| 158 | + if sharedCNAMEDDoSProtections.Switch != nil { |
| 159 | + sharedCNAMEDDoSProtectionsMap["switch"] = sharedCNAMEDDoSProtections.Switch |
| 160 | + } |
| 161 | + |
| 162 | + sharedCNAMEDDoSProtectionsList = append(sharedCNAMEDDoSProtectionsList, sharedCNAMEDDoSProtectionsMap) |
| 163 | + } |
| 164 | + |
| 165 | + dMap["shared_cname_ddos_protections"] = sharedCNAMEDDoSProtectionsList |
| 166 | + } |
| 167 | + |
| 168 | + _ = d.Set("ddos_protection", []interface{}{dMap}) |
| 169 | + return nil |
| 170 | +} |
| 171 | + |
| 172 | +func resourceTencentCloudTeoDdosProtectionConfigUpdate(d *schema.ResourceData, meta interface{}) error { |
| 173 | + defer tccommon.LogElapsed("resource.tencentcloud_teo_ddos_protection_config.update")() |
| 174 | + defer tccommon.InconsistentCheck(d, meta)() |
| 175 | + |
| 176 | + var ( |
| 177 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 178 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 179 | + request = teov20220901.NewModifyDDoSProtectionRequest() |
| 180 | + zoneId = d.Id() |
| 181 | + ) |
| 182 | + |
| 183 | + if dDoSProtectionMap, ok := helper.InterfacesHeadMap(d, "ddos_protection"); ok { |
| 184 | + dDoSProtection := teov20220901.DDoSProtection{} |
| 185 | + if v, ok := dDoSProtectionMap["protection_option"].(string); ok && v != "" { |
| 186 | + dDoSProtection.ProtectionOption = helper.String(v) |
| 187 | + } |
| 188 | + |
| 189 | + if v, ok := dDoSProtectionMap["domain_ddos_protections"]; ok { |
| 190 | + for _, item := range v.(*schema.Set).List() { |
| 191 | + domainDDoSProtectionsMap := item.(map[string]interface{}) |
| 192 | + domainDDoSProtection := teov20220901.DomainDDoSProtection{} |
| 193 | + if v, ok := domainDDoSProtectionsMap["domain"].(string); ok && v != "" { |
| 194 | + domainDDoSProtection.Domain = helper.String(v) |
| 195 | + } |
| 196 | + |
| 197 | + if v, ok := domainDDoSProtectionsMap["switch"].(string); ok && v != "" { |
| 198 | + domainDDoSProtection.Switch = helper.String(v) |
| 199 | + } |
| 200 | + |
| 201 | + dDoSProtection.DomainDDoSProtections = append(dDoSProtection.DomainDDoSProtections, &domainDDoSProtection) |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + request.DDoSProtection = &dDoSProtection |
| 206 | + } |
| 207 | + |
| 208 | + request.ZoneId = &zoneId |
| 209 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 210 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().ModifyDDoSProtectionWithContext(ctx, request) |
| 211 | + if e != nil { |
| 212 | + return tccommon.RetryError(e) |
| 213 | + } else { |
| 214 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 215 | + } |
| 216 | + |
| 217 | + return nil |
| 218 | + }) |
| 219 | + |
| 220 | + if reqErr != nil { |
| 221 | + log.Printf("[CRITAL]%s update teo ddos protection config failed, reason:%+v", logId, reqErr) |
| 222 | + return reqErr |
| 223 | + } |
| 224 | + |
| 225 | + return resourceTencentCloudTeoDdosProtectionConfigRead(d, meta) |
| 226 | +} |
| 227 | + |
| 228 | +func resourceTencentCloudTeoDdosProtectionConfigDelete(d *schema.ResourceData, meta interface{}) error { |
| 229 | + defer tccommon.LogElapsed("resource.tencentcloud_teo_ddos_protection_config.delete")() |
| 230 | + defer tccommon.InconsistentCheck(d, meta)() |
| 231 | + |
| 232 | + return nil |
| 233 | +} |
0 commit comments