| 1 |
package server |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"regexp" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/CenturyLinkCloud/clc-sdk/api" |
| 9 |
"github.com/CenturyLinkCloud/clc-sdk/status" |
| 10 |
) |
| 11 |
|
| 12 |
var ( |
| 13 |
ErrInvalidServer = fmt.Errorf("server: server missing required field(s). (Name, CPU, MemoryGB, GroupID, SourceServerID, Type)") |
| 14 |
) |
| 15 |
|
| 16 |
func New(client api.HTTP) *Service { |
| 17 |
return &Service{ |
| 18 |
client: client, |
| 19 |
config: client.Config(), |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
type Service struct { |
| 24 |
client api.HTTP |
| 25 |
config *api.Config |
| 26 |
} |
| 27 |
|
| 28 |
func (s *Service) Get(name string) (*Response, error) { |
| 29 |
url := fmt.Sprintf("%s/servers/%s/%s", s.config.BaseURL, s.config.Alias, name) |
| 30 |
if regexp.MustCompile("^[0-9a-f]{32}$").MatchString(name) { |
| 31 |
url = fmt.Sprintf("%s?uuid=true", url) |
| 32 |
} |
| 33 |
resp := &Response{} |
| 34 |
err := s.client.Get(url, resp) |
| 35 |
return resp, err |
| 36 |
} |
| 37 |
|
| 38 |
func (s *Service) Create(server Server) (*status.QueuedResponse, error) { |
| 39 |
if !server.Valid() { |
| 40 |
return nil, ErrInvalidServer |
| 41 |
} |
| 42 |
|
| 43 |
resp := &status.QueuedResponse{} |
| 44 |
url := fmt.Sprintf("%s/servers/%s", s.config.BaseURL, s.config.Alias) |
| 45 |
err := s.client.Post(url, server, resp) |
| 46 |
return resp, err |
| 47 |
} |
| 48 |
|
| 49 |
func (s *Service) Update(name string, updates ...api.Update) (*status.Status, error) { |
| 50 |
resp := &status.Status{} |
| 51 |
url := fmt.Sprintf("%s/servers/%s/%s", s.config.BaseURL, s.config.Alias, name) |
| 52 |
err := s.client.Patch(url, updates, resp) |
| 53 |
return resp, err |
| 54 |
} |
| 55 |
|
| 56 |
func (s *Service) Edit(name string, updates ...api.Update) error { |
| 57 |
url := fmt.Sprintf("%s/servers/%s/%s", s.config.BaseURL, s.config.Alias, name) |
| 58 |
err := s.client.Patch(url, updates, nil) |
| 59 |
return err |
| 60 |
} |
| 61 |
|
| 62 |
func (s *Service) Delete(name string) (*status.QueuedResponse, error) { |
| 63 |
url := fmt.Sprintf("%s/servers/%s/%s", s.config.BaseURL, s.config.Alias, name) |
| 64 |
resp := &status.QueuedResponse{} |
| 65 |
err := s.client.Delete(url, resp) |
| 66 |
return resp, err |
| 67 |
} |
| 68 |
|
| 69 |
func (s *Service) GetCredentials(name string) (Credentials, error) { |
| 70 |
url := fmt.Sprintf("%s/servers/%s/%s/credentials", s.config.BaseURL, s.config.Alias, name) |
| 71 |
resp := Credentials{} |
| 72 |
err := s.client.Get(url, &resp) |
| 73 |
return resp, err |
| 74 |
} |
| 75 |
|
| 76 |
type Credentials struct { |
| 77 |
Username string `json:"userName"` |
| 78 |
Password string `json:"password"` |
| 79 |
} |
| 80 |
|
| 81 |
func (s *Service) Archive(servers ...string) ([]*status.QueuedResponse, error) { |
| 82 |
url := fmt.Sprintf("%s/operations/%s/servers/archive", s.config.BaseURL, s.config.Alias) |
| 83 |
var resp []*status.QueuedResponse |
| 84 |
err := s.client.Post(url, servers, &resp) |
| 85 |
return resp, err |
| 86 |
} |
| 87 |
|
| 88 |
func (s *Service) Restore(name, group string) (*status.Status, error) { |
| 89 |
restore := map[string]string{"targetGroupId": group} |
| 90 |
url := fmt.Sprintf("%s/servers/%s/%s/restore", s.config.BaseURL, s.config.Alias, name) |
| 91 |
resp := &status.Status{} |
| 92 |
err := s.client.Post(url, restore, resp) |
| 93 |
return resp, err |
| 94 |
} |
| 95 |
|
| 96 |
func (s *Service) CreateSnapshot(expiration int, servers ...string) ([]*status.QueuedResponse, error) { |
| 97 |
snapshot := Snapshot{Expiration: expiration, Servers: servers} |
| 98 |
url := fmt.Sprintf("%s/operations/%s/servers/createSnapshot", s.config.BaseURL, s.config.Alias) |
| 99 |
var resp []*status.QueuedResponse |
| 100 |
err := s.client.Post(url, snapshot, &resp) |
| 101 |
return resp, err |
| 102 |
} |
| 103 |
|
| 104 |
func (s *Service) DeleteSnapshot(server, id string) (*status.Status, error) { |
| 105 |
url := fmt.Sprintf("%s/servers/%s/%s/snapshots/%s", s.config.BaseURL, s.config.Alias, server, id) |
| 106 |
resp := &status.Status{} |
| 107 |
err := s.client.Delete(url, resp) |
| 108 |
return resp, err |
| 109 |
} |
| 110 |
|
| 111 |
func (s *Service) RevertSnapshot(server, id string) (*status.Status, error) { |
| 112 |
url := fmt.Sprintf("%s/servers/%s/%s/snapshots/%s/restore", s.config.BaseURL, s.config.Alias, server, id) |
| 113 |
resp := &status.Status{} |
| 114 |
err := s.client.Post(url, nil, resp) |
| 115 |
return resp, err |
| 116 |
} |
| 117 |
|
| 118 |
type Snapshot struct { |
| 119 |
Expiration int `json:"snapshotExpirationDays"` |
| 120 |
Servers []string `json:"serverIds"` |
| 121 |
} |
| 122 |
|
| 123 |
func (s *Service) ExecutePackage(pkg Package, servers ...string) ([]*status.QueuedResponse, error) { |
| 124 |
url := fmt.Sprintf("%s/operations/%s/servers/executePackage", s.config.BaseURL, s.config.Alias) |
| 125 |
var resp []*status.QueuedResponse |
| 126 |
exec := executePackage{Servers: servers, Package: pkg} |
| 127 |
err := s.client.Post(url, exec, &resp) |
| 128 |
return resp, err |
| 129 |
} |
| 130 |
|
| 131 |
type executePackage struct { |
| 132 |
Servers []string `json:"servers"` |
| 133 |
Package Package `json:"package"` |
| 134 |
} |
| 135 |
|
| 136 |
type Package struct { |
| 137 |
ID string `json:"packageId"` |
| 138 |
Params map[string]string `json:"parameters"` |
| 139 |
} |
| 140 |
|
| 141 |
func (s *Service) PowerState(state PowerState, servers ...string) ([]*status.QueuedResponse, error) { |
| 142 |
url := fmt.Sprintf("%s/operations/%s/servers/%s", s.config.BaseURL, s.config.Alias, state) |
| 143 |
var resp []*status.QueuedResponse |
| 144 |
err := s.client.Post(url, servers, &resp) |
| 145 |
return resp, err |
| 146 |
} |
| 147 |
|
| 148 |
type PowerState int |
| 149 |
|
| 150 |
const ( |
| 151 |
On = iota |
| 152 |
Off |
| 153 |
Pause |
| 154 |
Reboot |
| 155 |
Reset |
| 156 |
ShutDown |
| 157 |
StartMaintenance |
| 158 |
StopMaintenance |
| 159 |
) |
| 160 |
|
| 161 |
func (p PowerState) String() string { |
| 162 |
switch p { |
| 163 |
case On: |
| 164 |
return "powerOn" |
| 165 |
case Off: |
| 166 |
return "powerOff" |
| 167 |
case Pause: |
| 168 |
return "pause" |
| 169 |
case Reboot: |
| 170 |
return "reboot" |
| 171 |
case Reset: |
| 172 |
return "reset" |
| 173 |
case ShutDown: |
| 174 |
return "shutDown" |
| 175 |
case StartMaintenance: |
| 176 |
return "startMaintenance" |
| 177 |
case StopMaintenance: |
| 178 |
return "stopMaintenance" |
| 179 |
} |
| 180 |
return "" |
| 181 |
} |
| 182 |
|
| 183 |
func (s *Service) GetPublicIP(name string, ip string) (*PublicIP, error) { |
| 184 |
url := fmt.Sprintf("%s/servers/%s/%s/publicIPAddresses/%s", s.config.BaseURL, s.config.Alias, name, ip) |
| 185 |
resp := &PublicIP{} |
| 186 |
err := s.client.Get(url, resp) |
| 187 |
return resp, err |
| 188 |
} |
| 189 |
|
| 190 |
func (s *Service) AddPublicIP(name string, ip PublicIP) (*status.Status, error) { |
| 191 |
url := fmt.Sprintf("%s/servers/%s/%s/publicIPAddresses", s.config.BaseURL, s.config.Alias, name) |
| 192 |
resp := &status.Status{} |
| 193 |
err := s.client.Post(url, ip, resp) |
| 194 |
return resp, err |
| 195 |
} |
| 196 |
|
| 197 |
func (s *Service) UpdatePublicIP(name string, public string, ip PublicIP) (*status.Status, error) { |
| 198 |
url := fmt.Sprintf("%s/servers/%s/%s/publicIPAddresses/%s", s.config.BaseURL, s.config.Alias, name, public) |
| 199 |
resp := &status.Status{} |
| 200 |
err := s.client.Put(url, ip, resp) |
| 201 |
return resp, err |
| 202 |
} |
| 203 |
|
| 204 |
func (s *Service) DeletePublicIP(name, ip string) (*status.Status, error) { |
| 205 |
url := fmt.Sprintf("%s/servers/%s/%s/publicIPAddresses/%s", s.config.BaseURL, s.config.Alias, name, ip) |
| 206 |
resp := &status.Status{} |
| 207 |
err := s.client.Delete(url, resp) |
| 208 |
return resp, err |
| 209 |
} |
| 210 |
|
| 211 |
type PublicIP struct { |
| 212 |
InternalIP string `json:"internalIPAddress,omitempty"` |
| 213 |
Ports []Port `json:"ports,omitempty"` |
| 214 |
SourceRestrictions []SourceRestriction `json:"sourceRestrictions,omitempty"` |
| 215 |
} |
| 216 |
|
| 217 |
type Port struct { |
| 218 |
Protocol string `json:"protocol"` |
| 219 |
Port int `json:"port"` |
| 220 |
PortTo int `json:"portTo,omitempty"` |
| 221 |
} |
| 222 |
|
| 223 |
type SourceRestriction struct { |
| 224 |
CIDR string `json:"cidr"` |
| 225 |
} |
| 226 |
|
| 227 |
type Disk struct { |
| 228 |
DiskID string `json:"diskId,omitempty"` |
| 229 |
Path string `json:"path,omitempty"` |
| 230 |
SizeGB int `json:"sizeGB,omitempty"` |
| 231 |
Type string `json:"type,omitempty"` |
| 232 |
} |
| 233 |
|
| 234 |
func (s *Service) AddSecondaryNetwork(name, networkId, ip string) (*status.Status, error) { |
| 235 |
url := fmt.Sprintf("%s/servers/%s/%s/networks", s.config.BaseURL, s.config.Alias, name) |
| 236 |
req := &SecondaryNetwork{ |
| 237 |
NetworkID: networkId, |
| 238 |
IPAddress: ip, |
| 239 |
} |
| 240 |
|
| 241 |
resp := &status.QueuedOperation{} |
| 242 |
err := s.client.Post(url, req, resp) |
| 243 |
if err != nil { |
| 244 |
return nil, err |
| 245 |
} |
| 246 |
return resp.Status(), nil |
| 247 |
} |
| 248 |
|
| 249 |
type SecondaryNetwork struct { |
| 250 |
NetworkID string `json:"networkId,omitempty"` |
| 251 |
IPAddress string `json:"ipAddress,omitempty"` |
| 252 |
} |
| 253 |
|
| 254 |
func UpdateCPU(num int) api.Update { |
| 255 |
return api.Update{ |
| 256 |
Op: "set", |
| 257 |
Member: "cpu", |
| 258 |
Value: num, |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
func UpdateMemory(num int) api.Update { |
| 263 |
return api.Update{ |
| 264 |
Op: "set", |
| 265 |
Member: "memory", |
| 266 |
Value: num, |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
func UpdateCredentials(current, updated string) api.Update { |
| 271 |
return api.Update{ |
| 272 |
Op: "set", |
| 273 |
Member: "password", |
| 274 |
Value: struct { |
| 275 |
Current string `json:"current"` |
| 276 |
Password string `json:"password"` |
| 277 |
}{ |
| 278 |
current, |
| 279 |
updated, |
| 280 |
}, |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
func UpdateGroup(group string) api.Update { |
| 285 |
return api.Update{ |
| 286 |
Op: "set", |
| 287 |
Member: "groupId", |
| 288 |
Value: group, |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
func UpdateDescription(desc string) api.Update { |
| 293 |
return api.Update{ |
| 294 |
Op: "set", |
| 295 |
Member: "description", |
| 296 |
Value: desc, |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
func UpdateAdditionaldisks(disks []Disk) api.Update { |
| 301 |
return api.Update{ |
| 302 |
Op: "set", |
| 303 |
Member: "disks", |
| 304 |
Value: disks, |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
func UpdateCustomfields(fields []api.Customfields) api.Update { |
| 309 |
return api.Update{ |
| 310 |
Op: "set", |
| 311 |
Member: "customFields", |
| 312 |
Value: fields, |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
type Server struct { |
| 317 |
Name string `json:"name"` |
| 318 |
Description string `json:"description,omitempty"` |
| 319 |
GroupID string `json:"groupId"` |
| 320 |
SourceServerID string `json:"sourceServerId"` |
| 321 |
IsManagedOS bool `json:"isManagedOS,omitempty"` |
| 322 |
IsManagedBackup bool `json:"isManagedBackup,omitempty"` |
| 323 |
PrimaryDNS string `json:"primaryDns,omitempty"` |
| 324 |
SecondaryDNS string `json:"secondaryDns,omitempty"` |
| 325 |
NetworkID string `json:"networkId,omitempty"` |
| 326 |
IPaddress string `json:"ipAddress,omitempty"` |
| 327 |
Password string `json:"password,omitempty"` |
| 328 |
SourceServerPassword string `json:"sourceServerPassword,omitempty"` |
| 329 |
CPU int `json:"cpu"` |
| 330 |
CPUAutoscalePolicyID string `json:"cpuAutoscalePolicyId,omitempty"` |
| 331 |
MemoryGB int `json:"memoryGB"` |
| 332 |
Type string `json:"type"` |
| 333 |
Storagetype string `json:"storageType,omitempty"` |
| 334 |
AntiAffinityPolicyID string `json:"antiAffinityPolicyId,omitempty"` |
| 335 |
Customfields []api.Customfields `json:"customFields,omitempty"` |
| 336 |
Additionaldisks []Disk `json:"additionalDisks,omitempty"` |
| 337 |
TTL *time.Time `json:"ttl,omitempty"` |
| 338 |
Packages []Package `json:"packages,omitempty"` |
| 339 |
ConfigurationID string `json:"configurationId,omitempty"` |
| 340 |
OSType string `json:"osType,omitempty"` |
| 341 |
} |
| 342 |
|
| 343 |
func (s *Server) Valid() bool { |
| 344 |
return s.Name != "" && s.CPU != 0 && s.MemoryGB != 0 && s.GroupID != "" && s.SourceServerID != "" && s.Type != "" |
| 345 |
} |
| 346 |
|
| 347 |
type Response struct { |
| 348 |
ID string `json:"id"` |
| 349 |
Name string `json:"name"` |
| 350 |
Description string `json:"description"` |
| 351 |
GroupID string `json:"groupId"` |
| 352 |
IsTemplate bool `json:"isTemplate"` |
| 353 |
LocationID string `json:"locationId"` |
| 354 |
OStype string `json:"osType"` |
| 355 |
OS string `json:"os"` |
| 356 |
Status string `json:"status"` |
| 357 |
Details struct { |
| 358 |
IPaddresses []struct { |
| 359 |
Internal string `json:"internal"` |
| 360 |
Public string `json:"public"` |
| 361 |
} `json:"ipAddresses"` |
| 362 |
AlertPolicies []struct { |
| 363 |
ID string `json:"id"` |
| 364 |
Name string `json:"name"` |
| 365 |
Links api.Links `json:"links"` |
| 366 |
} `json:"alertPolicies"` |
| 367 |
CPU int `json:"cpu"` |
| 368 |
Diskcount int `json:"diskCount"` |
| 369 |
Hostname string `json:"hostName"` |
| 370 |
InMaintenanceMode bool `json:"inMaintenanceMode"` |
| 371 |
MemoryMB int `json:"memoryMB"` |
| 372 |
Powerstate string `json:"powerState"` |
| 373 |
Storagegb int `json:"storageGB"` |
| 374 |
Disks []struct { |
| 375 |
ID string `json:"id"` |
| 376 |
SizeGB int `json:"sizeGB"` |
| 377 |
PartitionPaths []interface{} `json:"partitionPaths"` |
| 378 |
} `json:"disks"` |
| 379 |
Partitions []struct { |
| 380 |
SizeGB float64 `json:"sizeGB"` |
| 381 |
Path string `json:"path"` |
| 382 |
} `json:"partitions"` |
| 383 |
Snapshots []struct { |
| 384 |
Name string `json:"name"` |
| 385 |
Links api.Links `json:"links"` |
| 386 |
} `json:"snapshots"` |
| 387 |
Customfields []api.Customfields `json:"customFields,omitempty"` |
| 388 |
} `json:"details"` |
| 389 |
Type string `json:"type"` |
| 390 |
Storagetype string `json:"storageType"` |
| 391 |
ChangeInfo struct { |
| 392 |
CreatedDate string `json:"createdDate"` |
| 393 |
CreatedBy string `json:"createdBy"` |
| 394 |
ModifiedDate string `json:"modifiedDate"` |
| 395 |
ModifiedBy string `json:"modifiedBy"` |
| 396 |
} `json:"changeInfo"` |
| 397 |
Links api.Links `json:"links"` |
| 398 |
} |