Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 1 | // +build acceptance compute keypairs |
Jon Perritt | 5fdb722 | 2014-11-17 14:41:54 -0700 | [diff] [blame] | 2 | |
| 3 | package v2 |
| 4 | |
| 5 | import ( |
Jon Perritt | 5fdb722 | 2014-11-17 14:41:54 -0700 | [diff] [blame] | 6 | "testing" |
| 7 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame^] | 8 | "github.com/gophercloud/gophercloud/acceptance/clients" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 9 | "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs" |
| 10 | "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" |
Jon Perritt | 5fdb722 | 2014-11-17 14:41:54 -0700 | [diff] [blame] | 11 | ) |
| 12 | |
Jon Perritt | d1e861a | 2014-11-18 08:10:13 -0700 | [diff] [blame] | 13 | const keyName = "gophercloud_test_key_pair" |
| 14 | |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 15 | func TestKeypairsList(t *testing.T) { |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame^] | 16 | client, err := clients.NewComputeV2Client() |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 17 | if err != nil { |
| 18 | t.Fatalf("Unable to create a compute client: %v", err) |
| 19 | } |
Jon Perritt | 5fdb722 | 2014-11-17 14:41:54 -0700 | [diff] [blame] | 20 | |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 21 | allPages, err := keypairs.List(client).AllPages() |
| 22 | if err != nil { |
| 23 | t.Fatalf("Unable to retrieve keypairs: %s", err) |
| 24 | } |
| 25 | |
| 26 | allKeys, err := keypairs.ExtractKeyPairs(allPages) |
| 27 | if err != nil { |
| 28 | t.Fatalf("Unable to extract keypairs results: %s", err) |
| 29 | } |
| 30 | |
| 31 | for _, keypair := range allKeys { |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame^] | 32 | PrintKeyPair(t, &keypair) |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | |
| 36 | func TestKeypairsCreate(t *testing.T) { |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame^] | 37 | client, err := clients.NewComputeV2Client() |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 38 | if err != nil { |
| 39 | t.Fatalf("Unable to create a compute client: %v", err) |
| 40 | } |
| 41 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame^] | 42 | keyPair, err := CreateKeyPair(t, client) |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 43 | if err != nil { |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame^] | 44 | t.Fatalf("Unable to create key pair: %v", err) |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 45 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame^] | 46 | defer DeleteKeyPair(t, client, keyPair) |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 47 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame^] | 48 | PrintKeyPair(t, keyPair) |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | func TestKeypairsImportPublicKey(t *testing.T) { |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame^] | 52 | client, err := clients.NewComputeV2Client() |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 53 | if err != nil { |
| 54 | t.Fatalf("Unable to create a compute client: %v", err) |
| 55 | } |
| 56 | |
| 57 | publicKey, err := createKey() |
| 58 | if err != nil { |
| 59 | t.Fatalf("Unable to create public key: %s", err) |
| 60 | } |
| 61 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame^] | 62 | keyPair, err := ImportPublicKey(t, client, publicKey) |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 63 | if err != nil { |
| 64 | t.Fatalf("Unable to create keypair: %s", err) |
| 65 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame^] | 66 | defer DeleteKeyPair(t, client, keyPair) |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 67 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame^] | 68 | PrintKeyPair(t, keyPair) |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | func TestKeypairsServerCreateWithKey(t *testing.T) { |
Jon Perritt | 5fdb722 | 2014-11-17 14:41:54 -0700 | [diff] [blame] | 72 | if testing.Short() { |
| 73 | t.Skip("Skipping test that requires server creation in short mode.") |
| 74 | } |
| 75 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame^] | 76 | client, err := clients.NewComputeV2Client() |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 77 | if err != nil { |
| 78 | t.Fatalf("Unable to create a compute client: %v", err) |
| 79 | } |
Jon Perritt | 5fdb722 | 2014-11-17 14:41:54 -0700 | [diff] [blame] | 80 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame^] | 81 | choices, err := clients.AcceptanceTestChoicesFromEnv() |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 82 | if err != nil { |
| 83 | t.Fatal(err) |
| 84 | } |
Jon Perritt | 5fdb722 | 2014-11-17 14:41:54 -0700 | [diff] [blame] | 85 | |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 86 | publicKey, err := createKey() |
| 87 | if err != nil { |
| 88 | t.Fatalf("Unable to create public key: %s", err) |
| 89 | } |
| 90 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame^] | 91 | keyPair, err := ImportPublicKey(t, client, publicKey) |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 92 | if err != nil { |
| 93 | t.Fatalf("Unable to create keypair: %s", err) |
| 94 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame^] | 95 | defer DeleteKeyPair(t, client, keyPair) |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 96 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame^] | 97 | server, err := CreateServerWithPublicKey(t, client, choices, keyPair.Name) |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 98 | if err != nil { |
| 99 | t.Fatalf("Unable to create server: %s", err) |
| 100 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame^] | 101 | defer DeleteServer(t, client, server) |
Jon Perritt | d1e861a | 2014-11-18 08:10:13 -0700 | [diff] [blame] | 102 | |
| 103 | server, err = servers.Get(client, server.ID).Extract() |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 104 | if err != nil { |
| 105 | t.Fatalf("Unable to retrieve server: %s", err) |
| 106 | } |
Jon Perritt | 5fdb722 | 2014-11-17 14:41:54 -0700 | [diff] [blame] | 107 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame^] | 108 | if server.KeyName != keyPair.Name { |
| 109 | t.Fatalf("key name of server %s is %s, not %s", server.ID, server.KeyName, keyPair.Name) |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 110 | } |
| 111 | } |