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

     1  package networklists
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	validation "github.com/go-ozzo/ozzo-validation/v4"
     9  )
    10  
    11  type (
    12  	// The NetworkListDescription interface supports retrieving and updating a network list's description.
    13  	//
    14  	// https://developer.akamai.com/api/cloud_security/network_lists/v2.html#networklist
    15  	NetworkListDescription interface {
    16  		// https://developer.akamai.com/api/cloud_security/network_lists/v2.html#getlist
    17  		GetNetworkListDescription(ctx context.Context, params GetNetworkListDescriptionRequest) (*GetNetworkListDescriptionResponse, error)
    18  
    19  		// https://developer.akamai.com/api/cloud_security/network_lists/v2.html#putdetails
    20  		UpdateNetworkListDescription(ctx context.Context, params UpdateNetworkListDescriptionRequest) (*UpdateNetworkListDescriptionResponse, error)
    21  	}
    22  
    23  	// GetNetworkListDescriptionRequest contains request parameters for GetNetworkListDescription method
    24  	GetNetworkListDescriptionRequest struct {
    25  		UniqueID    string `json:"uniqueId"`
    26  		Name        string `json:"name"`
    27  		Description string `json:"description"`
    28  	}
    29  
    30  	// GetNetworkListDescriptionResponse contains response from GetNetworkListDescription method
    31  	GetNetworkListDescriptionResponse struct {
    32  		Name            string   `json:"name"`
    33  		UniqueID        string   `json:"uniqueId"`
    34  		Description     string   `json:"description"`
    35  		SyncPoint       int      `json:"syncPoint"`
    36  		Type            string   `json:"type"`
    37  		NetworkListType string   `json:"networkListType"`
    38  		ElementCount    int      `json:"elementCount"`
    39  		ReadOnly        bool     `json:"readOnly"`
    40  		Shared          bool     `json:"shared"`
    41  		List            []string `json:"list"`
    42  		Links           struct {
    43  			ActivateInProduction struct {
    44  				Href   string `json:"href"`
    45  				Method string `json:"method"`
    46  			} `json:"activateInProduction"`
    47  			ActivateInStaging struct {
    48  				Href   string `json:"href"`
    49  				Method string `json:"method"`
    50  			} `json:"activateInStaging"`
    51  			AppendItems struct {
    52  				Href   string `json:"href"`
    53  				Method string `json:"method"`
    54  			} `json:"appendItems"`
    55  			Retrieve struct {
    56  				Href string `json:"href"`
    57  			} `json:"retrieve"`
    58  			StatusInProduction struct {
    59  				Href string `json:"href"`
    60  			} `json:"statusInProduction"`
    61  			StatusInStaging struct {
    62  				Href string `json:"href"`
    63  			} `json:"statusInStaging"`
    64  			Update struct {
    65  				Href   string `json:"href"`
    66  				Method string `json:"method"`
    67  			} `json:"update"`
    68  		} `json:"links"`
    69  	}
    70  
    71  	// UpdateNetworkListDescriptionRequest contains request parameters for UpdateNetworkListDescription method
    72  	UpdateNetworkListDescriptionRequest struct {
    73  		UniqueID    string `json:"-"`
    74  		Name        string `json:"name"`
    75  		Description string `json:"description"`
    76  	}
    77  
    78  	// UpdateNetworkListDescriptionResponse contains response from UpdateNetworkListDescription method
    79  	UpdateNetworkListDescriptionResponse struct {
    80  		Empty string `json:"-"`
    81  	}
    82  )
    83  
    84  // Validate validates GetNetworkListDescriptionRequest
    85  func (v GetNetworkListDescriptionRequest) Validate() error {
    86  	return validation.Errors{
    87  		"UniqueID": validation.Validate(v.UniqueID, validation.Required),
    88  	}.Filter()
    89  }
    90  
    91  // Validate validates UpdateNetworkListDescriptionRequest
    92  func (v UpdateNetworkListDescriptionRequest) Validate() error {
    93  	return validation.Errors{
    94  		"UniqueID": validation.Validate(v.UniqueID, validation.Required),
    95  	}.Filter()
    96  }
    97  
    98  func (p *networklists) GetNetworkListDescription(ctx context.Context, params GetNetworkListDescriptionRequest) (*GetNetworkListDescriptionResponse, error) {
    99  	if err := params.Validate(); err != nil {
   100  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   101  	}
   102  
   103  	logger := p.Log(ctx)
   104  	logger.Debug("GetNetworkListDescription")
   105  
   106  	var rval GetNetworkListDescriptionResponse
   107  
   108  	uri := fmt.Sprintf(
   109  		"/network-list/v2/network-lists/%s",
   110  		params.UniqueID)
   111  
   112  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   113  	if err != nil {
   114  		return nil, fmt.Errorf("failed to create getnetworklistdescription request: %s", err.Error())
   115  	}
   116  
   117  	resp, err := p.Exec(req, &rval)
   118  	if err != nil {
   119  		return nil, fmt.Errorf("getnetworklistdescription  request failed: %s", err.Error())
   120  	}
   121  
   122  	if resp.StatusCode != http.StatusOK {
   123  		return nil, p.Error(resp)
   124  	}
   125  
   126  	return &rval, nil
   127  
   128  }
   129  
   130  // Update will update a NetworkListDescription.
   131  //
   132  // API Docs: // network_lists v2
   133  //
   134  // https://developer.akamai.com/api/cloud_security/network_lists/v2.html#putnetworklistdescription
   135  
   136  func (p *networklists) UpdateNetworkListDescription(ctx context.Context, params UpdateNetworkListDescriptionRequest) (*UpdateNetworkListDescriptionResponse, error) {
   137  	if err := params.Validate(); err != nil {
   138  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   139  	}
   140  
   141  	logger := p.Log(ctx)
   142  	logger.Debug("UpdateNetworkListDescription")
   143  
   144  	putURL := fmt.Sprintf(
   145  		"/network-list/v2/network-lists/%s/details",
   146  		params.UniqueID,
   147  	)
   148  
   149  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, putURL, nil)
   150  	if err != nil {
   151  		return nil, fmt.Errorf("failed to create create NetworkListDescriptionrequest: %s", err.Error())
   152  	}
   153  
   154  	var rval UpdateNetworkListDescriptionResponse
   155  	resp, err := p.Exec(req, &rval, params)
   156  	if err != nil {
   157  		return nil, fmt.Errorf("create NetworkListDescription request failed: %s", err.Error())
   158  	}
   159  
   160  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusNoContent {
   161  		return nil, p.Error(resp)
   162  	}
   163  
   164  	return &rval, nil
   165  }