github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/mccp/mccpv2/region.go (about) 1 package mccpv2 2 3 import ( 4 "net/http" 5 "strings" 6 7 "github.com/IBM-Cloud/bluemix-go/client" 8 "github.com/IBM-Cloud/bluemix-go/models" 9 "github.com/IBM-Cloud/bluemix-go/rest" 10 ) 11 12 const mccpEndpointOfPublicBluemix = "https://mccp.ng.bluemix.net" 13 14 //go:generate counterfeiter . RegionRepository 15 type RegionRepository interface { 16 PublicRegions() ([]models.Region, error) 17 Regions() ([]models.Region, error) 18 FindRegionByName(name string) (*models.Region, error) 19 FindRegionById(id string) (*models.Region, error) 20 } 21 22 type region struct { 23 client *client.Client 24 } 25 26 func newRegionRepositoryAPI(c *client.Client) RegionRepository { 27 return ®ion{ 28 client: c, 29 } 30 } 31 32 func (r *region) PublicRegions() ([]models.Region, error) { 33 return r.regions(mccpEndpointOfPublicBluemix) 34 } 35 36 func (r *region) Regions() ([]models.Region, error) { 37 return r.regions(*r.client.Config.Endpoint) 38 } 39 40 func (r *region) regions(endpoint string) ([]models.Region, error) { 41 var result []models.Region 42 resp, err := r.client.SendRequest(rest.GetRequest(endpoint+"/v2/regions"), &result) 43 if resp.StatusCode == http.StatusNotFound { 44 return []models.Region{}, nil 45 } 46 if err != nil { 47 return []models.Region{}, err 48 } 49 return result, nil 50 } 51 52 func (r *region) FindRegionByName(name string) (*models.Region, error) { 53 regions, err := r.Regions() 54 if err != nil { 55 return nil, err 56 } 57 for _, region := range regions { 58 if strings.EqualFold(region.Name, name) { 59 return ®ion, nil 60 } 61 } 62 return nil, nil 63 } 64 func (r *region) FindRegionById(id string) (*models.Region, error) { 65 regions, err := r.Regions() 66 if err != nil { 67 return nil, err 68 } 69 for _, region := range regions { 70 if strings.EqualFold(region.ID, id) { 71 return ®ion, nil 72 } 73 } 74 return nil, nil 75 }