-
Notifications
You must be signed in to change notification settings - Fork 24
/
region.go
160 lines (138 loc) · 4.32 KB
/
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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package civogo
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
)
// Region represents a geographical/DC region for Civo resources
type Region struct {
Code string `json:"code"`
Name string `json:"name"`
Type string `json:"type"`
OutOfCapacity bool `json:"out_of_capacity"`
Country string `json:"country"`
CountryName string `json:"country_name"`
Features Feature `json:"features"`
Default bool `json:"default"`
}
// Feature represent a all feature inside a region
type Feature struct {
Iaas bool `json:"iaas"`
Kubernetes bool `json:"kubernetes"`
ObjectStore bool `json:"object_store"`
LoadBalancer bool `json:"loadbalancer"`
GPU bool `json:"gpu"`
DBaaS bool `json:"dbaas"`
Volume bool `json:"volume"`
PaaS bool `json:"paas"`
KFaaS bool `json:"kfaas"`
PublicIPNodePools bool `json:"public_ip_node_pools"`
}
// CreateRegionRequest is the request to create a new region
type CreateRegionRequest struct {
Code string `json:"code"`
CountryISOCode string `json:"country_iso_code" `
Private bool `json:"private,omitempty"`
AccountIDs []string `json:"account_ids,omitempty"`
// Kubeconfig should be a base64 encoded kubeconfig content
Kubeconfig string `json:"kubeconfig"`
// ComputeSoftDeletionHours can only be configured for private regions.
ComputeSoftDeletionHours *int `json:"compute_soft_deletion_hours" `
Features map[string]bool `json:"features" `
}
// DisconnectRegionRequest is the request to disconnect a region
type DisconnectRegionRequest struct {
Code string `json:"code"`
}
// ConnectRegionRequest is the request to connect a region
type ConnectRegionRequest struct {
Code string `json:"code"`
}
// ListRegions returns all load balancers owned by the calling API account
func (c *Client) ListRegions() ([]Region, error) {
resp, err := c.SendGetRequest("/v2/regions")
if err != nil {
return nil, decodeError(err)
}
regions := make([]Region, 0)
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(®ions); err != nil {
return nil, err
}
return regions, nil
}
// FindRegion is a function to find a region
func (c *Client) FindRegion(search string) (*Region, error) {
allregion, err := c.ListRegions()
if err != nil {
return nil, decodeError(err)
}
exactMatch := false
partialMatchesCount := 0
result := Region{}
search = strings.ToUpper(search)
for _, value := range allregion {
name := strings.ToUpper(value.Name)
code := strings.ToUpper(value.Code)
if name == search || code == search {
exactMatch = true
result = value
} else if strings.Contains(name, search) || strings.Contains(code, search) {
if !exactMatch {
result = value
partialMatchesCount++
}
}
}
if exactMatch || partialMatchesCount == 1 {
return &result, nil
} else if partialMatchesCount > 1 {
err := fmt.Errorf("unable to find %s because there were multiple matches", search)
return nil, MultipleMatchesError.wrap(err)
} else {
err := fmt.Errorf("unable to find %s, zero matches", search)
return nil, ZeroMatchesError.wrap(err)
}
}
// GetDefaultRegion finds the default region for an account
func (c *Client) GetDefaultRegion() (*Region, error) {
allregion, err := c.ListRegions()
if err != nil {
return nil, decodeError(err)
}
for _, region := range allregion {
if region.Default {
return ®ion, nil
}
}
return nil, errors.New("no default region found")
}
// CreateRegion is a function to create a region
func (c *Client) CreateRegion(r *CreateRegionRequest) (*Region, error) {
resp, err := c.SendPostRequest("/v2/regions", r)
if err != nil {
return nil, decodeError(err)
}
region := Region{}
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(®ion); err != nil {
return nil, err
}
return ®ion, nil
}
// ConnectRegion connects a region to CivoAPI
func (c *Client) ConnectRegion(r *ConnectRegionRequest) error {
_, err := c.SendPostRequest("/v2/regions/connect", r)
if err != nil {
return decodeError(err)
}
return nil
}
// DisconnectRegion disconnects a region to CivoAPI
func (c *Client) DisconnectRegion(r *DisconnectRegionRequest) error {
_, err := c.SendPostRequest("/v2/regions/disconnect", r)
if err != nil {
return decodeError(err)
}
return nil
}