Skip to content

Commit fa1bcf1

Browse files
authored
Merge pull request #93 from pgaxatte/add-ovh-me
Add SSH Key support
2 parents e66b129 + ce809fd commit fa1bcf1

14 files changed

+781
-0
lines changed

go.sum

+159
Large diffs are not rendered by default.

ovh/data_source_ovh_me_ssh_key.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
7+
)
8+
9+
func dataSourceMeSshKey() *schema.Resource {
10+
return &schema.Resource{
11+
Read: dataSourceMeSshKeyRead,
12+
Schema: map[string]*schema.Schema{
13+
"key_name": {
14+
Type: schema.TypeString,
15+
Required: true,
16+
Description: "Name of this public Ssh key",
17+
},
18+
"key": {
19+
Type: schema.TypeString,
20+
Computed: true,
21+
Description: "ASCII encoded public Ssh key",
22+
},
23+
"default": {
24+
Type: schema.TypeBool,
25+
Computed: true,
26+
Description: "True when this public Ssh key is used for rescue mode and reinstallations",
27+
},
28+
},
29+
}
30+
}
31+
32+
func dataSourceMeSshKeyRead(d *schema.ResourceData, meta interface{}) error {
33+
config := meta.(*Config)
34+
35+
sshKey := &MeSshKeyResponse{}
36+
37+
keyName := d.Get("key_name").(string)
38+
err := config.OVHClient.Get(
39+
fmt.Sprintf("/me/sshKey/%s", keyName),
40+
sshKey,
41+
)
42+
if err != nil {
43+
return fmt.Errorf("Unable to find SSH key named %s:\n\t %q", keyName, err)
44+
}
45+
46+
d.SetId(sshKey.KeyName)
47+
d.Set("key_name", sshKey.KeyName)
48+
d.Set("key", sshKey.Key)
49+
d.Set("default", sshKey.Default)
50+
51+
return nil
52+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
)
10+
11+
func TestAccMeSshKeyDataSource_basic(t *testing.T) {
12+
sshKeyName := acctest.RandomWithPrefix(test_prefix)
13+
sshKey := "ssh-ed25519 AAAAC3NzaC1yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
14+
config := fmt.Sprintf(testAccMeSshKeyDatasourceConfig, sshKeyName, sshKey)
15+
16+
resource.Test(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheckCredentials(t) },
18+
Providers: testAccProviders,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: config,
22+
Check: resource.ComposeTestCheckFunc(
23+
resource.TestCheckResourceAttr(
24+
"data.ovh_me_ssh_key.key_1", "key_name", sshKeyName),
25+
resource.TestCheckResourceAttr(
26+
"data.ovh_me_ssh_key.key_1", "key", sshKey),
27+
),
28+
},
29+
},
30+
})
31+
}
32+
33+
const testAccMeSshKeyDatasourceConfig = `
34+
resource "ovh_me_ssh_key" "key_1" {
35+
key_name = "%s"
36+
key = "%s"
37+
}
38+
39+
data "ovh_me_ssh_key" "key_1" {
40+
key_name = ovh_me_ssh_key.key_1.key_name
41+
}
42+
`

