|
| 1 | +/* |
| 2 | +Provide a resource to create tke backup storage location. |
| 3 | +
|
| 4 | +~> **NOTE:** To create this resource, you need to create a cos bucket with prefix "tke-backup" in advance. |
| 5 | +
|
| 6 | +Example Usage |
| 7 | +``` |
| 8 | +
|
| 9 | + resource "tencentcloud_kubernetes_backup_storage_location" "example_backup" { |
| 10 | + name = "example-backup-1" |
| 11 | + storage_region = "ap-guangzhou" # region of you pre-created COS bucket |
| 12 | + bucket = "tke-backup-example-1" # bucket name of your pre-created COS bucket |
| 13 | + } |
| 14 | +
|
| 15 | +``` |
| 16 | +
|
| 17 | +Import |
| 18 | +
|
| 19 | +tke backup storage location can be imported, e.g. |
| 20 | +
|
| 21 | +``` |
| 22 | +$ terraform import tencentcloud_kubernetes_backup_storage_location.test xxx |
| 23 | +``` |
| 24 | +*/ |
| 25 | +package tencentcloud |
| 26 | + |
| 27 | +import ( |
| 28 | + "context" |
| 29 | + "fmt" |
| 30 | + |
| 31 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 32 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 33 | + tke "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke/v20180525" |
| 34 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 35 | +) |
| 36 | + |
| 37 | +func resourceTencentCloudTkeBackupStorageLocation() *schema.Resource { |
| 38 | + return &schema.Resource{ |
| 39 | + Importer: &schema.ResourceImporter{ |
| 40 | + State: schema.ImportStatePassthrough, |
| 41 | + }, |
| 42 | + Create: resourceTencentCloudTkeBackupStorageLocationCreate, |
| 43 | + Read: resourceTencentCloudTkeBackupStorageLocationRead, |
| 44 | + Delete: resourceTencentCloudTkeBackupStorageLocationDelete, |
| 45 | + Schema: map[string]*schema.Schema{ |
| 46 | + "name": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Required: true, |
| 49 | + ForceNew: true, |
| 50 | + Description: "Name of the backup storage location.", |
| 51 | + }, |
| 52 | + "storage_region": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Required: true, |
| 55 | + ForceNew: true, |
| 56 | + Description: "Region of the storage.", |
| 57 | + }, |
| 58 | + "bucket": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Required: true, |
| 61 | + ForceNew: true, |
| 62 | + Description: "Name of the bucket.", |
| 63 | + }, |
| 64 | + "path": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Optional: true, |
| 67 | + ForceNew: true, |
| 68 | + Description: "Prefix of the bucket.", |
| 69 | + }, |
| 70 | + "state": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Computed: true, |
| 73 | + Description: "State of the backup storage location.", |
| 74 | + }, |
| 75 | + "message": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Computed: true, |
| 78 | + Description: "Message of the backup storage location.", |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func resourceTencentCloudTkeBackupStorageLocationCreate(d *schema.ResourceData, meta interface{}) error { |
| 85 | + defer logElapsed("resource.tencentcloud_kubernetes_backup_storage_location.create")() |
| 86 | + |
| 87 | + logId := getLogId(contextNil) |
| 88 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 89 | + client := meta.(*TencentCloudClient).apiV3Conn |
| 90 | + service := TkeService{client: client} |
| 91 | + |
| 92 | + request := genCreateBackupStorageLocationRequest(d) |
| 93 | + err := service.createBackupStorageLocation(ctx, request) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + |
| 98 | + // wait for status ok |
| 99 | + err = resource.Retry(3*readRetryTimeout, func() *resource.RetryError { |
| 100 | + locations, errRet := service.describeBackupStorageLocations(ctx, []string{d.Get("name").(string)}) |
| 101 | + if errRet != nil { |
| 102 | + return retryError(errRet, InternalError) |
| 103 | + } |
| 104 | + if len(locations) != 1 { |
| 105 | + resource.RetryableError(fmt.Errorf("more than 1 location returnen in api response, expected 1 but got %d", len(locations))) |
| 106 | + } |
| 107 | + if locations[0].State == nil { |
| 108 | + return resource.RetryableError(fmt.Errorf("location %s is still in state nil", d.Get("name").(string))) |
| 109 | + } |
| 110 | + if len(locations) == 1 && *locations[0].State == backupStorageLocationStateAvailable { |
| 111 | + return nil |
| 112 | + } |
| 113 | + return resource.RetryableError(fmt.Errorf("location %s is still in state %s", d.Get("name").(string), *locations[0].State)) |
| 114 | + }) |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + d.SetId(d.Get("name").(string)) |
| 120 | + return resourceTencentCloudTkeBackupStorageLocationRead(d, meta) |
| 121 | +} |
| 122 | + |
| 123 | +func resourceTencentCloudTkeBackupStorageLocationRead(d *schema.ResourceData, meta interface{}) error { |
| 124 | + defer logElapsed("resource.tencentcloud_kubernetes_backup_storage_location.read")() |
| 125 | + |
| 126 | + logId := getLogId(contextNil) |
| 127 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 128 | + client := meta.(*TencentCloudClient).apiV3Conn |
| 129 | + service := TkeService{client: client} |
| 130 | + |
| 131 | + locations, err := service.describeBackupStorageLocations(ctx, []string{d.Id()}) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + for _, location := range locations { |
| 136 | + if *location.Name == d.Id() { |
| 137 | + _ = d.Set("name", location.Name) |
| 138 | + _ = d.Set("storage_region", location.StorageRegion) |
| 139 | + _ = d.Set("bucket", location.Bucket) |
| 140 | + _ = d.Set("path", location.Path) |
| 141 | + _ = d.Set("state", location.State) |
| 142 | + _ = d.Set("message", location.Message) |
| 143 | + return nil |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + d.SetId("") |
| 148 | + return nil |
| 149 | +} |
| 150 | + |
| 151 | +func resourceTencentCloudTkeBackupStorageLocationDelete(d *schema.ResourceData, meta interface{}) error { |
| 152 | + defer logElapsed("resource.tencentcloud_kubernetes_backup_storage_location.delete")() |
| 153 | + |
| 154 | + logId := getLogId(contextNil) |
| 155 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 156 | + client := meta.(*TencentCloudClient).apiV3Conn |
| 157 | + service := TkeService{client: client} |
| 158 | + |
| 159 | + err := service.deleteBackupStorageLocation(ctx, d.Id()) |
| 160 | + if err != nil { |
| 161 | + return err |
| 162 | + } |
| 163 | + |
| 164 | + // wait until location is deleted |
| 165 | + err = resource.Retry(3*readRetryTimeout, func() *resource.RetryError { |
| 166 | + locations, errRet := service.describeBackupStorageLocations(ctx, []string{d.Id()}) |
| 167 | + if errRet != nil { |
| 168 | + return retryError(errRet, InternalError) |
| 169 | + } |
| 170 | + if len(locations) == 0 { |
| 171 | + return nil |
| 172 | + } |
| 173 | + return resource.RetryableError(fmt.Errorf("location %s is still not deleted", d.Id())) |
| 174 | + }) |
| 175 | + if err != nil { |
| 176 | + return err |
| 177 | + } |
| 178 | + |
| 179 | + return nil |
| 180 | +} |
| 181 | + |
| 182 | +func genCreateBackupStorageLocationRequest(d *schema.ResourceData) (request *tke.CreateBackupStorageLocationRequest) { |
| 183 | + request = tke.NewCreateBackupStorageLocationRequest() |
| 184 | + if v, ok := d.GetOk("name"); ok { |
| 185 | + request.Name = helper.String(v.(string)) |
| 186 | + } |
| 187 | + if v, ok := d.GetOk("storage_region"); ok { |
| 188 | + request.StorageRegion = helper.String(v.(string)) |
| 189 | + } |
| 190 | + if v, ok := d.GetOk("bucket"); ok { |
| 191 | + request.Bucket = helper.String(v.(string)) |
| 192 | + } |
| 193 | + if v, ok := d.GetOk("path"); ok { |
| 194 | + request.Path = helper.String(v.(string)) |
| 195 | + } |
| 196 | + return request |
| 197 | +} |
0 commit comments