Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implemented Go SDK env var handling #975

Merged
merged 1 commit into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion go/rtl/constants.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package rtl

const (
DefaultApiVersion = "4.0"
baseUrlEnvKey = "LOOKERSDK_BASE_URL"
apiVersionEnvKey = "LOOKERSDK_API_VERSION"
verifySslEnvKey = "LOOKERSDK_VERIFY_SSL"
timeoutEnvKey = "LOOKERSDK_TIMEOUT"
clientIdEnvKey = "LOOKERSDK_CLIENT_ID"
clientSecretEnvKey = "LOOKERSDK_CLIENT_SECRET"
)
44 changes: 40 additions & 4 deletions go/rtl/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package rtl
import (
"fmt"
"gopkg.in/ini.v1"
"os"
"strconv"
"strings"
)

var defaultSectionName string = "Looker"
Expand All @@ -18,15 +21,19 @@ type ApiSettings struct {
ApiVersion string `ini:"api_version"`
}

var defaultSettings ApiSettings = ApiSettings{
VerifySsl: true,
ApiVersion: "4.0",
Timeout: 120,
}

func NewSettingsFromFile(file string, section *string) (ApiSettings, error) {
if section == nil {
section = &defaultSectionName
}

s := ApiSettings{
VerifySsl: true,
ApiVersion: DefaultApiVersion,
}
// Default values
s := defaultSettings

cfg, err := ini.Load(file)
if err != nil {
Expand All @@ -37,3 +44,32 @@ func NewSettingsFromFile(file string, section *string) (ApiSettings, error) {
return s, err

}

func NewSettingsFromEnv() (ApiSettings, error) {
settings := defaultSettings

if v, present := os.LookupEnv(baseUrlEnvKey); present {
settings.BaseUrl = v
}
if v, present := os.LookupEnv(apiVersionEnvKey); present {
settings.ApiVersion = v
}
if v, present := os.LookupEnv(verifySslEnvKey); present {
s := strings.ToLower(v)
settings.VerifySsl = s == "true" || s == "t" || s == "1" || s == "y" || s == "yes"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw other SDKs have either an isBool() or defaultBool() type of helper function. In this case, I think we want to default settings.VerifySSL to true

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good catch! I will fix.

Copy link
Collaborator Author

@jeremytchang jeremytchang Feb 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just reread the spec. My logic is correct. It already has verifySSL default to true at line 49. Only if the env variable is set does it change.

true, t, yes, y, or 1 (case insensitive) to enable SSL verification. Any other value is treated as false. Defaults to true if not set.

}
if v, present := os.LookupEnv(timeoutEnvKey); present {
timeout, err := strconv.ParseInt(v,10,32)
if err == nil {
settings.Timeout = int32(timeout)
}
}
if v, present := os.LookupEnv(clientIdEnvKey); present {
settings.ClientId = v
}
if v, present := os.LookupEnv(clientSecretEnvKey); present {
settings.ClientSecret = v
}

return settings, nil
}
104 changes: 104 additions & 0 deletions go/rtl/settings_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rtl

import (
"os"
"reflect"
"testing"
)
Expand Down Expand Up @@ -48,3 +49,106 @@ func TestNewSettingsFromFile(t *testing.T) {
})
}
}

type pair struct {
k string
v string
}

func TestNewSettingsFromEnv(t *testing.T) {
getEnv := func() []pair {
var pairs []pair

if v,present := os.LookupEnv(baseUrlEnvKey); present {
pairs = append(pairs, pair{ k: baseUrlEnvKey, v: v })
}
if v,present := os.LookupEnv(apiVersionEnvKey); present {
pairs = append(pairs, pair{ k: apiVersionEnvKey, v: v })
}
if v,present := os.LookupEnv(verifySslEnvKey); present {
pairs = append(pairs, pair{ k: verifySslEnvKey, v: v })
}
if v,present := os.LookupEnv(timeoutEnvKey); present {
pairs = append(pairs, pair{ k: timeoutEnvKey, v: v })
}
if v,present := os.LookupEnv(clientIdEnvKey); present {
pairs = append(pairs, pair{ k: clientIdEnvKey, v: v })
}
if v,present := os.LookupEnv(clientSecretEnvKey); present {
pairs = append(pairs, pair{ k: clientSecretEnvKey, v: v })
}

return pairs
}

clearEnv := func() {
os.Unsetenv(baseUrlEnvKey)
os.Unsetenv(apiVersionEnvKey)
os.Unsetenv(verifySslEnvKey)
os.Unsetenv(timeoutEnvKey)
os.Unsetenv(clientIdEnvKey)
os.Unsetenv(clientSecretEnvKey)
}

setEnv := func(pairs []pair ) {
clearEnv()
for _, pair := range pairs {
os.Setenv(pair.k, pair.v)
}
}

tests := []struct {
name string
env []pair
want ApiSettings
}{
{
name: "NewSettingsFromEnv() returns settings when all environment variables set",
env: []pair {
{baseUrlEnvKey, "url"},
{apiVersionEnvKey, "5.0"},
{verifySslEnvKey, "false"},
{timeoutEnvKey, "360"},
{clientIdEnvKey, "id"},
{clientSecretEnvKey, "secret"},
},
want: ApiSettings{
BaseUrl: "url",
ApiVersion: "5.0",
VerifySsl: false,
Timeout: 360,
ClientId: "id",
ClientSecret: "secret",
},
},
{
name: "NewSettingsFromEnv() sets defaults correctly if env vars not set for them",
env: []pair {
{baseUrlEnvKey, "url"},
{clientIdEnvKey, "id"},
{clientSecretEnvKey, "secret"},
},
want: ApiSettings{
BaseUrl: "url",
ApiVersion: "4.0",
VerifySsl: true,
Timeout: 120,
ClientId: "id",
ClientSecret: "secret",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
origEnv := getEnv()
defer setEnv(origEnv)

setEnv(tt.env)

got, _ := NewSettingsFromEnv()
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewSettingsFromEnv() got = %v, want %v", got, tt.want)
}
})
}
}