github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/edgeworkers/properties.go (about)

     1  package edgeworkers
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"net/http"
     8  	"net/url"
     9  	"strconv"
    10  
    11  	validation "github.com/go-ozzo/ozzo-validation/v4"
    12  )
    13  
    14  type (
    15  	// Properties is an edgeworkers properties API interface
    16  	Properties interface {
    17  		// ListProperties lists all properties for a given edgeworker ID
    18  		//
    19  		// See: https://techdocs.akamai.com/edgeworkers/reference/get-properties
    20  		ListProperties(context.Context, ListPropertiesRequest) (*ListPropertiesResponse, error)
    21  	}
    22  
    23  	// ListPropertiesRequest contains parameters used to list properties
    24  	ListPropertiesRequest struct {
    25  		EdgeWorkerID int
    26  		ActiveOnly   bool
    27  	}
    28  
    29  	// ListPropertiesResponse represents a response object returned by ListPropertiesRequest
    30  	ListPropertiesResponse struct {
    31  		Properties                []Property `json:"properties"`
    32  		LimitedAccessToProperties bool       `json:"limitedAccessToProperties"`
    33  	}
    34  
    35  	// Property represents a single property object
    36  	Property struct {
    37  		ID                int64  `json:"propertyId"`
    38  		Name              string `json:"propertyName"`
    39  		StagingVersion    int    `json:"stagingVersion"`
    40  		ProductionVersion int    `json:"productionVersion"`
    41  		LatestVersion     int    `json:"latestVersion"`
    42  	}
    43  )
    44  
    45  // Validate validates ListPropertiesRequest
    46  func (r ListPropertiesRequest) Validate() error {
    47  	return validation.Errors{
    48  		"EdgeWorkerID": validation.Validate(r.EdgeWorkerID, validation.Required),
    49  	}.Filter()
    50  }
    51  
    52  var (
    53  	// ErrListProperties is returned in case an error occurs on ListProperties operation
    54  	ErrListProperties = errors.New("list properties")
    55  )
    56  
    57  func (e *edgeworkers) ListProperties(ctx context.Context, params ListPropertiesRequest) (*ListPropertiesResponse, error) {
    58  	logger := e.Log(ctx)
    59  	logger.Debug("ListProperies")
    60  
    61  	if err := params.Validate(); err != nil {
    62  		return nil, fmt.Errorf("%s: %w: %s", ErrListProperties, ErrStructValidation, err)
    63  	}
    64  
    65  	uri, err := url.Parse(fmt.Sprintf("/edgeworkers/v1/ids/%d/properties", params.EdgeWorkerID))
    66  	if err != nil {
    67  		return nil, fmt.Errorf("%w: failed to parse url: %s", ErrListProperties, err)
    68  	}
    69  
    70  	q := uri.Query()
    71  	q.Add("activeOnly", strconv.FormatBool(params.ActiveOnly))
    72  	uri.RawQuery = q.Encode()
    73  
    74  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
    75  	if err != nil {
    76  		return nil, fmt.Errorf("%w: failed to create request: %s", ErrListProperties, err)
    77  	}
    78  
    79  	var result ListPropertiesResponse
    80  	resp, err := e.Exec(req, &result)
    81  	if err != nil {
    82  		return nil, fmt.Errorf("%w: request failed: %s", ErrListProperties, err)
    83  	}
    84  
    85  	if resp.StatusCode != http.StatusOK {
    86  		return nil, fmt.Errorf("%s: %w", ErrListProperties, e.Error(resp))
    87  	}
    88  
    89  	return &result, nil
    90  }