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

     1  package appsec
     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 VersionNotes interface supports retrieving and modifying the version notes for a configuration and version.
    13  	//
    14  	// https://developer.akamai.com/api/cloud_security/application_security/v1.html#versionnotesgroup
    15  	VersionNotes interface {
    16  		// https://developer.akamai.com/api/cloud_security/application_security/v1.html#getversionnotes
    17  		GetVersionNotes(ctx context.Context, params GetVersionNotesRequest) (*GetVersionNotesResponse, error)
    18  
    19  		// https://developer.akamai.com/api/cloud_security/application_security/v1.html#putversionnotes
    20  		UpdateVersionNotes(ctx context.Context, params UpdateVersionNotesRequest) (*UpdateVersionNotesResponse, error)
    21  	}
    22  
    23  	// GetVersionNotesRequest is used to retrieve the version notes for a configuration version.
    24  	GetVersionNotesRequest struct {
    25  		ConfigID int `json:"-"`
    26  		Version  int `json:"-"`
    27  	}
    28  
    29  	// GetVersionNotesResponse is returned from a call to GetVersionNotes.
    30  	GetVersionNotesResponse struct {
    31  		Notes string `json:"notes"`
    32  	}
    33  
    34  	// UpdateVersionNotesRequest is used to modify the version notes for a configuration version.
    35  	UpdateVersionNotesRequest struct {
    36  		ConfigID int `json:"-"`
    37  		Version  int `json:"-"`
    38  
    39  		Notes string `json:"notes"`
    40  	}
    41  
    42  	// UpdateVersionNotesResponse is returned from a call to UpdateVersionNotes.
    43  	UpdateVersionNotesResponse struct {
    44  		Notes string `json:"notes"`
    45  	}
    46  )
    47  
    48  // Validate validates a GetVersionNotesRequest.
    49  func (v GetVersionNotesRequest) Validate() error {
    50  	return validation.Errors{
    51  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    52  		"Version":  validation.Validate(v.Version, validation.Required),
    53  	}.Filter()
    54  }
    55  
    56  // Validate validates an UpdateVersionNotesRequest.
    57  func (v UpdateVersionNotesRequest) Validate() error {
    58  	return validation.Errors{
    59  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    60  		"Version":  validation.Validate(v.Version, validation.Required),
    61  	}.Filter()
    62  }
    63  
    64  func (p *appsec) GetVersionNotes(ctx context.Context, params GetVersionNotesRequest) (*GetVersionNotesResponse, error) {
    65  	logger := p.Log(ctx)
    66  	logger.Debug("GetVersionNotes")
    67  
    68  	if err := params.Validate(); err != nil {
    69  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
    70  	}
    71  
    72  	uri := fmt.Sprintf(
    73  		"/appsec/v1/configs/%d/versions/%d/version-notes",
    74  		params.ConfigID,
    75  		params.Version,
    76  	)
    77  
    78  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
    79  	if err != nil {
    80  		return nil, fmt.Errorf("failed to create GetVersionNotes request: %w", err)
    81  	}
    82  
    83  	var result GetVersionNotesResponse
    84  	resp, err := p.Exec(req, &result)
    85  	if err != nil {
    86  		return nil, fmt.Errorf("get version notes request failed: %w", err)
    87  	}
    88  	if resp.StatusCode != http.StatusOK {
    89  		return nil, p.Error(resp)
    90  	}
    91  
    92  	return &result, nil
    93  }
    94  
    95  func (p *appsec) UpdateVersionNotes(ctx context.Context, params UpdateVersionNotesRequest) (*UpdateVersionNotesResponse, error) {
    96  	logger := p.Log(ctx)
    97  	logger.Debug("UpdateVersionNotes")
    98  
    99  	if err := params.Validate(); err != nil {
   100  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   101  	}
   102  
   103  	uri := fmt.Sprintf(
   104  		"/appsec/v1/configs/%d/versions/%d/version-notes",
   105  		params.ConfigID,
   106  		params.Version,
   107  	)
   108  
   109  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   110  	if err != nil {
   111  		return nil, fmt.Errorf("failed to create UpdateVersionNotes request: %w", err)
   112  	}
   113  
   114  	var result UpdateVersionNotesResponse
   115  	resp, err := p.Exec(req, &result, params)
   116  	if err != nil {
   117  		return nil, fmt.Errorf("update version notes request failed: %w", err)
   118  	}
   119  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   120  		return nil, p.Error(resp)
   121  	}
   122  
   123  	return &result, nil
   124  }