diff --git a/civo/datasource_ssh_test.go b/civo/datasource_ssh_test.go new file mode 100644 index 00000000..60b27936 --- /dev/null +++ b/civo/datasource_ssh_test.go @@ -0,0 +1,63 @@ +package civo + +import ( + "crypto/rand" + "crypto/rsa" + "fmt" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "golang.org/x/crypto/ssh" +) + +func TestAccDataSourceCivoSSHKey_basic(t *testing.T) { + datasourceName := "data.civo_ssh_key.foobar" + name := acctest.RandomWithPrefix("sshkey-test") + pubKey, err := testAccGenerateDataSourceCivoSSHKeyPublic() + if err != nil { + t.Fatalf("Unable to generate public key: %v", err) + return + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceCivoSSHKeyConfig(name, pubKey), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr(datasourceName, "name", name), + ), + }, + }, + }) +} + +func testAccGenerateDataSourceCivoSSHKeyPublic() (string, error) { + privateKey, err := rsa.GenerateKey(rand.Reader, 1024) + if err != nil { + return "", fmt.Errorf("Unable to generate key: %v", err) + } + + publicKey, err := ssh.NewPublicKey(&privateKey.PublicKey) + if err != nil { + return "", fmt.Errorf("Unable to generate key: %v", err) + } + + return strings.TrimSpace(string(ssh.MarshalAuthorizedKey(publicKey))), nil +} + +func testAccDataSourceCivoSSHKeyConfig(name string, key string) string { + return fmt.Sprintf(` +resource "civo_ssh_key" "foobar" { + name = "%s" + public_key = "%s" +} + +data "civo_ssh_key" "foobar" { + name = civo_ssh_key.foobar.name +} +`, name, key) +} diff --git a/go.mod b/go.mod index 6774efce..67107368 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,7 @@ require ( github.com/ulikunitz/xz v0.5.7 // indirect github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect github.com/zclconf/go-cty v1.3.1 // indirect - golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4 // indirect + golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4 golang.org/x/tools v0.0.0-20200623045635-ff88973b1e4e // indirect google.golang.org/genproto v0.0.0-20200311144346-b662892dd51b // indirect google.golang.org/grpc v1.28.0 // indirect