github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/cloudlets/policy_property.go (about)

     1  package cloudlets
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"net/http"
     8  	"net/url"
     9  
    10  	"github.com/akamai/AkamaiOPEN-edgegrid-golang/v2/pkg/edgegriderr"
    11  
    12  	validation "github.com/go-ozzo/ozzo-validation/v4"
    13  )
    14  
    15  type (
    16  	// PolicyProperties interface is a cloudlets API interface for policy associated properties
    17  	PolicyProperties interface {
    18  		// GetPolicyProperties gets all the associated properties by the policyID
    19  		//
    20  		// See: https://developer.akamai.com/api/web_performance/cloudlets/v2.html#getpolicyproperties
    21  		GetPolicyProperties(context.Context, GetPolicyPropertiesRequest) (map[string]PolicyProperty, error)
    22  
    23  		// DeletePolicyProperty removes a property from a policy activation associated_properties list
    24  		DeletePolicyProperty(context.Context, DeletePolicyPropertyRequest) error
    25  	}
    26  
    27  	// GetPolicyPropertiesRequest contains request parameters for GetPolicyPropertiesRequest
    28  	GetPolicyPropertiesRequest struct {
    29  		PolicyID int64
    30  	}
    31  
    32  	// PolicyProperty contains the response data for a single property
    33  	PolicyProperty struct {
    34  		GroupID       int64         `json:"groupId"`
    35  		ID            int64         `json:"id"`
    36  		Name          string        `json:"name"`
    37  		NewestVersion NetworkStatus `json:"newestVersion"`
    38  		Production    NetworkStatus `json:"production"`
    39  		Staging       NetworkStatus `json:"staging"`
    40  	}
    41  
    42  	// NetworkStatus is the type for NetworkStatus of any activation
    43  	NetworkStatus struct {
    44  		ActivatedBy        string                     `json:"activatedBy"`
    45  		ActivationDate     string                     `json:"activationDate"`
    46  		Version            int64                      `json:"version"`
    47  		CloudletsOrigins   map[string]CloudletsOrigin `json:"cloudletsOrigins"`
    48  		ReferencedPolicies []string                   `json:"referencedPolicies"`
    49  	}
    50  
    51  	//nolint:revive
    52  	// CloudletsOrigin is the type for CloudletsOrigins in NetworkStatus
    53  	CloudletsOrigin struct {
    54  		OriginID    string     `json:"id"`
    55  		Hostname    string     `json:"hostname"`
    56  		Type        OriginType `json:"type"`
    57  		Checksum    string     `json:"checksum"`
    58  		Description string     `json:"description"`
    59  	}
    60  
    61  	// DeletePolicyPropertyRequest contains the request parameters for DeletePolicyProperty
    62  	DeletePolicyPropertyRequest struct {
    63  		PolicyID   int64
    64  		PropertyID int64
    65  		Network    PolicyActivationNetwork
    66  	}
    67  )
    68  
    69  var (
    70  	// ErrGetPolicyProperties is returned when GetPolicyProperties fails
    71  	ErrGetPolicyProperties = errors.New("get policy properties")
    72  	// ErrDeletePolicyProperty is returned when DeletePolicyProperty fails
    73  	ErrDeletePolicyProperty = errors.New("delete policy property")
    74  )
    75  
    76  // Validate validates DeletePolicyPropertyRequest
    77  func (r DeletePolicyPropertyRequest) Validate() error {
    78  	errs := validation.Errors{
    79  		"PolicyID":   validation.Validate(r.PolicyID, validation.Required),
    80  		"PropertyID": validation.Validate(r.PropertyID, validation.Required),
    81  	}
    82  	return edgegriderr.ParseValidationErrors(errs)
    83  }
    84  
    85  // GetPolicyProperties gets all the associated properties by the policyID
    86  func (c *cloudlets) GetPolicyProperties(ctx context.Context, params GetPolicyPropertiesRequest) (map[string]PolicyProperty, error) {
    87  	logger := c.Log(ctx)
    88  	logger.Debug("GetPolicyProperties")
    89  
    90  	uri, err := url.Parse(fmt.Sprintf("/cloudlets/api/v2/policies/%d/properties", params.PolicyID))
    91  	if err != nil {
    92  		return nil, fmt.Errorf("%w: failed to parse url: %s", ErrGetPolicyProperties, err)
    93  	}
    94  
    95  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
    96  	if err != nil {
    97  		return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetPolicyProperties, err)
    98  	}
    99  
   100  	var result map[string]PolicyProperty
   101  	resp, err := c.Exec(req, &result)
   102  	if err != nil {
   103  		return nil, fmt.Errorf("%w: request failed: %s", ErrGetPolicyProperties, err)
   104  	}
   105  
   106  	if resp.StatusCode != http.StatusOK {
   107  		return nil, fmt.Errorf("%s: %w", ErrGetPolicyProperties, c.Error(resp))
   108  	}
   109  
   110  	return result, nil
   111  }
   112  
   113  func (c *cloudlets) DeletePolicyProperty(ctx context.Context, params DeletePolicyPropertyRequest) error {
   114  	c.Log(ctx).Debug("DeletePolicyProperty")
   115  
   116  	if err := params.Validate(); err != nil {
   117  		return fmt.Errorf("%s: %w:\n%s", ErrDeletePolicyProperty, ErrStructValidation, err)
   118  	}
   119  
   120  	uri, err := url.Parse(fmt.Sprintf("/cloudlets/api/v2/policies/%d/properties/%d", params.PolicyID, params.PropertyID))
   121  	if err != nil {
   122  		return fmt.Errorf("%w: failed to parse url: %s", ErrDeletePolicyProperty, err.Error())
   123  	}
   124  
   125  	q := uri.Query()
   126  	q.Set("async", "true")
   127  	if params.Network != "" {
   128  		q.Set("network", string(params.Network))
   129  	}
   130  	uri.RawQuery = q.Encode()
   131  
   132  	req, err := http.NewRequestWithContext(ctx, http.MethodDelete, uri.String(), nil)
   133  	if err != nil {
   134  		return fmt.Errorf("%w: failed to create request: %s", ErrDeletePolicyProperty, err)
   135  	}
   136  
   137  	resp, err := c.Exec(req, nil)
   138  	if err != nil {
   139  		return fmt.Errorf("%w: request failed: %s", ErrDeletePolicyProperty, err)
   140  	}
   141  
   142  	if resp.StatusCode != http.StatusNoContent {
   143  		return fmt.Errorf("%w: %d", ErrDeletePolicyProperty, resp.StatusCode)
   144  	}
   145  
   146  	return nil
   147  }