Skip to content

Commit

Permalink
Merge pull request #9 in FEE/akamaiopen-edgegrid-golang from TFP-156-…
Browse files Browse the repository at this point in the history
…v2-edgehostnames to v2

* commit 'eddc5ada0061982ca61879bc0424ef1b14684cfa':
  [TFP-170] Add tests for edge hostnames
  [TFP-170] Add GET and POST EdgeHostname operations
  • Loading branch information
rodriguise committed Sep 15, 2020
2 parents 3ad5954 + eddc5ad commit 4af2390
Show file tree
Hide file tree
Showing 8 changed files with 1,301 additions and 81 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ go 1.12
require (
github.com/akamai/AkamaiOPEN-edgegrid-golang v0.9.18
github.com/apex/log v1.9.0
github.com/go-ozzo/ozzo-validation/v4 v4.2.2
github.com/google/go-querystring v1.0.0
github.com/google/uuid v1.1.1
github.com/mitchellh/go-homedir v1.1.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cast v1.3.1
github.com/stretchr/testify v1.6.1
github.com/tj/assert v0.0.3
github.com/xeipuuv/gojsonschema v1.2.0
gopkg.in/h2non/gock.v1 v1.0.15
gopkg.in/ini.v1 v1.51.1
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ github.com/apex/log v1.9.0/go.mod h1:m82fZlWIuiWzWP04XCTXmnX0xRkYYbCdYn8jbJeLBEA
github.com/apex/logs v1.0.0/go.mod h1:XzxuLZ5myVHDy9SAmYpamKKRNApGj54PfYLcFrXqDwo=
github.com/aphistic/golf v0.0.0-20180712155816-02c07f170c5a/go.mod h1:3NqKYiepwy8kCu4PNA+aP7WUV72eXWJeP9/r3/K9aLE=
github.com/aphistic/sweet v0.2.0/go.mod h1:fWDlIh/isSE9n6EPsRmC0det+whmX6dJid3stzu0Xys=
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
github.com/aws/aws-sdk-go v1.20.6/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59/go.mod h1:q/89r3U2H7sSsE2t6Kca0lfwTK8JdoNGS/yzM/4iH5I=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -13,6 +14,9 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible h1:msy24VGS42fKO9K1vLz82/GeYW1cILu7Nuuj1N3BBkE=
github.com/go-ozzo/ozzo-validation/v4 v4.2.2 h1:5uhbQAuRK6taB9orHJXA5GtOCuQbsHktskg8aWciC68=
github.com/go-ozzo/ozzo-validation/v4 v4.2.2/go.mod h1:2NKgrcHl3z6cJs+3Oo940FPRiTzuqKbvfrL2RxCj6Ew=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
Expand Down
97 changes: 47 additions & 50 deletions pkg/papi/cpcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package papi

import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"strings"

"github.com/akamai/AkamaiOPEN-edgegrid-golang/v2/pkg/papi/tools"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v2/pkg/session"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/spf13/cast"
"net/http"
)

type (
Expand Down Expand Up @@ -69,24 +67,44 @@ type (
}
)

var (
// ErrGroupEmpty is returned when a required 'groupId' param is missing from the request
ErrGroupEmpty = errors.New("provided group ID cannot be empty")
// ErrContractEmpty is returned when a required 'contractId' param is missing from the request
ErrContractEmpty = errors.New("provided contract ID cannot be empty")
// ErrIDEmpty is returned when a required resource ID param is missing from the request
ErrIDEmpty = errors.New("provided CP code ID cannot be empty")
// ErrInvalidLocation is returned when there was an error while fetching ID from location response object
ErrInvalidLocation = errors.New("response location URL is invalid")
)
// Validate validates GetCPCodesRequest
func (cp GetCPCodesRequest) Validate() error {
return validation.Errors{
"ContractID": validation.Validate(cp.ContractID, validation.Required),
"GroupID": validation.Validate(cp.GroupID, validation.Required),
}.Filter()
}

// Validate validates GetCPCodeRequest
func (cp GetCPCodeRequest) Validate() error {
return validation.Errors{
"ContractID": validation.Validate(cp.ContractID, validation.Required),
"GroupID": validation.Validate(cp.GroupID, validation.Required),
"CPCodeID": validation.Validate(cp.CPCodeID, validation.Required),
}.Filter()
}

