blob: ac5a1f2e9d5e12c9415c20c2c73031fee77423b6 [file] [log] [blame]
Ash Wilson0219eba2014-10-22 17:39:47 -04001// +build acceptance rackspace
2
3package v2
4
5import (
6 "testing"
7
8 "github.com/rackspace/gophercloud"
9 "github.com/rackspace/gophercloud/acceptance/tools"
10 os "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/keypairs"
11 "github.com/rackspace/gophercloud/pagination"
12 "github.com/rackspace/gophercloud/rackspace/compute/v2/keypairs"
13 th "github.com/rackspace/gophercloud/testhelper"
14)
15
16func deleteKeyPair(t *testing.T, client *gophercloud.ServiceClient, name string) {
17 err := keypairs.Delete(client, name).Extract()
18 th.AssertNoErr(t, err)
19 t.Logf("Successfully deleted key [%s].", name)
20}
21
22func TestCreateKeyPair(t *testing.T) {
23 client, err := newClient()
24 th.AssertNoErr(t, err)
25
26 name := tools.RandomString("createdkey-", 8)
27 k, err := keypairs.Create(client, os.CreateOpts{Name: name}).Extract()
28 th.AssertNoErr(t, err)
29 defer deleteKeyPair(t, client, name)
30
31 t.Logf("Created a new keypair:")
32 t.Logf(" name=[%s]", k.Name)
33 t.Logf(" fingerprint=[%s]", k.Fingerprint)
34 t.Logf(" publickey=[%s]", tools.Elide(k.PublicKey))
35 t.Logf(" privatekey=[%s]", tools.Elide(k.PrivateKey))
36 t.Logf(" userid=[%s]", k.UserID)
37}
38
39func TestImportKeyPair(t *testing.T) {
40 client, err := newClient()
41 th.AssertNoErr(t, err)
42
43 name := tools.RandomString("importedkey-", 8)
44 pubkey := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDlIQ3r+zd97kb9Hzmujd3V6pbO53eb3Go4q2E8iqVGWQfZTrFdL9KACJnqJIm9HmncfRkUTxE37hqeGCCv8uD+ZPmPiZG2E60OX1mGDjbbzAyReRwYWXgXHopggZTLak5k4mwZYaxwaufbVBDRn847e01lZnaXaszEToLM37NLw+uz29sl3TwYy2R0RGHPwPc160aWmdLjSyd1Nd4c9pvvOP/EoEuBjIC6NJJwg2Rvg9sjjx9jYj0QUgc8CqKLN25oMZ69kNJzlFylKRUoeeVr89txlR59yehJWk6Uw6lYFTdJmcmQOFVAJ12RMmS1hLWCM8UzAgtw+EDa0eqBxBDl smash@winter"
45
46 k, err := keypairs.Create(client, os.CreateOpts{
47 Name: name,
48 PublicKey: pubkey,
49 }).Extract()
50 th.AssertNoErr(t, err)
51 defer deleteKeyPair(t, client, name)
52
53 th.CheckEquals(t, pubkey, k.PublicKey)
54 th.CheckEquals(t, "", k.PrivateKey)
55
56 t.Logf("Imported an existing keypair:")
57 t.Logf(" name=[%s]", k.Name)
58 t.Logf(" fingerprint=[%s]", k.Fingerprint)
59 t.Logf(" publickey=[%s]", tools.Elide(k.PublicKey))
60 t.Logf(" privatekey=[%s]", tools.Elide(k.PrivateKey))
61 t.Logf(" userid=[%s]", k.UserID)
62}
63
64func TestListKeyPairs(t *testing.T) {
65 client, err := newClient()
66 th.AssertNoErr(t, err)
67
68 count := 0
69 err = keypairs.List(client).EachPage(func(page pagination.Page) (bool, error) {
70 count++
71 t.Logf("--- %02d ---", count)
72
73 ks, err := keypairs.ExtractKeyPairs(page)
74 th.AssertNoErr(t, err)
75
76 for i, keypair := range ks {
77 t.Logf("[%02d] name=[%s]", i, keypair.Name)
78 t.Logf(" fingerprint=[%s]", keypair.Fingerprint)
79 t.Logf(" publickey=[%s]", tools.Elide(keypair.PublicKey))
80 t.Logf(" privatekey=[%s]", tools.Elide(keypair.PrivateKey))
81 t.Logf(" userid=[%s]", keypair.UserID)
82 }
83
84 return true, nil
85 })
86 th.AssertNoErr(t, err)
87}