|
1 | 1 | package scaleway |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | | - "strings" |
6 | | - |
7 | | - "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
8 | 4 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
9 | | - account "github.com/scaleway/scaleway-sdk-go/api/account/v2alpha1" |
10 | | - "github.com/scaleway/scaleway-sdk-go/scw" |
11 | 5 | ) |
12 | 6 |
|
13 | 7 | func resourceScalewayAccountSSKKey() *schema.Resource { |
14 | | - return &schema.Resource{ |
15 | | - CreateContext: resourceScalewayAccountSSHKeyCreate, |
16 | | - ReadContext: resourceScalewayAccountSSHKeyRead, |
17 | | - UpdateContext: resourceScalewayAccountSSHKeyUpdate, |
18 | | - DeleteContext: resourceScalewayAccountSSHKeyDelete, |
19 | | - Importer: &schema.ResourceImporter{ |
20 | | - StateContext: schema.ImportStatePassthroughContext, |
21 | | - }, |
22 | | - Timeouts: &schema.ResourceTimeout{ |
23 | | - Default: schema.DefaultTimeout(defaultAccountSSHKeyTimeout), |
24 | | - }, |
25 | | - Schema: map[string]*schema.Schema{ |
26 | | - "name": { |
27 | | - Type: schema.TypeString, |
28 | | - Optional: true, |
29 | | - Description: "The name of the SSH key", |
30 | | - }, |
31 | | - "public_key": { |
32 | | - Type: schema.TypeString, |
33 | | - Required: true, |
34 | | - ForceNew: true, |
35 | | - Description: "The public SSH key", |
36 | | - // We don't consider trailing \n as diff |
37 | | - DiffSuppressFunc: func(k, oldValue, newValue string, d *schema.ResourceData) bool { |
38 | | - return strings.Trim(oldValue, "\n") == strings.Trim(newValue, "\n") |
39 | | - }, |
40 | | - }, |
41 | | - "organization_id": organizationIDSchema(), |
42 | | - "project_id": projectIDSchema(), |
43 | | - }, |
44 | | - } |
45 | | -} |
46 | | - |
47 | | -func resourceScalewayAccountSSHKeyCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
48 | | - accountAPI := accountAPI(meta) |
49 | | - |
50 | | - res, err := accountAPI.CreateSSHKey(&account.CreateSSHKeyRequest{ |
51 | | - Name: d.Get("name").(string), |
52 | | - PublicKey: strings.Trim(d.Get("public_key").(string), "\n"), |
53 | | - ProjectID: expandStringPtr(d.Get("project_id")), |
54 | | - }, scw.WithContext(ctx)) |
55 | | - if err != nil { |
56 | | - return diag.FromErr(err) |
57 | | - } |
58 | | - |
59 | | - d.SetId(res.ID) |
60 | | - |
61 | | - return resourceScalewayAccountSSHKeyRead(ctx, d, meta) |
62 | | -} |
63 | | - |
64 | | -func resourceScalewayAccountSSHKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
65 | | - accountAPI := accountAPI(meta) |
66 | | - |
67 | | - res, err := accountAPI.GetSSHKey(&account.GetSSHKeyRequest{ |
68 | | - SSHKeyID: d.Id(), |
69 | | - }, scw.WithContext(ctx)) |
70 | | - if err != nil { |
71 | | - if is404Error(err) { |
72 | | - d.SetId("") |
73 | | - return nil |
74 | | - } |
75 | | - return diag.FromErr(err) |
76 | | - } |
77 | | - |
78 | | - _ = d.Set("name", res.Name) |
79 | | - _ = d.Set("public_key", res.PublicKey) |
80 | | - _ = d.Set("organization_id", res.OrganizationID) |
81 | | - _ = d.Set("project_id", res.ProjectID) |
82 | | - |
83 | | - return nil |
84 | | -} |
85 | | - |
86 | | -func resourceScalewayAccountSSHKeyUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
87 | | - accountAPI := accountAPI(meta) |
88 | | - |
89 | | - if d.HasChange("name") { |
90 | | - _, err := accountAPI.UpdateSSHKey(&account.UpdateSSHKeyRequest{ |
91 | | - SSHKeyID: d.Id(), |
92 | | - Name: expandUpdatedStringPtr(d.Get("name")), |
93 | | - }, scw.WithContext(ctx)) |
94 | | - if err != nil { |
95 | | - return diag.FromErr(err) |
96 | | - } |
97 | | - } |
98 | | - |
99 | | - return resourceScalewayAccountSSHKeyRead(ctx, d, meta) |
100 | | -} |
101 | | - |
102 | | -func resourceScalewayAccountSSHKeyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
103 | | - accountAPI := accountAPI(meta) |
104 | | - |
105 | | - err := accountAPI.DeleteSSHKey(&account.DeleteSSHKeyRequest{ |
106 | | - SSHKeyID: d.Id(), |
107 | | - }, scw.WithContext(ctx)) |
108 | | - if err != nil && !is404Error(err) { |
109 | | - return diag.FromErr(err) |
110 | | - } |
111 | | - |
112 | | - return nil |
| 8 | + return resourceScalewayIamSSKKey() |
113 | 9 | } |
0 commit comments