|
| 1 | +/* |
| 2 | +Provides a resource to create a cynosdb cluster_resource_packages_attachment |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_cynosdb_cluster_resource_packages_attachment" "cluster_resource_packages_attachment" { |
| 8 | + cluster_id = "cynosdbmysql-q1d8151n" |
| 9 | + package_ids = ["package-hy4d2ppl"] |
| 10 | +} |
| 11 | +``` |
| 12 | +
|
| 13 | +Import |
| 14 | +
|
| 15 | +cynosdb cluster_resource_packages_attachment can be imported using the id, e.g. |
| 16 | +
|
| 17 | +``` |
| 18 | +terraform import tencentcloud_cynosdb_cluster_resource_packages_attachment.cluster_resource_packages_attachment cluster_resource_packages_attachment_id |
| 19 | +``` |
| 20 | +*/ |
| 21 | +package tencentcloud |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "fmt" |
| 26 | + "log" |
| 27 | + |
| 28 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 29 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 30 | + cynosdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb/v20190107" |
| 31 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 32 | +) |
| 33 | + |
| 34 | +func resourceTencentCloudCynosdbClusterResourcePackagesAttachment() *schema.Resource { |
| 35 | + return &schema.Resource{ |
| 36 | + Create: resourceTencentCloudCynosdbClusterResourcePackagesAttachmentCreate, |
| 37 | + Read: resourceTencentCloudCynosdbClusterResourcePackagesAttachmentRead, |
| 38 | + Delete: resourceTencentCloudCynosdbClusterResourcePackagesAttachmentDelete, |
| 39 | + Importer: &schema.ResourceImporter{ |
| 40 | + State: schema.ImportStatePassthrough, |
| 41 | + }, |
| 42 | + Schema: map[string]*schema.Schema{ |
| 43 | + "cluster_id": { |
| 44 | + Required: true, |
| 45 | + ForceNew: true, |
| 46 | + Type: schema.TypeString, |
| 47 | + Description: "Cluster ID.", |
| 48 | + }, |
| 49 | + "package_ids": { |
| 50 | + Required: true, |
| 51 | + ForceNew: true, |
| 52 | + Type: schema.TypeSet, |
| 53 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 54 | + Description: "Resource Package Unique ID.", |
| 55 | + }, |
| 56 | + }, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func resourceTencentCloudCynosdbClusterResourcePackagesAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 61 | + defer logElapsed("resource.tencentcloud_cynosdb_cluster_resource_packages_attachment.create")() |
| 62 | + defer inconsistentCheck(d, meta)() |
| 63 | + |
| 64 | + var ( |
| 65 | + logId = getLogId(contextNil) |
| 66 | + request = cynosdb.NewBindClusterResourcePackagesRequest() |
| 67 | + clusterId string |
| 68 | + ) |
| 69 | + |
| 70 | + if v, ok := d.GetOk("cluster_id"); ok { |
| 71 | + request.ClusterId = helper.String(v.(string)) |
| 72 | + clusterId = v.(string) |
| 73 | + } |
| 74 | + |
| 75 | + if v, ok := d.GetOk("package_ids"); ok { |
| 76 | + packageIdsSet := v.(*schema.Set).List() |
| 77 | + for i := range packageIdsSet { |
| 78 | + packageIds := packageIdsSet[i].(string) |
| 79 | + request.PackageIds = append(request.PackageIds, &packageIds) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 84 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseCynosdbClient().BindClusterResourcePackages(request) |
| 85 | + if e != nil { |
| 86 | + return retryError(e) |
| 87 | + } else { |
| 88 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 89 | + } |
| 90 | + |
| 91 | + if result == nil { |
| 92 | + e = fmt.Errorf("cynosdb clusterResourcePackagesAttachment not exists") |
| 93 | + return resource.NonRetryableError(e) |
| 94 | + } |
| 95 | + |
| 96 | + return nil |
| 97 | + }) |
| 98 | + |
| 99 | + if err != nil { |
| 100 | + log.Printf("[CRITAL]%s create cynosdb clusterResourcePackagesAttachment failed, reason:%+v", logId, err) |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + d.SetId(clusterId) |
| 105 | + |
| 106 | + return resourceTencentCloudCynosdbClusterResourcePackagesAttachmentRead(d, meta) |
| 107 | +} |
| 108 | + |
| 109 | +func resourceTencentCloudCynosdbClusterResourcePackagesAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 110 | + defer logElapsed("resource.tencentcloud_cynosdb_cluster_resource_packages_attachment.read")() |
| 111 | + defer inconsistentCheck(d, meta)() |
| 112 | + |
| 113 | + var ( |
| 114 | + logId = getLogId(contextNil) |
| 115 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 116 | + service = CynosdbService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 117 | + clusterId = d.Id() |
| 118 | + ) |
| 119 | + |
| 120 | + clusterResourcePackagesAttachment, err := service.DescribeCynosdbClusterResourcePackagesAttachmentById(ctx, clusterId) |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + |
| 125 | + if clusterResourcePackagesAttachment == nil { |
| 126 | + d.SetId("") |
| 127 | + log.Printf("[WARN]%s resource `CynosdbClusterResourcePackagesAttachment` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 128 | + return nil |
| 129 | + } |
| 130 | + |
| 131 | + if clusterResourcePackagesAttachment.ClusterId != nil { |
| 132 | + _ = d.Set("cluster_id", clusterResourcePackagesAttachment.ClusterId) |
| 133 | + } |
| 134 | + |
| 135 | + if clusterResourcePackagesAttachment.ResourcePackages != nil { |
| 136 | + tmpList := []interface{}{} |
| 137 | + for _, v := range clusterResourcePackagesAttachment.ResourcePackages { |
| 138 | + if v.PackageId != nil { |
| 139 | + tmpList = append(tmpList, v.PackageId) |
| 140 | + } |
| 141 | + } |
| 142 | + _ = d.Set("package_ids", tmpList) |
| 143 | + } |
| 144 | + |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +func resourceTencentCloudCynosdbClusterResourcePackagesAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 149 | + defer logElapsed("resource.tencentcloud_cynosdb_cluster_resource_packages_attachment.delete")() |
| 150 | + defer inconsistentCheck(d, meta)() |
| 151 | + |
| 152 | + var ( |
| 153 | + logId = getLogId(contextNil) |
| 154 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 155 | + service = CynosdbService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 156 | + clusterId = d.Id() |
| 157 | + packageIdsSet []*string |
| 158 | + ) |
| 159 | + |
| 160 | + if v, ok := d.GetOk("package_ids"); ok { |
| 161 | + idsSet := v.(*schema.Set).List() |
| 162 | + for i := range idsSet { |
| 163 | + ids := idsSet[i].(string) |
| 164 | + packageIdsSet = append(packageIdsSet, &ids) |
| 165 | + } |
| 166 | + |
| 167 | + if err := service.DeleteCynosdbClusterResourcePackagesAttachmentById(ctx, clusterId, packageIdsSet); err != nil { |
| 168 | + return err |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return nil |
| 173 | +} |
0 commit comments