-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathsearch.go
99 lines (84 loc) · 2.91 KB
/
search.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
package papi
import (
"context"
"errors"
"fmt"
"net/http"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/session"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type (
// SearchResponse contains response body of POST /search request
SearchResponse struct {
Versions SearchItems `json:"versions"`
}
// SearchItems contains a list of search results
SearchItems struct {
Items []SearchItem `json:"items"`
}
// SearchItem contains details of a search result
SearchItem struct {
AccountID string `json:"accountId"`
AssetID string `json:"assetId"`
ContractID string `json:"contractId"`
EdgeHostname string `json:"edgeHostname"`
GroupID string `json:"groupId"`
Hostname string `json:"hostname"`
ProductionStatus string `json:"productionStatus"`
PropertyID string `json:"propertyId"`
PropertyName string `json:"propertyName"`
PropertyVersion int `json:"propertyVersion"`
StagingStatus string `json:"stagingStatus"`
UpdatedByUser string `json:"updatedByUser"`
UpdatedDate string `json:"updatedDate"`
}
// SearchRequest contains key-value pair for search request
// Key must have one of three values: "edgeHostname", "hostname" or "propertyName"
SearchRequest struct {
Key string
Value string
}
)
const (
// SearchKeyEdgeHostname search request key
SearchKeyEdgeHostname = "edgeHostname"
// SearchKeyHostname search request key
SearchKeyHostname = "hostname"
// SearchKeyPropertyName search request key
SearchKeyPropertyName = "propertyName"
)
// Validate validate SearchRequest struct
func (s SearchRequest) Validate() error {
return validation.Errors{
"SearchKey": validation.Validate(s.Key,
validation.Required,
validation.In(SearchKeyEdgeHostname, SearchKeyHostname, SearchKeyPropertyName)),
"SearchValue": validation.Validate(s.Value, validation.Required),
}.Filter()
}
var (
// ErrSearchProperties represents error when searching for properties fails
ErrSearchProperties = errors.New("searching for properties")
)
func (p *papi) SearchProperties(ctx context.Context, request SearchRequest) (*SearchResponse, error) {
if err := request.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrSearchProperties, ErrStructValidation, err)
}
logger := p.Log(ctx)
logger.Debug("SearchProperties")
searchURL := "/papi/v1/search/find-by-value"
req, err := http.NewRequestWithContext(ctx, http.MethodPost, searchURL, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrSearchProperties, err)
}
var search SearchResponse
resp, err := p.Exec(req, &search, map[string]string{request.Key: request.Value})
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrSearchProperties, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrSearchProperties, p.Error(resp))
}
return &search, nil
}