-
Notifications
You must be signed in to change notification settings - Fork 125
/
rh_region.go
60 lines (55 loc) · 1.61 KB
/
rh_region.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
package sdk
import (
json2 "encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
)
type Region struct {
URL string
AWS []string
GCP []string
}
func GetRhRegions(ocmServiceUrl string) (map[string]Region, error) {
var regions map[string]Region
url, err := DetermineRegionDiscoveryUrl(ocmServiceUrl)
if err != nil {
return regions, fmt.Errorf("Can't determine region discovery URL: %s\n", err)
}
// Adding nolint here in order to prevent linter from failing due to variable http get
resp, err := http.Get(url) //nolint
if err != nil {
return regions, fmt.Errorf("Can't retrieve shards: %s", err)
}
err = json2.NewDecoder(resp.Body).Decode(®ions)
if err != nil {
return regions, fmt.Errorf("Can't decode shards: %s", err)
}
return regions, nil
}
func GetRhRegion(ocmServiceUrl string, regionName string) (Region, error) {
regions, err := GetRhRegions(ocmServiceUrl)
if err != nil {
return Region{}, err
}
for regName, regValue := range regions {
if regName == regionName {
return regValue, nil
}
}
return Region{}, fmt.Errorf("Can't find region %s", regionName)
}
func DetermineRegionDiscoveryUrl(ocmServiceUrl string) (string, error) {
baseUrl, err := url.Parse(ocmServiceUrl)
if err != nil {
return "", err
}
regionDiscoveryHost := "api.openshift.com"
if strings.HasSuffix(baseUrl.Hostname(), "integration.openshift.com") {
regionDiscoveryHost = "api.integration.openshift.com"
} else if strings.HasSuffix(baseUrl.Hostname(), "stage.openshift.com") {
regionDiscoveryHost = "api.stage.openshift.com"
}
return fmt.Sprintf("https://%s/static/ocm-shards.json", regionDiscoveryHost), nil
}