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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
package group
import (
"fmt"
"time"
"github.com/CenturyLinkCloud/clc-sdk/api"
"github.com/CenturyLinkCloud/clc-sdk/status"
)
func New(client api.HTTP) *Service {
return &Service{
client: client,
config: client.Config(),
}
}
type Service struct {
client api.HTTP
config *api.Config
}
func (s *Service) Get(id string) (*Response, error) {
url := fmt.Sprintf("%s/groups/%s/%s", s.config.BaseURL, s.config.Alias, id)
resp := &Response{}
err := s.client.Get(url, resp)
return resp, err
}
func (s *Service) Create(group Group) (*Response, error) {
resp := &Response{}
url := fmt.Sprintf("%s/groups/%s", s.config.BaseURL, s.config.Alias)
err := s.client.Post(url, group, resp)
return resp, err
}
func (s *Service) Update(id string, updates ...api.Update) error {
url := fmt.Sprintf("%s/groups/%s/%s", s.config.BaseURL, s.config.Alias, id)
return s.client.Patch(url, updates, nil)
}
func (s *Service) Delete(id string) (*status.Status, error) {
url := fmt.Sprintf("%s/groups/%s/%s", s.config.BaseURL, s.config.Alias, id)
resp := &status.Status{}
err := s.client.Delete(url, resp)
return resp, err
}
func (s *Service) Archive(id string) (*status.Status, error) {
url := fmt.Sprintf("%s/groups/%s/%s/archive", s.config.BaseURL, s.config.Alias, id)
resp := &status.Status{}
err := s.client.Post(url, "", resp)
return resp, err
}
func (s *Service) Restore(id, intoGroup string) (*status.QueuedResponse, error) {
url := fmt.Sprintf("%s/groups/%s/%s/restore", s.config.BaseURL, s.config.Alias, id)
resp := &status.QueuedResponse{}
body := fmt.Sprintf(`{"targetGroupId": "%v"}`, intoGroup)
err := s.client.Post(url, body, resp)
return resp, err
}
func (s *Service) SetDefaults(id string, defaults *GroupDefaults) error {
url := fmt.Sprintf("%s/groups/%s/%s/defaults", s.config.BaseURL, s.config.Alias, id)
var resp interface{}
err := s.client.Post(url, defaults, resp)
return err
}
func (s *Service) SetHorizontalAutoscalePolicy(id string, policy *HorizontalAutoscalePolicy) (*interface{}, error) {
url := fmt.Sprintf("%s/groups/%s/%s/horizontalAutoscalePolicy", s.config.BaseURL, s.config.Alias, id)
var resp interface{}
err := s.client.Put(url, policy, resp)
return &resp, err
}
func UpdateName(name string) api.Update {
return api.Update{
Op: "set",
Member: "name",
Value: name,
}
}
func UpdateDescription(desc string) api.Update {
return api.Update{
Op: "set",
Member: "description",
Value: desc,
}
}
func UpdateParentGroupID(id string) api.Update {
return api.Update{
Op: "set",
Member: "parentGroupId",
Value: id,
}
}
func UpdateCustomfields(fields []api.Customfields) api.Update {
return api.Update{
Op: "set",
Member: "customFields",
Value: fields,
}
}
// request body for creating groups
type Group struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
ParentGroupID string `json:"parentGroupId"`
CustomFields []api.Customfields `json:"customFields,omitempty"`
}
// response body for group get
type Response struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Locationid string `json:"locationId"`
Type string `json:"type"`
Status string `json:"status"`
Links api.Links `json:"links"`
Groups []Groups `json:"groups"`
Changeinfo struct {
Createddate time.Time `json:"createdDate"`
Createdby string `json:"createdBy"`
Modifieddate time.Time `json:"modifiedDate"`
Modifiedby string `json:"modifiedBy"`
} `json:"changeInfo"`
Customfields []api.Customfields `json:"customFields"`
}
func (r *Response) ParentGroupID() string {
if ok, link := r.Links.GetLink("parentGroup"); ok {
return link.ID
}
return ""
}
func (r *Response) Servers() []string {
ids := make([]string, 0)
for _, l := range r.Links {
if l.Rel == "server" {
ids = append(ids, l.ID)
}
}
return ids
}
// nested groups under response
type Groups struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Locationid string `json:"locationId"`
Type string `json:"type"`
Status string `json:"status"`
Serverscount int `json:"serversCount"`
Groups []Groups `json:"groups"`
Links api.Links `json:"links"`
}
// request body for /v2/groups/ALIAS/ID/defaults
type GroupDefaults struct {
CPU string `json:"cpu,omitempty"`
MemoryGB string `json:"memoryGB,omitempty"`
NetworkID string `json:"networkId,omitempty"`
PrimaryDns string `json:"primaryDns,omitempty"`
SecondaryDns string `json:"secondaryDns,omitempty"`
TemplateName string `json:"templateName,omitempty"`
}
// request body for /v2/groups/ALIAS/ID/horizontalAutoscalePolicy
type HorizontalAutoscalePolicy struct {
PolicyId string `json:"policyId,omitempty"`
LoadBalancerPool []PoolPolicy `json:"loadBalancerPool,omitempty"`
}
type PoolPolicy struct {
ID string `json:"id,omitempty"`
PrivatePort int `json:"privatePort,omitempty"`
PublicPort int `json:"publicPort,omitempty"`
}
|