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 ( |
| 6 | "crypto/rand" |
| 7 | "crypto/rsa" |
| 8 | "testing" |
| 9 | |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 10 | "github.com/gophercloud/gophercloud" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 11 | "github.com/gophercloud/gophercloud/acceptance/tools" |
| 12 | "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs" |
| 13 | "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" |
Jon Perritt | 5fdb722 | 2014-11-17 14:41:54 -0700 | [diff] [blame] | 14 | |
JackSpirou | 0e58712 | 2015-04-11 10:46:23 -0500 | [diff] [blame] | 15 | "golang.org/x/crypto/ssh" |
Jon Perritt | 5fdb722 | 2014-11-17 14:41:54 -0700 | [diff] [blame] | 16 | ) |
| 17 | |
Jon Perritt | d1e861a | 2014-11-18 08:10:13 -0700 | [diff] [blame] | 18 | const keyName = "gophercloud_test_key_pair" |
| 19 | |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 20 | func TestKeypairsList(t *testing.T) { |
Jon Perritt | 5fdb722 | 2014-11-17 14:41:54 -0700 | [diff] [blame] | 21 | client, err := newClient() |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 22 | if err != nil { |
| 23 | t.Fatalf("Unable to create a compute client: %v", err) |
| 24 | } |
Jon Perritt | 5fdb722 | 2014-11-17 14:41:54 -0700 | [diff] [blame] | 25 | |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 26 | allPages, err := keypairs.List(client).AllPages() |
| 27 | if err != nil { |
| 28 | t.Fatalf("Unable to retrieve keypairs: %s", err) |
| 29 | } |
| 30 | |
| 31 | allKeys, err := keypairs.ExtractKeyPairs(allPages) |
| 32 | if err != nil { |
| 33 | t.Fatalf("Unable to extract keypairs results: %s", err) |
| 34 | } |
| 35 | |
| 36 | for _, keypair := range allKeys { |
| 37 | printKeyPair(t, &keypair) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestKeypairsCreate(t *testing.T) { |
| 42 | client, err := newClient() |
| 43 | if err != nil { |
| 44 | t.Fatalf("Unable to create a compute client: %v", err) |
| 45 | } |
| 46 | |
| 47 | createOpts := keypairs.CreateOpts{ |
| 48 | Name: keyName, |
| 49 | } |
| 50 | keyPair, err := keypairs.Create(client, createOpts).Extract() |
| 51 | if err != nil { |
| 52 | t.Fatalf("Unable to create keypair: %s", err) |
| 53 | } |
| 54 | defer deleteKeyPair(t, client, keyPair) |
| 55 | |
| 56 | printKeyPair(t, keyPair) |
| 57 | } |
| 58 | |
| 59 | func TestKeypairsImportPublicKey(t *testing.T) { |
| 60 | client, err := newClient() |
| 61 | if err != nil { |
| 62 | t.Fatalf("Unable to create a compute client: %v", err) |
| 63 | } |
| 64 | |
| 65 | publicKey, err := createKey() |
| 66 | if err != nil { |
| 67 | t.Fatalf("Unable to create public key: %s", err) |
| 68 | } |
| 69 | |
| 70 | createOpts := keypairs.CreateOpts{ |
| 71 | Name: keyName, |
| 72 | PublicKey: publicKey, |
| 73 | } |
| 74 | keyPair, err := keypairs.Create(client, createOpts).Extract() |
| 75 | if err != nil { |
| 76 | t.Fatalf("Unable to create keypair: %s", err) |
| 77 | } |
| 78 | defer deleteKeyPair(t, client, keyPair) |
| 79 | |
| 80 | printKeyPair(t, keyPair) |
| 81 | } |
| 82 | |
| 83 | func TestKeypairsServerCreateWithKey(t *testing.T) { |
Jon Perritt | 5fdb722 | 2014-11-17 14:41:54 -0700 | [diff] [blame] | 84 | if testing.Short() { |
| 85 | t.Skip("Skipping test that requires server creation in short mode.") |
| 86 | } |
| 87 | |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 88 | client, err := newClient() |
| 89 | if err != nil { |
| 90 | t.Fatalf("Unable to create a compute client: %v", err) |
| 91 | } |
Jon Perritt | 5fdb722 | 2014-11-17 14:41:54 -0700 | [diff] [blame] | 92 | |
| 93 | choices, err := ComputeChoicesFromEnv() |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 94 | if err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
Jon Perritt | 5fdb722 | 2014-11-17 14:41:54 -0700 | [diff] [blame] | 97 | |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 98 | publicKey, err := createKey() |
| 99 | if err != nil { |
| 100 | t.Fatalf("Unable to create public key: %s", err) |
| 101 | } |
| 102 | |
| 103 | createOpts := keypairs.CreateOpts{ |
| 104 | Name: keyName, |
| 105 | PublicKey: publicKey, |
| 106 | } |
| 107 | |
| 108 | keyPair, err := keypairs.Create(client, createOpts).Extract() |
| 109 | if err != nil { |
| 110 | t.Fatalf("Unable to create keypair: %s", err) |
| 111 | } |
| 112 | defer deleteKeyPair(t, client, keyPair) |
| 113 | |
| 114 | networkID, err := getNetworkIDFromTenantNetworks(t, client, choices.NetworkName) |
| 115 | if err != nil { |
| 116 | t.Fatalf("Failed to obtain network ID: %v", err) |
| 117 | } |
| 118 | |
| 119 | name := tools.RandomString("ACPTTEST", 16) |
| 120 | t.Logf("Attempting to create server: %s", name) |
Jon Perritt | 5fdb722 | 2014-11-17 14:41:54 -0700 | [diff] [blame] | 121 | |
| 122 | serverCreateOpts := servers.CreateOpts{ |
| 123 | Name: name, |
| 124 | FlavorRef: choices.FlavorID, |
| 125 | ImageRef: choices.ImageID, |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 126 | Networks: []servers.Network{ |
| 127 | servers.Network{UUID: networkID}, |
| 128 | }, |
Jon Perritt | 5fdb722 | 2014-11-17 14:41:54 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | server, err := servers.Create(client, keypairs.CreateOptsExt{ |
| 132 | serverCreateOpts, |
Jon Perritt | d1e861a | 2014-11-18 08:10:13 -0700 | [diff] [blame] | 133 | keyName, |
Jon Perritt | 5fdb722 | 2014-11-17 14:41:54 -0700 | [diff] [blame] | 134 | }).Extract() |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 135 | if err != nil { |
| 136 | t.Fatalf("Unable to create server: %s", err) |
| 137 | } |
| 138 | |
Jon Perritt | d1e861a | 2014-11-18 08:10:13 -0700 | [diff] [blame] | 139 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
| 140 | t.Fatalf("Unable to wait for server: %v", err) |
| 141 | } |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 142 | defer deleteServer(t, client, server) |
Jon Perritt | d1e861a | 2014-11-18 08:10:13 -0700 | [diff] [blame] | 143 | |
| 144 | server, err = servers.Get(client, server.ID).Extract() |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 145 | if err != nil { |
| 146 | t.Fatalf("Unable to retrieve server: %s", err) |
| 147 | } |
Jon Perritt | 5fdb722 | 2014-11-17 14:41:54 -0700 | [diff] [blame] | 148 | |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 149 | if server.KeyName != keyName { |
| 150 | t.Fatalf("key name of server %s is %s, not %s", server.ID, server.KeyName, keyName) |
| 151 | } |
| 152 | } |
Jon Perritt | 5fdb722 | 2014-11-17 14:41:54 -0700 | [diff] [blame] | 153 | |
Joe Topjian | c382078 | 2016-07-25 04:15:42 +0000 | [diff] [blame] | 154 | func createKey() (string, error) { |
| 155 | privateKey, err := rsa.GenerateKey(rand.Reader, 2048) |
| 156 | if err != nil { |
| 157 | return "", err |
| 158 | } |
| 159 | |
| 160 | publicKey := privateKey.PublicKey |
| 161 | pub, err := ssh.NewPublicKey(&publicKey) |
| 162 | if err != nil { |
| 163 | return "", err |
| 164 | } |
| 165 | |
| 166 | pubBytes := ssh.MarshalAuthorizedKey(pub) |
| 167 | pk := string(pubBytes) |
| 168 | return pk, nil |
| 169 | } |
| 170 | |
| 171 | func deleteKeyPair(t *testing.T, client *gophercloud.ServiceClient, keyPair *keypairs.KeyPair) { |
| 172 | err := keypairs.Delete(client, keyPair.Name).ExtractErr() |
| 173 | if err != nil { |
| 174 | t.Fatalf("Unable to delete keypair %s: %v", keyPair.Name, err) |
| 175 | } |
| 176 | |
| 177 | t.Logf("Deleted keypair: %s", keyPair.Name) |
| 178 | } |
| 179 | |
| 180 | func printKeyPair(t *testing.T, keypair *keypairs.KeyPair) { |
| 181 | t.Logf("Name: %s", keypair.Name) |
| 182 | t.Logf("Fingerprint: %s", keypair.Fingerprint) |
| 183 | t.Logf("Public Key: %s", keypair.PublicKey) |
| 184 | t.Logf("Private Key: %s", keypair.PrivateKey) |
| 185 | t.Logf("UserID: %s", keypair.UserID) |
Jon Perritt | 5fdb722 | 2014-11-17 14:41:54 -0700 | [diff] [blame] | 186 | } |