@@ -67,6 +67,7 @@ type API interface {
6767 // Service Instance
6868 ListServiceInstances (ctx context.Context ) ([]string , error )
6969 ServiceInstanceGUIDToName (ctx context.Context , id string ) (string , error )
70+ ServiceInstanceNameToGUID (ctx context.Context , name string ) (string , error )
7071
7172 // Security Group
7273 ListSecurityGroupRules (ctx context.Context , securityGroupID string ) (* vpcv1.SecurityGroupRuleCollection , error )
@@ -89,8 +90,10 @@ type Client struct {
8990
9091// cisServiceID is the Cloud Internet Services' catalog service ID.
9192const (
92- cisServiceID = "75874a60-cb12-11e7-948e-37ac098eb1b9"
93- dnsServiceID = "b4ed8a30-936f-11e9-b289-1d079699cbe5"
93+ cisServiceID = "75874a60-cb12-11e7-948e-37ac098eb1b9"
94+ dnsServiceID = "b4ed8a30-936f-11e9-b289-1d079699cbe5"
95+ serviceInstanceType = "service_instance"
96+ compositeInstanceType = "composite_instance"
9497)
9598
9699// DNSZoneResponse represents a DNS zone response.
@@ -743,7 +746,7 @@ func (c *Client) ListServiceInstances(ctx context.Context) ([]string, error) {
743746 continue
744747 }
745748
746- if resourceInstance .Type != nil && (* resourceInstance .Type == "service_instance" || * resourceInstance .Type == "composite_instance" ) {
749+ if resourceInstance .Type != nil && (* resourceInstance .Type == serviceInstanceType || * resourceInstance .Type == compositeInstanceType ) {
747750 serviceInstances = append (serviceInstances , fmt .Sprintf ("%s %s" , * resource .Name , * resource .GUID ))
748751 }
749752 }
@@ -819,7 +822,7 @@ func (c *Client) ServiceInstanceGUIDToName(ctx context.Context, id string) (stri
819822 continue
820823 }
821824
822- if resourceInstance .Type != nil && (* resourceInstance .Type == "service_instance" || * resourceInstance .Type == "composite_instance" ) {
825+ if resourceInstance .Type != nil && (* resourceInstance .Type == serviceInstanceType || * resourceInstance .Type == compositeInstanceType ) {
823826 if resourceInstance .GUID != nil && * resourceInstance .GUID == id {
824827 if resourceInstance .Name == nil {
825828 return "" , nil
@@ -846,6 +849,87 @@ func (c *Client) ServiceInstanceGUIDToName(ctx context.Context, id string) (stri
846849 return "" , nil
847850}
848851
852+ // ServiceInstanceNameToGUID returns the name of the matching service instance GUID which was passed in.
853+ func (c * Client ) ServiceInstanceNameToGUID (ctx context.Context , name string ) (string , error ) {
854+ var (
855+ options * resourcecontrollerv2.ListResourceInstancesOptions
856+ resources * resourcecontrollerv2.ResourceInstancesList
857+ err error
858+ perPage int64 = 10
859+ moreData = true
860+ nextURL * string
861+ groupID = c .BXCli .PowerVSResourceGroup
862+ )
863+
864+ // If the user passes in a human readable group id, then we need to convert it to a UUID
865+ listGroupOptions := c .managementAPI .NewListResourceGroupsOptions ()
866+ listGroupOptions .AccountID = & c .BXCli .User .Account
867+ groups , _ , err := c .managementAPI .ListResourceGroupsWithContext (ctx , listGroupOptions )
868+ if err != nil {
869+ return "" , fmt .Errorf ("failed to list resource groups: %w" , err )
870+ }
871+ for _ , group := range groups .Resources {
872+ if * group .Name == groupID {
873+ groupID = * group .ID
874+ }
875+ }
876+
877+ options = c .controllerAPI .NewListResourceInstancesOptions ()
878+ options .SetResourceGroupID (groupID )
879+ // resource ID for Power Systems Virtual Server in the Global catalog
880+ options .SetResourceID (powerIAASResourceID )
881+ options .SetLimit (perPage )
882+
883+ for moreData {
884+ resources , _ , err = c .controllerAPI .ListResourceInstancesWithContext (ctx , options )
885+ if err != nil {
886+ return "" , fmt .Errorf ("failed to list resource instances: %w" , err )
887+ }
888+
889+ for _ , resource := range resources .Resources {
890+ var (
891+ getResourceOptions * resourcecontrollerv2.GetResourceInstanceOptions
892+ resourceInstance * resourcecontrollerv2.ResourceInstance
893+ response * core.DetailedResponse
894+ )
895+
896+ getResourceOptions = c .controllerAPI .NewGetResourceInstanceOptions (* resource .ID )
897+
898+ resourceInstance , response , err = c .controllerAPI .GetResourceInstance (getResourceOptions )
899+ if err != nil {
900+ return "" , fmt .Errorf ("failed to get instance: %w" , err )
901+ }
902+ if response != nil && response .StatusCode == http .StatusNotFound || response .StatusCode == http .StatusInternalServerError {
903+ continue
904+ }
905+
906+ if resourceInstance .Type != nil && (* resourceInstance .Type == serviceInstanceType || * resourceInstance .Type == compositeInstanceType ) {
907+ if resourceInstance .Name != nil && * resourceInstance .Name == name {
908+ if resourceInstance .GUID == nil {
909+ return "" , nil
910+ }
911+ return * resourceInstance .GUID , nil
912+ }
913+ }
914+ }
915+
916+ // Based on: https://cloud.ibm.com/apidocs/resource-controller/resource-controller?code=go#list-resource-instances
917+ nextURL , err = core .GetQueryParam (resources .NextURL , "start" )
918+ if err != nil {
919+ return "" , fmt .Errorf ("failed to GetQueryParam on start: %w" , err )
920+ }
921+ if nextURL == nil {
922+ options .SetStart ("" )
923+ } else {
924+ options .SetStart (* nextURL )
925+ }
926+
927+ moreData = * resources .RowsCount == perPage
928+ }
929+
930+ return "" , nil
931+ }
932+
849933// GetDatacenterCapabilities retrieves the capabilities of the specified datacenter.
850934func (c * Client ) GetDatacenterCapabilities (ctx context.Context , region string ) (map [string ]bool , error ) {
851935 var err error
0 commit comments