|
| 1 | +/* |
| 2 | +Manage Guetzli compression functionality |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +
|
| 8 | +resource "tencentcloud_ci_guetzli" "foo" { |
| 9 | + bucket = "examplebucket-1250000000" |
| 10 | + status = "on" |
| 11 | +} |
| 12 | +
|
| 13 | +``` |
| 14 | +
|
| 15 | +Import |
| 16 | +
|
| 17 | +Resource guetzli can be imported using the id, e.g. |
| 18 | +
|
| 19 | +``` |
| 20 | +$ terraform import tencentcloud_ci_guetzli.example examplebucket-1250000000 |
| 21 | +``` |
| 22 | +
|
| 23 | +*/ |
| 24 | +package tencentcloud |
| 25 | + |
| 26 | +import ( |
| 27 | + "context" |
| 28 | + "log" |
| 29 | + |
| 30 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 31 | + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" |
| 32 | +) |
| 33 | + |
| 34 | +func resourceTencentCloudCIGuetzli() *schema.Resource { |
| 35 | + return &schema.Resource{ |
| 36 | + Create: resourceTencentCloudCIGuetzliCreate, |
| 37 | + Read: resourceTencentCloudCIGuetzliRead, |
| 38 | + Update: resourceTencentCloudCIGuetzliUpdate, |
| 39 | + Delete: resourceTencentCloudCIGuetzliDelete, |
| 40 | + Importer: &schema.ResourceImporter{ |
| 41 | + State: schema.ImportStatePassthrough, |
| 42 | + }, |
| 43 | + Schema: map[string]*schema.Schema{ |
| 44 | + "bucket": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Required: true, |
| 47 | + ForceNew: true, |
| 48 | + Description: "The name of a bucket, the format should be [custom name]-[appid], for example `mycos-1258798060`.", |
| 49 | + ValidateFunc: validateCosBucketName, |
| 50 | + }, |
| 51 | + "status": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Required: true, |
| 54 | + Description: "Whether Guetzli is set, options: on/off.", |
| 55 | + ValidateFunc: validation.StringInSlice([]string{"on", "off"}, false), |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func resourceTencentCloudCIGuetzliCreate(d *schema.ResourceData, meta interface{}) error { |
| 62 | + defer logElapsed("resource.tencentcloud_ci_guetzli.create")() |
| 63 | + |
| 64 | + d.SetId(d.Get("bucket").(string)) |
| 65 | + return resourceTencentCloudCIGuetzliUpdate(d, meta) |
| 66 | +} |
| 67 | + |
| 68 | +func resourceTencentCloudCIGuetzliRead(d *schema.ResourceData, meta interface{}) error { |
| 69 | + defer logElapsed("resource.tencentcloud_ci_guetzli.read")() |
| 70 | + defer inconsistentCheck(d, meta)() |
| 71 | + |
| 72 | + logId := getLogId(contextNil) |
| 73 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 74 | + |
| 75 | + bucket := d.Id() |
| 76 | + service := CiService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 77 | + res, err := service.GetCiGuetzliById(ctx, bucket) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + log.Printf("[DEBUG] bucket=%s status=%s", bucket, res.GuetzliStatus) |
| 83 | + _ = d.Set("bucket", bucket) |
| 84 | + _ = d.Set("status", res.GuetzliStatus) |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func resourceTencentCloudCIGuetzliUpdate(d *schema.ResourceData, meta interface{}) error { |
| 89 | + defer logElapsed("resource.tencentcloud_ci_guetzli.update")() |
| 90 | + |
| 91 | + logId := getLogId(contextNil) |
| 92 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 93 | + |
| 94 | + bucket := d.Id() |
| 95 | + if d.HasChange("status") { |
| 96 | + var err error |
| 97 | + service := CiService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 98 | + |
| 99 | + newStatus := d.Get("status") |
| 100 | + if newStatus == "on" { |
| 101 | + err = service.OpenCiGuetzliById(ctx, bucket) |
| 102 | + } else { |
| 103 | + err = service.CloseCiGuetzliById(ctx, bucket) |
| 104 | + } |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + return resourceTencentCloudCIGuetzliRead(d, meta) |
| 109 | + |
| 110 | + } |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +func resourceTencentCloudCIGuetzliDelete(d *schema.ResourceData, meta interface{}) error { |
| 115 | + defer logElapsed("resource.tencentcloud_ci_guetzli.delete")() |
| 116 | + |
| 117 | + logId := getLogId(contextNil) |
| 118 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 119 | + |
| 120 | + bucket := d.Get("bucket").(string) |
| 121 | + service := CiService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 122 | + err := service.CloseCiGuetzliById(ctx, bucket) |
| 123 | + |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + return nil |
| 128 | +} |
0 commit comments