Merge pull request #255 from smashwilson/rackspace-keypairs
Keypair resource
diff --git a/acceptance/rackspace/compute/v2/keypairs_test.go b/acceptance/rackspace/compute/v2/keypairs_test.go
new file mode 100644
index 0000000..ac5a1f2
--- /dev/null
+++ b/acceptance/rackspace/compute/v2/keypairs_test.go
@@ -0,0 +1,87 @@
+// +build acceptance rackspace
+
+package v2
+
+import (
+ "testing"
+
+ "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/acceptance/tools"
+ os "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/keypairs"
+ "github.com/rackspace/gophercloud/pagination"
+ "github.com/rackspace/gophercloud/rackspace/compute/v2/keypairs"
+ th "github.com/rackspace/gophercloud/testhelper"
+)
+
+func deleteKeyPair(t *testing.T, client *gophercloud.ServiceClient, name string) {
+ err := keypairs.Delete(client, name).Extract()
+ th.AssertNoErr(t, err)
+ t.Logf("Successfully deleted key [%s].", name)
+}
+
+func TestCreateKeyPair(t *testing.T) {
+ client, err := newClient()
+ th.AssertNoErr(t, err)
+
+ name := tools.RandomString("createdkey-", 8)
+ k, err := keypairs.Create(client, os.CreateOpts{Name: name}).Extract()
+ th.AssertNoErr(t, err)
+ defer deleteKeyPair(t, client, name)
+
+ t.Logf("Created a new keypair:")
+ t.Logf(" name=[%s]", k.Name)
+ t.Logf(" fingerprint=[%s]", k.Fingerprint)
+ t.Logf(" publickey=[%s]", tools.Elide(k.PublicKey))
+ t.Logf(" privatekey=[%s]", tools.Elide(k.PrivateKey))
+ t.Logf(" userid=[%s]", k.UserID)
+}
+
+func TestImportKeyPair(t *testing.T) {
+ client, err := newClient()
+ th.AssertNoErr(t, err)
+
+ name := tools.RandomString("importedkey-", 8)
+ pubkey := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDlIQ3r+zd97kb9Hzmujd3V6pbO53eb3Go4q2E8iqVGWQfZTrFdL9KACJnqJIm9HmncfRkUTxE37hqeGCCv8uD+ZPmPiZG2E60OX1mGDjbbzAyReRwYWXgXHopggZTLak5k4mwZYaxwaufbVBDRn847e01lZnaXaszEToLM37NLw+uz29sl3TwYy2R0RGHPwPc160aWmdLjSyd1Nd4c9pvvOP/EoEuBjIC6NJJwg2Rvg9sjjx9jYj0QUgc8CqKLN25oMZ69kNJzlFylKRUoeeVr89txlR59yehJWk6Uw6lYFTdJmcmQOFVAJ12RMmS1hLWCM8UzAgtw+EDa0eqBxBDl smash@winter"
+
+ k, err := keypairs.Create(client, os.CreateOpts{
+ Name: name,
+ PublicKey: pubkey,
+ }).Extract()
+ th.AssertNoErr(t, err)
+ defer deleteKeyPair(t, client, name)
+
+ th.CheckEquals(t, pubkey, k.PublicKey)
+ th.CheckEquals(t, "", k.PrivateKey)
+
+ t.Logf("Imported an existing keypair:")
+ t.Logf(" name=[%s]", k.Name)
+ t.Logf(" fingerprint=[%s]", k.Fingerprint)
+ t.Logf(" publickey=[%s]", tools.Elide(k.PublicKey))
+ t.Logf(" privatekey=[%s]", tools.Elide(k.PrivateKey))
+ t.Logf(" userid=[%s]", k.UserID)
+}
+
+func TestListKeyPairs(t *testing.T) {
+ client, err := newClient()
+ th.AssertNoErr(t, err)
+
+ count := 0
+ err = keypairs.List(client).EachPage(func(page pagination.Page) (bool, error) {
+ count++
+ t.Logf("--- %02d ---", count)
+
+ ks, err := keypairs.ExtractKeyPairs(page)
+ th.AssertNoErr(t, err)
+
+ for i, keypair := range ks {
+ t.Logf("[%02d] name=[%s]", i, keypair.Name)
+ t.Logf(" fingerprint=[%s]", keypair.Fingerprint)
+ t.Logf(" publickey=[%s]", tools.Elide(keypair.PublicKey))
+ t.Logf(" privatekey=[%s]", tools.Elide(keypair.PrivateKey))
+ t.Logf(" userid=[%s]", keypair.UserID)
+ }
+
+ return true, nil
+ })
+ th.AssertNoErr(t, err)
+}
diff --git a/acceptance/tools/tools.go b/acceptance/tools/tools.go
index b3f3ea7..61b1d7a 100644
--- a/acceptance/tools/tools.go
+++ b/acceptance/tools/tools.go
@@ -71,3 +71,12 @@
}
return prefix + string(bytes)
}
+
+// Elide returns the first bit of its input string with a suffix of "..." if it's longer than
+// a comfortable 40 characters.
+func Elide(value string) string {
+ if len(value) > 40 {
+ return value[0:37] + "..."
+ }
+ return value
+}
diff --git a/openstack/compute/v2/extensions/keypairs/fixtures.go b/openstack/compute/v2/extensions/keypairs/fixtures.go
index b025d45..d10af99 100644
--- a/openstack/compute/v2/extensions/keypairs/fixtures.go
+++ b/openstack/compute/v2/extensions/keypairs/fixtures.go
@@ -1,4 +1,5 @@
// +build fixtures
+
package keypairs
import (
diff --git a/rackspace/compute/v2/keypairs/delegate.go b/rackspace/compute/v2/keypairs/delegate.go
new file mode 100644
index 0000000..3e53525
--- /dev/null
+++ b/rackspace/compute/v2/keypairs/delegate.go
@@ -0,0 +1,33 @@
+package keypairs
+
+import (
+ "github.com/rackspace/gophercloud"
+ os "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/keypairs"
+ "github.com/rackspace/gophercloud/pagination"
+)
+
+// List returns a Pager that allows you to iterate over a collection of KeyPairs.
+func List(client *gophercloud.ServiceClient) pagination.Pager {
+ return os.List(client)
+}
+
+// Create requests the creation of a new keypair on the server, or to import a pre-existing
+// keypair.
+func Create(client *gophercloud.ServiceClient, opts os.CreateOptsBuilder) os.CreateResult {
+ return os.Create(client, opts)
+}
+
+// Get returns public data about a previously uploaded KeyPair.
+func Get(client *gophercloud.ServiceClient, name string) os.GetResult {
+ return os.Get(client, name)
+}
+
+// Delete requests the deletion of a previous stored KeyPair from the server.
+func Delete(client *gophercloud.ServiceClient, name string) os.DeleteResult {
+ return os.Delete(client, name)
+}
+
+// ExtractKeyPairs interprets a page of results as a slice of KeyPairs.
+func ExtractKeyPairs(page pagination.Page) ([]os.KeyPair, error) {
+ return os.ExtractKeyPairs(page)
+}
diff --git a/rackspace/compute/v2/keypairs/delegate_test.go b/rackspace/compute/v2/keypairs/delegate_test.go
new file mode 100644
index 0000000..b72e69e
--- /dev/null
+++ b/rackspace/compute/v2/keypairs/delegate_test.go
@@ -0,0 +1,72 @@
+package keypairs
+
+import (
+ "testing"
+
+ os "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/keypairs"
+ "github.com/rackspace/gophercloud/pagination"
+ th "github.com/rackspace/gophercloud/testhelper"
+ "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+func TestList(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ os.HandleListSuccessfully(t)
+
+ count := 0
+ err := List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
+ count++
+ actual, err := ExtractKeyPairs(page)
+ th.AssertNoErr(t, err)
+ th.CheckDeepEquals(t, os.ExpectedKeyPairSlice, actual)
+
+ return true, nil
+ })
+ th.AssertNoErr(t, err)
+ th.CheckEquals(t, 1, count)
+}
+
+func TestCreate(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ os.HandleCreateSuccessfully(t)
+
+ actual, err := Create(client.ServiceClient(), os.CreateOpts{
+ Name: "createdkey",
+ }).Extract()
+ th.AssertNoErr(t, err)
+ th.CheckDeepEquals(t, &os.CreatedKeyPair, actual)
+}
+
+func TestImport(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ os.HandleImportSuccessfully(t)
+
+ actual, err := Create(client.ServiceClient(), os.CreateOpts{
+ Name: "importedkey",
+ PublicKey: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDx8nkQv/zgGgB4rMYmIf+6A4l6Rr+o/6lHBQdW5aYd44bd8JttDCE/F/pNRr0lRE+PiqSPO8nDPHw0010JeMH9gYgnnFlyY3/OcJ02RhIPyyxYpv9FhY+2YiUkpwFOcLImyrxEsYXpD/0d3ac30bNH6Sw9JD9UZHYcpSxsIbECHw== Generated by Nova",
+ }).Extract()
+ th.AssertNoErr(t, err)
+ th.CheckDeepEquals(t, &os.ImportedKeyPair, actual)
+}
+
+func TestGet(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ os.HandleGetSuccessfully(t)
+
+ actual, err := Get(client.ServiceClient(), "firstkey").Extract()
+ th.AssertNoErr(t, err)
+ th.CheckDeepEquals(t, &os.FirstKeyPair, actual)
+}
+
+func TestDelete(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ os.HandleDeleteSuccessfully(t)
+
+ err := Delete(client.ServiceClient(), "deletedkey").Extract()
+ th.AssertNoErr(t, err)
+}