|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/cloud-temple/terraform-provider-cloudtemple/internal/client" |
| 7 | + "github.com/cloud-temple/terraform-provider-cloudtemple/internal/provider/helpers" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | +) |
| 11 | + |
| 12 | +func resourceStorageAccount() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Description: "Create and manage object storage storage accounts.", |
| 15 | + |
| 16 | + CreateContext: objectStorageStorageAccountCreate, |
| 17 | + ReadContext: objectStorageStorageAccountRead, |
| 18 | + DeleteContext: objectStorageStorageAccountDelete, |
| 19 | + Importer: &schema.ResourceImporter{ |
| 20 | + StateContext: schema.ImportStatePassthroughContext, |
| 21 | + }, |
| 22 | + |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + // In |
| 25 | + "name": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Required: true, |
| 28 | + ForceNew: true, |
| 29 | + Description: "The name of the storage account.", |
| 30 | + }, |
| 31 | + |
| 32 | + // Out |
| 33 | + "id": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Computed: true, |
| 36 | + Description: "The ID of the storage account.", |
| 37 | + }, |
| 38 | + "access_key_id": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Computed: true, |
| 41 | + Description: "The access key ID of the storage account.", |
| 42 | + }, |
| 43 | + "access_secret_key": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Computed: true, |
| 46 | + Sensitive: true, |
| 47 | + Description: "The secret access key of the storage account. This is only available during creation.", |
| 48 | + }, |
| 49 | + "arn": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Computed: true, |
| 52 | + Description: "The ARN of the storage account.", |
| 53 | + }, |
| 54 | + "create_date": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Computed: true, |
| 57 | + Description: "The creation date of the storage account.", |
| 58 | + }, |
| 59 | + "path": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Computed: true, |
| 62 | + Description: "The path of the storage account.", |
| 63 | + }, |
| 64 | + "tags": { |
| 65 | + Type: schema.TypeList, |
| 66 | + Computed: true, |
| 67 | + Description: "The tags associated with the storage account.", |
| 68 | + Elem: &schema.Resource{ |
| 69 | + Schema: map[string]*schema.Schema{ |
| 70 | + "key": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Computed: true, |
| 73 | + }, |
| 74 | + "value": { |
| 75 | + Type: schema.TypeString, |
| 76 | + Computed: true, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func objectStorageStorageAccountCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { |
| 86 | + c := getClient(meta) |
| 87 | + |
| 88 | + createResp, err := c.ObjectStorage().StorageAccount().Create(ctx, &client.CreateStorageAccountRequest{ |
| 89 | + Name: d.Get("name").(string), |
| 90 | + }) |
| 91 | + if err != nil { |
| 92 | + return diag.Errorf("failed to create storage account: %s", err) |
| 93 | + } |
| 94 | + |
| 95 | + // Set ID - the name is the ID |
| 96 | + d.SetId(d.Get("name").(string)) |
| 97 | + |
| 98 | + // Set the sensitive keys from creation response |
| 99 | + d.Set("access_key_id", createResp.AccessKeyID) |
| 100 | + d.Set("access_secret_key", createResp.SecretAccessKey) |
| 101 | + |
| 102 | + return objectStorageStorageAccountRead(ctx, d, meta) |
| 103 | +} |
| 104 | + |
| 105 | +func objectStorageStorageAccountRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { |
| 106 | + c := getClient(meta) |
| 107 | + var diags diag.Diagnostics |
| 108 | + |
| 109 | + storageAccount, err := c.ObjectStorage().StorageAccount().Read(ctx, d.Id()) |
| 110 | + if err != nil { |
| 111 | + return diag.Errorf("failed to read storage account: %s", err) |
| 112 | + } |
| 113 | + if storageAccount == nil { |
| 114 | + d.SetId("") |
| 115 | + return nil |
| 116 | + } |
| 117 | + |
| 118 | + // Use helper to flatten the storage account data |
| 119 | + storageAccountData := helpers.FlattenStorageAccount(storageAccount) |
| 120 | + |
| 121 | + // Add tags |
| 122 | + tags := make([]map[string]interface{}, len(storageAccount.Tags)) |
| 123 | + for i, tag := range storageAccount.Tags { |
| 124 | + tags[i] = map[string]interface{}{ |
| 125 | + "key": tag.Key, |
| 126 | + "value": tag.Value, |
| 127 | + } |
| 128 | + } |
| 129 | + storageAccountData["tags"] = tags |
| 130 | + |
| 131 | + // Set all data in state (except sensitive keys which are only available at creation) |
| 132 | + for k, v := range storageAccountData { |
| 133 | + if err := d.Set(k, v); err != nil { |
| 134 | + return diag.FromErr(err) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return diags |
| 139 | +} |
| 140 | + |
| 141 | +func objectStorageStorageAccountDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { |
| 142 | + c := getClient(meta) |
| 143 | + |
| 144 | + activityId, err := c.ObjectStorage().StorageAccount().Delete(ctx, d.Id()) |
| 145 | + if err != nil { |
| 146 | + return diag.Errorf("failed to delete storage account: %s", err) |
| 147 | + } |
| 148 | + if _, err := c.Activity().WaitForCompletion(ctx, activityId, getWaiterOptions(ctx)); err != nil { |
| 149 | + return diag.Errorf("failed to delete storage account: %s", err) |
| 150 | + } |
| 151 | + |
| 152 | + return nil |
| 153 | +} |
0 commit comments