Skip to content

Commit ecb4a0c

Browse files
feat: add ssh key data source (scaleway#262)
* feat: add ssh key data source
1 parent 5361f60 commit ecb4a0c

File tree

3 files changed

+125
-4
lines changed

3 files changed

+125
-4
lines changed

scaleway/data_source_account_ssh.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package scaleway
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hashicorp/terraform/helper/schema"
7+
account "github.com/scaleway/scaleway-sdk-go/api/account/v2alpha1"
8+
)
9+
10+
func dataSourceScalewayAccountSSH() *schema.Resource {
11+
return &schema.Resource{
12+
Read: dataSourceScalewayAccountSSHRead,
13+
14+
Schema: map[string]*schema.Schema{
15+
"name": {
16+
Type: schema.TypeString,
17+
Optional: true,
18+
Computed: true,
19+
Description: "The name of the SSH key",
20+
},
21+
"ssh_key_id": {
22+
Type: schema.TypeString,
23+
Optional: true,
24+
Computed: true,
25+
Description: "The name of the SSH key",
26+
ValidateFunc: validationUUIDorUUIDWithLocality(),
27+
},
28+
"public_key": {
29+
Type: schema.TypeString,
30+
Computed: true,
31+
Description: "The public SSH key",
32+
},
33+
"organization_id": organizationIDSchema(),
34+
},
35+
}
36+
}
37+
38+
func dataSourceScalewayAccountSSHRead(d *schema.ResourceData, m interface{}) error {
39+
accountAPI := getAccountAPI(m)
40+
41+
var sshKey *account.SSHKey
42+
sshKeyID, ok := d.GetOk("ssh_key_id")
43+
if ok {
44+
res, err := accountAPI.GetSSHKey(&account.GetSSHKeyRequest{SSHKeyID: expandID(sshKeyID)})
45+
if err != nil {
46+
return err
47+
}
48+
sshKey = res
49+
} else {
50+
res, err := accountAPI.ListSSHKeys(&account.ListSSHKeysRequest{
51+
Name: String(d.Get("name").(string)),
52+
})
53+
if err != nil {
54+
return err
55+
}
56+
if len(res.SSHKeys) == 0 {
57+
return fmt.Errorf("no SSH Key found with the name %s", d.Get("name"))
58+
}
59+
if len(res.SSHKeys) > 1 {
60+
return fmt.Errorf("%d SSH Keys found with the same name %s", len(res.SSHKeys), d.Get("name"))
61+
}
62+
sshKey = res.SSHKeys[0]
63+
}
64+
65+
d.SetId(sshKey.ID)
66+
d.Set("name", sshKey.Name)
67+
d.Set("ssh_key_id", sshKey.ID)
68+
d.Set("public_key", sshKey.PublicKey)
69+
d.Set("organization_id", sshKey.OrganizationID)
70+
71+
return nil
72+
}
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package scaleway
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform/helper/acctest"
7+
"github.com/hashicorp/terraform/helper/resource"
8+
)
9+
10+
func TestAccScalewayDataSourceAccountSSH_Basic(t *testing.T) {
11+
sshKeyName := acctest.RandString(10)
12+
resource.ParallelTest(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheck(t) },
14+
Providers: testAccProviders,
15+
CheckDestroy: testAccCheckScalewayAccountSSHKeyDestroy,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccDataSourceScalewayAccountSSHKeyConfig(sshKeyName),
19+
Check: resource.ComposeTestCheckFunc(
20+
testAccCheckScalewayAccountSSHKeyExists("data.scaleway_account_ssh_key.prod"),
21+
resource.TestCheckResourceAttr("data.scaleway_account_ssh_key.prod", "name", sshKeyName),
22+
resource.TestCheckResourceAttr("data.scaleway_account_ssh_key.prod", "public_key", dataSourceAccountSSHKey),
23+
testAccCheckScalewayAccountSSHKeyExists("data.scaleway_account_ssh_key.stg"),
24+
resource.TestCheckResourceAttr("data.scaleway_account_ssh_key.stg", "name", sshKeyName),
25+
resource.TestCheckResourceAttr("data.scaleway_account_ssh_key.stg", "public_key", dataSourceAccountSSHKey),
26+
),
27+
},
28+
},
29+
})
30+
}
31+
32+
const dataSourceAccountSSHKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDCtMlb2pIlcCC3Exui1z77NfFWtlf59P/L38OwfHCKnxgHZfcjWZZEOdJPxeA6/iRCUrJo+mUKOedNJTCR9Wgg5zhUM1dd/fiyCS+STLf9fzoA1evPEEIHp0iobIpVQ0pGIdNqipL2n8BcV7f1oC2AkELCUp4gogkeUkKtK71DtsjGJQJBRlg01U2gTAnU0Q3kf5wxhCIELke9J3eblTpvdnNonqpXsQuy+InpT51NDtMbBdgkwNsgw6wDxI64NY92mEXO7PK/uhSAmxIM9a4evLhgFxSr8vFNTwqq5fbSyDeeQwHG23U0CmhM0tOwLuJAbQotWrfYsLCpinnBb+sp [email protected]"
33+
34+
func testAccDataSourceScalewayAccountSSHKeyConfig(name string) string {
35+
return `
36+
resource "scaleway_account_ssh_key" "main" {
37+
name = "` + name + `"
38+
public_key = "` + dataSourceAccountSSHKey + `"
39+
}
40+
41+
data "scaleway_account_ssh_key" "prod" {
42+
name = "${scaleway_account_ssh_key.main.name}"
43+
}
44+
45+
data "scaleway_account_ssh_key" "stg" {
46+
ssh_key_id = "${scaleway_account_ssh_key.main.id}"
47+
}`
48+
}

scaleway/provider.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,11 @@ func Provider() terraform.ResourceProvider {
209209
},
210210

211211
DataSourcesMap: map[string]*schema.Resource{
212-
"scaleway_bootscript": dataSourceScalewayBootscript(),
213-
"scaleway_image": dataSourceScalewayImage(),
214-
"scaleway_security_group": dataSourceScalewaySecurityGroup(),
215-
"scaleway_volume": dataSourceScalewayVolume(),
212+
"scaleway_bootscript": dataSourceScalewayBootscript(),
213+
"scaleway_image": dataSourceScalewayImage(),
214+
"scaleway_security_group": dataSourceScalewaySecurityGroup(),
215+
"scaleway_volume": dataSourceScalewayVolume(),
216+
"scaleway_account_ssh_key": dataSourceScalewayAccountSSH(),
216217
},
217218

218219
ConfigureFunc: providerConfigure,

0 commit comments

Comments
 (0)