|
| 1 | +package scaleway |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1" |
| 10 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 11 | +) |
| 12 | + |
| 13 | +func resourceScalewayIamSSKKey() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + CreateContext: resourceScalewayIamSSKKeyCreate, |
| 16 | + ReadContext: resourceScalewayIamSSHKeyRead, |
| 17 | + UpdateContext: resourceScalewayIamSSKKeyUpdate, |
| 18 | + DeleteContext: resourceScalewayIamSSKKeyDelete, |
| 19 | + Importer: &schema.ResourceImporter{ |
| 20 | + StateContext: schema.ImportStatePassthroughContext, |
| 21 | + }, |
| 22 | + SchemaVersion: 0, |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "name": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Computed: true, |
| 27 | + Optional: true, |
| 28 | + Description: "The name of the iam SSH key", |
| 29 | + }, |
| 30 | + "public_key": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + ForceNew: true, |
| 34 | + Description: "The public SSH key", |
| 35 | + // We don't consider trailing \n as diff |
| 36 | + DiffSuppressFunc: func(k, oldValue, newValue string, d *schema.ResourceData) bool { |
| 37 | + return strings.Trim(oldValue, "\n") == strings.Trim(newValue, "\n") |
| 38 | + }, |
| 39 | + }, |
| 40 | + "fingerprint": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Computed: true, |
| 43 | + Description: "The fingerprint of the iam SSH key", |
| 44 | + }, |
| 45 | + "created_at": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Computed: true, |
| 48 | + Description: "The date and time of the creation of the iam SSH Key", |
| 49 | + }, |
| 50 | + "updated_at": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + Description: "The date and time of the last update of the iam SSH Key", |
| 54 | + }, |
| 55 | + "organization_id": organizationIDSchema(), |
| 56 | + "project_id": projectIDSchema(), |
| 57 | + "disabled": { |
| 58 | + Type: schema.TypeBool, |
| 59 | + Optional: true, |
| 60 | + Default: false, |
| 61 | + Description: "The SSH key status", |
| 62 | + }, |
| 63 | + }, |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func resourceScalewayIamSSKKeyCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 68 | + iamAPI := iamAPI(meta) |
| 69 | + |
| 70 | + res, err := iamAPI.CreateSSHKey(&iam.CreateSSHKeyRequest{ |
| 71 | + Name: d.Get("name").(string), |
| 72 | + PublicKey: strings.Trim(d.Get("public_key").(string), "\n"), |
| 73 | + ProjectID: (d.Get("project_id")).(string), |
| 74 | + }, scw.WithContext(ctx)) |
| 75 | + if err != nil { |
| 76 | + return diag.FromErr(err) |
| 77 | + } |
| 78 | + |
| 79 | + if _, disabledExists := d.GetOk("disabled"); disabledExists { |
| 80 | + _, err = iamAPI.UpdateSSHKey(&iam.UpdateSSHKeyRequest{ |
| 81 | + SSHKeyID: d.Id(), |
| 82 | + Disabled: expandBoolPtr(getBool(d, "disabled")), |
| 83 | + }, scw.WithContext(ctx)) |
| 84 | + if err != nil { |
| 85 | + return diag.FromErr(err) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + d.SetId(res.ID) |
| 90 | + |
| 91 | + return resourceScalewayIamSSHKeyRead(ctx, d, meta) |
| 92 | +} |
| 93 | + |
| 94 | +func resourceScalewayIamSSHKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 95 | + iamAPI := iamAPI(meta) |
| 96 | + |
| 97 | + res, err := iamAPI.GetSSHKey(&iam.GetSSHKeyRequest{ |
| 98 | + SSHKeyID: d.Id(), |
| 99 | + }, scw.WithContext(ctx)) |
| 100 | + if err != nil { |
| 101 | + if is404Error(err) { |
| 102 | + d.SetId("") |
| 103 | + return nil |
| 104 | + } |
| 105 | + return diag.FromErr(err) |
| 106 | + } |
| 107 | + |
| 108 | + _ = d.Set("name", res.Name) |
| 109 | + _ = d.Set("public_key", res.PublicKey) |
| 110 | + _ = d.Set("fingerprint", res.Fingerprint) |
| 111 | + _ = d.Set("created_at", flattenTime(res.CreatedAt)) |
| 112 | + _ = d.Set("updated_at", flattenTime(res.UpdatedAt)) |
| 113 | + _ = d.Set("organization_id", res.OrganizationID) |
| 114 | + _ = d.Set("project_id", res.ProjectID) |
| 115 | + _ = d.Set("disabled", res.Disabled) |
| 116 | + |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +func resourceScalewayIamSSKKeyUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 121 | + iamAPI := iamAPI(meta) |
| 122 | + |
| 123 | + req := &iam.UpdateSSHKeyRequest{ |
| 124 | + SSHKeyID: d.Id(), |
| 125 | + } |
| 126 | + |
| 127 | + hasUpdated := false |
| 128 | + |
| 129 | + if d.HasChange("name") { |
| 130 | + req.Name = expandStringPtr(d.Get("name")) |
| 131 | + hasUpdated = true |
| 132 | + } |
| 133 | + |
| 134 | + if d.HasChange("disabled") { |
| 135 | + if _, disabledExists := d.GetOk("disabled"); !disabledExists { |
| 136 | + _, err := iamAPI.UpdateSSHKey(&iam.UpdateSSHKeyRequest{ |
| 137 | + SSHKeyID: d.Id(), |
| 138 | + Disabled: expandBoolPtr(false), |
| 139 | + }) |
| 140 | + if err != nil { |
| 141 | + return diag.FromErr(err) |
| 142 | + } |
| 143 | + } else { |
| 144 | + _, err := iamAPI.UpdateSSHKey(&iam.UpdateSSHKeyRequest{ |
| 145 | + SSHKeyID: d.Id(), |
| 146 | + Disabled: expandBoolPtr(getBool(d, "disabled")), |
| 147 | + }) |
| 148 | + if err != nil { |
| 149 | + return diag.FromErr(err) |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + if hasUpdated { |
| 155 | + _, err := iamAPI.UpdateSSHKey(req, scw.WithContext(ctx)) |
| 156 | + if err != nil { |
| 157 | + return diag.FromErr(err) |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + return resourceScalewayIamSSHKeyRead(ctx, d, meta) |
| 162 | +} |
| 163 | + |
| 164 | +func resourceScalewayIamSSKKeyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 165 | + iamAPI := iamAPI(meta) |
| 166 | + |
| 167 | + err := iamAPI.DeleteSSHKey(&iam.DeleteSSHKeyRequest{ |
| 168 | + SSHKeyID: 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