Skip to content

Commit

Permalink
Merge pull request #56 from xataio/rs/export-env-constants
Browse files Browse the repository at this point in the history
Export environment variable constants
  • Loading branch information
philkra authored Jan 15, 2024
2 parents 76f3edb + 3670498 commit 6b60282
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 71 deletions.
7 changes: 3 additions & 4 deletions internal/integration-tests/databases_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ import (
"os"
"testing"

"github.com/stretchr/testify/assert"

"github.com/hashicorp/go-retryablehttp"
"github.com/stretchr/testify/assert"
"github.com/xataio/xata-go/xata"
)

func Test_databasesClient(t *testing.T) {
apiKey, found := os.LookupEnv("XATA_API_KEY")
apiKey, found := os.LookupEnv(xata.EnvXataAPIKey)
if !found {
t.Skipf("%s not found in env vars", "XATA_API_KEY")
t.Skipf("%s not found in env vars", xata.EnvXataAPIKey)
}

ctx := context.Background()
Expand Down
10 changes: 4 additions & 6 deletions internal/integration-tests/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"net/http"
"os"

//"strings"

"github.com/hashicorp/go-retryablehttp"
"github.com/xataio/xata-go/xata"
)
Expand All @@ -28,15 +26,15 @@ type config struct {

func setupDatabase() (*config, error) {
ctx := context.Background()
apiKey, found := os.LookupEnv("XATA_API_KEY")
apiKey, found := os.LookupEnv(xata.EnvXataAPIKey)
if !found {
return nil, fmt.Errorf("%s not found in env vars", "XATA_API_KEY")
return nil, fmt.Errorf("%s not found in env vars", xata.EnvXataAPIKey)
}
// require workspace ID to come from the env var
// instead of creating new workspace on each client
wsID, found := os.LookupEnv("XATA_WORKSPACE_ID")
wsID, found := os.LookupEnv(xata.EnvXataWorkspaceID)
if !found {
return nil, fmt.Errorf("%s not found in env vars", "XATA_WORKSPACE_ID")
return nil, fmt.Errorf("%s not found in env vars", xata.EnvXataWorkspaceID)
}

testID := testIdentifier()
Expand Down
4 changes: 2 additions & 2 deletions internal/integration-tests/users_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
)

func Test_usersClient(t *testing.T) {
apiKey, found := os.LookupEnv("XATA_API_KEY")
apiKey, found := os.LookupEnv(xata.EnvXataAPIKey)
if !found {
t.Skipf("%s not found in env vars", "XATA_API_KEY")
t.Skipf("%s not found in env vars", xata.EnvXataAPIKey)
}

t.Run("should get the current user", func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions internal/integration-tests/workspaces_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
)

func Test_workspacesClient(t *testing.T) {
apiKey, found := os.LookupEnv("XATA_API_KEY")
apiKey, found := os.LookupEnv(xata.EnvXataAPIKey)
if !found {
t.Skipf("%s not found in env vars", "XATA_API_KEY")
t.Skipf("%s not found in env vars", xata.EnvXataAPIKey)
}

t.Run("should create, get, list, update and delete workspace", func(t *testing.T) {
Expand Down
39 changes: 21 additions & 18 deletions xata/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,34 @@ import (
"github.com/joho/godotenv"
)

// Environment variables
const (
EnvXataAPIKey = "XATA_API_KEY"
EnvXataWorkspaceID = "XATA_WORKSPACE_ID"
EnvXataBranch = "XATA_BRANCH"
EnvXataRegion = "XATA_REGION"
)

const (
personalAPIKeyLocation = "~/.config/xata/key"
defaultControlPlaneDomain = "api.xata.io"
xataAPIKeyEnvVar = "XATA_API_KEY"
xataWsIDEnvVar = "XATA_WORKSPACE_ID"
dbURLFormat = "https://{workspace_id}.{region}.xata.sh/db/{db_name}:{branch_name}"
defaultBranchName = "main"
configFileName = ".xatarc"
branchNameEnvVar = "XATA_BRANCH"
defaultDataPlaneDomain = "xata.sh"
defaultRegion = "us-east-1"
regionEnvVar = "XATA_REGION"
)

var errAPIKey = fmt.Errorf("no API key found. Searched in `%s` env, %s, and .env", xataAPIKeyEnvVar, personalAPIKeyLocation)
var errAPIKey = fmt.Errorf("no API key found. Searched in `%s` env, %s, and .env", EnvXataAPIKey, personalAPIKeyLocation)

// assignAPIkey add the API key to the ClientOptions by going through the following options in order:
// - In env vars by the xataAPIKeyEnvVar dbName.
// - In .env file with the xataAPIKeyEnvVar dbName.
// - In env vars by the EnvXataAPIKey dbName.
// - In .env file with the EnvXataAPIKey dbName.
// - In .xatarc config file (TODO: not ready!)
//
// See: https://xata.io/docs/python-sdk/overview#authorization
func getAPIKey() (string, error) {
if key, found := os.LookupEnv(xataAPIKeyEnvVar); found {
if key, found := os.LookupEnv(EnvXataAPIKey); found {
return key, nil
}

Expand All @@ -50,7 +54,7 @@ func getAPIKey() (string, error) {
}
}

if key, found := myEnv[xataAPIKeyEnvVar]; found {
if key, found := myEnv[EnvXataAPIKey]; found {
return key, nil
}

Expand Down Expand Up @@ -187,13 +191,13 @@ func getEnvVar(name string, defaultValue string) string {
// getBranchName retrieves the branch name.
// If not found, falls back to defaultBranchName
func getBranchName() string {
return getEnvVar(branchNameEnvVar, defaultBranchName)
return getEnvVar(EnvXataBranch, defaultBranchName)
}

// Get the region if the corresponding env var `XATA_REGION` is set
// otherwise return the default region: us-east-1
func getRegion() string {
return getEnvVar(regionEnvVar, defaultRegion)
return getEnvVar(EnvXataRegion, defaultRegion)
}

// loadDatabaseConfig will return config with defaults if the error is not nil.
Expand All @@ -205,15 +209,14 @@ func loadDatabaseConfig() (databaseConfig, error) {
}

// Setup with env var
// XATA_WORKSPACE_ID to set the workspace Id
wsID := getEnvVar(xataWsIDEnvVar, "")
// XATA_WORKSPACE_ID to set the workspace ID
wsID := getEnvVar(EnvXataWorkspaceID, "")
if wsID != "" {
region := getRegion()
branch := getBranchName()
db := databaseConfig{
workspaceID: wsID,
region: region,
branchName: branch,
workspaceID: wsID,
region: getRegion(),
branchName: getBranchName(),
domainWorkspace: defaultDataPlaneDomain,
}
return db, nil
}
Expand Down
61 changes: 22 additions & 39 deletions xata/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (

func TestClientOptions_getAPIKey(t *testing.T) {
apiKeyFromEnv := "test-API-key-from-env"
err := os.Setenv(xataAPIKeyEnvVar, apiKeyFromEnv)
err := os.Setenv(EnvXataAPIKey, apiKeyFromEnv)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { os.Unsetenv(xataAPIKeyEnvVar) })
t.Cleanup(func() { os.Unsetenv(EnvXataAPIKey) })

t.Run("should assign the API key from the env vars", func(t *testing.T) {
apiKey, err := getAPIKey()
Expand All @@ -34,7 +34,7 @@ func Test_getBranchName(t *testing.T) {
})

setBranchName := "feature-042"
err := os.Setenv(branchNameEnvVar, setBranchName)
err := os.Setenv(EnvXataBranch, setBranchName)
if err != nil {
t.Fatal(err)
}
Expand All @@ -45,7 +45,7 @@ func Test_getBranchName(t *testing.T) {
assert.Equal(t, gotBranchName, setBranchName)
})

t.Cleanup(func() { os.Unsetenv(branchNameEnvVar) })
t.Cleanup(func() { os.Unsetenv(EnvXataBranch) })
}

func Test_getRegion(t *testing.T) {
Expand All @@ -56,7 +56,7 @@ func Test_getRegion(t *testing.T) {
})

setRegion := "eu-west-3"
err := os.Setenv(regionEnvVar, setRegion)
err := os.Setenv(EnvXataRegion, setRegion)
if err != nil {
t.Fatal(err)
}
Expand All @@ -67,7 +67,7 @@ func Test_getRegion(t *testing.T) {
assert.Equal(t, gotRegion, setRegion)
})

t.Cleanup(func() { os.Unsetenv(regionEnvVar) })
t.Cleanup(func() { os.Unsetenv(EnvXataRegion) })
}

func Test_parseDatabaseURL(t *testing.T) {
Expand Down Expand Up @@ -176,61 +176,44 @@ func Test_loadConfig(t *testing.T) {

func Test_loadDatabaseConfig_with_envvars(t *testing.T) {
setWsId := "workspace-0lac00"
err := os.Setenv(xataWsIDEnvVar, setWsId)
err := os.Setenv(EnvXataWorkspaceID, setWsId)
if err != nil {
t.Fatal(err)
}

// test workspace id from env var
t.Run("load config from WORKSPACE_ID env var", func(t *testing.T) {
dbCfg, err := loadDatabaseConfig()
if err != nil {
t.Fatalf("Error loading config: %v", err)
}

if dbCfg.workspaceID != setWsId {
t.Fatalf("Expected Workspace ID: %s, got: %s", setWsId, dbCfg.workspaceID)
}
if dbCfg.branchName != defaultBranchName {
t.Fatalf("Expected branch name: %s, got: %s", defaultBranchName, dbCfg.branchName)
}
if dbCfg.region != defaultRegion {
t.Fatalf("Expected region: %s, got: %s", defaultRegion, dbCfg.region)
}
assert.NoError(t, err)
assert.Equal(t, setWsId, dbCfg.workspaceID)
assert.Equal(t, defaultBranchName, dbCfg.branchName)
assert.Equal(t, defaultRegion, dbCfg.region)
assert.Equal(t, defaultDataPlaneDomain, dbCfg.domainWorkspace)
})

setBranch := "branch123"
err2 := os.Setenv(branchNameEnvVar, setBranch)
err2 := os.Setenv(EnvXataBranch, setBranch)
if err2 != nil {
t.Fatal(err2)
}
setRegion := "ap-southeast-16"
err3 := os.Setenv(regionEnvVar, setRegion)
err3 := os.Setenv(EnvXataRegion, setRegion)
if err3 != nil {
t.Fatal(err3)
}

// with branch and region env vars
t.Run("load config from XATA_WORKSPACE_ID, regionEnvVar and XATA_BRANCH env vars", func(t *testing.T) {
t.Run("load config from XATA_WORKSPACE_ID, XATA_REGION and XATA_BRANCH env vars", func(t *testing.T) {
dbCfg, err := loadDatabaseConfig()
if err != nil {
t.Fatalf("Error loading config: %v", err)
}

if dbCfg.workspaceID != setWsId {
t.Fatalf("Expected Workspace ID: %s, got: %s", setWsId, dbCfg.workspaceID)
}
if dbCfg.branchName != setBranch {
t.Fatalf("Expected branch name: %s, got: %s", setBranch, dbCfg.branchName)
}
if dbCfg.region != setRegion {
t.Fatalf("Expected region: %s, got: %s", setRegion, dbCfg.region)
}
assert.NoError(t, err)
assert.Equal(t, setWsId, dbCfg.workspaceID)
assert.Equal(t, setBranch, dbCfg.branchName)
assert.Equal(t, setRegion, dbCfg.region)
})

t.Cleanup(func() {
os.Unsetenv(xataWsIDEnvVar)
os.Unsetenv(branchNameEnvVar)
os.Unsetenv(regionEnvVar)
assert.NoError(t, os.Unsetenv(EnvXataWorkspaceID))
assert.NoError(t, os.Unsetenv(EnvXataBranch))
assert.NoError(t, os.Unsetenv(EnvXataRegion))
})
}

0 comments on commit 6b60282

Please sign in to comment.