ovh/data_source_ovh_me_ssh_keys.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"sort"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
9+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
10+
)
11+
12+
func dataSourceMeSshKeys() *schema.Resource {
13+
return &schema.Resource{
14+
Read: dataSourceMeSshKeysRead,
15+
Schema: map[string]*schema.Schema{
16+
"names": {
17+
Type: schema.TypeSet,
18+
Computed: true,
19+
Elem: &schema.Schema{Type: schema.TypeString},
20+
Set: schema.HashString,
21+
},
22+
},
23+
}
24+
}
25+
26+
func dataSourceMeSshKeysRead(d *schema.ResourceData, meta interface{}) error {
27+
config := meta.(*Config)
28+
29+
names := make([]string, 0)
30+
err := config.OVHClient.Get("/me/sshKey", &names)
31+
32+
if err != nil {
33+
return fmt.Errorf("Error calling /me/sshKey:\n\t %q", err)
34+
}
35+
36+
sort.Strings(names)
37+
d.SetId(hashcode.Strings(names))
38+
d.Set("names", names)
39+
40+
log.Printf("[DEBUG] Read SSH Keys names %s", names)
41+
return nil
42+
}
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
)
10+
11+
func TestAccMeSshKeysDataSource_basic(t *testing.T) {
12+
sshKey1Name := acctest.RandomWithPrefix(test_prefix)
13+
sshKey1 := "ssh-ed25519 AAAAC3NzaC1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
14+
sshKey2Name := acctest.RandomWithPrefix(test_prefix)
15+
sshKey2 := "ssh-ed25519 AAAAC3NzaC1yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
16+
17+
preSetup := fmt.Sprintf(
18+
testAccMeSshKeysDatasourceConfig_preSetup,
19+
sshKey1Name,
20+
sshKey1,
21+
sshKey2Name,
22+
sshKey2,
23+
)
24+
config := fmt.Sprintf(
25+
testAccMeSshKeysDatasourceConfig_keys,
26+
sshKey1Name,
27+
sshKey1,
28+
sshKey2Name,
29+
sshKey2,
30+
)
31+
32+
resource.Test(t, resource.TestCase{
33+
PreCheck: func() { testAccPreCheckCredentials(t) },
34+
Providers: testAccProviders,
35+
Steps: []resource.TestStep{
36+
{
37+
Config: preSetup,
38+
Check: resource.ComposeTestCheckFunc(
39+
resource.TestCheckResourceAttr(
40+
"ovh_me_ssh_key.key_1", "key_name", sshKey1Name),
41+
resource.TestCheckResourceAttr(
42+
"ovh_me_ssh_key.key_1", "key", sshKey1),
43+
resource.TestCheckResourceAttr(
44+
"ovh_me_ssh_key.key_2", "key_name", sshKey2Name),
45+
resource.TestCheckResourceAttr(
46+
"ovh_me_ssh_key.key_2", "key", sshKey2),
47+
),
48+
}, {
49+
Config: config,
50+
Check: resource.ComposeTestCheckFunc(
51+
resource.TestCheckOutput(
52+
"keys_present", "true"),
53+
),
54+
},
55+
},
56+
})
57+
}
58+
59+
const testAccMeSshKeysDatasourceConfig_preSetup = `
60+
resource "ovh_me_ssh_key" "key_1" {
61+
key_name = "%s"
62+
key = "%s"
63+
}
64+
65+
resource "ovh_me_ssh_key" "key_2" {
66+
key_name = "%s"
67+
key = "%s"
68+
}
69+
`
70+
71+
const testAccMeSshKeysDatasourceConfig_keys = `
72+
resource "ovh_me_ssh_key" "key_1" {
73+
key_name = "%s"
74+
key = "%s"
75+
}
76+
77+
resource "ovh_me_ssh_key" "key_2" {
78+
key_name = "%s"
79+
key = "%s"
80+
}
81+
82+
data "ovh_me_ssh_keys" "keys" {}
83+
84+
output "keys_present" {
85+
value = tostring(contains(data.ovh_me_ssh_keys.keys.names, ovh_me_ssh_key.key_1.key_name) && contains(data.ovh_me_ssh_keys.keys.names, ovh_me_ssh_key.key_2.key_name))
86+
}
87+
`

ovh/import_me_ssh_key_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
)
10+
11+
func TestAccMeSshKey_importBasic(t *testing.T) {
12+
resourceName := "ovh_me_ssh_key.key_1"
13+
sshKey := acctest.RandomWithPrefix(test_prefix)
14+
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: func() { testAccPreCheckCredentials(t) },
17+
Providers: testAccProviders,
18+
Steps: []resource.TestStep{
19+
{
20+
Config: fmt.Sprintf(testAccMeSshKeyConfig_import, sshKey),
21+
},
22+
{
23+
ResourceName: resourceName,
24+
ImportState: true,
25+
ImportStateVerify: true,
26+
},
27+
},
28+
})
29+
}
30+
31+
const testAccMeSshKeyConfig_import = `
32+
resource "ovh_me_ssh_key" "key_1" {
33+
key_name = "%s"
34+
key = "ssh-ed25519 AAAAC3NzaC1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
35+
}
36+
`

ovh/provider.go

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ func Provider() terraform.ResourceProvider {
4747
"ovh_iploadbalancing": dataSourceIpLoadbalancing(),
4848
"ovh_me_paymentmean_bankaccount": dataSourceMePaymentmeanBankaccount(),
4949
"ovh_me_paymentmean_creditcard": dataSourceMePaymentmeanCreditcard(),
50+
"ovh_me_ssh_key": dataSourceMeSshKey(),
51+
"ovh_me_ssh_keys": dataSourceMeSshKeys(),
5052

5153
// Legacy naming schema (publiccloud)
5254
"ovh_publiccloud_region": deprecated(dataSourcePublicCloudRegion(),
@@ -71,6 +73,7 @@ func Provider() terraform.ResourceProvider {
7173
"ovh_cloud_network_private": resourcePublicCloudPrivateNetwork(),
7274
"ovh_cloud_network_private_subnet": resourcePublicCloudPrivateNetworkSubnet(),
7375
"ovh_cloud_user": resourcePublicCloudUser(),
76+
"ovh_me_ssh_key": resourceMeSshKey(),
7477
"ovh_vrack_cloudproject": resourceVRackPublicCloudAttachment(),
7578

7679
// Legacy naming schema (publiccloud)

0 commit comments

Comments
 (0)