| Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame^] | 1 | // Package v2 contains common functions for creating compute-based resources | 
 | 2 | // for use in acceptance tests. See the `*_test.go` files for example usages. | 
 | 3 | package v2 | 
 | 4 |  | 
 | 5 | import ( | 
 | 6 | 	"crypto/rand" | 
 | 7 | 	"crypto/rsa" | 
 | 8 | 	"fmt" | 
 | 9 | 	"testing" | 
 | 10 |  | 
 | 11 | 	"github.com/gophercloud/gophercloud" | 
 | 12 | 	"github.com/gophercloud/gophercloud/acceptance/clients" | 
 | 13 | 	"github.com/gophercloud/gophercloud/acceptance/tools" | 
 | 14 | 	"github.com/gophercloud/gophercloud/openstack/blockstorage/v1/volumes" | 
 | 15 | 	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/bootfromvolume" | 
 | 16 | 	dsr "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/defsecrules" | 
 | 17 | 	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips" | 
 | 18 | 	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs" | 
 | 19 | 	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/networks" | 
 | 20 | 	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/quotasets" | 
 | 21 | 	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/schedulerhints" | 
 | 22 | 	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/secgroups" | 
 | 23 | 	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/servergroups" | 
 | 24 | 	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/tenantnetworks" | 
 | 25 | 	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/volumeattach" | 
 | 26 | 	"github.com/gophercloud/gophercloud/openstack/compute/v2/flavors" | 
 | 27 | 	"github.com/gophercloud/gophercloud/openstack/compute/v2/images" | 
 | 28 | 	"github.com/gophercloud/gophercloud/openstack/compute/v2/servers" | 
 | 29 |  | 
 | 30 | 	"golang.org/x/crypto/ssh" | 
 | 31 | ) | 
 | 32 |  | 
 | 33 | // AssociateFloatingIP will associate a floating IP with an instance. An error | 
 | 34 | // will be returned if the floating IP was unable to be associated. | 
 | 35 | func AssociateFloatingIP(t *testing.T, client *gophercloud.ServiceClient, floatingIP *floatingips.FloatingIP, server *servers.Server) error { | 
 | 36 | 	associateOpts := floatingips.AssociateOpts{ | 
 | 37 | 		FloatingIP: floatingIP.IP, | 
 | 38 | 	} | 
 | 39 |  | 
 | 40 | 	t.Logf("Attempting to associate floating IP %s to instance %s", floatingIP.IP, server.ID) | 
 | 41 | 	err := floatingips.AssociateInstance(client, server.ID, associateOpts).ExtractErr() | 
 | 42 | 	if err != nil { | 
 | 43 | 		return err | 
 | 44 | 	} | 
 | 45 |  | 
 | 46 | 	return nil | 
 | 47 | } | 
 | 48 |  | 
 | 49 | // AssociateFloatingIPWithFixedIP will associate a floating IP with an | 
 | 50 | // instance's specific fixed IP. An error will be returend if the floating IP | 
 | 51 | // was unable to be associated. | 
 | 52 | func AssociateFloatingIPWithFixedIP(t *testing.T, client *gophercloud.ServiceClient, floatingIP *floatingips.FloatingIP, server *servers.Server, fixedIP string) error { | 
 | 53 | 	associateOpts := floatingips.AssociateOpts{ | 
 | 54 | 		FloatingIP: floatingIP.IP, | 
 | 55 | 		FixedIP:    fixedIP, | 
 | 56 | 	} | 
 | 57 |  | 
 | 58 | 	t.Logf("Attempting to associate floating IP %s to fixed IP %s on instance %s", floatingIP.IP, fixedIP, server.ID) | 
 | 59 | 	err := floatingips.AssociateInstance(client, server.ID, associateOpts).ExtractErr() | 
 | 60 | 	if err != nil { | 
 | 61 | 		return err | 
 | 62 | 	} | 
 | 63 |  | 
 | 64 | 	return nil | 
 | 65 | } | 
 | 66 |  | 
 | 67 | // CreateBootableVolumeServer works like CreateServer but is configured with | 
 | 68 | // one or more block devices defined by passing in []bootfromvolume.BlockDevice. | 
 | 69 | // An error will be returned if a server was unable to be created. | 
 | 70 | func CreateBootableVolumeServer(t *testing.T, client *gophercloud.ServiceClient, blockDevices []bootfromvolume.BlockDevice, choices *clients.AcceptanceTestChoices) (*servers.Server, error) { | 
 | 71 | 	if testing.Short() { | 
 | 72 | 		t.Skip("Skipping test that requires server creation in short mode.") | 
 | 73 | 	} | 
 | 74 |  | 
 | 75 | 	var server *servers.Server | 
 | 76 |  | 
 | 77 | 	networkID, err := GetNetworkIDFromTenantNetworks(t, client, choices.NetworkName) | 
 | 78 | 	if err != nil { | 
 | 79 | 		return server, err | 
 | 80 | 	} | 
 | 81 |  | 
 | 82 | 	name := tools.RandomString("ACPTTEST", 16) | 
 | 83 | 	t.Logf("Attempting to create bootable volume server: %s", name) | 
 | 84 |  | 
 | 85 | 	serverCreateOpts := servers.CreateOpts{ | 
 | 86 | 		Name:      name, | 
 | 87 | 		FlavorRef: choices.FlavorID, | 
 | 88 | 		ImageRef:  choices.ImageID, | 
 | 89 | 		Networks: []servers.Network{ | 
 | 90 | 			servers.Network{UUID: networkID}, | 
 | 91 | 		}, | 
 | 92 | 	} | 
 | 93 |  | 
 | 94 | 	server, err = bootfromvolume.Create(client, bootfromvolume.CreateOptsExt{ | 
 | 95 | 		serverCreateOpts, | 
 | 96 | 		blockDevices, | 
 | 97 | 	}).Extract() | 
 | 98 |  | 
 | 99 | 	if err != nil { | 
 | 100 | 		return server, err | 
 | 101 | 	} | 
 | 102 |  | 
 | 103 | 	return server, nil | 
 | 104 | } | 
 | 105 |  | 
 | 106 | // CreateDefaultRule will create a default security group rule with a | 
 | 107 | // random port range between 80 and 90. An error will be returned if | 
 | 108 | // a default rule was unable to be created. | 
 | 109 | func CreateDefaultRule(t *testing.T, client *gophercloud.ServiceClient) (dsr.DefaultRule, error) { | 
 | 110 | 	createOpts := dsr.CreateOpts{ | 
 | 111 | 		FromPort:   tools.RandomInt(80, 89), | 
 | 112 | 		ToPort:     tools.RandomInt(90, 99), | 
 | 113 | 		IPProtocol: "TCP", | 
 | 114 | 		CIDR:       "0.0.0.0/0", | 
 | 115 | 	} | 
 | 116 |  | 
 | 117 | 	defaultRule, err := dsr.Create(client, createOpts).Extract() | 
 | 118 | 	if err != nil { | 
 | 119 | 		return *defaultRule, err | 
 | 120 | 	} | 
 | 121 |  | 
 | 122 | 	t.Logf("Created default rule: %s", defaultRule.ID) | 
 | 123 |  | 
 | 124 | 	return *defaultRule, nil | 
 | 125 | } | 
 | 126 |  | 
 | 127 | // CreateFloatingIP will allocate a floating IP. | 
 | 128 | // An error will be returend if one was unable to be allocated. | 
 | 129 | func CreateFloatingIP(t *testing.T, client *gophercloud.ServiceClient, choices *clients.AcceptanceTestChoices) (*floatingips.FloatingIP, error) { | 
 | 130 | 	createOpts := floatingips.CreateOpts{ | 
 | 131 | 		Pool: choices.FloatingIPPoolName, | 
 | 132 | 	} | 
 | 133 | 	floatingIP, err := floatingips.Create(client, createOpts).Extract() | 
 | 134 | 	if err != nil { | 
 | 135 | 		return floatingIP, err | 
 | 136 | 	} | 
 | 137 |  | 
 | 138 | 	t.Logf("Created floating IP: %s", floatingIP.ID) | 
 | 139 | 	return floatingIP, nil | 
 | 140 | } | 
 | 141 |  | 
 | 142 | func createKey() (string, error) { | 
 | 143 | 	privateKey, err := rsa.GenerateKey(rand.Reader, 2048) | 
 | 144 | 	if err != nil { | 
 | 145 | 		return "", err | 
 | 146 | 	} | 
 | 147 |  | 
 | 148 | 	publicKey := privateKey.PublicKey | 
 | 149 | 	pub, err := ssh.NewPublicKey(&publicKey) | 
 | 150 | 	if err != nil { | 
 | 151 | 		return "", err | 
 | 152 | 	} | 
 | 153 |  | 
 | 154 | 	pubBytes := ssh.MarshalAuthorizedKey(pub) | 
 | 155 | 	pk := string(pubBytes) | 
 | 156 | 	return pk, nil | 
 | 157 | } | 
 | 158 |  | 
 | 159 | // CreateKeyPair will create a KeyPair with a random name. An error will occur | 
 | 160 | // if the keypair failed to be created. An error will be returned if the | 
 | 161 | // keypair was unable to be created. | 
 | 162 | func CreateKeyPair(t *testing.T, client *gophercloud.ServiceClient) (*keypairs.KeyPair, error) { | 
 | 163 | 	keyPairName := tools.RandomString("keypair_", 5) | 
 | 164 |  | 
 | 165 | 	t.Logf("Attempting to create keypair: %s", keyPairName) | 
 | 166 | 	createOpts := keypairs.CreateOpts{ | 
 | 167 | 		Name: keyPairName, | 
 | 168 | 	} | 
 | 169 | 	keyPair, err := keypairs.Create(client, createOpts).Extract() | 
 | 170 | 	if err != nil { | 
 | 171 | 		return keyPair, err | 
 | 172 | 	} | 
 | 173 |  | 
 | 174 | 	t.Logf("Created keypair: %s", keyPairName) | 
 | 175 | 	return keyPair, nil | 
 | 176 | } | 
 | 177 |  | 
 | 178 | // CreateSecurityGroup will create a security group with a random name. | 
 | 179 | // An error will be returned if one was failed to be created. | 
 | 180 | func CreateSecurityGroup(t *testing.T, client *gophercloud.ServiceClient) (secgroups.SecurityGroup, error) { | 
 | 181 | 	createOpts := secgroups.CreateOpts{ | 
 | 182 | 		Name:        tools.RandomString("secgroup_", 5), | 
 | 183 | 		Description: "something", | 
 | 184 | 	} | 
 | 185 |  | 
 | 186 | 	securityGroup, err := secgroups.Create(client, createOpts).Extract() | 
 | 187 | 	if err != nil { | 
 | 188 | 		return *securityGroup, err | 
 | 189 | 	} | 
 | 190 |  | 
 | 191 | 	t.Logf("Created security group: %s", securityGroup.ID) | 
 | 192 | 	return *securityGroup, nil | 
 | 193 | } | 
 | 194 |  | 
 | 195 | // CreateSecurityGroupRule will create a security group rule with a random name | 
 | 196 | // and a random TCP port range between port 80 and 99. An error will be | 
 | 197 | // returned if the rule failed to be created. | 
 | 198 | func CreateSecurityGroupRule(t *testing.T, client *gophercloud.ServiceClient, securityGroupID string) (secgroups.Rule, error) { | 
 | 199 | 	createOpts := secgroups.CreateRuleOpts{ | 
 | 200 | 		ParentGroupID: securityGroupID, | 
 | 201 | 		FromPort:      tools.RandomInt(80, 89), | 
 | 202 | 		ToPort:        tools.RandomInt(90, 99), | 
 | 203 | 		IPProtocol:    "TCP", | 
 | 204 | 		CIDR:          "0.0.0.0/0", | 
 | 205 | 	} | 
 | 206 |  | 
 | 207 | 	rule, err := secgroups.CreateRule(client, createOpts).Extract() | 
 | 208 | 	if err != nil { | 
 | 209 | 		return *rule, err | 
 | 210 | 	} | 
 | 211 |  | 
 | 212 | 	t.Logf("Created security group rule: %s", rule.ID) | 
 | 213 | 	return *rule, nil | 
 | 214 | } | 
 | 215 |  | 
 | 216 | // CreateServer creates a basic instance with a randomly generated name. | 
 | 217 | // The flavor of the instance will be the value of the OS_FLAVOR_ID environment variable. | 
 | 218 | // The image will be the value of the OS_IMAGE_ID environment variable. | 
 | 219 | // The instance will be launched on the network specified in OS_NETWORK_NAME. | 
 | 220 | // An error will be returned if the instance was unable to be created. | 
 | 221 | func CreateServer(t *testing.T, client *gophercloud.ServiceClient, choices *clients.AcceptanceTestChoices) (*servers.Server, error) { | 
 | 222 | 	if testing.Short() { | 
 | 223 | 		t.Skip("Skipping test that requires server creation in short mode.") | 
 | 224 | 	} | 
 | 225 |  | 
 | 226 | 	var server *servers.Server | 
 | 227 |  | 
 | 228 | 	networkID, err := GetNetworkIDFromTenantNetworks(t, client, choices.NetworkName) | 
 | 229 | 	if err != nil { | 
 | 230 | 		return server, err | 
 | 231 | 	} | 
 | 232 |  | 
 | 233 | 	name := tools.RandomString("ACPTTEST", 16) | 
 | 234 | 	t.Logf("Attempting to create server: %s", name) | 
 | 235 |  | 
 | 236 | 	pwd := tools.MakeNewPassword("") | 
 | 237 |  | 
 | 238 | 	server, err = servers.Create(client, servers.CreateOpts{ | 
 | 239 | 		Name:      name, | 
 | 240 | 		FlavorRef: choices.FlavorID, | 
 | 241 | 		ImageRef:  choices.ImageID, | 
 | 242 | 		AdminPass: pwd, | 
 | 243 | 		Networks: []servers.Network{ | 
 | 244 | 			servers.Network{UUID: networkID}, | 
 | 245 | 		}, | 
 | 246 | 		Personality: servers.Personality{ | 
 | 247 | 			&servers.File{ | 
 | 248 | 				Path:     "/etc/test", | 
 | 249 | 				Contents: []byte("hello world"), | 
 | 250 | 			}, | 
 | 251 | 		}, | 
 | 252 | 	}).Extract() | 
 | 253 | 	if err != nil { | 
 | 254 | 		return server, err | 
 | 255 | 	} | 
 | 256 |  | 
 | 257 | 	if err = WaitForComputeStatus(client, server, "ACTIVE"); err != nil { | 
 | 258 | 		return server, err | 
 | 259 | 	} | 
 | 260 |  | 
 | 261 | 	return server, nil | 
 | 262 | } | 
 | 263 |  | 
 | 264 | // CreateServerGroup will create a server with a random name. An error will be | 
 | 265 | // returned if the server group failed to be created. | 
 | 266 | func CreateServerGroup(t *testing.T, client *gophercloud.ServiceClient, policy string) (*servergroups.ServerGroup, error) { | 
 | 267 | 	sg, err := servergroups.Create(client, &servergroups.CreateOpts{ | 
 | 268 | 		Name:     "test", | 
 | 269 | 		Policies: []string{policy}, | 
 | 270 | 	}).Extract() | 
 | 271 |  | 
 | 272 | 	if err != nil { | 
 | 273 | 		return sg, err | 
 | 274 | 	} | 
 | 275 |  | 
 | 276 | 	return sg, nil | 
 | 277 | } | 
 | 278 |  | 
 | 279 | // CreateServerInServerGroup works like CreateServer but places the instance in | 
 | 280 | // a specified Server Group. | 
 | 281 | func CreateServerInServerGroup(t *testing.T, client *gophercloud.ServiceClient, choices *clients.AcceptanceTestChoices, serverGroup *servergroups.ServerGroup) (*servers.Server, error) { | 
 | 282 | 	if testing.Short() { | 
 | 283 | 		t.Skip("Skipping test that requires server creation in short mode.") | 
 | 284 | 	} | 
 | 285 |  | 
 | 286 | 	var server *servers.Server | 
 | 287 |  | 
 | 288 | 	networkID, err := GetNetworkIDFromTenantNetworks(t, client, choices.NetworkName) | 
 | 289 | 	if err != nil { | 
 | 290 | 		return server, err | 
 | 291 | 	} | 
 | 292 |  | 
 | 293 | 	name := tools.RandomString("ACPTTEST", 16) | 
 | 294 | 	t.Logf("Attempting to create server: %s", name) | 
 | 295 |  | 
 | 296 | 	pwd := tools.MakeNewPassword("") | 
 | 297 |  | 
 | 298 | 	serverCreateOpts := servers.CreateOpts{ | 
 | 299 | 		Name:      name, | 
 | 300 | 		FlavorRef: choices.FlavorID, | 
 | 301 | 		ImageRef:  choices.ImageID, | 
 | 302 | 		AdminPass: pwd, | 
 | 303 | 		Networks: []servers.Network{ | 
 | 304 | 			servers.Network{UUID: networkID}, | 
 | 305 | 		}, | 
 | 306 | 	} | 
 | 307 |  | 
 | 308 | 	schedulerHintsOpts := schedulerhints.CreateOptsExt{ | 
 | 309 | 		serverCreateOpts, | 
 | 310 | 		schedulerhints.SchedulerHints{ | 
 | 311 | 			Group: serverGroup.ID, | 
 | 312 | 		}, | 
 | 313 | 	} | 
 | 314 | 	server, err = servers.Create(client, schedulerHintsOpts).Extract() | 
 | 315 | 	if err != nil { | 
 | 316 | 		return server, err | 
 | 317 | 	} | 
 | 318 |  | 
 | 319 | 	return server, nil | 
 | 320 | } | 
 | 321 |  | 
 | 322 | // CreateServerWithPublicKey works the same as CreateServer, but additionally | 
 | 323 | // configures the server with a specified Key Pair name. | 
 | 324 | func CreateServerWithPublicKey(t *testing.T, client *gophercloud.ServiceClient, choices *clients.AcceptanceTestChoices, keyPairName string) (*servers.Server, error) { | 
 | 325 | 	if testing.Short() { | 
 | 326 | 		t.Skip("Skipping test that requires server creation in short mode.") | 
 | 327 | 	} | 
 | 328 |  | 
 | 329 | 	var server *servers.Server | 
 | 330 |  | 
 | 331 | 	networkID, err := GetNetworkIDFromTenantNetworks(t, client, choices.NetworkName) | 
 | 332 | 	if err != nil { | 
 | 333 | 		return server, err | 
 | 334 | 	} | 
 | 335 |  | 
 | 336 | 	name := tools.RandomString("ACPTTEST", 16) | 
 | 337 | 	t.Logf("Attempting to create server: %s", name) | 
 | 338 |  | 
 | 339 | 	serverCreateOpts := servers.CreateOpts{ | 
 | 340 | 		Name:      name, | 
 | 341 | 		FlavorRef: choices.FlavorID, | 
 | 342 | 		ImageRef:  choices.ImageID, | 
 | 343 | 		Networks: []servers.Network{ | 
 | 344 | 			servers.Network{UUID: networkID}, | 
 | 345 | 		}, | 
 | 346 | 	} | 
 | 347 |  | 
 | 348 | 	server, err = servers.Create(client, keypairs.CreateOptsExt{ | 
 | 349 | 		serverCreateOpts, | 
 | 350 | 		keyPairName, | 
 | 351 | 	}).Extract() | 
 | 352 | 	if err != nil { | 
 | 353 | 		return server, err | 
 | 354 | 	} | 
 | 355 |  | 
 | 356 | 	if err = WaitForComputeStatus(client, server, "ACTIVE"); err != nil { | 
 | 357 | 		return server, err | 
 | 358 | 	} | 
 | 359 |  | 
 | 360 | 	return server, nil | 
 | 361 | } | 
 | 362 |  | 
 | 363 | // CreateVolumeAttachment will attach a volume to a servert. An error will be | 
 | 364 | // returned if the volume failed to attach. | 
 | 365 | func CreateVolumeAttachment(t *testing.T, client *gophercloud.ServiceClient, blockClient *gophercloud.ServiceClient, server *servers.Server, volume *volumes.Volume) (*volumeattach.VolumeAttachment, error) { | 
 | 366 | 	volumeAttachOptions := volumeattach.CreateOpts{ | 
 | 367 | 		VolumeID: volume.ID, | 
 | 368 | 	} | 
 | 369 |  | 
 | 370 | 	t.Logf("Attempting to attach volume %s to server %s", volume.ID, server.ID) | 
 | 371 | 	volumeAttachment, err := volumeattach.Create(client, server.ID, volumeAttachOptions).Extract() | 
 | 372 | 	if err != nil { | 
 | 373 | 		return volumeAttachment, err | 
 | 374 | 	} | 
 | 375 |  | 
 | 376 | 	if err = volumes.WaitForStatus(blockClient, volume.ID, "in-use", 60); err != nil { | 
 | 377 | 		return volumeAttachment, err | 
 | 378 | 	} | 
 | 379 |  | 
 | 380 | 	return volumeAttachment, nil | 
 | 381 | } | 
 | 382 |  | 
 | 383 | // DeleteDefaultRule deletes a default security group rule. | 
 | 384 | // A fatal error will occur if the rule failed to delete. This works best when | 
 | 385 | // using it as a deferred function. | 
 | 386 | func DeleteDefaultRule(t *testing.T, client *gophercloud.ServiceClient, defaultRule dsr.DefaultRule) { | 
 | 387 | 	err := dsr.Delete(client, defaultRule.ID).ExtractErr() | 
 | 388 | 	if err != nil { | 
 | 389 | 		t.Fatalf("Unable to delete default rule %s: %v", defaultRule.ID, err) | 
 | 390 | 	} | 
 | 391 |  | 
 | 392 | 	t.Logf("Deleted default rule: %s", defaultRule.ID) | 
 | 393 | } | 
 | 394 |  | 
 | 395 | // DeleteFloatingIP will de-allocate a floating IP. A fatal error will occur if | 
 | 396 | // the floating IP failed to de-allocate. This works best when using it as a | 
 | 397 | // deferred function. | 
 | 398 | func DeleteFloatingIP(t *testing.T, client *gophercloud.ServiceClient, floatingIP *floatingips.FloatingIP) { | 
 | 399 | 	err := floatingips.Delete(client, floatingIP.ID).ExtractErr() | 
 | 400 | 	if err != nil { | 
 | 401 | 		t.Fatalf("Unable to delete floating IP %s: %v", floatingIP.ID, err) | 
 | 402 | 	} | 
 | 403 |  | 
 | 404 | 	t.Logf("Deleted floating IP: %s", floatingIP.ID) | 
 | 405 | } | 
 | 406 |  | 
 | 407 | // DeleteKeyPair will delete a specified keypair. A fatal error will occur if | 
 | 408 | // the keypair failed to be deleted. This works best when used as a deferred | 
 | 409 | // function. | 
 | 410 | func DeleteKeyPair(t *testing.T, client *gophercloud.ServiceClient, keyPair *keypairs.KeyPair) { | 
 | 411 | 	err := keypairs.Delete(client, keyPair.Name).ExtractErr() | 
 | 412 | 	if err != nil { | 
 | 413 | 		t.Fatalf("Unable to delete keypair %s: %v", keyPair.Name, err) | 
 | 414 | 	} | 
 | 415 |  | 
 | 416 | 	t.Logf("Deleted keypair: %s", keyPair.Name) | 
 | 417 | } | 
 | 418 |  | 
 | 419 | // DeleteSecurityGroup will delete a security group. A fatal error will occur | 
 | 420 | // if the group failed to be deleted. This works best as a deferred function. | 
 | 421 | func DeleteSecurityGroup(t *testing.T, client *gophercloud.ServiceClient, securityGroup secgroups.SecurityGroup) { | 
 | 422 | 	err := secgroups.Delete(client, securityGroup.ID).ExtractErr() | 
 | 423 | 	if err != nil { | 
 | 424 | 		t.Fatalf("Unable to delete security group %s: %s", securityGroup.ID, err) | 
 | 425 | 	} | 
 | 426 |  | 
 | 427 | 	t.Logf("Deleted security group: %s", securityGroup.ID) | 
 | 428 | } | 
 | 429 |  | 
 | 430 | // DeleteSecurityGroupRule will delete a security group rule. A fatal error | 
 | 431 | // will occur if the rule failed to be deleted. This works best when used | 
 | 432 | // as a deferred function. | 
 | 433 | func DeleteSecurityGroupRule(t *testing.T, client *gophercloud.ServiceClient, rule secgroups.Rule) { | 
 | 434 | 	err := secgroups.DeleteRule(client, rule.ID).ExtractErr() | 
 | 435 | 	if err != nil { | 
 | 436 | 		t.Fatalf("Unable to delete rule: %v", err) | 
 | 437 | 	} | 
 | 438 |  | 
 | 439 | 	t.Logf("Deleted security group rule: %s", rule.ID) | 
 | 440 | } | 
 | 441 |  | 
 | 442 | // DeleteServer deletes an instance via its UUID. | 
 | 443 | // A fatal error will occur if the instance failed to be destroyed. This works | 
 | 444 | // best when using it as a deferred function. | 
 | 445 | func DeleteServer(t *testing.T, client *gophercloud.ServiceClient, server *servers.Server) { | 
 | 446 | 	err := servers.Delete(client, server.ID).ExtractErr() | 
 | 447 | 	if err != nil { | 
 | 448 | 		t.Fatalf("Unable to delete server %s: %s", server.ID, err) | 
 | 449 | 	} | 
 | 450 |  | 
 | 451 | 	t.Logf("Deleted server: %s", server.ID) | 
 | 452 | } | 
 | 453 |  | 
 | 454 | // DeleteServerGroup will delete a server group. A fatal error will occur if | 
 | 455 | // the server group failed to be deleted. This works best when used as a | 
 | 456 | // deferred function. | 
 | 457 | func DeleteServerGroup(t *testing.T, client *gophercloud.ServiceClient, serverGroup *servergroups.ServerGroup) { | 
 | 458 | 	err := servergroups.Delete(client, serverGroup.ID).ExtractErr() | 
 | 459 | 	if err != nil { | 
 | 460 | 		t.Fatalf("Unable to delete server group %s: %v", serverGroup.ID, err) | 
 | 461 | 	} | 
 | 462 |  | 
 | 463 | 	t.Logf("Deleted server group %s", serverGroup.ID) | 
 | 464 | } | 
 | 465 |  | 
 | 466 | // DeleteVolumeAttachment will disconnect a volume from an instance. A fatal | 
 | 467 | // error will occur if the volume failed to detach. This works best when used | 
 | 468 | // as a deferred functino. | 
 | 469 | func DeleteVolumeAttachment(t *testing.T, client *gophercloud.ServiceClient, blockClient *gophercloud.ServiceClient, server *servers.Server, volumeAttachment *volumeattach.VolumeAttachment) { | 
 | 470 |  | 
 | 471 | 	err := volumeattach.Delete(client, server.ID, volumeAttachment.VolumeID).ExtractErr() | 
 | 472 | 	if err != nil { | 
 | 473 | 		t.Fatalf("Unable to detach volume: %v", err) | 
 | 474 | 	} | 
 | 475 |  | 
 | 476 | 	if err = volumes.WaitForStatus(blockClient, volumeAttachment.ID, "available", 60); err != nil { | 
 | 477 | 		t.Fatalf("Unable to wait for volume: %v", err) | 
 | 478 | 	} | 
 | 479 | 	t.Logf("Deleted volume: %s", volumeAttachment.VolumeID) | 
 | 480 | } | 
 | 481 |  | 
 | 482 | // DisassociateFloatingIP will disassociate a floating IP from an instance. A | 
 | 483 | // fatal error will occur if the floating IP failed to disassociate. This works | 
 | 484 | // best when using it as a deferred function. | 
 | 485 | func DisassociateFloatingIP(t *testing.T, client *gophercloud.ServiceClient, floatingIP *floatingips.FloatingIP, server *servers.Server) { | 
 | 486 | 	disassociateOpts := floatingips.DisassociateOpts{ | 
 | 487 | 		FloatingIP: floatingIP.IP, | 
 | 488 | 	} | 
 | 489 |  | 
 | 490 | 	err := floatingips.DisassociateInstance(client, server.ID, disassociateOpts).ExtractErr() | 
 | 491 | 	if err != nil { | 
 | 492 | 		t.Fatalf("Unable to disassociate floating IP %s from server %s: %v", floatingIP.IP, server.ID, err) | 
 | 493 | 	} | 
 | 494 |  | 
 | 495 | 	t.Logf("Disassociated floating IP %s from server %s", floatingIP.IP, server.ID) | 
 | 496 | } | 
 | 497 |  | 
 | 498 | // GetNetworkIDFromNetworks will return the network ID from a specified network | 
 | 499 | // UUID using the os-networks API extension. An error will be returned if the | 
 | 500 | // network could not be retrieved. | 
 | 501 | func GetNetworkIDFromNetworks(t *testing.T, client *gophercloud.ServiceClient, networkName string) (string, error) { | 
 | 502 | 	allPages, err := networks.List(client).AllPages() | 
 | 503 | 	if err != nil { | 
 | 504 | 		t.Fatalf("Unable to list networks: %v", err) | 
 | 505 | 	} | 
 | 506 |  | 
 | 507 | 	networkList, err := networks.ExtractNetworks(allPages) | 
 | 508 | 	if err != nil { | 
 | 509 | 		t.Fatalf("Unable to list networks: %v", err) | 
 | 510 | 	} | 
 | 511 |  | 
 | 512 | 	networkID := "" | 
 | 513 | 	for _, network := range networkList { | 
 | 514 | 		t.Logf("Network: %v", network) | 
 | 515 | 		if network.Label == networkName { | 
 | 516 | 			networkID = network.ID | 
 | 517 | 		} | 
 | 518 | 	} | 
 | 519 |  | 
 | 520 | 	t.Logf("Found network ID for %s: %s", networkName, networkID) | 
 | 521 |  | 
 | 522 | 	return networkID, nil | 
 | 523 | } | 
 | 524 |  | 
 | 525 | // GetNetworkIDFromTenantNetworks will return the network UUID for a given | 
 | 526 | // network name using the os-tenant-networks API extension. An error will be | 
 | 527 | // returned if the network could not be retrieved. | 
 | 528 | func GetNetworkIDFromTenantNetworks(t *testing.T, client *gophercloud.ServiceClient, networkName string) (string, error) { | 
 | 529 | 	allPages, err := tenantnetworks.List(client).AllPages() | 
 | 530 | 	if err != nil { | 
 | 531 | 		return "", err | 
 | 532 | 	} | 
 | 533 |  | 
 | 534 | 	allTenantNetworks, err := tenantnetworks.ExtractNetworks(allPages) | 
 | 535 | 	if err != nil { | 
 | 536 | 		return "", err | 
 | 537 | 	} | 
 | 538 |  | 
 | 539 | 	for _, network := range allTenantNetworks { | 
 | 540 | 		if network.Name == networkName { | 
 | 541 | 			return network.ID, nil | 
 | 542 | 		} | 
 | 543 | 	} | 
 | 544 |  | 
 | 545 | 	return "", fmt.Errorf("Failed to obtain network ID for network %s", networkName) | 
 | 546 | } | 
 | 547 |  | 
 | 548 | // ImportPublicKey will create a KeyPair with a random name and a specified | 
 | 549 | // public key. An error will be returned if the keypair failed to be created. | 
 | 550 | func ImportPublicKey(t *testing.T, client *gophercloud.ServiceClient, publicKey string) (*keypairs.KeyPair, error) { | 
 | 551 | 	keyPairName := tools.RandomString("keypair_", 5) | 
 | 552 |  | 
 | 553 | 	t.Logf("Attempting to create keypair: %s", keyPairName) | 
 | 554 | 	createOpts := keypairs.CreateOpts{ | 
 | 555 | 		Name:      keyPairName, | 
 | 556 | 		PublicKey: publicKey, | 
 | 557 | 	} | 
 | 558 | 	keyPair, err := keypairs.Create(client, createOpts).Extract() | 
 | 559 | 	if err != nil { | 
 | 560 | 		return keyPair, err | 
 | 561 | 	} | 
 | 562 |  | 
 | 563 | 	t.Logf("Created keypair: %s", keyPairName) | 
 | 564 | 	return keyPair, nil | 
 | 565 | } | 
 | 566 |  | 
 | 567 | // ResizeServer performs a resize action on an instance. An error will be | 
 | 568 | // returned if the instance failed to resize. | 
 | 569 | // The new flavor that the instance will be resized to is specified in OS_FLAVOR_ID_RESIZE. | 
 | 570 | func ResizeServer(t *testing.T, client *gophercloud.ServiceClient, server *servers.Server, choices *clients.AcceptanceTestChoices) error { | 
 | 571 | 	opts := &servers.ResizeOpts{ | 
 | 572 | 		FlavorRef: choices.FlavorIDResize, | 
 | 573 | 	} | 
 | 574 | 	if res := servers.Resize(client, server.ID, opts); res.Err != nil { | 
 | 575 | 		return res.Err | 
 | 576 | 	} | 
 | 577 |  | 
 | 578 | 	if err := WaitForComputeStatus(client, server, "VERIFY_RESIZE"); err != nil { | 
 | 579 | 		return err | 
 | 580 | 	} | 
 | 581 |  | 
 | 582 | 	return nil | 
 | 583 | } | 
 | 584 |  | 
 | 585 | // WaitForComputeStatus will poll an instance's status until it either matches | 
 | 586 | // the specified status or the status becomes ERROR. | 
 | 587 | func WaitForComputeStatus(client *gophercloud.ServiceClient, server *servers.Server, status string) error { | 
 | 588 | 	return tools.WaitFor(func() (bool, error) { | 
 | 589 | 		latest, err := servers.Get(client, server.ID).Extract() | 
 | 590 | 		if err != nil { | 
 | 591 | 			return false, err | 
 | 592 | 		} | 
 | 593 |  | 
 | 594 | 		if latest.Status == status { | 
 | 595 | 			// Success! | 
 | 596 | 			return true, nil | 
 | 597 | 		} | 
 | 598 |  | 
 | 599 | 		if latest.Status == "ERROR" { | 
 | 600 | 			return false, fmt.Errorf("Instance in ERROR state") | 
 | 601 | 		} | 
 | 602 |  | 
 | 603 | 		return false, nil | 
 | 604 | 	}) | 
 | 605 | } | 
 | 606 |  | 
 | 607 | // PrintServer will print an instance and all of its attributes. | 
 | 608 | func PrintServer(t *testing.T, server *servers.Server) { | 
 | 609 | 	t.Logf("ID: %s", server.ID) | 
 | 610 | 	t.Logf("TenantID: %s", server.TenantID) | 
 | 611 | 	t.Logf("UserID: %s", server.UserID) | 
 | 612 | 	t.Logf("Name: %s", server.Name) | 
 | 613 | 	t.Logf("Updated: %s", server.Updated) | 
 | 614 | 	t.Logf("Created: %s", server.Created) | 
 | 615 | 	t.Logf("HostID: %s", server.HostID) | 
 | 616 | 	t.Logf("Status: %s", server.Status) | 
 | 617 | 	t.Logf("Progress: %d", server.Progress) | 
 | 618 | 	t.Logf("AccessIPv4: %s", server.AccessIPv4) | 
 | 619 | 	t.Logf("AccessIPv6: %s", server.AccessIPv6) | 
 | 620 | 	t.Logf("Image: %s", server.Image) | 
 | 621 | 	t.Logf("Flavor: %s", server.Flavor) | 
 | 622 | 	t.Logf("Addresses: %#v", server.Addresses) | 
 | 623 | 	t.Logf("Metadata: %#v", server.Metadata) | 
 | 624 | 	t.Logf("Links: %#v", server.Links) | 
 | 625 | 	t.Logf("KeyName: %s", server.KeyName) | 
 | 626 | 	t.Logf("AdminPass: %s", server.AdminPass) | 
 | 627 | 	t.Logf("SecurityGroups: %#v", server.SecurityGroups) | 
 | 628 | } | 
 | 629 |  | 
 | 630 | // PrintDefaultRule will print a default security group rule and all of its attributes. | 
 | 631 | func PrintDefaultRule(t *testing.T, defaultRule *dsr.DefaultRule) { | 
 | 632 | 	t.Logf("\tID: %s", defaultRule.ID) | 
 | 633 | 	t.Logf("\tFrom Port: %d", defaultRule.FromPort) | 
 | 634 | 	t.Logf("\tTo Port: %d", defaultRule.ToPort) | 
 | 635 | 	t.Logf("\tIP Protocol: %s", defaultRule.IPProtocol) | 
 | 636 | 	t.Logf("\tIP Range: %s", defaultRule.IPRange.CIDR) | 
 | 637 | 	t.Logf("\tParent Group ID: %s", defaultRule.ParentGroupID) | 
 | 638 | 	t.Logf("\tGroup Tenant ID: %s", defaultRule.Group.TenantID) | 
 | 639 | 	t.Logf("\tGroup Name: %s", defaultRule.Group.Name) | 
 | 640 | } | 
 | 641 |  | 
 | 642 | // PrintFlavor will print a flavor and all of its attributes. | 
 | 643 | func PrintFlavor(t *testing.T, flavor *flavors.Flavor) { | 
 | 644 | 	t.Logf("ID: %s", flavor.ID) | 
 | 645 | 	t.Logf("Name: %s", flavor.Name) | 
 | 646 | 	t.Logf("RAM: %d", flavor.RAM) | 
 | 647 | 	t.Logf("Disk: %d", flavor.Disk) | 
 | 648 | 	t.Logf("Swap: %d", flavor.Swap) | 
 | 649 | 	t.Logf("RxTxFactor: %f", flavor.RxTxFactor) | 
 | 650 | } | 
 | 651 |  | 
 | 652 | // PrintFloatingIP will print a floating IP and all of its attributes. | 
 | 653 | func PrintFloatingIP(t *testing.T, floatingIP *floatingips.FloatingIP) { | 
 | 654 | 	t.Logf("ID: %s", floatingIP.ID) | 
 | 655 | 	t.Logf("Fixed IP: %s", floatingIP.FixedIP) | 
 | 656 | 	t.Logf("Instance ID: %s", floatingIP.InstanceID) | 
 | 657 | 	t.Logf("IP: %s", floatingIP.IP) | 
 | 658 | 	t.Logf("Pool: %s", floatingIP.Pool) | 
 | 659 | } | 
 | 660 |  | 
 | 661 | // PrintImage will print an image and all of its attributes. | 
 | 662 | func PrintImage(t *testing.T, image images.Image) { | 
 | 663 | 	t.Logf("ID: %s", image.ID) | 
 | 664 | 	t.Logf("Name: %s", image.Name) | 
 | 665 | 	t.Logf("MinDisk: %d", image.MinDisk) | 
 | 666 | 	t.Logf("MinRAM: %d", image.MinRAM) | 
 | 667 | 	t.Logf("Status: %s", image.Status) | 
 | 668 | 	t.Logf("Progress: %d", image.Progress) | 
 | 669 | 	t.Logf("Metadata: %#v", image.Metadata) | 
 | 670 | 	t.Logf("Created: %s", image.Created) | 
 | 671 | 	t.Logf("Updated: %s", image.Updated) | 
 | 672 | } | 
 | 673 |  | 
 | 674 | // PrintKeyPair will print keypair and all of its attributes. | 
 | 675 | func PrintKeyPair(t *testing.T, keypair *keypairs.KeyPair) { | 
 | 676 | 	t.Logf("Name: %s", keypair.Name) | 
 | 677 | 	t.Logf("Fingerprint: %s", keypair.Fingerprint) | 
 | 678 | 	t.Logf("Public Key: %s", keypair.PublicKey) | 
 | 679 | 	t.Logf("Private Key: %s", keypair.PrivateKey) | 
 | 680 | 	t.Logf("UserID: %s", keypair.UserID) | 
 | 681 | } | 
 | 682 |  | 
 | 683 | //  PrintNetwork will print an os-networks based network and all of its attributes. | 
 | 684 | func PrintNetwork(t *testing.T, network *networks.Network) { | 
 | 685 | 	t.Logf("Bridge: %s", network.Bridge) | 
 | 686 | 	t.Logf("BridgeInterface: %s", network.BridgeInterface) | 
 | 687 | 	t.Logf("Broadcast: %s", network.Broadcast) | 
 | 688 | 	t.Logf("CIDR: %s", network.CIDR) | 
 | 689 | 	t.Logf("CIDRv6: %s", network.CIDRv6) | 
 | 690 | 	t.Logf("CreatedAt: %v", network.CreatedAt) | 
 | 691 | 	t.Logf("Deleted: %t", network.Deleted) | 
 | 692 | 	t.Logf("DeletedAt: %v", network.DeletedAt) | 
 | 693 | 	t.Logf("DHCPStart: %s", network.DHCPStart) | 
 | 694 | 	t.Logf("DNS1: %s", network.DNS1) | 
 | 695 | 	t.Logf("DNS2: %s", network.DNS2) | 
 | 696 | 	t.Logf("Gateway: %s", network.Gateway) | 
 | 697 | 	t.Logf("Gatewayv6: %s", network.Gatewayv6) | 
 | 698 | 	t.Logf("Host: %s", network.Host) | 
 | 699 | 	t.Logf("ID: %s", network.ID) | 
 | 700 | 	t.Logf("Injected: %t", network.Injected) | 
 | 701 | 	t.Logf("Label: %s", network.Label) | 
 | 702 | 	t.Logf("MultiHost: %t", network.MultiHost) | 
 | 703 | 	t.Logf("Netmask: %s", network.Netmask) | 
 | 704 | 	t.Logf("Netmaskv6: %s", network.Netmaskv6) | 
 | 705 | 	t.Logf("Priority: %d", network.Priority) | 
 | 706 | 	t.Logf("ProjectID: %s", network.ProjectID) | 
 | 707 | 	t.Logf("RXTXBase: %d", network.RXTXBase) | 
 | 708 | 	t.Logf("UpdatedAt: %v", network.UpdatedAt) | 
 | 709 | 	t.Logf("VLAN: %d", network.VLAN) | 
 | 710 | 	t.Logf("VPNPrivateAddress: %s", network.VPNPrivateAddress) | 
 | 711 | 	t.Logf("VPNPublicAddress: %s", network.VPNPublicAddress) | 
 | 712 | 	t.Logf("VPNPublicPort: %d", network.VPNPublicPort) | 
 | 713 | } | 
 | 714 |  | 
 | 715 | //  PrintQuotaSet will print a quota set and all of its attributes. | 
 | 716 | func PrintQuotaSet(t *testing.T, quotaSet *quotasets.QuotaSet) { | 
 | 717 | 	t.Logf("instances: %d\n", quotaSet.Instances) | 
 | 718 | 	t.Logf("cores: %d\n", quotaSet.Cores) | 
 | 719 | 	t.Logf("ram: %d\n", quotaSet.Ram) | 
 | 720 | 	t.Logf("key_pairs: %d\n", quotaSet.KeyPairs) | 
 | 721 | 	t.Logf("metadata_items: %d\n", quotaSet.MetadataItems) | 
 | 722 | 	t.Logf("security_groups: %d\n", quotaSet.SecurityGroups) | 
 | 723 | 	t.Logf("security_group_rules: %d\n", quotaSet.SecurityGroupRules) | 
 | 724 | 	t.Logf("fixed_ips: %d\n", quotaSet.FixedIps) | 
 | 725 | 	t.Logf("floating_ips: %d\n", quotaSet.FloatingIps) | 
 | 726 | 	t.Logf("injected_file_content_bytes: %d\n", quotaSet.InjectedFileContentBytes) | 
 | 727 | 	t.Logf("injected_file_path_bytes: %d\n", quotaSet.InjectedFilePathBytes) | 
 | 728 | 	t.Logf("injected_files: %d\n", quotaSet.InjectedFiles) | 
 | 729 | } | 
 | 730 |  | 
 | 731 | //  PrintSecurityGroup will print a security group and all of its attributes and rules. | 
 | 732 | func PrintSecurityGroup(t *testing.T, securityGroup *secgroups.SecurityGroup) { | 
 | 733 | 	t.Logf("ID: %s", securityGroup.ID) | 
 | 734 | 	t.Logf("Name: %s", securityGroup.Name) | 
 | 735 | 	t.Logf("Description: %s", securityGroup.Description) | 
 | 736 | 	t.Logf("Tenant ID: %s", securityGroup.TenantID) | 
 | 737 | 	t.Logf("Rules:") | 
 | 738 |  | 
 | 739 | 	for _, rule := range securityGroup.Rules { | 
 | 740 | 		t.Logf("\tID: %s", rule.ID) | 
 | 741 | 		t.Logf("\tFrom Port: %d", rule.FromPort) | 
 | 742 | 		t.Logf("\tTo Port: %d", rule.ToPort) | 
 | 743 | 		t.Logf("\tIP Protocol: %s", rule.IPProtocol) | 
 | 744 | 		t.Logf("\tIP Range: %s", rule.IPRange.CIDR) | 
 | 745 | 		t.Logf("\tParent Group ID: %s", rule.ParentGroupID) | 
 | 746 | 		t.Logf("\tGroup Tenant ID: %s", rule.Group.TenantID) | 
 | 747 | 		t.Logf("\tGroup Name: %s", rule.Group.Name) | 
 | 748 | 	} | 
 | 749 | } | 
 | 750 |  | 
 | 751 | // PrintServerGroup will print a server group and all of its attributes. | 
 | 752 | func PrintServerGroup(t *testing.T, serverGroup *servergroups.ServerGroup) { | 
 | 753 | 	t.Logf("ID: %s", serverGroup.ID) | 
 | 754 | 	t.Logf("Name: %s", serverGroup.Name) | 
 | 755 | 	t.Logf("Policies: %#v", serverGroup.Policies) | 
 | 756 | 	t.Logf("Members: %#v", serverGroup.Members) | 
 | 757 | 	t.Logf("Metadata: %#v", serverGroup.Metadata) | 
 | 758 | } | 
 | 759 |  | 
 | 760 | // PrintTenantNetwork will print an os-tenant-networks based network and all of its attributes. | 
 | 761 | func PrintTenantNetwork(t *testing.T, network *tenantnetworks.Network) { | 
 | 762 | 	t.Logf("ID: %s", network.ID) | 
 | 763 | 	t.Logf("Name: %s", network.Name) | 
 | 764 | 	t.Logf("CIDR: %s", network.CIDR) | 
 | 765 | } | 
 | 766 |  | 
 | 767 | // PrintVolumeAttachment will print a volume attachment and all of its attributes. | 
 | 768 | func PrintVolumeAttachment(t *testing.T, volumeAttachment *volumeattach.VolumeAttachment) { | 
 | 769 | 	t.Logf("ID: %s", volumeAttachment.ID) | 
 | 770 | 	t.Logf("Device: %s", volumeAttachment.Device) | 
 | 771 | 	t.Logf("VolumeID: %s", volumeAttachment.VolumeID) | 
 | 772 | 	t.Logf("ServerID: %s", volumeAttachment.ServerID) | 
 | 773 | } |