Skip to content

Commit 4246531

Browse files
committed
Add /me/sshKey resources and datasources
1 parent f454cc6 commit 4246531

15 files changed

+760
-0
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/terraform-providers/terraform-provider-ovh
33
require (
44
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e // indirect
55
github.com/hashicorp/go-cleanhttp v0.5.1
6+
github.com/hashicorp/terraform v0.12.15
67
github.com/hashicorp/terraform-plugin-sdk v1.0.0
78
github.com/jtolds/gls v4.20.0+incompatible // indirect
89
github.com/mitchellh/go-homedir v1.1.0

go.sum

+161
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+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
sshKey := acctest.RandomWithPrefix(test_prefix)
13+
resource.Test(t, resource.TestCase{
14+
PreCheck: func() { testAccPreCheckCredentials(t) },
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: fmt.Sprintf(testAccMeSshKeyDatasourceConfig, sshKey),
19+
Check: resource.ComposeTestCheckFunc(
20+
resource.TestCheckResourceAttr(
21+
"data.ovh_me_ssh_key.key_1", "key_name", sshKey),
22+
),
23+
},
24+
},
25+
})
26+
}
27+
28+
const testAccMeSshKeyDatasourceConfig = `
29+
resource "ovh_me_ssh_key" "key_1" {
30+
key_name = "%s"
31+
key = "ssh-ed25519 AAAAC3NzaC1yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
32+
}
33+
34+
data "ovh_me_ssh_key" "key_1" {
35+
key_name = ovh_me_ssh_key.key_1.key_name
36+
}
37+
`

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.Sort(sort.StringSlice(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+
}
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
sshKey1 := acctest.RandomWithPrefix(test_prefix)
13+
sshKey2 := acctest.RandomWithPrefix(test_prefix)
14+
resource.Test(t, resource.TestCase{
15+
PreCheck: func() { testAccPreCheckCredentials(t) },
16+
Providers: testAccProviders,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: fmt.Sprintf(
20+
testAccMeSshKeysDatasourceConfig_preSetup,
21+
sshKey1,
22+
sshKey2,
23+
),
24+
Check: resource.ComposeTestCheckFunc(
25+
resource.TestCheckResourceAttr(
26+
"ovh_me_ssh_key.key_1", "key_name", sshKey1),
27+
resource.TestCheckResourceAttr(
28+
"ovh_me_ssh_key.key_2", "key_name", sshKey2),
29+
),
30+
}, {
31+
Config: fmt.Sprintf(
32+
testAccMeSshKeysDatasourceConfig_keys,
33+
sshKey1,
34+
sshKey2,
35+
),
36+
Check: resource.ComposeTestCheckFunc(
37+
resource.TestCheckOutput(
38+
"keys_present", "true"),
39+
),
40+
},
41+
},
42+
})
43+
}
44+
45+
const testAccMeSshKeysDatasourceConfig_preSetup = `
46+
resource "ovh_me_ssh_key" "key_1" {
47+
key_name = "%s"
48+
key = "ssh-ed25519 AAAAC3NzaC1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
49+
}
50+
51+
resource "ovh_me_ssh_key" "key_2" {
52+
key_name = "%s"
53+
key = "ssh-ed25519 AAAAC3NzaC1yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
54+
}
55+
`
56+
57+
const testAccMeSshKeysDatasourceConfig_keys = `
58+
resource "ovh_me_ssh_key" "key_1" {
59+
key_name = "%s"
60+
key = "ssh-ed25519 AAAAC3NzaC1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
61+
}
62+
63+
resource "ovh_me_ssh_key" "key_2" {
64+
key_name = "%s"
65+
key = "ssh-ed25519 AAAAC3NzaC1yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
66+
}
67+
68+
data "ovh_me_ssh_keys" "keys" {}
69+
70+
output "keys_present" {
71+
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))
72+
}
73+
`

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)