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