| 1 |
package group |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"time" |
| 6 |
|
| 7 |
"github.com/CenturyLinkCloud/clc-sdk/api" |
| 8 |
"github.com/CenturyLinkCloud/clc-sdk/status" |
| 9 |
) |
| 10 |
|
| 11 |
func New(client api.HTTP) *Service { |
| 12 |
return &Service{ |
| 13 |
client: client, |
| 14 |
config: client.Config(), |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
type Service struct { |
| 19 |
client api.HTTP |
| 20 |
config *api.Config |
| 21 |
} |
| 22 |
|
| 23 |
func (s *Service) Get(id string) (*Response, error) { |
| 24 |
url := fmt.Sprintf("%s/groups/%s/%s", s.config.BaseURL, s.config.Alias, id) |
| 25 |
resp := &Response{} |
| 26 |
err := s.client.Get(url, resp) |
| 27 |
return resp, err |
| 28 |
} |
| 29 |
|
| 30 |
func (s *Service) Create(group Group) (*Response, error) { |
| 31 |
resp := &Response{} |
| 32 |
url := fmt.Sprintf("%s/groups/%s", s.config.BaseURL, s.config.Alias) |
| 33 |
err := s.client.Post(url, group, resp) |
| 34 |
return resp, err |
| 35 |
} |
| 36 |
|
| 37 |
func (s *Service) Update(id string, updates ...api.Update) error { |
| 38 |
url := fmt.Sprintf("%s/groups/%s/%s", s.config.BaseURL, s.config.Alias, id) |
| 39 |
return s.client.Patch(url, updates, nil) |
| 40 |
} |
| 41 |
|
| 42 |
func (s *Service) Delete(id string) (*status.Status, error) { |
| 43 |
url := fmt.Sprintf("%s/groups/%s/%s", s.config.BaseURL, s.config.Alias, id) |
| 44 |
resp := &status.Status{} |
| 45 |
err := s.client.Delete(url, resp) |
| 46 |
return resp, err |
| 47 |
} |
| 48 |
|
| 49 |
func (s *Service) Archive(id string) (*status.Status, error) { |
| 50 |
url := fmt.Sprintf("%s/groups/%s/%s/archive", s.config.BaseURL, s.config.Alias, id) |
| 51 |
resp := &status.Status{} |
| 52 |
err := s.client.Post(url, "", resp) |
| 53 |
return resp, err |
| 54 |
} |
| 55 |
|
| 56 |
func (s *Service) Restore(id, intoGroup string) (*status.QueuedResponse, error) { |
| 57 |
url := fmt.Sprintf("%s/groups/%s/%s/restore", s.config.BaseURL, s.config.Alias, id) |
| 58 |
resp := &status.QueuedResponse{} |
| 59 |
body := fmt.Sprintf(`{"targetGroupId": "%v"}`, intoGroup) |
| 60 |
err := s.client.Post(url, body, resp) |
| 61 |
return resp, err |
| 62 |
} |
| 63 |
|
| 64 |
func (s *Service) SetDefaults(id string, defaults *GroupDefaults) error { |
| 65 |
url := fmt.Sprintf("%s/groups/%s/%s/defaults", s.config.BaseURL, s.config.Alias, id) |
| 66 |
var resp interface{} |
| 67 |
err := s.client.Post(url, defaults, resp) |
| 68 |
return err |
| 69 |
} |
| 70 |
|
| 71 |
func (s *Service) SetHorizontalAutoscalePolicy(id string, policy *HorizontalAutoscalePolicy) (*interface{}, error) { |
| 72 |
url := fmt.Sprintf("%s/groups/%s/%s/horizontalAutoscalePolicy", s.config.BaseURL, s.config.Alias, id) |
| 73 |
var resp interface{} |
| 74 |
err := s.client.Put(url, policy, resp) |
| 75 |
return &resp, err |
| 76 |
} |
| 77 |
|
| 78 |
func UpdateName(name string) api.Update { |
| 79 |
return api.Update{ |
| 80 |
Op: "set", |
| 81 |
Member: "name", |
| 82 |
Value: name, |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
func UpdateDescription(desc string) api.Update { |
| 87 |
return api.Update{ |
| 88 |
Op: "set", |
| 89 |
Member: "description", |
| 90 |
Value: desc, |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func UpdateParentGroupID(id string) api.Update { |
| 95 |
return api.Update{ |
| 96 |
Op: "set", |
| 97 |
Member: "parentGroupId", |
| 98 |
Value: id, |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
func UpdateCustomfields(fields []api.Customfields) api.Update { |
| 103 |
return api.Update{ |
| 104 |
Op: "set", |
| 105 |
Member: "customFields", |
| 106 |
Value: fields, |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
type Group struct { |
| 112 |
Name string `json:"name"` |
| 113 |
Description string `json:"description,omitempty"` |
| 114 |
ParentGroupID string `json:"parentGroupId"` |
| 115 |
CustomFields []api.Customfields `json:"customFields,omitempty"` |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
type Response struct { |
| 120 |
ID string `json:"id"` |
| 121 |
Name string `json:"name"` |
| 122 |
Description string `json:"description"` |
| 123 |
Locationid string `json:"locationId"` |
| 124 |
Type string `json:"type"` |
| 125 |
Status string `json:"status"` |
| 126 |
Links api.Links `json:"links"` |
| 127 |
Groups []Groups `json:"groups"` |
| 128 |
Changeinfo struct { |
| 129 |
Createddate time.Time `json:"createdDate"` |
| 130 |
Createdby string `json:"createdBy"` |
| 131 |
Modifieddate time.Time `json:"modifiedDate"` |
| 132 |
Modifiedby string `json:"modifiedBy"` |
| 133 |
} `json:"changeInfo"` |
| 134 |
Customfields []api.Customfields `json:"customFields"` |
| 135 |
} |
| 136 |
|
| 137 |
func (r *Response) ParentGroupID() string { |
| 138 |
if ok, link := r.Links.GetLink("parentGroup"); ok { |
| 139 |
return link.ID |
| 140 |
} |
| 141 |
return "" |
| 142 |
} |
| 143 |
|
| 144 |
func (r *Response) Servers() []string { |
| 145 |
ids := make([]string, 0) |
| 146 |
for _, l := range r.Links { |
| 147 |
if l.Rel == "server" { |
| 148 |
ids = append(ids, l.ID) |
| 149 |
} |
| 150 |
} |
| 151 |
return ids |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
type Groups struct { |
| 156 |
ID string `json:"id"` |
| 157 |
Name string `json:"name"` |
| 158 |
Description string `json:"description"` |
| 159 |
Locationid string `json:"locationId"` |
| 160 |
Type string `json:"type"` |
| 161 |
Status string `json:"status"` |
| 162 |
Serverscount int `json:"serversCount"` |
| 163 |
Groups []Groups `json:"groups"` |
| 164 |
Links api.Links `json:"links"` |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
type GroupDefaults struct { |
| 169 |
CPU string `json:"cpu,omitempty"` |
| 170 |
MemoryGB string `json:"memoryGB,omitempty"` |
| 171 |
NetworkID string `json:"networkId,omitempty"` |
| 172 |
PrimaryDns string `json:"primaryDns,omitempty"` |
| 173 |
SecondaryDns string `json:"secondaryDns,omitempty"` |
| 174 |
TemplateName string `json:"templateName,omitempty"` |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
type HorizontalAutoscalePolicy struct { |
| 179 |
PolicyId string `json:"policyId,omitempty"` |
| 180 |
LoadBalancerPool []PoolPolicy `json:"loadBalancerPool,omitempty"` |
| 181 |
} |
| 182 |
|
| 183 |
type PoolPolicy struct { |
| 184 |
ID string `json:"id,omitempty"` |
| 185 |
PrivatePort int `json:"privatePort,omitempty"` |
| 186 |
PublicPort int `json:"publicPort,omitempty"` |
| 187 |
} |