github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.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/v8/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://techdocs.akamai.com/cloudlets/v2/reference/get-policy-properties
    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  func (c *cloudlets) GetPolicyProperties(ctx context.Context, params GetPolicyPropertiesRequest) (map[string]PolicyProperty, error) {
    86  	logger := c.Log(ctx)
    87  	logger.Debug("GetPolicyProperties")
    88  
    89  	uri, err := url.Parse(fmt.Sprintf("/cloudlets/api/v2/policies/%d/properties", params.PolicyID))
    90  	if err != nil {
    91  		return nil, fmt.Errorf("%w: failed to parse url: %s", ErrGetPolicyProperties, err)
    92  	}
    93  
    94  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
    95  	if err != nil {
    96  		return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetPolicyProperties, err)
    97  	}
    98  
    99  	var result map[string]PolicyProperty
   100  	resp, err := c.Exec(req, &result)
   101  	if err != nil {
   102  		return nil, fmt.Errorf("%w: request failed: %s", ErrGetPolicyProperties, err)
   103  	}
   104  
   105  	if resp.StatusCode != http.StatusOK {
   106  		return nil, fmt.Errorf("%s: %w", ErrGetPolicyProperties, c.Error(resp))
   107  	}
   108  
   109  	return result, nil
   110  }
   111  
   112  func (c *cloudlets) DeletePolicyProperty(ctx context.Context, params DeletePolicyPropertyRequest) error {
   113  	c.Log(ctx).Debug("DeletePolicyProperty")
   114  
   115  	if err := params.Validate(); err != nil {
   116  		return fmt.Errorf("%s: %w:\n%s", ErrDeletePolicyProperty, ErrStructValidation, err)
   117  	}
   118  
   119  	uri, err := url.Parse(fmt.Sprintf("/cloudlets/api/v2/policies/%d/properties/%d", params.PolicyID, params.PropertyID))
   120  	if err != nil {
   121  		return fmt.Errorf("%w: failed to parse url: %s", ErrDeletePolicyProperty, err.Error())
   122  	}
   123  
   124  	q := uri.Query()
   125  	q.Set("async", "true")
   126  	if params.Network != "" {
   127  		q.Set("network", string(params.Network))
   128  	}
   129  	uri.RawQuery = q.Encode()
   130  
   131  	req, err := http.NewRequestWithContext(ctx, http.MethodDelete, uri.String(), nil)
   132  	if err != nil {
   133  		return fmt.Errorf("%w: failed to create request: %s", ErrDeletePolicyProperty, err)
   134  	}
   135  
   136  	resp, err := c.Exec(req, nil)
   137  	if err != nil {
   138  		return fmt.Errorf("%w: request failed: %s", ErrDeletePolicyProperty, err)
   139  	}
   140  
   141  	if resp.StatusCode != http.StatusNoContent {
   142  		return fmt.Errorf("%w: %d", ErrDeletePolicyProperty, resp.StatusCode)
   143  	}
   144  
   145  	return nil
   146  }