Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
venkat-iblox committed Feb 8, 2024
1 parent 236508e commit f343fb0
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 94 deletions.
167 changes: 82 additions & 85 deletions client/configuration.go
Original file line number Diff line number Diff line change
@@ -1,103 +1,100 @@
package client

import (
"errors"
"fmt"
"net/http"
"os"
"errors"
"fmt"
"net/http"
"os"

"github.com/infobloxopen/bloxone-go-client/internal"
"github.com/infobloxopen/bloxone-go-client/internal"
)

const (
ENVBloxOneCSPURL = "BLOXONE_CSP_URL"
ENVBloxOneAPIKey = "BLOXONE_API_KEY"
ENVBloxOneCSPURL = "BLOXONE_CSP_URL"
ENVBloxOneAPIKey = "BLOXONE_API_KEY"
)

const (
HeaderClient = "x-infoblox-client"
HeaderSDK = "x-infoblox-sdk"
HeaderAuthorization = "Authorization"
version = "0.1"
sdkIdentifier = "golang-sdk"
HeaderClient = "x-infoblox-client"
HeaderSDK = "x-infoblox-sdk"
HeaderAuthorization = "Authorization"
version = "0.1"
sdkIdentifier = "golang-sdk"
)

// Configuration stores the configuration of the API client
type Configuration struct {
// ClientName is the name of the client using the SDK.
// Required.
ClientName string

// CSPURL is the URL for BloxOne Cloud Services Portal.
// Can also be configured using the `BLOXONE_CSP_URL` environment variable.
// Optional. Default is https://csp.infoblox.com
CSPURL string

// APIKey for accessing the BloxOne API.
// Can also be configured by using the `BLOXONE_API_KEY` environment variable.
// https://docs.infoblox.com/space/BloxOneCloud/35430405/Configuring+User+API+Keys
// Required.
APIKey string

// HTTPClient to use for the SDK.
// Optional. The default HTTPClient will be used if not provided.
HTTPClient *http.Client

// Default global tags the client can set for all requests.
DefaultTags map[string]string
// ClientName is the name of the client using the SDK.
// Required.
ClientName string

// CSPURL is the URL for BloxOne Cloud Services Portal.
// Can also be configured using the `BLOXONE_CSP_URL` environment variable.
// Optional. Default is https://csp.infoblox.com
CSPURL string

// APIKey for accessing the BloxOne API.
// Can also be configured by using the `BLOXONE_API_KEY` environment variable.
// https://docs.infoblox.com/space/BloxOneCloud/35430405/Configuring+User+API+Keys
// Required.
APIKey string

// HTTPClient to use for the SDK.
// Optional. The default HTTPClient will be used if not provided.
HTTPClient *http.Client

// Default tags the client can set for objects that has tags support.
// Optional. The default is an empty map.
DefaultTags map[string]string
}

func (c Configuration) internal(basePath string) (*internal.Configuration, error) {
cspURL := "https://csp.infoblox.com"
if v, ok := os.LookupEnv(ENVBloxOneCSPURL); ok {
cspURL = v
}
if len(c.CSPURL) > 0 {
cspURL = c.CSPURL
}
cspURL = cspURL + basePath

apiKey := ""
if v, ok := os.LookupEnv(ENVBloxOneAPIKey); ok {
apiKey = v
}
if len(c.APIKey) > 0 {
apiKey = c.APIKey
}
if len(apiKey) == 0 {
return nil, errors.New("APIKey is required")
}

if len(c.ClientName) == 0 {
return nil, errors.New("ClientName is required")
}

httpClient := c.HTTPClient
if httpClient == nil {
httpClient = http.DefaultClient
}

defaultHeaders := map[string]string{
HeaderAuthorization: "Token " + apiKey,
HeaderClient: c.ClientName,
HeaderSDK: sdkIdentifier,
}

userAgent := fmt.Sprintf("bloxone-%s/%s", sdkIdentifier, version)

ic := &internal.Configuration{
DefaultHeader: defaultHeaders,
UserAgent: userAgent,
Debug: false,
OperationServers: nil,
Servers: []internal.ServerConfiguration{{URL: cspURL}},
HTTPClient: httpClient,
DefaultTags: make(map[string]string),
}
// Add default tags set
if c.DefaultTags != nil {
ic.AddDefaultTags(c.DefaultTags)
}

return ic, nil
cspURL := "https://csp.infoblox.com"
if v, ok := os.LookupEnv(ENVBloxOneCSPURL); ok {
cspURL = v
}
if len(c.CSPURL) > 0 {
cspURL = c.CSPURL
}
cspURL = cspURL + basePath

apiKey := ""
if v, ok := os.LookupEnv(ENVBloxOneAPIKey); ok {
apiKey = v
}
if len(c.APIKey) > 0 {
apiKey = c.APIKey
}
if len(apiKey) == 0 {
return nil, errors.New("APIKey is required")
}

if len(c.ClientName) == 0 {
return nil, errors.New("ClientName is required")
}

httpClient := c.HTTPClient
if httpClient == nil {
httpClient = http.DefaultClient
}

defaultHeaders := map[string]string{
HeaderAuthorization: "Token " + apiKey,
HeaderClient: c.ClientName,
HeaderSDK: sdkIdentifier,
}

userAgent := fmt.Sprintf("bloxone-%s/%s", sdkIdentifier, version)

ic := &internal.Configuration{
DefaultHeader: defaultHeaders,
UserAgent: userAgent,
Debug: false,
OperationServers: nil,
Servers: []internal.ServerConfiguration{{URL: cspURL}},
HTTPClient: httpClient,
DefaultTags: c.DefaultTags,
}

return ic, nil
}
2 changes: 1 addition & 1 deletion client/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestConfiguration_internal(t *testing.T) {
UserAgent: fmt.Sprintf("bloxone-%s/%s", sdkIdentifier, version),
Servers: []internal.ServerConfiguration{{URL: "https://csp.infoblox.com"}},
HTTPClient: http.DefaultClient,
DefaultTags: map[string]string{},
DefaultTags: nil,
},
false,
},
Expand Down
6 changes: 0 additions & 6 deletions internal/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,6 @@ func (c *Configuration) AddDefaultHeader(key string, value string) {
c.DefaultHeader[key] = value
}

func (c *Configuration) AddDefaultTags(m map[string]string) {
for k, v := range m {
c.DefaultTags[k] = v
}
}

func (c *Configuration) GetDefaultTags() map[string]string {
return c.DefaultTags
}
Expand Down
3 changes: 1 addition & 2 deletions internal/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func TestConfiguration_AddDefaultTags(t *testing.T) {
},
},
expected: map[string]string{
"key1": "value1",
"key2": "value2",
},
},
Expand All @@ -66,7 +65,7 @@ func TestConfiguration_AddDefaultTags(t *testing.T) {
c := &Configuration{
DefaultTags: tt.fields.DefaultTags,
}
c.AddDefaultTags(tt.args.m)
c.DefaultTags = tt.args.m
if !reflect.DeepEqual(c.GetDefaultTags(), tt.expected) {
t.Errorf("internal() want = %v, got = %v", tt.expected, c.GetDefaultTags())
return
Expand Down

0 comments on commit f343fb0

Please sign in to comment.