|
| 1 | +/* |
| 2 | +Provides a resource to create a lighthouse blueprint |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_lighthouse_blueprint" "blueprint" { |
| 8 | + blueprint_name = "blueprint_name_test" |
| 9 | + description = "blueprint_description_test" |
| 10 | + instance_id = "lhins-xxxxxx" |
| 11 | +} |
| 12 | +``` |
| 13 | +
|
| 14 | +Import |
| 15 | +
|
| 16 | +lighthouse blueprint can be imported using the id, e.g. |
| 17 | +
|
| 18 | +``` |
| 19 | +terraform import tencentcloud_lighthouse_blueprint.blueprint blueprint_id |
| 20 | +``` |
| 21 | +*/ |
| 22 | +package tencentcloud |
| 23 | + |
| 24 | +import ( |
| 25 | + "context" |
| 26 | + "log" |
| 27 | + "time" |
| 28 | + |
| 29 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 30 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 31 | + lighthouse "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse/v20200324" |
| 32 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 33 | +) |
| 34 | + |
| 35 | +func resourceTencentCloudLighthouseBlueprint() *schema.Resource { |
| 36 | + return &schema.Resource{ |
| 37 | + Create: resourceTencentCloudLighthouseBlueprintCreate, |
| 38 | + Read: resourceTencentCloudLighthouseBlueprintRead, |
| 39 | + Update: resourceTencentCloudLighthouseBlueprintUpdate, |
| 40 | + Delete: resourceTencentCloudLighthouseBlueprintDelete, |
| 41 | + Importer: &schema.ResourceImporter{ |
| 42 | + State: schema.ImportStatePassthrough, |
| 43 | + }, |
| 44 | + Schema: map[string]*schema.Schema{ |
| 45 | + "blueprint_name": { |
| 46 | + Required: true, |
| 47 | + Type: schema.TypeString, |
| 48 | + Description: "Blueprint name, which can contain up to 60 characters.", |
| 49 | + }, |
| 50 | + |
| 51 | + "description": { |
| 52 | + Optional: true, |
| 53 | + Type: schema.TypeString, |
| 54 | + Description: "Blueprint description, which can contain up to 60 characters.", |
| 55 | + }, |
| 56 | + |
| 57 | + "instance_id": { |
| 58 | + Optional: true, |
| 59 | + Type: schema.TypeString, |
| 60 | + Description: "ID of the instance for which to make a blueprint.", |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func resourceTencentCloudLighthouseBlueprintCreate(d *schema.ResourceData, meta interface{}) error { |
| 67 | + defer logElapsed("resource.tencentcloud_lighthouse_blueprint.create")() |
| 68 | + defer inconsistentCheck(d, meta)() |
| 69 | + |
| 70 | + logId := getLogId(contextNil) |
| 71 | + |
| 72 | + var ( |
| 73 | + request = lighthouse.NewCreateBlueprintRequest() |
| 74 | + response = lighthouse.NewCreateBlueprintResponse() |
| 75 | + blueprintId string |
| 76 | + ) |
| 77 | + if v, ok := d.GetOk("blueprint_name"); ok { |
| 78 | + request.BlueprintName = helper.String(v.(string)) |
| 79 | + } |
| 80 | + |
| 81 | + if v, ok := d.GetOk("description"); ok { |
| 82 | + request.Description = helper.String(v.(string)) |
| 83 | + } |
| 84 | + |
| 85 | + if v, ok := d.GetOk("instance_id"); ok { |
| 86 | + request.InstanceId = helper.String(v.(string)) |
| 87 | + } |
| 88 | + |
| 89 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 90 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseLighthouseClient().CreateBlueprint(request) |
| 91 | + if e != nil { |
| 92 | + return retryError(e) |
| 93 | + } else { |
| 94 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 95 | + } |
| 96 | + response = result |
| 97 | + return nil |
| 98 | + }) |
| 99 | + if err != nil { |
| 100 | + log.Printf("[CRITAL]%s create lighthouse blueprint failed, reason:%+v", logId, err) |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + blueprintId = *response.Response.BlueprintId |
| 105 | + d.SetId(blueprintId) |
| 106 | + |
| 107 | + service := LightHouseService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 108 | + |
| 109 | + conf := BuildStateChangeConf([]string{}, []string{"NORMAL"}, 10*readRetryTimeout, time.Second, service.LighthouseBlueprintStateRefreshFunc(d.Id(), []string{})) |
| 110 | + |
| 111 | + if _, e := conf.WaitForState(); e != nil { |
| 112 | + return e |
| 113 | + } |
| 114 | + |
| 115 | + return resourceTencentCloudLighthouseBlueprintRead(d, meta) |
| 116 | +} |
| 117 | + |
| 118 | +func resourceTencentCloudLighthouseBlueprintRead(d *schema.ResourceData, meta interface{}) error { |
| 119 | + defer logElapsed("resource.tencentcloud_lighthouse_blueprint.read")() |
| 120 | + defer inconsistentCheck(d, meta)() |
| 121 | + |
| 122 | + logId := getLogId(contextNil) |
| 123 | + |
| 124 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 125 | + |
| 126 | + service := LightHouseService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 127 | + |
| 128 | + blueprintId := d.Id() |
| 129 | + |
| 130 | + blueprint, err := service.DescribeLighthouseBlueprintById(ctx, blueprintId) |
| 131 | + if err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + |
| 135 | + if blueprint == nil { |
| 136 | + d.SetId("") |
| 137 | + log.Printf("[WARN]%s resource `LighthouseBlueprint` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 138 | + return nil |
| 139 | + } |
| 140 | + |
| 141 | + if blueprint.BlueprintName != nil { |
| 142 | + _ = d.Set("blueprint_name", blueprint.BlueprintName) |
| 143 | + } |
| 144 | + |
| 145 | + if blueprint.Description != nil { |
| 146 | + _ = d.Set("description", blueprint.Description) |
| 147 | + } |
| 148 | + |
| 149 | + return nil |
| 150 | +} |
| 151 | + |
| 152 | +func resourceTencentCloudLighthouseBlueprintUpdate(d *schema.ResourceData, meta interface{}) error { |
| 153 | + defer logElapsed("resource.tencentcloud_lighthouse_blueprint.update")() |
| 154 | + defer inconsistentCheck(d, meta)() |
| 155 | + |
| 156 | + logId := getLogId(contextNil) |
| 157 | + |
| 158 | + request := lighthouse.NewModifyBlueprintAttributeRequest() |
| 159 | + |
| 160 | + blueprintId := d.Id() |
| 161 | + |
| 162 | + request.BlueprintId = &blueprintId |
| 163 | + |
| 164 | + if d.HasChange("blueprint_name") { |
| 165 | + if v, ok := d.GetOk("blueprint_name"); ok { |
| 166 | + request.BlueprintName = helper.String(v.(string)) |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + if d.HasChange("description") { |
| 171 | + if v, ok := d.GetOk("description"); ok { |
| 172 | + request.Description = helper.String(v.(string)) |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 177 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseLighthouseClient().ModifyBlueprintAttribute(request) |
| 178 | + if e != nil { |
| 179 | + return retryError(e) |
| 180 | + } else { |
| 181 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 182 | + } |
| 183 | + return nil |
| 184 | + }) |
| 185 | + if err != nil { |
| 186 | + log.Printf("[CRITAL]%s update lighthouse blueprint failed, reason:%+v", logId, err) |
| 187 | + return err |
| 188 | + } |
| 189 | + |
| 190 | + return resourceTencentCloudLighthouseBlueprintRead(d, meta) |
| 191 | +} |
| 192 | + |
| 193 | +func resourceTencentCloudLighthouseBlueprintDelete(d *schema.ResourceData, meta interface{}) error { |
| 194 | + defer logElapsed("resource.tencentcloud_lighthouse_blueprint.delete")() |
| 195 | + defer inconsistentCheck(d, meta)() |
| 196 | + |
| 197 | + logId := getLogId(contextNil) |
| 198 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 199 | + |
| 200 | + service := LightHouseService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 201 | + blueprintId := d.Id() |
| 202 | + |
| 203 | + if err := service.DeleteLighthouseBlueprintById(ctx, blueprintId); err != nil { |
| 204 | + return err |
| 205 | + } |
| 206 | + |
| 207 | + return nil |
| 208 | +} |
0 commit comments