-
Notifications
You must be signed in to change notification settings - Fork 787
/
regions.go
98 lines (82 loc) · 2.03 KB
/
regions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package iks
import (
"fmt"
"strings"
ibmcloud "github.com/IBM-Cloud/bluemix-go"
"github.com/IBM-Cloud/bluemix-go/client"
"github.com/IBM-Cloud/bluemix-go/http"
"github.com/IBM-Cloud/bluemix-go/rest"
gohttp "net/http"
)
const containerEndpointOfPublicBluemix = "https://containers.bluemix.net"
type Region struct {
Name string `json:"name"`
Alias string `json:"alias"`
CfURL string `json:"cfURL"`
FreeEnabled bool `json:"freeEnabled"`
}
type RegionResponse struct {
Regions []Region `json:"regions"`
}
type Regions interface {
GetRegions() ([]Region, error)
GetRegion(region string) (*Region, error)
}
type region struct {
*client.Client
regions []Region
}
func newRegionsAPI(c *client.Client) Regions {
return ®ion{
Client: c,
regions: nil,
}
}
func (v *region) fetch() error {
if v.regions == nil {
regions := RegionResponse{}
_, err := v.Client.Get("/v1/regions", ®ions)
if err != nil {
return err
}
v.regions = regions.Regions
}
return nil
}
//List ...
func (v *region) GetRegions() ([]Region, error) {
if err := v.fetch(); err != nil {
return nil, err
}
return v.regions, nil
}
func (v *region) GetRegion(regionarg string) (*Region, error) {
if err := v.fetch(); err != nil {
return nil, err
}
for _, region := range v.regions {
if strings.Compare(regionarg, region.Name) == 0 {
return ®ion, nil
}
}
return nil, fmt.Errorf("region %q not found", regionarg)
}
func GetAuthRegions(config *ibmcloud.Config) ([]string, error) {
regions := RegionResponse{}
if config.HTTPClient == nil {
config.HTTPClient = http.NewHTTPClient(config)
}
client := client.New(config, ibmcloud.MccpService, nil)
resp, err := client.SendRequest(rest.GetRequest(containerEndpointOfPublicBluemix+"/v1/regions"), ®ions)
if resp.StatusCode == gohttp.StatusNotFound {
return []string{}, nil
}
if err != nil {
return nil, err
}
strregions := make([]string, len(regions.Regions))
for i, region := range regions.Regions {
strregions[i] = region.Alias
}
return strregions, nil
}