|
| 1 | +package teo |
| 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 | + teov20220901 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo/v20220901" |
| 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 ResourceTencentCloudTeoCustomizeErrorPage() *schema.Resource { |
| 18 | + return &schema.Resource{ |
| 19 | + Create: resourceTencentCloudTeoCustomizeErrorPageCreate, |
| 20 | + Read: resourceTencentCloudTeoCustomizeErrorPageRead, |
| 21 | + Update: resourceTencentCloudTeoCustomizeErrorPageUpdate, |
| 22 | + Delete: resourceTencentCloudTeoCustomizeErrorPageDelete, |
| 23 | + Importer: &schema.ResourceImporter{ |
| 24 | + State: schema.ImportStatePassthrough, |
| 25 | + }, |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "zone_id": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Required: true, |
| 30 | + ForceNew: true, |
| 31 | + Description: "Zone ID.", |
| 32 | + }, |
| 33 | + |
| 34 | + "name": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Required: true, |
| 37 | + Description: "Custom error page name. The name must be 2-30 characters long.", |
| 38 | + }, |
| 39 | + |
| 40 | + "content_type": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Required: true, |
| 43 | + Description: "Custom error page type, with values:<li>text/html; </li><li>application/json;</li><li>text/plain;</li><li>text/xml.</li>.", |
| 44 | + }, |
| 45 | + |
| 46 | + "description": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Optional: true, |
| 49 | + Description: "Custom error page description, not exceeding 60 characters.", |
| 50 | + }, |
| 51 | + |
| 52 | + "content": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Optional: true, |
| 55 | + Description: "Custom error page content, not exceeding 2 KB.", |
| 56 | + }, |
| 57 | + |
| 58 | + // computed |
| 59 | + "page_id": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Computed: true, |
| 62 | + Description: "Page ID.", |
| 63 | + }, |
| 64 | + }, |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func resourceTencentCloudTeoCustomizeErrorPageCreate(d *schema.ResourceData, meta interface{}) error { |
| 69 | + defer tccommon.LogElapsed("resource.tencentcloud_teo_customize_error_page.create")() |
| 70 | + defer tccommon.InconsistentCheck(d, meta)() |
| 71 | + |
| 72 | + var ( |
| 73 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 74 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 75 | + request = teov20220901.NewCreateCustomizeErrorPageRequest() |
| 76 | + response = teov20220901.NewCreateCustomizeErrorPageResponse() |
| 77 | + zoneId string |
| 78 | + pageId string |
| 79 | + ) |
| 80 | + |
| 81 | + if v, ok := d.GetOk("zone_id"); ok { |
| 82 | + request.ZoneId = helper.String(v.(string)) |
| 83 | + zoneId = v.(string) |
| 84 | + } |
| 85 | + |
| 86 | + if v, ok := d.GetOk("name"); ok { |
| 87 | + request.Name = helper.String(v.(string)) |
| 88 | + } |
| 89 | + |
| 90 | + if v, ok := d.GetOk("content_type"); ok { |
| 91 | + request.ContentType = helper.String(v.(string)) |
| 92 | + } |
| 93 | + |
| 94 | + if v, ok := d.GetOk("description"); ok { |
| 95 | + request.Description = helper.String(v.(string)) |
| 96 | + } |
| 97 | + |
| 98 | + if v, ok := d.GetOk("content"); ok { |
| 99 | + request.Content = helper.String(v.(string)) |
| 100 | + } |
| 101 | + |
| 102 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 103 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().CreateCustomizeErrorPageWithContext(ctx, request) |
| 104 | + if e != nil { |
| 105 | + return tccommon.RetryError(e) |
| 106 | + } else { |
| 107 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 108 | + } |
| 109 | + |
| 110 | + if result == nil || result.Response == nil { |
| 111 | + return resource.NonRetryableError(fmt.Errorf("Create teo customize error page failed, Response is nil.")) |
| 112 | + } |
| 113 | + |
| 114 | + response = result |
| 115 | + return nil |
| 116 | + }) |
| 117 | + |
| 118 | + if reqErr != nil { |
| 119 | + log.Printf("[CRITAL]%s create teo customize error page failed, reason:%+v", logId, reqErr) |
| 120 | + return reqErr |
| 121 | + } |
| 122 | + |
| 123 | + if response.Response.PageId == nil { |
| 124 | + return fmt.Errorf("PageId is nil.") |
| 125 | + } |
| 126 | + |
| 127 | + pageId = *response.Response.PageId |
| 128 | + d.SetId(strings.Join([]string{zoneId, pageId}, tccommon.FILED_SP)) |
| 129 | + return resourceTencentCloudTeoCustomizeErrorPageRead(d, meta) |
| 130 | +} |
| 131 | + |
| 132 | +func resourceTencentCloudTeoCustomizeErrorPageRead(d *schema.ResourceData, meta interface{}) error { |
| 133 | + defer tccommon.LogElapsed("resource.tencentcloud_teo_customize_error_page.read")() |
| 134 | + defer tccommon.InconsistentCheck(d, meta)() |
| 135 | + |
| 136 | + var ( |
| 137 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 138 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 139 | + service = TeoService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 140 | + ) |
| 141 | + |
| 142 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 143 | + if len(idSplit) != 2 { |
| 144 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 145 | + } |
| 146 | + |
| 147 | + zoneId := idSplit[0] |
| 148 | + pageId := idSplit[1] |
| 149 | + |
| 150 | + respData, err := service.DescribeTeoCustomizeErrorPageById(ctx, zoneId, pageId) |
| 151 | + if err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + |
| 155 | + if respData == nil { |
| 156 | + log.Printf("[WARN]%s resource `teo_customize_error_page` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 157 | + d.SetId("") |
| 158 | + return nil |
| 159 | + } |
| 160 | + |
| 161 | + _ = d.Set("zone_id", zoneId) |
| 162 | + _ = d.Set("page_id", pageId) |
| 163 | + |
| 164 | + if respData.Name != nil { |
| 165 | + _ = d.Set("name", respData.Name) |
| 166 | + } |
| 167 | + |
| 168 | + if respData.ContentType != nil { |
| 169 | + _ = d.Set("content_type", respData.ContentType) |
| 170 | + } |
| 171 | + |
| 172 | + if respData.Description != nil { |
| 173 | + _ = d.Set("description", respData.Description) |
| 174 | + } |
| 175 | + |
| 176 | + if respData.Content != nil { |
| 177 | + _ = d.Set("content", respData.Content) |
| 178 | + } |
| 179 | + |
| 180 | + return nil |
| 181 | +} |
| 182 | + |
| 183 | +func resourceTencentCloudTeoCustomizeErrorPageUpdate(d *schema.ResourceData, meta interface{}) error { |
| 184 | + defer tccommon.LogElapsed("resource.tencentcloud_teo_customize_error_page.update")() |
| 185 | + defer tccommon.InconsistentCheck(d, meta)() |
| 186 | + |
| 187 | + var ( |
| 188 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 189 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 190 | + ) |
| 191 | + |
| 192 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 193 | + if len(idSplit) != 2 { |
| 194 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 195 | + } |
| 196 | + |
| 197 | + zoneId := idSplit[0] |
| 198 | + pageId := idSplit[1] |
| 199 | + |
| 200 | + needChange := false |
| 201 | + mutableArgs := []string{"name", "description", "content_type", "content"} |
| 202 | + for _, v := range mutableArgs { |
| 203 | + if d.HasChange(v) { |
| 204 | + needChange = true |
| 205 | + break |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + if needChange { |
| 210 | + request := teov20220901.NewModifyCustomErrorPageRequest() |
| 211 | + if v, ok := d.GetOk("name"); ok { |
| 212 | + request.Name = helper.String(v.(string)) |
| 213 | + } |
| 214 | + |
| 215 | + if v, ok := d.GetOk("description"); ok { |
| 216 | + request.Description = helper.String(v.(string)) |
| 217 | + } |
| 218 | + |
| 219 | + if v, ok := d.GetOk("content_type"); ok { |
| 220 | + request.ContentType = helper.String(v.(string)) |
| 221 | + } |
| 222 | + |
| 223 | + if v, ok := d.GetOk("content"); ok { |
| 224 | + request.Content = helper.String(v.(string)) |
| 225 | + } |
| 226 | + |
| 227 | + request.ZoneId = &zoneId |
| 228 | + request.PageId = &pageId |
| 229 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 230 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().ModifyCustomErrorPageWithContext(ctx, request) |
| 231 | + if e != nil { |
| 232 | + return tccommon.RetryError(e) |
| 233 | + } else { |
| 234 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 235 | + } |
| 236 | + |
| 237 | + return nil |
| 238 | + }) |
| 239 | + |
| 240 | + if reqErr != nil { |
| 241 | + log.Printf("[CRITAL]%s update teo customize error page failed, reason:%+v", logId, reqErr) |
| 242 | + return reqErr |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + return resourceTencentCloudTeoCustomizeErrorPageRead(d, meta) |
| 247 | +} |
| 248 | + |
| 249 | +func resourceTencentCloudTeoCustomizeErrorPageDelete(d *schema.ResourceData, meta interface{}) error { |
| 250 | + defer tccommon.LogElapsed("resource.tencentcloud_teo_customize_error_page.delete")() |
| 251 | + defer tccommon.InconsistentCheck(d, meta)() |
| 252 | + |
| 253 | + var ( |
| 254 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 255 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 256 | + request = teov20220901.NewDeleteCustomErrorPageRequest() |
| 257 | + ) |
| 258 | + |
| 259 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 260 | + if len(idSplit) != 2 { |
| 261 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 262 | + } |
| 263 | + |
| 264 | + zoneId := idSplit[0] |
| 265 | + pageId := idSplit[1] |
| 266 | + |
| 267 | + request.ZoneId = &zoneId |
| 268 | + request.PageId = &pageId |
| 269 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 270 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().DeleteCustomErrorPageWithContext(ctx, request) |
| 271 | + if e != nil { |
| 272 | + return tccommon.RetryError(e) |
| 273 | + } else { |
| 274 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 275 | + } |
| 276 | + |
| 277 | + return nil |
| 278 | + }) |
| 279 | + |
| 280 | + if reqErr != nil { |
| 281 | + log.Printf("[CRITAL]%s delete teo customize error page failed, reason:%+v", logId, reqErr) |
| 282 | + return reqErr |
| 283 | + } |
| 284 | + |
| 285 | + return nil |
| 286 | +} |
0 commit comments