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

     1  package cloudwrapper
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"net/http"
     8  	"net/url"
     9  	"strconv"
    10  
    11  	"github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/edgegriderr"
    12  	validation "github.com/go-ozzo/ozzo-validation/v4"
    13  )
    14  
    15  type (
    16  	// Properties is a CloudWrapper properties API interface
    17  	Properties interface {
    18  		// ListProperties lists unused properties
    19  		//
    20  		// See: https://techdocs.akamai.com/cloud-wrapper/reference/get-properties
    21  		ListProperties(context.Context, ListPropertiesRequest) (*ListPropertiesResponse, error)
    22  		// ListOrigins lists property origins
    23  		//
    24  		// See: https://techdocs.akamai.com/cloud-wrapper/reference/get-origins
    25  		ListOrigins(context.Context, ListOriginsRequest) (*ListOriginsResponse, error)
    26  	}
    27  
    28  	// ListPropertiesRequest holds parameters for ListProperties
    29  	ListPropertiesRequest struct {
    30  		Unused      bool
    31  		ContractIDs []string
    32  	}
    33  
    34  	// ListOriginsRequest holds parameters for ListOrigins
    35  	ListOriginsRequest struct {
    36  		PropertyID int64
    37  		ContractID string
    38  		GroupID    int64
    39  	}
    40  
    41  	// ListPropertiesResponse contains response from ListProperties
    42  	ListPropertiesResponse struct {
    43  		Properties []Property `json:"properties"`
    44  	}
    45  
    46  	// ListOriginsResponse contains response from ListOrigins
    47  	ListOriginsResponse struct {
    48  		Children []Child    `json:"children"`
    49  		Default  []Behavior `json:"default"`
    50  	}
    51  
    52  	// Child represents children rules in a property
    53  	Child struct {
    54  		Name      string     `json:"name"`
    55  		Behaviors []Behavior `json:"behaviors"`
    56  	}
    57  
    58  	// Behavior contains behavior information
    59  	Behavior struct {
    60  		Hostname   string     `json:"hostname"`
    61  		OriginType OriginType `json:"originType"`
    62  	}
    63  
    64  	// Property represents property object
    65  	Property struct {
    66  		GroupID      int64        `json:"groupId"`
    67  		ContractID   string       `json:"contractId"`
    68  		PropertyID   int64        `json:"propertyId"`
    69  		PropertyName string       `json:"propertyName"`
    70  		Type         PropertyType `json:"type"`
    71  	}
    72  
    73  	// OriginType represents the type of the origin
    74  	OriginType string
    75  
    76  	// PropertyType represents the type of the property
    77  	PropertyType string
    78  )
    79  
    80  const (
    81  	// PropertyTypeWeb is the web type of the property
    82  	PropertyTypeWeb PropertyType = "WEB"
    83  	// PropertyTypeMedia is the media type of the property
    84  	PropertyTypeMedia PropertyType = "MEDIA"
    85  	// OriginTypeCustomer is the customer type of the origin
    86  	OriginTypeCustomer OriginType = "CUSTOMER"
    87  	// OriginTypeNetStorage is the net storage type of the origin
    88  	OriginTypeNetStorage OriginType = "NET_STORAGE"
    89  )
    90  
    91  // Validate validates ListOriginsRequest
    92  func (r ListOriginsRequest) Validate() error {
    93  	return edgegriderr.ParseValidationErrors(validation.Errors{
    94  		"PropertyID": validation.Validate(r.PropertyID, validation.Required),
    95  		"ContractID": validation.Validate(r.ContractID, validation.Required),
    96  		"GroupID":    validation.Validate(r.GroupID, validation.Required),
    97  	})
    98  }
    99  
   100  var (
   101  	// ErrListProperties is returned when ListProperties fails
   102  	ErrListProperties = errors.New("list properties")
   103  	// ErrListOrigins is returned when ListOrigins fails
   104  	ErrListOrigins = errors.New("list origins")
   105  )
   106  
   107  func (c *cloudwrapper) ListProperties(ctx context.Context, params ListPropertiesRequest) (*ListPropertiesResponse, error) {
   108  	logger := c.Log(ctx)
   109  	logger.Debug("ListProperties")
   110  
   111  	uri, err := url.Parse("/cloud-wrapper/v1/properties")
   112  	if err != nil {
   113  		return nil, fmt.Errorf("%w: failed to parse url: %s", ErrListProperties, err)
   114  	}
   115  
   116  	q := uri.Query()
   117  	q.Add("unused", strconv.FormatBool(params.Unused))
   118  	for _, ctr := range params.ContractIDs {
   119  		q.Add("contractIds", ctr)
   120  	}
   121  	uri.RawQuery = q.Encode()
   122  
   123  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
   124  	if err != nil {
   125  		return nil, fmt.Errorf("%w: failed to create request: %s", ErrListProperties, err)
   126  	}
   127  
   128  	var result ListPropertiesResponse
   129  	resp, err := c.Exec(req, &result)
   130  	if err != nil {
   131  		return nil, fmt.Errorf("%w: request failed: %s", ErrListProperties, err)
   132  	}
   133  
   134  	if resp.StatusCode != http.StatusOK {
   135  		return nil, fmt.Errorf("%s: %w", ErrListProperties, c.Error(resp))
   136  	}
   137  
   138  	return &result, nil
   139  }
   140  
   141  func (c *cloudwrapper) ListOrigins(ctx context.Context, params ListOriginsRequest) (*ListOriginsResponse, error) {
   142  	logger := c.Log(ctx)
   143  	logger.Debug("ListOrigins")
   144  
   145  	if err := params.Validate(); err != nil {
   146  		return nil, fmt.Errorf("%s: %w: %s", ErrListOrigins, ErrStructValidation, err)
   147  	}
   148  
   149  	uri, err := url.Parse(fmt.Sprintf("/cloud-wrapper/v1/properties/%d/origins", params.PropertyID))
   150  	if err != nil {
   151  		return nil, fmt.Errorf("%w: failed to parse url: %s", ErrListOrigins, err)
   152  	}
   153  
   154  	q := uri.Query()
   155  	q.Add("contractId", params.ContractID)
   156  	q.Add("groupId", strconv.FormatInt(params.GroupID, 10))
   157  	uri.RawQuery = q.Encode()
   158  
   159  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
   160  	if err != nil {
   161  		return nil, fmt.Errorf("%w: failed to create request: %s", ErrListOrigins, err)
   162  	}
   163  
   164  	var result ListOriginsResponse
   165  	resp, err := c.Exec(req, &result)
   166  	if err != nil {
   167  		return nil, fmt.Errorf("%w: request failed: %s", ErrListOrigins, err)
   168  	}
   169  
   170  	if resp.StatusCode != http.StatusOK {
   171  		return nil, fmt.Errorf("%s: %w", ErrListOrigins, c.Error(resp))
   172  	}
   173  
   174  	return &result, nil
   175  }