Skip to content

Commit

Permalink
generator changes and support for custom tags
Browse files Browse the repository at this point in the history
  • Loading branch information
venkat-iblox committed Jan 25, 2024
1 parent 9c67577 commit 9ec26cd
Show file tree
Hide file tree
Showing 167 changed files with 4,761 additions and 1,759 deletions.
43 changes: 31 additions & 12 deletions client/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"fmt"
"net/http"
"os"

"strings"

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

Expand All @@ -22,27 +23,31 @@ const (

const version = "0.1"
const sdkIdentifier = "golang-sdk"
const clientIdentifier = "automation"

// 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
}

func (c Configuration) internal(basePath string) (*internal.Configuration, error) {
Expand All @@ -54,7 +59,7 @@ func (c Configuration) internal(basePath string) (*internal.Configuration, error
cspURL = c.CSPURL
}
cspURL = cspURL + basePath

apiKey := ""
if v, ok := os.LookupEnv(ENVBloxOneAPIKey); ok {
apiKey = v
Expand All @@ -65,30 +70,44 @@ func (c Configuration) internal(basePath string) (*internal.Configuration, error
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)

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

// setting up custom tag to identify the client
dfTags := make(map[string]string)
// Extract client from ClientName string
// Format: <client>/version#commit
dfTags[clientIdentifier] = strings.Split(c.ClientName, "/")[0]
ic.AddDefaultTags(dfTags)

return ic, nil
}
120 changes: 120 additions & 0 deletions client/configuration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package client

import (
"fmt"
"net/http"
"reflect"
"testing"

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

func TestConfiguration_internal(t *testing.T) {
type fields struct {
ClientName string
CSPURL string
APIKey string
HTTPClient *http.Client
DefaultTags map[string]string
}
type args struct {
basePath string
}
tests := []struct {
name string
fields fields
args args
want *internal.Configuration
wantErr bool
}{
{
"empty API key",
fields{
APIKey: "",
},
args{basePath: ""},
nil,
true,
},
{"empty clientName",
fields{
ClientName: "",
},
args{basePath: ""},
nil,
true,
},
{
"empty DefaultTags",
fields{
ClientName: "terraform/v1.1#yug278872h",
APIKey: "12323455",
},
args{basePath: ""},
&internal.Configuration{
DefaultHeader: map[string]string{
HeaderAuthorization: "Token 12323455",
HeaderClient: "terraform/v1.1#yug278872h",
HeaderSDK: sdkIdentifier,
},
Debug: false,
UserAgent: fmt.Sprintf("bloxone-%s/%s", sdkIdentifier, version),
Servers: []internal.ServerConfiguration{{URL: "https://csp.infoblox.com"}},
HTTPClient: http.DefaultClient,
DefaultTags: map[string]string{
clientIdentifier: "terraform",
},
},
false,
},
{
"DefaultTags provided",
fields{
CSPURL: "https://stage.csp.infoblox.com",
ClientName: "terraformv1.1#yug278872h",
APIKey: "12323455",
DefaultTags: map[string]string{
"site": "A",
"env": "test",
},
},
args{basePath: ""},
&internal.Configuration{
DefaultHeader: map[string]string{
HeaderAuthorization: "Token 12323455",
HeaderClient: "terraformv1.1#yug278872h",
HeaderSDK: sdkIdentifier,
},
Debug: false,
UserAgent: fmt.Sprintf("bloxone-%s/%s", sdkIdentifier, version),
Servers: []internal.ServerConfiguration{{URL: "https://stage.csp.infoblox.com"}},
HTTPClient: http.DefaultClient,
DefaultTags: map[string]string{
clientIdentifier: "terraformv1.1#yug278872h",
"site": "A",
"env": "test",
},
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := Configuration{
ClientName: tt.fields.ClientName,
CSPURL: tt.fields.CSPURL,
APIKey: tt.fields.APIKey,
HTTPClient: tt.fields.HTTPClient,
DefaultTags: tt.fields.DefaultTags,
}
got, err := c.internal(tt.args.basePath)
if (err != nil) != tt.wantErr {
t.Errorf("internal() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("internal() got = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion dns_config/.openapi-generator/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.0.1
7.1.0
10 changes: 5 additions & 5 deletions dns_config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ Default configuration comes with `Servers` field that contains server objects as

### Select Server Configuration

For using other server than the one defined on index 0 set context value `sw.ContextServerIndex` of type `int`.
For using other server than the one defined on index 0 set context value `dns_config.ContextServerIndex` of type `int`.

```golang
ctx := context.WithValue(context.Background(), dns_config.ContextServerIndex, 1)
```

### Templated Server URL

Templated server URL is formatted using default variables from configuration or from context value `sw.ContextServerVariables` of type `map[string]string`.
Templated server URL is formatted using default variables from configuration or from context value `dns_config.ContextServerVariables` of type `map[string]string`.

```golang
ctx := context.WithValue(context.Background(), dns_config.ContextServerVariables, map[string]string{
Expand All @@ -60,7 +60,7 @@ Note, enum values are always validated and all unused variables are silently ign

Each operation can use different server URL defined using `OperationServers` map in the `Configuration`.
An operation is uniquely identified by `"{classname}Service.{nickname}"` string.
Similar rules for overriding default operation server index and variables applies by using `sw.ContextOperationServerIndices` and `sw.ContextOperationServerVariables` context maps.
Similar rules for overriding default operation server index and variables applies by using `dns_config.ContextOperationServerIndices` and `dns_config.ContextOperationServerVariables` context maps.

```golang
ctx := context.WithValue(context.Background(), dns_config.ContextOperationServerIndices, map[string]int{
Expand Down Expand Up @@ -269,8 +269,8 @@ Example
```golang
auth := context.WithValue(
context.Background(),
sw.ContextAPIKeys,
map[string]sw.APIKey{
dns_config.ContextAPIKeys,
map[string]dns_config.APIKey{
"Authorization": {Key: "API_KEY_STRING"},
},
)
Expand Down
Loading

0 comments on commit 9ec26cd

Please sign in to comment.