Skip to content

Commit dc8694f

Browse files
authored
feat(account): deprecate resource account ssh key (#1863)
* feat(account): deprecate resource account ssh key * fix(ci): bump cassettes baremetal test * fix(baremetal): replace account api on baremetal test * fix(test): remove 500 from cassettes * test: update cassettes flexible-ip and baremetal
1 parent e688e0e commit dc8694f

25 files changed

+34700
-63751
lines changed

docs/resources/account_ssh_key.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ description: |-
88

99
Manages user SSH keys to access servers provisioned on Scaleway.
1010

11+
~> **Important:** The resource `scaleway_account_ssh_key` has been deprecated and will no longer be supported. Instead, use `scaleway_iam_ssh_key`.
12+
1113
## Example Usage
1214

1315
```hcl
Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,9 @@
11
package scaleway
22

33
import (
4-
"context"
5-
"fmt"
6-
7-
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
84
"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"
115
)
126

137
func dataSourceScalewayAccountSSHKey() *schema.Resource {
14-
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayAccountSSKKey().Schema)
15-
addOptionalFieldsToSchema(dsSchema, "name")
16-
17-
dsSchema["name"].ConflictsWith = []string{"ssh_key_id"}
18-
dsSchema["ssh_key_id"] = &schema.Schema{
19-
Type: schema.TypeString,
20-
Optional: true,
21-
Description: "The ID of the SSH key",
22-
ValidateFunc: validationUUID(),
23-
}
24-
25-
return &schema.Resource{
26-
ReadContext: dataSourceScalewayAccountSSHKeyRead,
27-
Schema: dsSchema,
28-
}
29-
}
30-
31-
func dataSourceScalewayAccountSSHKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
32-
accountAPI := accountAPI(meta)
33-
34-
var sshKey *account.SSHKey
35-
sshKeyID, ok := d.GetOk("ssh_key_id")
36-
if ok {
37-
res, err := accountAPI.GetSSHKey(&account.GetSSHKeyRequest{SSHKeyID: expandID(sshKeyID)}, scw.WithContext(ctx))
38-
if err != nil {
39-
return diag.FromErr(err)
40-
}
41-
sshKey = res
42-
} else {
43-
res, err := accountAPI.ListSSHKeys(&account.ListSSHKeysRequest{
44-
Name: expandStringPtr(d.Get("name")),
45-
ProjectID: expandStringPtr(d.Get("project_id")),
46-
}, scw.WithContext(ctx))
47-
if err != nil {
48-
return diag.FromErr(err)
49-
}
50-
if len(res.SSHKeys) == 0 {
51-
return diag.FromErr(fmt.Errorf("no SSH Key found with the name %s", d.Get("name")))
52-
}
53-
if len(res.SSHKeys) > 1 {
54-
return diag.FromErr(fmt.Errorf("%d SSH Keys found with the same name %s", len(res.SSHKeys), d.Get("name")))
55-
}
56-
sshKey = res.SSHKeys[0]
57-
}
58-
59-
d.SetId(sshKey.ID)
60-
_ = d.Set("ssh_key_id", sshKey.ID)
61-
62-
return resourceScalewayAccountSSHKeyRead(ctx, d, meta)
8+
return dataSourceScalewayIamSSHKey()
639
}

scaleway/data_source_account_ssh_key_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestAccScalewayDataSourceAccountSSHKey_Basic(t *testing.T) {
1515
resource.ParallelTest(t, resource.TestCase{
1616
PreCheck: func() { testAccPreCheck(t) },
1717
ProviderFactories: tt.ProviderFactories,
18-
CheckDestroy: testAccCheckScalewayAccountSSHKeyDestroy(tt),
18+
CheckDestroy: testAccCheckScalewayIamSSHKeyDestroy(tt),
1919
Steps: []resource.TestStep{
2020
{
2121
Config: fmt.Sprintf(`
@@ -40,10 +40,10 @@ func TestAccScalewayDataSourceAccountSSHKey_Basic(t *testing.T) {
4040
ssh_key_id = "${scaleway_account_ssh_key.main.id}"
4141
}`, sshKeyName, dataSourceAccountSSHKey),
4242
Check: resource.ComposeTestCheckFunc(
43-
testAccCheckScalewayAccountSSHKeyExists(tt, "data.scaleway_account_ssh_key.prod"),
43+
testAccCheckScalewayIamSSHKeyExists(tt, "data.scaleway_account_ssh_key.prod"),
4444
resource.TestCheckResourceAttr("data.scaleway_account_ssh_key.prod", "name", sshKeyName),
4545
resource.TestCheckResourceAttr("data.scaleway_account_ssh_key.prod", "public_key", dataSourceAccountSSHKey),
46-
testAccCheckScalewayAccountSSHKeyExists(tt, "data.scaleway_account_ssh_key.stg"),
46+
testAccCheckScalewayIamSSHKeyExists(tt, "data.scaleway_account_ssh_key.stg"),
4747
resource.TestCheckResourceAttr("data.scaleway_account_ssh_key.stg", "name", sshKeyName),
4848
resource.TestCheckResourceAttr("data.scaleway_account_ssh_key.stg", "public_key", dataSourceAccountSSHKey),
4949
),

scaleway/data_source_baremetal_server_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestAccScalewayDataSourceBaremetalServer_Basic(t *testing.T) {
3232
name = "EM-B112X-SSD"
3333
}
3434
35-
resource "scaleway_account_ssh_key" "main" {
35+
resource "scaleway_iam_ssh_key" "main" {
3636
name = "%s"
3737
public_key = "%s"
3838
}
@@ -44,7 +44,7 @@ func TestAccScalewayDataSourceBaremetalServer_Basic(t *testing.T) {
4444
offer = data.scaleway_baremetal_offer.my_offer.offer_id
4545
os = data.scaleway_baremetal_os.my_os.os_id
4646
47-
ssh_key_ids = [ scaleway_account_ssh_key.main.id ]
47+
ssh_key_ids = [ scaleway_iam_ssh_key.main.id ]
4848
}
4949
`, SSHKeyName, SSHKeyBaremetal, name),
5050
},
@@ -61,7 +61,7 @@ func TestAccScalewayDataSourceBaremetalServer_Basic(t *testing.T) {
6161
name = "EM-B112X-SSD"
6262
}
6363
64-
resource "scaleway_account_ssh_key" "main" {
64+
resource "scaleway_iam_ssh_key" "main" {
6565
name = "%s"
6666
public_key = "%s"
6767
}
@@ -73,7 +73,7 @@ func TestAccScalewayDataSourceBaremetalServer_Basic(t *testing.T) {
7373
offer = data.scaleway_baremetal_offer.my_offer.offer_id
7474
os = data.scaleway_baremetal_os.my_os.os_id
7575
76-
ssh_key_ids = [ scaleway_account_ssh_key.main.id ]
76+
ssh_key_ids = [ scaleway_iam_ssh_key.main.id ]
7777
}
7878
7979
data "scaleway_baremetal_server" "by_name" {

scaleway/helpers_account.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
11
package scaleway
22

33
import (
4-
"time"
5-
64
accountV2 "github.com/scaleway/scaleway-sdk-go/api/account/v2"
7-
account "github.com/scaleway/scaleway-sdk-go/api/account/v2alpha1"
8-
)
9-
10-
const (
11-
defaultAccountSSHKeyTimeout = 1 * time.Minute
125
)
136

14-
// accountAPI returns a new account API.
15-
func accountAPI(m interface{}) *account.API {
16-
meta := m.(*Meta)
17-
return account.NewAPI(meta.scwClient)
18-
}
19-
207
// accountV2API supports projects.
218
func accountV2API(m interface{}) *accountV2.API {
229
meta := m.(*Meta)

scaleway/provider_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ func TestAccScalewayProvider_SSHKeys(t *testing.T) {
576576
},
577577
}
578578
}(),
579-
CheckDestroy: testAccCheckScalewayAccountSSHKeyDestroy(tt),
579+
CheckDestroy: testAccCheckScalewayIamSSHKeyDestroy(tt),
580580
Steps: []resource.TestStep{
581581
{
582582
Config: fmt.Sprintf(`
@@ -593,8 +593,8 @@ func TestAccScalewayProvider_SSHKeys(t *testing.T) {
593593
}
594594
`, SSHKeyName, SSHKey),
595595
Check: resource.ComposeTestCheckFunc(
596-
testAccCheckScalewayAccountSSHKeyExists(tt, "scaleway_account_ssh_key.prod"),
597-
testAccCheckScalewayAccountSSHKeyExists(tt, "scaleway_account_ssh_key.dev"),
596+
testAccCheckScalewayIamSSHKeyExists(tt, "scaleway_account_ssh_key.prod"),
597+
testAccCheckScalewayIamSSHKeyExists(tt, "scaleway_account_ssh_key.dev"),
598598
),
599599
},
600600
},
@@ -633,7 +633,7 @@ func TestAccScalewayProvider_InstanceIPZones(t *testing.T) {
633633
},
634634
}
635635
}(),
636-
CheckDestroy: testAccCheckScalewayAccountSSHKeyDestroy(tt),
636+
CheckDestroy: testAccCheckScalewayIamSSHKeyDestroy(tt),
637637
Steps: []resource.TestStep{
638638
{
639639
Config: `
Lines changed: 1 addition & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,9 @@
11
package scaleway
22

33
import (
4-
"context"
5-
"strings"
6-
7-
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
84
"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"
115
)
126

137
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()
1139
}

0 commit comments

Comments
 (0)