Skip to content

Commit 2816600

Browse files
committed
fix(iam_ssh_key): do not read public key in resource
This caused the value to differ from requested input and this can lead to issue in terraform if the value is used in other resources. Also as it cannot be updated, it seems right to assume the local key is the correct key
1 parent 1079fc3 commit 2816600

File tree

4 files changed

+42
-25
lines changed

4 files changed

+42
-25
lines changed

internal/services/iam/ssh_key.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ func ResourceSSKKey() *schema.Resource {
5555
areEqual := bytes.Equal(marshalledOldValue, marshalledNewValue)
5656
return areEqual
5757
},
58+
StateFunc: func(v interface{}) string {
59+
switch v := v.(type) {
60+
case string:
61+
return strings.TrimSpace(v)
62+
default:
63+
return ""
64+
}
65+
},
5866
},
5967
"fingerprint": {
6068
Type: schema.TypeString,
@@ -125,7 +133,6 @@ func resourceIamSSHKeyRead(ctx context.Context, d *schema.ResourceData, m interf
125133
}
126134

127135
_ = d.Set("name", res.Name)
128-
_ = d.Set("public_key", res.PublicKey)
129136
_ = d.Set("fingerprint", res.Fingerprint)
130137
_ = d.Set("created_at", types.FlattenTime(res.CreatedAt))
131138
_ = d.Set("updated_at", types.FlattenTime(res.UpdatedAt))

internal/services/iam/ssh_key_account_test.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
func TestAccSSHKeyAccount_basic(t *testing.T) {
1313
name := "tf-test-account-ssh-key-basic"
1414
SSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEEYrzDOZmhItdKaDAEqJQ4ORS2GyBMtBozYsK5kiXXX [email protected]"
15-
FormattedSSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEEYrzDOZmhItdKaDAEqJQ4ORS2GyBMtBozYsK5kiXXX"
1615
tt := acctest.NewTestTools(t)
1716
defer tt.Cleanup()
1817

@@ -31,7 +30,7 @@ func TestAccSSHKeyAccount_basic(t *testing.T) {
3130
Check: resource.ComposeTestCheckFunc(
3231
iamchecks.CheckSSHKeyExists(tt, "scaleway_account_ssh_key.main"),
3332
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "name", name),
34-
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "public_key", FormattedSSHKey),
33+
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "public_key", SSHKey),
3534
),
3635
},
3736
{
@@ -44,7 +43,7 @@ func TestAccSSHKeyAccount_basic(t *testing.T) {
4443
Check: resource.ComposeTestCheckFunc(
4544
iamchecks.CheckSSHKeyExists(tt, "scaleway_account_ssh_key.main"),
4645
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "name", name+"-updated"),
47-
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "public_key", FormattedSSHKey),
46+
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "public_key", SSHKey),
4847
),
4948
},
5049
},
@@ -54,7 +53,6 @@ func TestAccSSHKeyAccount_basic(t *testing.T) {
5453
func TestAccSSHKeyAccount_WithNewLine(t *testing.T) {
5554
name := "tf-test-account-ssh-key-newline"
5655
SSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDjfkdWCwkYlVQMDUfiZlVrmjaGOfBYnmkucssae8Iup [email protected]"
57-
FormattedSSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDjfkdWCwkYlVQMDUfiZlVrmjaGOfBYnmkucssae8Iup"
5856
tt := acctest.NewTestTools(t)
5957
defer tt.Cleanup()
6058

@@ -73,7 +71,7 @@ func TestAccSSHKeyAccount_WithNewLine(t *testing.T) {
7371
Check: resource.ComposeTestCheckFunc(
7472
iamchecks.CheckSSHKeyExists(tt, "scaleway_account_ssh_key.main"),
7573
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "name", name),
76-
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "public_key", FormattedSSHKey),
74+
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "public_key", SSHKey),
7775
),
7876
},
7977
},
@@ -83,7 +81,6 @@ func TestAccSSHKeyAccount_WithNewLine(t *testing.T) {
8381
func TestAccSSHKeyAccount_ChangeResourceName(t *testing.T) {
8482
name := "TestAccScalewayAccountSSHKey_ChangeResourceName"
8583
SSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICJEoOOgQBLJPs4g/XcPTKT82NywNPpxeuA20FlOPlpO [email protected]"
86-
FormattedSSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICJEoOOgQBLJPs4g/XcPTKT82NywNPpxeuA20FlOPlpO"
8784
tt := acctest.NewTestTools(t)
8885
defer tt.Cleanup()
8986

@@ -102,7 +99,7 @@ func TestAccSSHKeyAccount_ChangeResourceName(t *testing.T) {
10299
Check: resource.ComposeTestCheckFunc(
103100
iamchecks.CheckSSHKeyExists(tt, "scaleway_account_ssh_key.first"),
104101
resource.TestCheckResourceAttr("scaleway_account_ssh_key.first", "name", name),
105-
resource.TestCheckResourceAttr("scaleway_account_ssh_key.first", "public_key", FormattedSSHKey),
102+
resource.TestCheckResourceAttr("scaleway_account_ssh_key.first", "public_key", SSHKey),
106103
),
107104
},
108105
{
@@ -115,7 +112,7 @@ func TestAccSSHKeyAccount_ChangeResourceName(t *testing.T) {
115112
Check: resource.ComposeTestCheckFunc(
116113
iamchecks.CheckSSHKeyExists(tt, "scaleway_account_ssh_key.second"),
117114
resource.TestCheckResourceAttr("scaleway_account_ssh_key.second", "name", name),
118-
resource.TestCheckResourceAttr("scaleway_account_ssh_key.second", "public_key", FormattedSSHKey),
115+
resource.TestCheckResourceAttr("scaleway_account_ssh_key.second", "public_key", SSHKey),
119116
),
120117
},
121118
},

internal/services/iam/ssh_key_data_source.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1"
99
"github.com/scaleway/scaleway-sdk-go/scw"
1010
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
11+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
1112
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
1213
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
1314
)
@@ -64,14 +65,27 @@ func DataSourceIamSSHKeyRead(ctx context.Context, d *schema.ResourceData, m inte
6465
return diag.FromErr(err)
6566
}
6667

67-
diags := resourceIamSSHKeyRead(ctx, d, m)
68-
if diags != nil {
69-
return append(diags, diag.Errorf("failed to read iam ssh key state")...)
70-
}
68+
api := NewAPI(m)
7169

72-
if d.Id() == "" {
73-
return diag.Errorf("iam ssh key (%s) not found", sshKeyID)
70+
res, err := api.GetSSHKey(&iam.GetSSHKeyRequest{
71+
SSHKeyID: d.Id(),
72+
}, scw.WithContext(ctx))
73+
if err != nil {
74+
if httperrors.Is404(err) {
75+
d.SetId("")
76+
return diag.Errorf("iam ssh key (%s) not found", sshKeyID)
77+
}
78+
return diag.FromErr(err)
7479
}
7580

81+
_ = d.Set("name", res.Name)
82+
_ = d.Set("fingerprint", res.Fingerprint)
83+
_ = d.Set("created_at", types.FlattenTime(res.CreatedAt))
84+
_ = d.Set("updated_at", types.FlattenTime(res.UpdatedAt))
85+
_ = d.Set("organization_id", res.OrganizationID)
86+
_ = d.Set("project_id", res.ProjectID)
87+
_ = d.Set("disabled", res.Disabled)
88+
_ = d.Set("public_key", res.PublicKey)
89+
7690
return nil
7791
}

internal/services/iam/ssh_key_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import (
1010
)
1111

1212
const (
13-
SSHKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICJEoOOgQBLJPs4g/XcPTKT82NywNPpxeuA20FlOPlpO [email protected]"
14-
SSHKeyWithoutComment = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICJEoOOgQBLJPs4g/XcPTKT82NywNPpxeuA20FlOPlpO"
13+
SSHKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICJEoOOgQBLJPs4g/XcPTKT82NywNPpxeuA20FlOPlpO [email protected]"
1514
)
1615

1716
func TestAccSSHKey_basic(t *testing.T) {
@@ -34,7 +33,7 @@ func TestAccSSHKey_basic(t *testing.T) {
3433
Check: resource.ComposeTestCheckFunc(
3534
iamchecks.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.main"),
3635
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "name", name),
37-
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKeyWithoutComment),
36+
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKey),
3837
),
3938
},
4039
{
@@ -47,7 +46,7 @@ func TestAccSSHKey_basic(t *testing.T) {
4746
Check: resource.ComposeTestCheckFunc(
4847
iamchecks.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.main"),
4948
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "name", name+"-updated"),
50-
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKeyWithoutComment),
49+
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKey),
5150
),
5251
},
5352
},
@@ -74,7 +73,7 @@ func TestAccSSHKey_WithNewLine(t *testing.T) {
7473
Check: resource.ComposeTestCheckFunc(
7574
iamchecks.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.main"),
7675
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "name", name),
77-
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKeyWithoutComment),
76+
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKey),
7877
),
7978
},
8079
},
@@ -101,7 +100,7 @@ func TestAccSSHKey_ChangeResourceName(t *testing.T) {
101100
Check: resource.ComposeTestCheckFunc(
102101
iamchecks.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.first"),
103102
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.first", "name", name),
104-
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.first", "public_key", SSHKeyWithoutComment),
103+
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.first", "public_key", SSHKey),
105104
),
106105
},
107106
{
@@ -114,7 +113,7 @@ func TestAccSSHKey_ChangeResourceName(t *testing.T) {
114113
Check: resource.ComposeTestCheckFunc(
115114
iamchecks.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.second"),
116115
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.second", "name", name),
117-
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.second", "public_key", SSHKeyWithoutComment),
116+
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.second", "public_key", SSHKey),
118117
),
119118
},
120119
},
@@ -141,7 +140,7 @@ func TestAccSSHKey_Disabled(t *testing.T) {
141140
Check: resource.ComposeTestCheckFunc(
142141
iamchecks.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.main"),
143142
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "name", name),
144-
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKeyWithoutComment),
143+
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKey),
145144
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "disabled", "false"),
146145
),
147146
},
@@ -156,7 +155,7 @@ func TestAccSSHKey_Disabled(t *testing.T) {
156155
Check: resource.ComposeTestCheckFunc(
157156
iamchecks.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.main"),
158157
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "name", name),
159-
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKeyWithoutComment),
158+
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKey),
160159
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "disabled", "true"),
161160
),
162161
},
@@ -171,7 +170,7 @@ func TestAccSSHKey_Disabled(t *testing.T) {
171170
Check: resource.ComposeTestCheckFunc(
172171
iamchecks.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.main"),
173172
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "name", name),
174-
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKeyWithoutComment),
173+
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKey),
175174
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "disabled", "false"),
176175
),
177176
},

0 commit comments

Comments
 (0)