github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/papi/search.go (about) 1 package papi 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "net/http" 8 9 validation "github.com/go-ozzo/ozzo-validation/v4" 10 ) 11 12 type ( 13 // Search contains SearchProperty method used for fetching properties 14 Search interface { 15 // SearchProperties searches properties by name, or by the hostname or edge hostname for which it’s currently active 16 // 17 // See: https://techdocs.akamai.com/property-mgr/reference/post-search-find-by-value 18 SearchProperties(context.Context, SearchRequest) (*SearchResponse, error) 19 } 20 21 // SearchResponse contains response body of POST /search request 22 SearchResponse struct { 23 Versions SearchItems `json:"versions"` 24 } 25 26 // SearchItems contains a list of search results 27 SearchItems struct { 28 Items []SearchItem `json:"items"` 29 } 30 31 // SearchItem contains details of a search result 32 SearchItem struct { 33 AccountID string `json:"accountId"` 34 AssetID string `json:"assetId"` 35 ContractID string `json:"contractId"` 36 EdgeHostname string `json:"edgeHostname"` 37 GroupID string `json:"groupId"` 38 Hostname string `json:"hostname"` 39 ProductionStatus string `json:"productionStatus"` 40 PropertyID string `json:"propertyId"` 41 PropertyName string `json:"propertyName"` 42 PropertyVersion int `json:"propertyVersion"` 43 StagingStatus string `json:"stagingStatus"` 44 UpdatedByUser string `json:"updatedByUser"` 45 UpdatedDate string `json:"updatedDate"` 46 } 47 48 // SearchRequest contains key-value pair for search request 49 // Key must have one of three values: "edgeHostname", "hostname" or "propertyName" 50 SearchRequest struct { 51 Key string 52 Value string 53 } 54 ) 55 56 const ( 57 // SearchKeyEdgeHostname search request key 58 SearchKeyEdgeHostname = "edgeHostname" 59 // SearchKeyHostname search request key 60 SearchKeyHostname = "hostname" 61 // SearchKeyPropertyName search request key 62 SearchKeyPropertyName = "propertyName" 63 ) 64 65 // Validate validate SearchRequest struct 66 func (s SearchRequest) Validate() error { 67 return validation.Errors{ 68 "SearchKey": validation.Validate(s.Key, 69 validation.Required, 70 validation.In(SearchKeyEdgeHostname, SearchKeyHostname, SearchKeyPropertyName)), 71 "SearchValue": validation.Validate(s.Value, validation.Required), 72 }.Filter() 73 } 74 75 var ( 76 // ErrSearchProperties represents error when searching for properties fails 77 ErrSearchProperties = errors.New("searching for properties") 78 ) 79 80 func (p *papi) SearchProperties(ctx context.Context, request SearchRequest) (*SearchResponse, error) { 81 if err := request.Validate(); err != nil { 82 return nil, fmt.Errorf("%s: %w: %s", ErrSearchProperties, ErrStructValidation, err) 83 } 84 85 logger := p.Log(ctx) 86 logger.Debug("SearchProperties") 87 88 searchURL := "/papi/v1/search/find-by-value" 89 req, err := http.NewRequestWithContext(ctx, http.MethodPost, searchURL, nil) 90 if err != nil { 91 return nil, fmt.Errorf("%w: failed to create request: %s", ErrSearchProperties, err) 92 } 93 94 var search SearchResponse 95 resp, err := p.Exec(req, &search, map[string]string{request.Key: request.Value}) 96 if err != nil { 97 return nil, fmt.Errorf("%w: request failed: %s", ErrSearchProperties, err) 98 } 99 100 if resp.StatusCode != http.StatusOK { 101 return nil, fmt.Errorf("%s: %w", ErrSearchProperties, p.Error(resp)) 102 } 103 104 return &search, nil 105 }