-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathcapacity.go
101 lines (83 loc) · 2.7 KB
/
capacity.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
package cloudwrapper
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/session"
)
type (
// ListCapacitiesRequest is a request struct
ListCapacitiesRequest struct {
ContractIDs []string
}
// ListCapacitiesResponse contains response list of location capacities
ListCapacitiesResponse struct {
Capacities []LocationCapacity `json:"capacities"`
}
// LocationCapacity contains location capacity information
LocationCapacity struct {
LocationID int `json:"locationId"`
LocationName string `json:"locationName"`
ContractID string `json:"contractId"`
Type CapacityType `json:"type"`
ApprovedCapacity Capacity `json:"approvedCapacity"`
AssignedCapacity Capacity `json:"assignedCapacity"`
UnassignedCapacity Capacity `json:"unassignedCapacity"`
}
// CapacityType is a type of the properties, this capacity is related to
CapacityType string
// Capacity struct holds capacity information
Capacity struct {
Value int64 `json:"value"`
Unit Unit `json:"unit"`
}
// Unit type of capacity value. Can be either GB or TB
Unit string
)
const (
// UnitGB is a const value for capacity unit
UnitGB Unit = "GB"
// UnitTB is a const value for capacity unit
UnitTB Unit = "TB"
)
const (
// CapacityTypeMedia type
CapacityTypeMedia CapacityType = "MEDIA"
// CapacityTypeWebStandardTLS type
CapacityTypeWebStandardTLS CapacityType = "WEB_STANDARD_TLS"
// CapacityTypeWebEnhancedTLS type
CapacityTypeWebEnhancedTLS CapacityType = "WEB_ENHANCED_TLS"
)
var (
// ErrListCapacities is returned in case an error occurs on ListCapacities operation
ErrListCapacities = errors.New("list capacities")
)
func (c *cloudwrapper) ListCapacities(ctx context.Context, params ListCapacitiesRequest) (*ListCapacitiesResponse, error) {
logger := c.Log(ctx)
logger.Debug("ListCapacities")
uri, err := url.Parse("/cloud-wrapper/v1/capacity")
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrListCapacities, err)
}
q := uri.Query()
for _, contractID := range params.ContractIDs {
q.Add("contractIds", contractID)
}
uri.RawQuery = q.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrListCapacities, err)
}
var result ListCapacitiesResponse
resp, err := c.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListCapacities, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListCapacities, c.Error(resp))
}
return &result, nil
}