// Validate validates CreateCPCodeRequest
func (cp CreateCPCodeRequest) Validate() error {
return validation.Errors{
"ContractID": validation.Validate(cp.ContractID, validation.Required),
"GroupID": validation.Validate(cp.GroupID, validation.Required),
"CPCode": validation.Validate(cp.CPCode, validation.Required),
}.Filter()
}

// Validate validates CreateCPCode
func (cp CreateCPCode) Validate() error {
return validation.Errors{
"ProductID": validation.Validate(cp.ProductID, validation.Required),
"CPCodeName": validation.Validate(cp.CPCodeName, validation.Required),
}.Filter()
}

// GetCPCodes is used to list all available CP codes for given group and contract
func (p *papi) GetCPCodes(ctx context.Context, params GetCPCodesRequest) (*GetCPCodesResponse, error) {
if params.ContractID == "" {
return nil, ErrContractEmpty
}
if params.GroupID == "" {
return nil, ErrGroupEmpty
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
}

logger := p.Log(ctx)
Expand All @@ -97,9 +115,6 @@ func (p *papi) GetCPCodes(ctx context.Context, params GetCPCodesRequest) (*GetCP
params.ContractID,
params.GroupID,
)
if len(params.Options) > 0 {
getURL = fmt.Sprintf("%s&options=%s", getURL, strings.Join(params.Options, ","))
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
if err != nil {
return nil, fmt.Errorf("failed to create getcpcodes request: %w", err)
Expand All @@ -124,21 +139,15 @@ func (p *papi) GetCPCodes(ctx context.Context, params GetCPCodesRequest) (*GetCP

// GetCPCodes is used to fetch a CP code with provided ID
func (p *papi) GetCPCode(ctx context.Context, params GetCPCodeRequest) (*GetCPCodesResponse, error) {
if params.ContractID == "" {
return nil, ErrContractEmpty
}
if params.GroupID == "" {
return nil, ErrGroupEmpty
}
if params.CPCodeID == "" {
return nil, ErrIDEmpty
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
}

logger := p.Log(ctx)
logger.Debug("GetCPCode")

createURL := fmt.Sprintf("/papi/v1/cpcodes/%s?contractId=%s&groupId=%s", params.CPCodeID, params.ContractID, params.GroupID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, createURL, nil)
getURL := fmt.Sprintf("/papi/v1/cpcodes/%s?contractId=%s&groupId=%s", params.CPCodeID, params.ContractID, params.GroupID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
if err != nil {
return nil, fmt.Errorf("failed to create getcpcode request: %w", err)
}
Expand All @@ -151,7 +160,7 @@ func (p *papi) GetCPCode(ctx context.Context, params GetCPCodeRequest) (*GetCPCo
}

if resp.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("%w: %s", session.ErrNotFound, createURL)
return nil, fmt.Errorf("%w: %s", session.ErrNotFound, getURL)
}
if resp.StatusCode != http.StatusOK {
return nil, session.NewAPIError(resp, logger)
Expand All @@ -162,11 +171,8 @@ func (p *papi) GetCPCode(ctx context.Context, params GetCPCodeRequest) (*GetCPCo

// CreateCPCode creates a new CP code with provided CreateCPCodeRequest data
func (p *papi) CreateCPCode(ctx context.Context, r CreateCPCodeRequest) (*CreateCPCodeResponse, error) {
if r.ContractID == "" {
return nil, ErrContractEmpty
}
if r.GroupID == "" {
return nil, ErrGroupEmpty
if err := r.Validate(); err != nil {
return nil, fmt.Errorf("%w: %v", ErrStructValidation, err)
}

logger := p.Log(ctx)
Expand All @@ -187,19 +193,10 @@ func (p *papi) CreateCPCode(ctx context.Context, r CreateCPCodeRequest) (*Create
if resp.StatusCode != http.StatusCreated {
return nil, session.NewAPIError(resp, logger)
}
id, err := fetchIDFromLocation(createResponse.CPCodeLink)
id, err := tools.FetchIDFromLocation(createResponse.CPCodeLink)
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrInvalidLocation, err.Error())
return nil, fmt.Errorf("%w: %s", tools.ErrInvalidLocation, err.Error())
}
createResponse.CPCodeID = id
return &createResponse, nil
}

func fetchIDFromLocation(loc string) (string, error) {
locURL, err := url.Parse(loc)
if err != nil {
return "", err
}
pathSplit := strings.Split(locURL.Path, "/")
return pathSplit[len(pathSplit)-1], nil
}
Loading

0 comments on commit 4af2390

Please sign in to comment.