diff --git a/docs/resources/iam_ssh_key.md b/docs/resources/iam_ssh_key.md index 6936c1ab06..309fb74b53 100644 --- a/docs/resources/iam_ssh_key.md +++ b/docs/resources/iam_ssh_key.md @@ -22,7 +22,7 @@ resource "scaleway_iam_ssh_key" "main" { The following arguments are supported: - `name` - (Required) The name of the SSH key. -- `public_key` - (Required) The public SSH key to be added. +- `public_key` - (Required) The public SSH key to be added. Key will be stored in state without its comment. - `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the SSH key is associated with. - `disabled` - (Optional) The SSH key status. diff --git a/internal/services/iam/ssh_key.go b/internal/services/iam/ssh_key.go index 015765210f..83b3638e68 100644 --- a/internal/services/iam/ssh_key.go +++ b/internal/services/iam/ssh_key.go @@ -55,6 +55,14 @@ func ResourceSSKKey() *schema.Resource { areEqual := bytes.Equal(marshalledOldValue, marshalledNewValue) return areEqual }, + StateFunc: func(v interface{}) string { + switch v := v.(type) { + case string: + return strings.TrimSpace(v) + default: + return "" + } + }, }, "fingerprint": { Type: schema.TypeString, @@ -125,7 +133,6 @@ func resourceIamSSHKeyRead(ctx context.Context, d *schema.ResourceData, m interf } _ = d.Set("name", res.Name) - _ = d.Set("public_key", res.PublicKey) _ = d.Set("fingerprint", res.Fingerprint) _ = d.Set("created_at", types.FlattenTime(res.CreatedAt)) _ = d.Set("updated_at", types.FlattenTime(res.UpdatedAt)) diff --git a/internal/services/iam/ssh_key_account_test.go b/internal/services/iam/ssh_key_account_test.go index 95b1bae8e9..72f2b92193 100644 --- a/internal/services/iam/ssh_key_account_test.go +++ b/internal/services/iam/ssh_key_account_test.go @@ -12,7 +12,6 @@ import ( func TestAccSSHKeyAccount_basic(t *testing.T) { name := "tf-test-account-ssh-key-basic" SSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEEYrzDOZmhItdKaDAEqJQ4ORS2GyBMtBozYsK5kiXXX opensource@scaleway.com" - FormattedSSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEEYrzDOZmhItdKaDAEqJQ4ORS2GyBMtBozYsK5kiXXX" tt := acctest.NewTestTools(t) defer tt.Cleanup() @@ -31,7 +30,7 @@ func TestAccSSHKeyAccount_basic(t *testing.T) { Check: resource.ComposeTestCheckFunc( iamchecks.CheckSSHKeyExists(tt, "scaleway_account_ssh_key.main"), resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "name", name), - resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "public_key", FormattedSSHKey), + resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "public_key", SSHKey), ), }, { @@ -44,7 +43,7 @@ func TestAccSSHKeyAccount_basic(t *testing.T) { Check: resource.ComposeTestCheckFunc( iamchecks.CheckSSHKeyExists(tt, "scaleway_account_ssh_key.main"), resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "name", name+"-updated"), - resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "public_key", FormattedSSHKey), + resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "public_key", SSHKey), ), }, }, @@ -54,7 +53,6 @@ func TestAccSSHKeyAccount_basic(t *testing.T) { func TestAccSSHKeyAccount_WithNewLine(t *testing.T) { name := "tf-test-account-ssh-key-newline" SSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDjfkdWCwkYlVQMDUfiZlVrmjaGOfBYnmkucssae8Iup opensource@scaleway.com" - FormattedSSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDjfkdWCwkYlVQMDUfiZlVrmjaGOfBYnmkucssae8Iup" tt := acctest.NewTestTools(t) defer tt.Cleanup() @@ -73,7 +71,7 @@ func TestAccSSHKeyAccount_WithNewLine(t *testing.T) { Check: resource.ComposeTestCheckFunc( iamchecks.CheckSSHKeyExists(tt, "scaleway_account_ssh_key.main"), resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "name", name), - resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "public_key", FormattedSSHKey), + resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "public_key", SSHKey), ), }, }, @@ -83,7 +81,6 @@ func TestAccSSHKeyAccount_WithNewLine(t *testing.T) { func TestAccSSHKeyAccount_ChangeResourceName(t *testing.T) { name := "TestAccScalewayAccountSSHKey_ChangeResourceName" SSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICJEoOOgQBLJPs4g/XcPTKT82NywNPpxeuA20FlOPlpO opensource@scaleway.com" - FormattedSSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICJEoOOgQBLJPs4g/XcPTKT82NywNPpxeuA20FlOPlpO" tt := acctest.NewTestTools(t) defer tt.Cleanup() @@ -102,7 +99,7 @@ func TestAccSSHKeyAccount_ChangeResourceName(t *testing.T) { Check: resource.ComposeTestCheckFunc( iamchecks.CheckSSHKeyExists(tt, "scaleway_account_ssh_key.first"), resource.TestCheckResourceAttr("scaleway_account_ssh_key.first", "name", name), - resource.TestCheckResourceAttr("scaleway_account_ssh_key.first", "public_key", FormattedSSHKey), + resource.TestCheckResourceAttr("scaleway_account_ssh_key.first", "public_key", SSHKey), ), }, { @@ -115,7 +112,7 @@ func TestAccSSHKeyAccount_ChangeResourceName(t *testing.T) { Check: resource.ComposeTestCheckFunc( iamchecks.CheckSSHKeyExists(tt, "scaleway_account_ssh_key.second"), resource.TestCheckResourceAttr("scaleway_account_ssh_key.second", "name", name), - resource.TestCheckResourceAttr("scaleway_account_ssh_key.second", "public_key", FormattedSSHKey), + resource.TestCheckResourceAttr("scaleway_account_ssh_key.second", "public_key", SSHKey), ), }, }, diff --git a/internal/services/iam/ssh_key_data_source.go b/internal/services/iam/ssh_key_data_source.go index 8f6073cdd0..05a72aa9e0 100644 --- a/internal/services/iam/ssh_key_data_source.go +++ b/internal/services/iam/ssh_key_data_source.go @@ -8,6 +8,7 @@ import ( iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1" "github.com/scaleway/scaleway-sdk-go/scw" "github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors" "github.com/scaleway/terraform-provider-scaleway/v2/internal/types" "github.com/scaleway/terraform-provider-scaleway/v2/internal/verify" ) @@ -64,14 +65,27 @@ func DataSourceIamSSHKeyRead(ctx context.Context, d *schema.ResourceData, m inte return diag.FromErr(err) } - diags := resourceIamSSHKeyRead(ctx, d, m) - if diags != nil { - return append(diags, diag.Errorf("failed to read iam ssh key state")...) - } + api := NewAPI(m) - if d.Id() == "" { - return diag.Errorf("iam ssh key (%s) not found", sshKeyID) + res, err := api.GetSSHKey(&iam.GetSSHKeyRequest{ + SSHKeyID: d.Id(), + }, scw.WithContext(ctx)) + if err != nil { + if httperrors.Is404(err) { + d.SetId("") + return diag.Errorf("iam ssh key (%s) not found", sshKeyID) + } + return diag.FromErr(err) } + _ = d.Set("name", res.Name) + _ = d.Set("fingerprint", res.Fingerprint) + _ = d.Set("created_at", types.FlattenTime(res.CreatedAt)) + _ = d.Set("updated_at", types.FlattenTime(res.UpdatedAt)) + _ = d.Set("organization_id", res.OrganizationID) + _ = d.Set("project_id", res.ProjectID) + _ = d.Set("disabled", res.Disabled) + _ = d.Set("public_key", res.PublicKey) + return nil } diff --git a/internal/services/iam/ssh_key_test.go b/internal/services/iam/ssh_key_test.go index 65607cca41..cb46b25932 100644 --- a/internal/services/iam/ssh_key_test.go +++ b/internal/services/iam/ssh_key_test.go @@ -10,8 +10,7 @@ import ( ) const ( - SSHKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICJEoOOgQBLJPs4g/XcPTKT82NywNPpxeuA20FlOPlpO opensource@scaleway.com" - SSHKeyWithoutComment = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICJEoOOgQBLJPs4g/XcPTKT82NywNPpxeuA20FlOPlpO" + SSHKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICJEoOOgQBLJPs4g/XcPTKT82NywNPpxeuA20FlOPlpO opensource@scaleway.com" ) func TestAccSSHKey_basic(t *testing.T) { @@ -34,7 +33,7 @@ func TestAccSSHKey_basic(t *testing.T) { Check: resource.ComposeTestCheckFunc( iamchecks.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.main"), resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "name", name), - resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKeyWithoutComment), + resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKey), ), }, { @@ -47,7 +46,7 @@ func TestAccSSHKey_basic(t *testing.T) { Check: resource.ComposeTestCheckFunc( iamchecks.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.main"), resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "name", name+"-updated"), - resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKeyWithoutComment), + resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKey), ), }, }, @@ -74,7 +73,7 @@ func TestAccSSHKey_WithNewLine(t *testing.T) { Check: resource.ComposeTestCheckFunc( iamchecks.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.main"), resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "name", name), - resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKeyWithoutComment), + resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKey), ), }, }, @@ -101,7 +100,7 @@ func TestAccSSHKey_ChangeResourceName(t *testing.T) { Check: resource.ComposeTestCheckFunc( iamchecks.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.first"), resource.TestCheckResourceAttr("scaleway_iam_ssh_key.first", "name", name), - resource.TestCheckResourceAttr("scaleway_iam_ssh_key.first", "public_key", SSHKeyWithoutComment), + resource.TestCheckResourceAttr("scaleway_iam_ssh_key.first", "public_key", SSHKey), ), }, { @@ -114,7 +113,7 @@ func TestAccSSHKey_ChangeResourceName(t *testing.T) { Check: resource.ComposeTestCheckFunc( iamchecks.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.second"), resource.TestCheckResourceAttr("scaleway_iam_ssh_key.second", "name", name), - resource.TestCheckResourceAttr("scaleway_iam_ssh_key.second", "public_key", SSHKeyWithoutComment), + resource.TestCheckResourceAttr("scaleway_iam_ssh_key.second", "public_key", SSHKey), ), }, }, @@ -141,7 +140,7 @@ func TestAccSSHKey_Disabled(t *testing.T) { Check: resource.ComposeTestCheckFunc( iamchecks.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.main"), resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "name", name), - resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKeyWithoutComment), + resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKey), resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "disabled", "false"), ), }, @@ -156,7 +155,7 @@ func TestAccSSHKey_Disabled(t *testing.T) { Check: resource.ComposeTestCheckFunc( iamchecks.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.main"), resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "name", name), - resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKeyWithoutComment), + resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKey), resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "disabled", "true"), ), }, @@ -171,7 +170,7 @@ func TestAccSSHKey_Disabled(t *testing.T) { Check: resource.ComposeTestCheckFunc( iamchecks.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.main"), resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "name", name), - resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKeyWithoutComment), + resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKey), resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "disabled", "false"), ), },