|
| 1 | +package keymanager |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/types" |
| 7 | + |
| 8 | + key_manager "github.com/scaleway/scaleway-sdk-go/api/key_manager/v1alpha1" |
| 9 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 13 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional" |
| 14 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account" |
| 15 | +) |
| 16 | + |
| 17 | +func ResourceKey() *schema.Resource { |
| 18 | + return &schema.Resource{ |
| 19 | + CreateContext: resourceKeyCreate, |
| 20 | + ReadContext: resourceKeyRead, |
| 21 | + UpdateContext: resourceKeyUpdate, |
| 22 | + DeleteContext: resourceKeyDelete, |
| 23 | + Importer: &schema.ResourceImporter{ |
| 24 | + StateContext: schema.ImportStatePassthroughContext, |
| 25 | + }, |
| 26 | + Timeouts: &schema.ResourceTimeout{ |
| 27 | + Read: schema.DefaultTimeout(defaultKeyTimeout), |
| 28 | + Update: schema.DefaultTimeout(defaultKeyTimeout), |
| 29 | + Delete: schema.DefaultTimeout(defaultKeyTimeout), |
| 30 | + Default: schema.DefaultTimeout(defaultKeyTimeout), |
| 31 | + }, |
| 32 | + SchemaVersion: 0, |
| 33 | + Schema: map[string]*schema.Schema{ |
| 34 | + "name": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Optional: true, |
| 37 | + Description: "The name of the key.", |
| 38 | + }, |
| 39 | + "description": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Optional: true, |
| 42 | + Description: "The description of the key.", |
| 43 | + }, |
| 44 | + "state": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Computed: true, |
| 47 | + Description: "The state of the key (enabled, disabled, pending_key_material)", |
| 48 | + }, |
| 49 | + "locked": { |
| 50 | + Type: schema.TypeBool, |
| 51 | + Computed: true, |
| 52 | + Description: "The locked state of the key.", |
| 53 | + }, |
| 54 | + "protected": { |
| 55 | + Type: schema.TypeBool, |
| 56 | + Computed: true, |
| 57 | + Description: "Returns `true` if key protection is applied to the key", |
| 58 | + }, |
| 59 | + "rotation_count": { |
| 60 | + Type: schema.TypeInt, |
| 61 | + Computed: true, |
| 62 | + Description: "The number of times the key has been rotated", |
| 63 | + }, |
| 64 | + "rotated_at": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Computed: true, |
| 67 | + Description: "The date and time of the last rotation of the key", |
| 68 | + }, |
| 69 | + "created_at": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Computed: true, |
| 72 | + Description: "The date and time of the creation of the key", |
| 73 | + }, |
| 74 | + "updated_at": { |
| 75 | + Type: schema.TypeString, |
| 76 | + Computed: true, |
| 77 | + Description: "The date and time of the last update of the key", |
| 78 | + }, |
| 79 | + "tags": { |
| 80 | + Type: schema.TypeList, |
| 81 | + Optional: true, |
| 82 | + Elem: &schema.Schema{ |
| 83 | + Type: schema.TypeString, |
| 84 | + }, |
| 85 | + Description: "List of tags [\"tag1\", \"tag2\", ...] attached to a key", |
| 86 | + }, |
| 87 | + "origin": { |
| 88 | + Type: schema.TypeString, |
| 89 | + Computed: true, |
| 90 | + Description: "The origin of the key (scaleway_kms, external)", |
| 91 | + }, |
| 92 | + // Computed |
| 93 | + "project_id": account.ProjectIDSchema(), |
| 94 | + "region": regional.ComputedSchema(), |
| 95 | + }, |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func resourceKeyCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { |
| 100 | + api, region, err := newKeyManagerAPI(d, meta) |
| 101 | + if err != nil { |
| 102 | + return diag.FromErr(err) |
| 103 | + } |
| 104 | + |
| 105 | + keyCreateRequest := &key_manager.CreateKeyRequest{ |
| 106 | + Region: region, |
| 107 | + ProjectID: d.Get("project_id").(string), |
| 108 | + Name: types.ExpandStringPtr(d.Get("name")), |
| 109 | + Description: types.ExpandStringPtr(d.Get("description")), |
| 110 | + Unprotected: d.Get("protected").(bool), |
| 111 | + } |
| 112 | + |
| 113 | + rawTag, tagExist := d.GetOk("tags") |
| 114 | + if tagExist { |
| 115 | + keyCreateRequest.Tags = types.ExpandStrings(rawTag) |
| 116 | + } |
| 117 | + |
| 118 | + rawDescription, descriptionExist := d.GetOk("description") |
| 119 | + if descriptionExist { |
| 120 | + keyCreateRequest.Description = types.ExpandStringPtr(rawDescription) |
| 121 | + } |
| 122 | + |
| 123 | + keyResponse, err := api.CreateKey(keyCreateRequest, scw.WithContext(ctx)) |
| 124 | + if err != nil { |
| 125 | + return diag.FromErr(err) |
| 126 | + } |
| 127 | + |
| 128 | + d.SetId(regional.NewIDString(region, keyResponse.ID)) |
| 129 | + |
| 130 | + return resourceKeyRead(ctx, d, meta) |
| 131 | +} |
| 132 | + |
| 133 | +func resourceKeyRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { |
| 134 | + api, region, id, err := NewKeyManagerAPIWithRegionAndID(meta, d.Id()) |
| 135 | + if err != nil { |
| 136 | + return diag.FromErr(err) |
| 137 | + } |
| 138 | + |
| 139 | + key, err := api.GetKey(&key_manager.GetKeyRequest{ |
| 140 | + Region: region, |
| 141 | + KeyID: id, |
| 142 | + }, scw.WithContext(ctx)) |
| 143 | + if err != nil { |
| 144 | + return diag.FromErr(err) |
| 145 | + } |
| 146 | + |
| 147 | + _ = d.Set("region", key.Region) |
| 148 | + _ = d.Set("project_id", key.ProjectID) |
| 149 | + _ = d.Set("description", key.Description) |
| 150 | + _ = d.Set("name", key.Name) |
| 151 | + _ = d.Set("state", key.State) |
| 152 | + _ = d.Set("rotation_count", key.RotationCount) |
| 153 | + _ = d.Set("created_at", types.FlattenTime(key.CreatedAt)) |
| 154 | + _ = d.Set("updated_at", types.FlattenTime(key.UpdatedAt)) |
| 155 | + _ = d.Set("origin", key.Origin) |
| 156 | + _ = d.Set("rotated_at", types.FlattenTime(key.RotatedAt)) |
| 157 | + _ = d.Set("locked", key.Locked) |
| 158 | + _ = d.Set("protected", key.Protected) |
| 159 | + |
| 160 | + return nil |
| 161 | +} |
| 162 | + |
| 163 | +func resourceKeyUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { |
| 164 | + keyManagerAPI, region, ID, err := NewKeyManagerAPIWithRegionAndID(meta, d.Id()) |
| 165 | + if err != nil { |
| 166 | + return diag.FromErr(err) |
| 167 | + } |
| 168 | + |
| 169 | + shouldUpdate := false |
| 170 | + req := &key_manager.UpdateKeyRequest{ |
| 171 | + Region: region, |
| 172 | + KeyID: ID, |
| 173 | + } |
| 174 | + |
| 175 | + if d.HasChange("name") { |
| 176 | + req.Name = types.ExpandStringPtr(d.Get("name")) |
| 177 | + shouldUpdate = true |
| 178 | + } |
| 179 | + |
| 180 | + if d.HasChange("tags") { |
| 181 | + req.Tags = types.ExpandUpdatedStringsPtr(d.Get("tags")) |
| 182 | + } |
| 183 | + |
| 184 | + if shouldUpdate { |
| 185 | + _, err = keyManagerAPI.UpdateKey(req, scw.WithContext(ctx)) |
| 186 | + if err != nil { |
| 187 | + return diag.FromErr(err) |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + return resourceKeyRead(ctx, d, meta) |
| 192 | +} |
| 193 | + |
| 194 | +func resourceKeyDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { |
| 195 | + keyManagerAPI, region, ID, err := NewKeyManagerAPIWithRegionAndID(meta, d.Id()) |
| 196 | + if err != nil { |
| 197 | + return diag.FromErr(err) |
| 198 | + } |
| 199 | + |
| 200 | + err = keyManagerAPI.DeleteKey(&key_manager.DeleteKeyRequest{ |
| 201 | + KeyID: ID, |
| 202 | + Region: region, |
| 203 | + }, scw.WithContext(ctx)) |
| 204 | + if err != nil { |
| 205 | + return diag.FromErr(err) |
| 206 | + } |
| 207 | + |
| 208 | + return nil |
| 209 | +} |
0 commit comments