Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SSH Key support #93

Merged
merged 1 commit into from
Nov 18, 2019
Merged
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
159 changes: 159 additions & 0 deletions go.sum

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions ovh/data_source_ovh_me_ssh_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ovh

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceMeSshKey() *schema.Resource {
return &schema.Resource{
Read: dataSourceMeSshKeyRead,
Schema: map[string]*schema.Schema{
"key_name": {
Type: schema.TypeString,
Required: true,
Description: "Name of this public Ssh key",
},
"key": {
Type: schema.TypeString,
Computed: true,
Description: "ASCII encoded public Ssh key",
},
"default": {
Type: schema.TypeBool,
Computed: true,
Description: "True when this public Ssh key is used for rescue mode and reinstallations",
},
},
}
}

func dataSourceMeSshKeyRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

sshKey := &MeSshKeyResponse{}

keyName := d.Get("key_name").(string)
err := config.OVHClient.Get(
fmt.Sprintf("/me/sshKey/%s", keyName),
sshKey,
)
if err != nil {
return fmt.Errorf("Unable to find SSH key named %s:\n\t %q", keyName, err)
}

d.SetId(sshKey.KeyName)
d.Set("key_name", sshKey.KeyName)
d.Set("key", sshKey.Key)
d.Set("default", sshKey.Default)

return nil
}
42 changes: 42 additions & 0 deletions ovh/data_source_ovh_me_ssh_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ovh

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccMeSshKeyDataSource_basic(t *testing.T) {
sshKeyName := acctest.RandomWithPrefix(test_prefix)
sshKey := "ssh-ed25519 AAAAC3NzaC1yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
config := fmt.Sprintf(testAccMeSshKeyDatasourceConfig, sshKeyName, sshKey)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckCredentials(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"data.ovh_me_ssh_key.key_1", "key_name", sshKeyName),
resource.TestCheckResourceAttr(
"data.ovh_me_ssh_key.key_1", "key", sshKey),
),
},
},
})
}

const testAccMeSshKeyDatasourceConfig = `
resource "ovh_me_ssh_key" "key_1" {
key_name = "%s"
key = "%s"
}

data "ovh_me_ssh_key" "key_1" {
key_name = ovh_me_ssh_key.key_1.key_name
}
`
42 changes: 42 additions & 0 deletions ovh/data_source_ovh_me_ssh_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ovh

import (
"fmt"
"log"
"sort"

"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceMeSshKeys() *schema.Resource {
return &schema.Resource{
Read: dataSourceMeSshKeysRead,
Schema: map[string]*schema.Schema{
"names": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
}
}

func dataSourceMeSshKeysRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

names := make([]string, 0)
err := config.OVHClient.Get("/me/sshKey", &names)

if err != nil {
return fmt.Errorf("Error calling /me/sshKey:\n\t %q", err)
}

sort.Strings(names)
d.SetId(hashcode.Strings(names))
d.Set("names", names)

log.Printf("[DEBUG] Read SSH Keys names %s", names)
return nil
}
87 changes: 87 additions & 0 deletions ovh/data_source_ovh_me_ssh_keys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package ovh

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccMeSshKeysDataSource_basic(t *testing.T) {
sshKey1Name := acctest.RandomWithPrefix(test_prefix)
sshKey1 := "ssh-ed25519 AAAAC3NzaC1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
sshKey2Name := acctest.RandomWithPrefix(test_prefix)
sshKey2 := "ssh-ed25519 AAAAC3NzaC1yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"

preSetup := fmt.Sprintf(
testAccMeSshKeysDatasourceConfig_preSetup,
sshKey1Name,
sshKey1,
sshKey2Name,
sshKey2,
)
config := fmt.Sprintf(
testAccMeSshKeysDatasourceConfig_keys,
sshKey1Name,
sshKey1,
sshKey2Name,
sshKey2,
)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckCredentials(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: preSetup,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"ovh_me_ssh_key.key_1", "key_name", sshKey1Name),
resource.TestCheckResourceAttr(
"ovh_me_ssh_key.key_1", "key", sshKey1),
resource.TestCheckResourceAttr(
"ovh_me_ssh_key.key_2", "key_name", sshKey2Name),
resource.TestCheckResourceAttr(
"ovh_me_ssh_key.key_2", "key", sshKey2),
),
}, {
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckOutput(
"keys_present", "true"),
),
},
},
})
}

const testAccMeSshKeysDatasourceConfig_preSetup = `
resource "ovh_me_ssh_key" "key_1" {
key_name = "%s"
key = "%s"
}

resource "ovh_me_ssh_key" "key_2" {
key_name = "%s"
key = "%s"
}
`

const testAccMeSshKeysDatasourceConfig_keys = `
resource "ovh_me_ssh_key" "key_1" {
key_name = "%s"
key = "%s"
}

resource "ovh_me_ssh_key" "key_2" {
key_name = "%s"
key = "%s"
}

data "ovh_me_ssh_keys" "keys" {}

output "keys_present" {
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))
}
`
36 changes: 36 additions & 0 deletions ovh/import_me_ssh_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ovh

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccMeSshKey_importBasic(t *testing.T) {
resourceName := "ovh_me_ssh_key.key_1"
sshKey := acctest.RandomWithPrefix(test_prefix)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckCredentials(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testAccMeSshKeyConfig_import, sshKey),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

const testAccMeSshKeyConfig_import = `
resource "ovh_me_ssh_key" "key_1" {
key_name = "%s"
key = "ssh-ed25519 AAAAC3NzaC1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
`
3 changes: 3 additions & 0 deletions ovh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func Provider() terraform.ResourceProvider {
"ovh_iploadbalancing": dataSourceIpLoadbalancing(),
"ovh_me_paymentmean_bankaccount": dataSourceMePaymentmeanBankaccount(),
"ovh_me_paymentmean_creditcard": dataSourceMePaymentmeanCreditcard(),
"ovh_me_ssh_key": dataSourceMeSshKey(),
"ovh_me_ssh_keys": dataSourceMeSshKeys(),

// Legacy naming schema (publiccloud)
"ovh_publiccloud_region": deprecated(dataSourcePublicCloudRegion(),
Expand All @@ -71,6 +73,7 @@ func Provider() terraform.ResourceProvider {
"ovh_cloud_network_private": resourcePublicCloudPrivateNetwork(),
"ovh_cloud_network_private_subnet": resourcePublicCloudPrivateNetworkSubnet(),
"ovh_cloud_user": resourcePublicCloudUser(),
"ovh_me_ssh_key": resourceMeSshKey(),
"ovh_vrack_cloudproject": resourceVRackPublicCloudAttachment(),

// Legacy naming schema (publiccloud)
Expand Down
Loading