@@ -23,6 +23,7 @@ import (
2323 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/servergroups"
2424 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/tenantnetworks"
2525 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/volumeattach"
26+ "github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
2627 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
2728
2829 "golang.org/x/crypto/ssh"
@@ -136,6 +137,29 @@ func CreateDefaultRule(t *testing.T, client *gophercloud.ServiceClient) (dsr.Def
136137 return * defaultRule , nil
137138}
138139
140+ // CreateFlavor will create a flavor with a random name.
141+ // An error will be returned if the flavor could not be created.
142+ func CreateFlavor (t * testing.T , client * gophercloud.ServiceClient ) (* flavors.Flavor , error ) {
143+ flavorName := tools .RandomString ("flavor_" , 5 )
144+ t .Logf ("Attempting to create flavor %s" , flavorName )
145+
146+ createOpts := flavors.CreateOpts {
147+ Name : flavorName ,
148+ RAM : 1 ,
149+ VCPUs : 1 ,
150+ Disk : gophercloud .IntToPointer (1 ),
151+ }
152+
153+ flavor , err := flavors .Create (client , createOpts ).Extract ()
154+ if err != nil {
155+ return nil , err
156+ }
157+
158+ t .Logf ("Successfully created flavor %s" , flavor .ID )
159+
160+ return flavor , nil
161+ }
162+
139163// CreateFloatingIP will allocate a floating IP.
140164// An error will be returend if one was unable to be allocated.
141165func CreateFloatingIP (t * testing.T , client * gophercloud.ServiceClient ) (* floatingips.FloatingIP , error ) {
@@ -531,6 +555,17 @@ func DeleteDefaultRule(t *testing.T, client *gophercloud.ServiceClient, defaultR
531555 t .Logf ("Deleted default rule: %s" , defaultRule .ID )
532556}
533557
558+ // DeleteFlavor will delete a flavor. A fatal error will occur if the flavor
559+ // could not be deleted. This works best when using it as a deferred function.
560+ func DeleteFlavor (t * testing.T , client * gophercloud.ServiceClient , flavor * flavors.Flavor ) {
561+ err := flavors .Delete (client , flavor .ID ).ExtractErr ()
562+ if err != nil {
563+ t .Fatalf ("Unable to delete flavor %s" , flavor .ID )
564+ }
565+
566+ t .Logf ("Deleted flavor: %s" , flavor .ID )
567+ }
568+
534569// DeleteFloatingIP will de-allocate a floating IP. A fatal error will occur if
535570// the floating IP failed to de-allocate. This works best when using it as a
536571// deferred function.
0 commit comments