@@ -40,7 +40,6 @@ import (
4040 "github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
4141 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
4242 "github.com/gophercloud/gophercloud/openstack/identity/v3/tokens"
43- "github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
4443 netext "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions"
4544 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/attributestags"
4645 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/trunks"
@@ -49,6 +48,8 @@ import (
4948 "github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
5049 "github.com/gophercloud/gophercloud/pagination"
5150 "github.com/gophercloud/utils/openstack/clientconfig"
51+ flavorutils "github.com/gophercloud/utils/openstack/compute/v2/flavors"
52+ imageutils "github.com/gophercloud/utils/openstack/compute/v2/images"
5253 configclient "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
5354 machinev1 "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1"
5455 "github.com/openshift/machine-api-operator/pkg/util"
@@ -366,36 +367,6 @@ func GetSecurityGroups(is *InstanceService, sg_param []openstackconfigv1.Securit
366367 return sgIDs , nil
367368}
368369
369- // Helper function for getting image ID from name
370- func getImageID (is * InstanceService , imageName string ) (string , error ) {
371- if imageName == "" {
372- return "" , nil
373- }
374-
375- opts := images.ListOpts {
376- Name : imageName ,
377- }
378-
379- pages , err := images .List (is .imagesClient , opts ).AllPages ()
380- if err != nil {
381- return "" , err
382- }
383-
384- allImages , err := images .ExtractImages (pages )
385- if err != nil {
386- return "" , err
387- }
388-
389- switch len (allImages ) {
390- case 0 :
391- return "" , fmt .Errorf ("no image with the name %s could be found" , imageName )
392- case 1 :
393- return allImages [0 ].ID , nil
394- default :
395- return "" , fmt .Errorf ("too many images with the name, %s, were found" , imageName )
396- }
397- }
398-
399370// InstanceCreate creates a compute instance.
400371// If ServerGroupName is nonempty and no server group exists with that name,
401372// then InstanceCreate creates a server group with that name.
@@ -580,15 +551,21 @@ func (is *InstanceService) InstanceCreate(clusterName string, name string, clust
580551 }
581552
582553 // Get image ID
583- imageID , err := getImageID (is , config .Image )
554+ imageID , err := imageutils .IDFromName (is .imagesClient , config .Image )
555+ if err != nil {
556+ return nil , fmt .Errorf ("Create new server err: %v" , err )
557+ }
558+
559+ // Get flavor ID
560+ flavorID , err := flavorutils .IDFromName (is .computeClient , config .Flavor )
584561 if err != nil {
585562 return nil , fmt .Errorf ("Create new server err: %v" , err )
586563 }
587564
588565 var serverCreateOpts servers.CreateOptsBuilder = servers.CreateOpts {
589566 Name : name ,
590567 ImageRef : imageID ,
591- FlavorName : config . Flavor ,
568+ FlavorRef : flavorID ,
592569 AvailabilityZone : config .AvailabilityZone ,
593570 Networks : ports_list ,
594571 UserData : []byte (userData ),
@@ -608,7 +585,7 @@ func (is *InstanceService) InstanceCreate(clusterName string, name string, clust
608585 // change serverCreateOpts to exclude imageRef from them
609586 serverCreateOpts = servers.CreateOpts {
610587 Name : name ,
611- FlavorName : config . Flavor ,
588+ FlavorRef : flavorID ,
612589 AvailabilityZone : config .AvailabilityZone ,
613590 Networks : ports_list ,
614591 UserData : []byte (userData ),
@@ -623,7 +600,7 @@ func (is *InstanceService) InstanceCreate(clusterName string, name string, clust
623600 // if source type is "image" then we have to create a volume from the image first
624601 klog .Infof ("Creating a bootable volume from image %v." , config .RootVolume .SourceUUID )
625602
626- imageID , err := getImageID (is , config .RootVolume .SourceUUID )
603+ imageID , err := imageutils . IDFromName (is . imagesClient , config .RootVolume .SourceUUID )
627604 if err != nil {
628605 return nil , fmt .Errorf ("Create new server err: %v" , err )
629606 }
@@ -897,22 +874,14 @@ func (is *InstanceService) GetInstanceList(opts *InstanceListOpts) ([]*Instance,
897874
898875// DoesFlavorExist return an error if flavor with given name doesn't exist, and nil otherwise
899876func (is * InstanceService ) DoesFlavorExist (flavorName string ) error {
900- _ , err := flavors .IDFromName (is .computeClient , flavorName )
901- if err != nil {
902- return err
903- }
904-
905- return nil
877+ _ , err := flavorutils .IDFromName (is .computeClient , flavorName )
878+ return err
906879}
907880
908881// DoesImageExist return an error if image with the given name doesn't exist, and nil otherwise
909882func (is * InstanceService ) DoesImageExist (imageName string ) error {
910- _ , err := getImageID (is , imageName )
911- if err != nil {
912- return err
913- }
914-
915- return nil
883+ _ , err := imageutils .IDFromName (is .imagesClient , imageName )
884+ return err
916885}
917886
918887func (is * InstanceService ) GetInstance (resourceId string ) (instance * Instance , err error ) {
0 commit comments