Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/resources/iam_ssh_key.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 8 additions & 1 deletion internal/services/iam/ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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))
Expand Down
13 changes: 5 additions & 8 deletions internal/services/iam/ssh_key_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
func TestAccSSHKeyAccount_basic(t *testing.T) {
name := "tf-test-account-ssh-key-basic"
SSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEEYrzDOZmhItdKaDAEqJQ4ORS2GyBMtBozYsK5kiXXX [email protected]"
FormattedSSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEEYrzDOZmhItdKaDAEqJQ4ORS2GyBMtBozYsK5kiXXX"
tt := acctest.NewTestTools(t)
defer tt.Cleanup()

Expand All @@ -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),
),
},
{
Expand All @@ -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),
),
},
},
Expand All @@ -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 [email protected]"
FormattedSSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDjfkdWCwkYlVQMDUfiZlVrmjaGOfBYnmkucssae8Iup"
tt := acctest.NewTestTools(t)
defer tt.Cleanup()

Expand All @@ -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),
),
},
},
Expand All @@ -83,7 +81,6 @@ func TestAccSSHKeyAccount_WithNewLine(t *testing.T) {
func TestAccSSHKeyAccount_ChangeResourceName(t *testing.T) {
name := "TestAccScalewayAccountSSHKey_ChangeResourceName"
SSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICJEoOOgQBLJPs4g/XcPTKT82NywNPpxeuA20FlOPlpO [email protected]"
FormattedSSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICJEoOOgQBLJPs4g/XcPTKT82NywNPpxeuA20FlOPlpO"
tt := acctest.NewTestTools(t)
defer tt.Cleanup()

Expand All @@ -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),
),
},
{
Expand All @@ -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),
),
},
},
Expand Down
26 changes: 20 additions & 6 deletions internal/services/iam/ssh_key_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
19 changes: 9 additions & 10 deletions internal/services/iam/ssh_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
)

const (
SSHKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICJEoOOgQBLJPs4g/XcPTKT82NywNPpxeuA20FlOPlpO [email protected]"
SSHKeyWithoutComment = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICJEoOOgQBLJPs4g/XcPTKT82NywNPpxeuA20FlOPlpO"
SSHKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICJEoOOgQBLJPs4g/XcPTKT82NywNPpxeuA20FlOPlpO [email protected]"
)

func TestAccSSHKey_basic(t *testing.T) {
Expand All @@ -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),
),
},
{
Expand All @@ -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),
),
},
},
Expand All @@ -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),
),
},
},
Expand All @@ -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),
),
},
{
Expand All @@ -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),
),
},
},
Expand All @@ -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"),
),
},
Expand All @@ -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"),
),
},
Expand All @@ -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"),
),
},
Expand Down
Loading