-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathproperties.go
204 lines (168 loc) · 7.09 KB
/
properties.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package cloudaccess
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/edgegriderr"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/session"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type (
// LookupPropertiesRequest holds parameters for LookupProperties
LookupPropertiesRequest struct {
AccessKeyUID int64
Version int64
}
// LookupPropertiesResponse contains response for LookupProperties
LookupPropertiesResponse struct {
Properties []Property `json:"properties"`
}
// Property holds information about property related to given access key
Property struct {
AccessKeyUID int64 `json:"accessKeyUid"`
Version int64 `json:"version"`
PropertyID string `json:"propertyId"`
PropertyName string `json:"propertyName"`
ProductionVersion *int64 `json:"productionVersion"`
StagingVersion *int64 `json:"stagingVersion"`
}
// GetAsyncPropertiesLookupIDRequest holds parameters for GetAsyncPropertiesLookupID
GetAsyncPropertiesLookupIDRequest struct {
AccessKeyUID int64
Version int64
}
// GetAsyncPropertiesLookupIDResponse contains response for GetAsyncPropertiesLookupID
GetAsyncPropertiesLookupIDResponse struct {
LookupID int64 `json:"lookupId"`
RetryAfter int64 `json:"retryAfter"`
}
// PerformAsyncPropertiesLookupRequest holds parameters for PerformAsyncPropertiesLookup
PerformAsyncPropertiesLookupRequest struct {
LookupID int64
}
// PerformAsyncPropertiesLookupResponse contains response for PerformAsyncPropertiesLookup
PerformAsyncPropertiesLookupResponse struct {
LookupID int64 `json:"lookupId"`
LookupStatus LookupStatus `json:"lookupStatus"`
Properties []Property `json:"properties"`
}
// LookupStatus represents a lookup status
LookupStatus string
)
const (
// LookupComplete represents complete asynchronous property lookup status
LookupComplete LookupStatus = "COMPLETE"
// LookupError represents error asynchronous property lookup status
LookupError LookupStatus = "ERROR"
// LookupInProgress represents in progress asynchronous property lookup status
LookupInProgress LookupStatus = "IN_PROGRESS"
// LookupPending represents pending asynchronous property lookup status
LookupPending LookupStatus = "PENDING"
// LookupSubmitted represents submitted asynchronous property lookup status
LookupSubmitted LookupStatus = "SUBMITTED"
)
// Validate validates LookupPropertiesRequest
func (r LookupPropertiesRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"Version": validation.Validate(r.Version, validation.Required),
"AccessKeyUID": validation.Validate(r.AccessKeyUID, validation.Required),
})
}
// Validate validates GetAsyncPropertiesLookupIDRequest
func (r GetAsyncPropertiesLookupIDRequest) Validate() interface{} {
return edgegriderr.ParseValidationErrors(validation.Errors{
"Version": validation.Validate(r.Version, validation.Required),
"AccessKeyUID": validation.Validate(r.AccessKeyUID, validation.Required),
})
}
// Validate validates PerformAsyncPropertiesLookupRequest
func (r PerformAsyncPropertiesLookupRequest) Validate() interface{} {
return edgegriderr.ParseValidationErrors(validation.Errors{
"LookupID": validation.Validate(r.LookupID, validation.Required),
})
}
var (
// ErrLookupProperties is returned when LookupProperties fails
ErrLookupProperties = errors.New("lookup properties")
// ErrGetAsyncLookupIDProperties is returned when GetAsyncPropertiesLookupID fails
ErrGetAsyncLookupIDProperties = errors.New("get lookup properties id async")
// ErrPerformAsyncLookupProperties is returned when PerformAsyncPropertiesLookup fails
ErrPerformAsyncLookupProperties = errors.New("perform async lookup properties")
)
func (c *cloudaccess) LookupProperties(ctx context.Context, params LookupPropertiesRequest) (*LookupPropertiesResponse, error) {
logger := c.Log(ctx)
logger.Debug("LookupProperties")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrLookupProperties, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/cam/v1/access-keys/%d/versions/%d/properties", params.AccessKeyUID, params.Version))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrLookupProperties, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrLookupProperties, err)
}
var result LookupPropertiesResponse
resp, err := c.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrLookupProperties, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrLookupProperties, c.Error(resp))
}
return &result, nil
}
func (c *cloudaccess) GetAsyncPropertiesLookupID(ctx context.Context, params GetAsyncPropertiesLookupIDRequest) (*GetAsyncPropertiesLookupIDResponse, error) {
logger := c.Log(ctx)
logger.Debug("GetAsyncPropertiesLookupID")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrGetAsyncLookupIDProperties, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/cam/v1/access-keys/%d/versions/%d/property-lookup-id", params.AccessKeyUID, params.Version))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrGetAsyncLookupIDProperties, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetAsyncLookupIDProperties, err)
}
var result GetAsyncPropertiesLookupIDResponse
resp, err := c.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetAsyncLookupIDProperties, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusAccepted {
return nil, fmt.Errorf("%s: %w", ErrGetAsyncLookupIDProperties, c.Error(resp))
}
return &result, nil
}
func (c *cloudaccess) PerformAsyncPropertiesLookup(ctx context.Context, params PerformAsyncPropertiesLookupRequest) (*PerformAsyncPropertiesLookupResponse, error) {
logger := c.Log(ctx)
logger.Debug("PerformAsyncPropertiesLookup")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrPerformAsyncLookupProperties, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/cam/v1/property-lookups/%d", params.LookupID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrPerformAsyncLookupProperties, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrPerformAsyncLookupProperties, err)
}
var result PerformAsyncPropertiesLookupResponse
resp, err := c.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrPerformAsyncLookupProperties, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrPerformAsyncLookupProperties, c.Error(resp))
}
return &result, nil
}