|
| 1 | +package scaleway |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1" |
| 9 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 10 | +) |
| 11 | + |
| 12 | +func resourceScalewayIamAPIKey() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + CreateContext: resourceScalewayIamAPIKeyCreate, |
| 15 | + ReadContext: resourceScalewayIamAPIKeyRead, |
| 16 | + UpdateContext: resourceScalewayIamAPIKeyUpdate, |
| 17 | + DeleteContext: resourceScalewayIamAPIKeyDelete, |
| 18 | + Importer: &schema.ResourceImporter{ |
| 19 | + StateContext: schema.ImportStatePassthroughContext, |
| 20 | + }, |
| 21 | + SchemaVersion: 0, |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "description": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Optional: true, |
| 26 | + Description: "The description of the iam api key", |
| 27 | + }, |
| 28 | + "created_at": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Computed: true, |
| 31 | + Description: "The date and time of the creation of the iam api key", |
| 32 | + }, |
| 33 | + "updated_at": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Computed: true, |
| 36 | + Description: "The date and time of the last update of the iam api key", |
| 37 | + }, |
| 38 | + "expires_at": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Optional: true, |
| 41 | + ForceNew: true, |
| 42 | + Description: "The date and time of the expiration of the iam api key. Cannot be changed afterwards", |
| 43 | + ValidateDiagFunc: validateDate(), |
| 44 | + }, |
| 45 | + "access_key": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Computed: true, |
| 48 | + Description: "The access key of the iam api key", |
| 49 | + }, |
| 50 | + "secret_key": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + Description: "The secret Key of the iam api key", |
| 54 | + }, |
| 55 | + "application_id": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Optional: true, |
| 58 | + Description: "ID of the application attached to the api key", |
| 59 | + ConflictsWith: []string{"user_id"}, |
| 60 | + ValidateFunc: validationUUID(), |
| 61 | + }, |
| 62 | + "user_id": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Optional: true, |
| 65 | + Description: "ID of the user attached to the api key", |
| 66 | + ConflictsWith: []string{"application_id"}, |
| 67 | + ValidateFunc: validationUUID(), |
| 68 | + }, |
| 69 | + "editable": { |
| 70 | + Type: schema.TypeBool, |
| 71 | + Computed: true, |
| 72 | + Description: "Whether or not the iam api key is editable", |
| 73 | + }, |
| 74 | + "creation_ip": { |
| 75 | + Type: schema.TypeString, |
| 76 | + Computed: true, |
| 77 | + Description: "The IPv4 Address of the device which created the API key", |
| 78 | + }, |
| 79 | + "default_project_id": projectIDSchema(), |
| 80 | + }, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func resourceScalewayIamAPIKeyCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 85 | + iamAPI := iamAPI(meta) |
| 86 | + res, err := iamAPI.CreateAPIKey(&iam.CreateAPIKeyRequest{ |
| 87 | + ApplicationID: expandStringPtr(d.Get("application_id")), |
| 88 | + UserID: expandStringPtr(d.Get("user_id")), |
| 89 | + ExpiresAt: expandTimePtr(d.Get("expires_at")), |
| 90 | + DefaultProjectID: expandStringPtr(d.Get("project_id")), |
| 91 | + Description: d.Get("description").(string), |
| 92 | + }, scw.WithContext(ctx)) |
| 93 | + if err != nil { |
| 94 | + return diag.FromErr(err) |
| 95 | + } |
| 96 | + |
| 97 | + d.SetId(res.AccessKey) |
| 98 | + |
| 99 | + return resourceScalewayIamAPIKeyRead(ctx, d, meta) |
| 100 | +} |
| 101 | + |
| 102 | +func resourceScalewayIamAPIKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 103 | + api := iamAPI(meta) |
| 104 | + res, err := api.GetAPIKey(&iam.GetAPIKeyRequest{ |
| 105 | + AccessKey: d.Id(), |
| 106 | + }, scw.WithContext(ctx)) |
| 107 | + if err != nil { |
| 108 | + if is404Error(err) { |
| 109 | + d.SetId("") |
| 110 | + return nil |
| 111 | + } |
| 112 | + return diag.FromErr(err) |
| 113 | + } |
| 114 | + _ = d.Set("description", res.Description) |
| 115 | + _ = d.Set("created_at", flattenTime(res.CreatedAt)) |
| 116 | + _ = d.Set("updated_at", flattenTime(res.UpdatedAt)) |
| 117 | + _ = d.Set("expires_at", flattenTime(res.ExpiresAt)) |
| 118 | + _ = d.Set("access_key", res.AccessKey) |
| 119 | + _ = d.Set("secret_key", res.SecretKey) |
| 120 | + |
| 121 | + if res.ApplicationID != nil { |
| 122 | + _ = d.Set("application_id", res.ApplicationID) |
| 123 | + } |
| 124 | + if res.UserID != nil { |
| 125 | + _ = d.Set("user_id", res.UserID) |
| 126 | + } |
| 127 | + |
| 128 | + _ = d.Set("editable", res.Editable) |
| 129 | + _ = d.Set("creation_ip", res.CreationIP) |
| 130 | + _ = d.Set("default_project_id", res.DefaultProjectID) |
| 131 | + |
| 132 | + return nil |
| 133 | +} |
| 134 | + |
| 135 | +func resourceScalewayIamAPIKeyUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 136 | + api := iamAPI(meta) |
| 137 | + |
| 138 | + req := &iam.UpdateAPIKeyRequest{ |
| 139 | + AccessKey: d.Id(), |
| 140 | + } |
| 141 | + |
| 142 | + hasChanged := false |
| 143 | + |
| 144 | + if d.HasChange("description") { |
| 145 | + req.Description = expandStringPtr(d.Get("description")) |
| 146 | + hasChanged = true |
| 147 | + } |
| 148 | + |
| 149 | + if d.HasChange("default_project_id") { |
| 150 | + req.DefaultProjectID = expandStringPtr(d.Get("default_project_id")) |
| 151 | + hasChanged = true |
| 152 | + } |
| 153 | + |
| 154 | + if hasChanged { |
| 155 | + _, err := api.UpdateAPIKey(req, scw.WithContext(ctx)) |
| 156 | + if err != nil { |
| 157 | + return diag.FromErr(err) |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + return resourceScalewayIamAPIKeyRead(ctx, d, meta) |
| 162 | +} |
| 163 | + |
| 164 | +func resourceScalewayIamAPIKeyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 165 | + api := iamAPI(meta) |
| 166 | + |
| 167 | + err := api.DeleteAPIKey(&iam.DeleteAPIKeyRequest{ |
| 168 | + AccessKey: d.Id(), |
| 169 | + }, scw.WithContext(ctx)) |
| 170 | + if err != nil && !is404Error(err) { |
| 171 | + return diag.FromErr(err) |
| 172 | + } |
| 173 | + |
| 174 | + return nil |
| 175 | +} |
0 commit comments