Skip to content

Commit

Permalink
added generation as query perameter in all RIaaS APIs and some imp (#114
Browse files Browse the repository at this point in the history
)
  • Loading branch information
arahamad-zz authored and akgunjal committed Aug 1, 2019
1 parent 60b28e2 commit 3ec5013
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 40 deletions.
6 changes: 1 addition & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,7 @@ type VPCProviderConfig struct {
VPCTimeout string `toml:"vpc_api_timeout,omitempty" envconfig:"VPC_API_TIMEOUT"`
MaxRetryAttempt int `toml:"max_retry_attempt,omitempty" envconfig:"VPC_RETRY_ATTEMPT"`
MaxRetryGap int `toml:"max_retry_gap,omitempty" envconfig:"VPC_RETRY_INTERVAL"`

// This is in seconds
VPCAPIRetryAttempt int `toml:"vpc_api_retry_attempt,omitempty" envconfig:"VPC_API_RETRY_ATTEMPT"`
VPCAPIRetryInterval int `toml:"vpc_api_retry_interval,omitempty" envconfig:"VPC_API_RETRY_INTERVAL"`
IsVPCAPIExpoRetry bool `toml:"is_vpc_api_expo_retry,omitempty" envconfig:"IS_VPC_API_EXPO_RETRY"`
VPCAPIGeneration int `toml:"vpc_api_generation" envconfig:"VPC_API_GENERATION"`

APIVersion string `toml:"api_version,omitempty" envconfig:"VPC_API_VERSION"`
IsIKS bool `toml:"is_iks,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion config/test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
vpc_timeout = "120s"
max_retry_attempt = 10
max_retry_gap = 60
api_version = "2019-01-01"
api_version = "2019-07-02"
2 changes: 1 addition & 1 deletion e2e/config/vpc-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@
vpc_timeout = "120s"
max_retry_attempt = 60
max_retry_gap = 10
api_version = "2019-01-01"
api_version = "2019-07-02"
5 changes: 3 additions & 2 deletions etc/libconfig.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
iam_client_secret = "bx"
iam_api_key = "XXX"
refresh_token = ""
containers_api_route = "https://containers.dev.cloud.ibm.com"
containers_api_route = "https://containers.test.cloud.ibm.com"

[softlayer]
softlayer_block_enabled = false
Expand Down Expand Up @@ -40,7 +40,8 @@
vpc_api_timeout = "120s"
max_retry_attempt = 10 # 10 times with exponential re-try with max gap max_retry_gap
max_retry_gap = 120 # 2 minutes
api_version = "2019-01-01"
api_version = "2019-07-02"
vpc_api_generation = 1

[IKS]
iks_enabled = true
Expand Down
7 changes: 4 additions & 3 deletions volume-providers/vpc/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ func NewProvider(conf *config.Config, logger *zap.Logger) (local.Provider, error
contextCF: contextCF,
httpClient: httpClient,
APIConfig: riaas.Config{
BaseURL: conf.VPC.EndpointURL,
HTTPClient: httpClient,
APIVersion: conf.VPC.APIVersion,
BaseURL: conf.VPC.EndpointURL,
HTTPClient: httpClient,
APIVersion: conf.VPC.APIVersion,
APIGeneration: conf.VPC.VPCAPIGeneration,
},
}
// Update VPC config for IKS deployment
Expand Down
2 changes: 1 addition & 1 deletion volume-providers/vpc/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const (
IamAPIKey = "test-iam_api_key"
RefreshToken = "test-refresh_token"
TestEndpointURL = "http://some_endpoint"
TestAPIVersion = "2019-01-01"
TestAPIVersion = "2019-07-02"
)

var _ local.ContextCredentialsFactory = &auth.ContextCredentialsFactory{}
Expand Down
11 changes: 2 additions & 9 deletions volume-providers/vpc/vpcclient/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,12 @@ type client struct {
}

// New creates a new instance of a SessionClient
func New(ctx context.Context, baseURL string, httpClient *http.Client, contextID string, APIVersion string) SessionClient {
// Default API version
backendAPIVersion := models.APIVersion

// Overwrite if the version is passed
if len(APIVersion) > 0 {
backendAPIVersion = APIVersion
}
func New(ctx context.Context, baseURL string, queryValues url.Values, httpClient *http.Client, contextID string) SessionClient {
return &client{
baseURL: baseURL,
httpClient: httpClient,
pathParams: Params{},
queryValues: url.Values{"version": []string{backendAPIVersion}},
queryValues: queryValues,
authenHandler: &authenticationHandler{},
contextID: contextID,
context: ctx,
Expand Down
40 changes: 30 additions & 10 deletions volume-providers/vpc/vpcclient/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"strings"
"testing"

Expand Down Expand Up @@ -193,7 +194,7 @@ func TestDebugMode(t *testing.T) {
operation: getOperation,
verify: func(t *testing.T) {
assert.Contains(t, log.String(), "REQUEST:")
assert.Contains(t, log.String(), "GET /resource?version="+models.APIVersion+" HTTP/1.1")
assert.Contains(t, log.String(), "GET /resource?version=2019-01-01 HTTP/1.1")
},
}, {
name: "records the request body",
Expand Down Expand Up @@ -227,6 +228,10 @@ func TestDebugMode(t *testing.T) {
},
}

queryValues := map[string][]string{
"version": []string{"2019-01-01"},
}

for _, testcase := range testcases {

t.Run(testcase.name, func(t *testing.T) {
Expand All @@ -236,7 +241,7 @@ func TestDebugMode(t *testing.T) {

log = &bytes.Buffer{}

riaas = client.New(context.Background(), s.URL, http.DefaultClient, "test-context", "2019-01-01").WithDebug(log).WithAuthToken("auth-token")
riaas = client.New(context.Background(), s.URL, queryValues, http.DefaultClient, "test-context").WithDebug(log).WithAuthToken("auth-token")

defer s.Close()

Expand Down Expand Up @@ -273,27 +278,27 @@ func TestOperationURLProcessing(t *testing.T) {
"absolute path",
"http://127.0.0.1/v2",
&client.Operation{PathPattern: "/absolute/path"},
"http://127.0.0.1/absolute/path?version=" + models.APIVersion,
"http://127.0.0.1/absolute/path?generation=" + strconv.Itoa(models.APIGeneration) + "&version=" + models.APIVersion,
}, {
"relative path base does not end with slash",
"http://127.0.0.1/v2",
&client.Operation{PathPattern: "relative/path"},
"http://127.0.0.1/v2/relative/path?version=" + models.APIVersion,
"http://127.0.0.1/v2/relative/path?generation=" + strconv.Itoa(models.APIGeneration) + "&version=" + models.APIVersion,
}, {
"relative path when base ends with slash",
"http://127.0.0.1/v2/",
&client.Operation{PathPattern: "relative/path"},
"http://127.0.0.1/v2/relative/path?version=" + models.APIVersion,
"http://127.0.0.1/v2/relative/path?generation=" + strconv.Itoa(models.APIGeneration) + "&version=" + models.APIVersion,
}, {
"relative path parent",
"http://127.0.0.1/v2",
&client.Operation{PathPattern: "../path"},
"http://127.0.0.1/path?version=" + models.APIVersion,
"http://127.0.0.1/path?generation=" + strconv.Itoa(models.APIGeneration) + "&version=" + models.APIVersion,
}, {
"relative path with .. beyond root",
"http://127.0.0.1/v2",
&client.Operation{PathPattern: "../../../../path"},
"http://127.0.0.1/path?version=" + models.APIVersion,
"http://127.0.0.1/path?generation=" + strconv.Itoa(models.APIGeneration) + "&version=" + models.APIVersion,
}, {
"broken base URL",
"://127.0.0.1/v2",
Expand All @@ -307,10 +312,15 @@ func TestOperationURLProcessing(t *testing.T) {
},
}

queryValues := map[string][]string{
"generation": []string{strconv.Itoa(models.APIGeneration)},
"version": []string{models.APIVersion},
}

for _, testcase := range testcases {

t.Run(testcase.name, func(t *testing.T) {
c := client.New(context.Background(), testcase.baseURL, http.DefaultClient, "test-context", "2019-01-01")
c := client.New(context.Background(), testcase.baseURL, queryValues, http.DefaultClient, "test-context")
actualURL := c.NewRequest(testcase.operation).URL()
assert.Equal(t, testcase.expectedURL, actualURL)
})
Expand All @@ -323,7 +333,12 @@ func TestWithPathParameter(t *testing.T) {

log := &bytes.Buffer{}

riaas := client.New(context.Background(), s.URL, http.DefaultClient, "test-context", "2019-01-01").WithDebug(log).WithAuthToken("auth-token").WithPathParameter("test", "test")
queryValues := map[string][]string{
"version": []string{models.APIVersion},
"generation": []string{strconv.Itoa(models.APIGeneration)},
}

riaas := client.New(context.Background(), s.URL, queryValues, http.DefaultClient, "test-context").WithDebug(log).WithAuthToken("auth-token").WithPathParameter("test", "test")
assert.NotNil(t, riaas)
defer s.Close()
}
Expand All @@ -334,7 +349,12 @@ func TestWithQueryValue(t *testing.T) {

log := &bytes.Buffer{}

riaas := client.New(context.Background(), s.URL, http.DefaultClient, "test-context", "2019-01-01").WithDebug(log).WithAuthToken("auth-token").WithQueryValue("test", "test")
queryValues := map[string][]string{
"version": []string{models.APIVersion},
"generation": []string{strconv.Itoa(models.APIGeneration)},
}

riaas := client.New(context.Background(), s.URL, queryValues, http.DefaultClient, "test-context").WithDebug(log).WithAuthToken("auth-token").WithQueryValue("test", "test")
assert.NotNil(t, riaas)
defer s.Close()
}
5 changes: 4 additions & 1 deletion volume-providers/vpc/vpcclient/models/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ package models

const (
// APIVersion is the target RIaaS API spec version
APIVersion = "2019-01-01"
APIVersion = "2019-07-02"

// APIGeneration ...
APIGeneration = 1

// UserAgent identifies IKS to the RIaaS API
UserAgent = "IBM-Kubernetes-Service"
Expand Down
9 changes: 5 additions & 4 deletions volume-providers/vpc/vpcclient/riaas/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ type Config struct {
Password string
ContextID string

DebugWriter io.Writer
HTTPClient *http.Client
Context context.Context
APIVersion string
DebugWriter io.Writer
HTTPClient *http.Client
Context context.Context
APIVersion string
APIGeneration int
}

func (c Config) httpClient() *http.Client {
Expand Down
24 changes: 23 additions & 1 deletion volume-providers/vpc/vpcclient/riaas/riaas.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import (
"context"
"github.com/IBM/ibmcloud-storage-volume-lib/volume-providers/vpc/vpcclient/client"
"github.com/IBM/ibmcloud-storage-volume-lib/volume-providers/vpc/vpcclient/instances"
"github.com/IBM/ibmcloud-storage-volume-lib/volume-providers/vpc/vpcclient/models"
"github.com/IBM/ibmcloud-storage-volume-lib/volume-providers/vpc/vpcclient/vpcvolume"
"net/url"
"strconv"
)

// RegionalAPI is the main interface for the RIAAS API client. From here, service
Expand Down Expand Up @@ -44,7 +47,26 @@ func New(config Config) (*Session, error) {
ctx = context.Background()
}

riaasClient := client.New(ctx, config.baseURL(), config.httpClient(), config.ContextID, config.APIVersion)
// Default API version
backendAPIVersion := models.APIVersion

// Overwrite if the version is passed
if len(config.APIVersion) > 0 {
backendAPIVersion = config.APIVersion
}

// Overwrite if the generation is passed
apiGen := models.APIGeneration
if config.APIGeneration > 0 {
apiGen = config.APIGeneration
}

queryValues := url.Values{
"version": []string{backendAPIVersion},
"generation": []string{strconv.Itoa(apiGen)},
}

riaasClient := client.New(ctx, config.baseURL(), queryValues, config.httpClient(), config.ContextID)

if config.DebugWriter != nil {
riaasClient.WithDebug(config.DebugWriter)
Expand Down
18 changes: 17 additions & 1 deletion volume-providers/vpc/vpcclient/riaas/riaas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func TestNewSession(t *testing.T) {
ResourceGroup: "test resource group",
Password: "tester",
ContextID: "tester",
APIVersion: "01-01-2019",
APIVersion: "2019-06-05",
APIGeneration: 2,
HTTPClient: &http.Client{},
DebugWriter: io.Writer(&b),
}
Expand All @@ -58,6 +59,21 @@ func TestNewSession(t *testing.T) {
regionalAPI, err := d.New(cfg)
assert.Nil(t, err)
assert.NotNil(t, regionalAPI)

noAPIVerAndGen := Config{
BaseURL: "http://gc",
AccountID: "test account ID",
Username: "tester",
APIKey: "tester",
ResourceGroup: "test resource group",
Password: "tester",
ContextID: "tester",
HTTPClient: &http.Client{},
DebugWriter: io.Writer(&b),
}
sessionAPI, err := New(noAPIVerAndGen)
assert.Nil(t, err)
assert.NotNil(t, sessionAPI)
}

func TestVolumeService(t *testing.T) {
Expand Down
6 changes: 5 additions & 1 deletion volume-providers/vpc/vpcclient/riaas/test/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/IBM/ibmcloud-storage-volume-lib/volume-providers/vpc/vpcclient/client"
"github.com/IBM/ibmcloud-storage-volume-lib/volume-providers/vpc/vpcclient/models"
"github.com/stretchr/testify/assert"
)

Expand All @@ -31,7 +33,9 @@ func SetupServer(t *testing.T) (m *http.ServeMux, c client.SessionClient, teardo

log := new(bytes.Buffer)

c = client.New(context.Background(), s.URL, http.DefaultClient, "test-context", "2019-01-01").WithDebug(log).WithAuthToken("auth-token")
queryValues := url.Values{"version": []string{models.APIVersion}}

c = client.New(context.Background(), s.URL, queryValues, http.DefaultClient, "test-context").WithDebug(log).WithAuthToken("auth-token")

teardown = func() {
s.Close()
Expand Down

0 comments on commit 3ec5013

Please sign in to comment.