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

fix: allow AccountURL function to handle accounts in orgs spanning multiple regions #2080

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 7 additions & 4 deletions pkg/snowflake/current_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var regionMapping = map[string]string{
"aws_us_west_2": "", // left black as this is the default
"aws_us_east_2": "us-east-2.aws",
"aws_us_east_1": "us-east-1",
"aws_us_east_1_gov": "us-east-1-gov.aws",
"aws_us_gov_east_1": "us-east-1-gov.aws",
"aws_ca_central_1": "ca-central-1.aws",
"aws_sa_east_1": "sa-east-1.aws",
"aws_eu_west_1": "eu-west-1",
Expand Down Expand Up @@ -48,13 +48,16 @@ var regionMapping = map[string]string{
"azure_australiaeast": "australia-east.azure",
}

// SelectCurrentAccount returns the query that will return the current account, region_group, and region
// the CURRENT_REGION() function returns the format region_group.region (e.g. PUBLIC.AWS_US_WEST_2) only when organizations have accounts in multiple region groups. Otherwise, this function returns the snowflake region without the region_group.
func SelectCurrentAccount() string {
return `SELECT CURRENT_ACCOUNT() AS "account", CURRENT_REGION() AS "region";`
return `SELECT current_account AS "account", CASE WHEN CONTAINS(current_region, '.') THEN LEFT(current_region, POSITION('.' IN current_region) - 1) ELSE 'PUBLIC' END AS "region_group", CASE WHEN CONTAINS(current_region, '.') THEN RIGHT(current_region, LENGTH(current_region) - POSITION('.' IN current_region)) ELSE current_region END AS "region" FROM (SELECT CURRENT_ACCOUNT() AS current_account, CURRENT_REGION() AS current_region);`
}

type CurrentAccount struct {
Account string `db:"account"`
Region string `db:"region"`
Account string `db:"account"`
RegionGroup string `db:"region_group"`
Region string `db:"region"`
}

func ScanCurrentAccount(row *sqlx.Row) (*CurrentAccount, error) {
Expand Down
20 changes: 14 additions & 6 deletions pkg/snowflake/current_account_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package snowflake_test

import (
"regexp"
"testing"

sqlmock "github.com/DATA-DOG/go-sqlmock"
Expand All @@ -11,39 +12,45 @@ import (

func TestCurrentAccountSelect(t *testing.T) {
r := require.New(t)
r.Equal(`SELECT CURRENT_ACCOUNT() AS "account", CURRENT_REGION() AS "region";`, snowflake.SelectCurrentAccount())
r.Equal(`SELECT current_account AS "account", CASE WHEN CONTAINS(current_region, '.') THEN LEFT(current_region, POSITION('.' IN current_region) - 1) ELSE 'PUBLIC' END AS "region_group", CASE WHEN CONTAINS(current_region, '.') THEN RIGHT(current_region, LENGTH(current_region) - POSITION('.' IN current_region)) ELSE current_region END AS "region" FROM (SELECT CURRENT_ACCOUNT() AS current_account, CURRENT_REGION() AS current_region);`, snowflake.SelectCurrentAccount())
}

func TestCurrentAccountRead(t *testing.T) {
type testCaseEntry struct {
account string
region string
url string
account string
region_group string
region string
url string
}

testCases := map[string]testCaseEntry{
"aws oregon": {
"ab1234",
"PUBLIC",
"AWS_US_WEST_2",
"https://ab1234.snowflakecomputing.com",
},
"aws n virginia": {
"cd5678",
"PUBLIC",
"AWS_US_EAST_1",
"https://cd5678.us-east-1.snowflakecomputing.com",
},
"aws canada central": {
"ef9012",
"PUBLIC",
"AWS_CA_CENTRAL_1",
"https://ef9012.ca-central-1.aws.snowflakecomputing.com",
},
"gcp canada central": {
"gh3456",
"PUBLIC",
"gcp_us_central1",
"https://gh3456.us-central1.gcp.snowflakecomputing.com",
},
"azure washington": {
"ij7890",
"PUBLIC",
"azure_westus2",
"https://ij7890.west-us-2.azure.snowflakecomputing.com",
},
Expand All @@ -58,12 +65,13 @@ func TestCurrentAccountRead(t *testing.T) {
defer mockDB.Close()
sqlxDB := sqlx.NewDb(mockDB, "sqlmock")

rows := sqlmock.NewRows([]string{"account", "region"}).AddRow(tc.account, tc.region)
mock.ExpectQuery(`SELECT CURRENT_ACCOUNT\(\) AS "account", CURRENT_REGION\(\) AS "region";`).WillReturnRows(rows)
rows := sqlmock.NewRows([]string{"account", "region_group", "region"}).AddRow(tc.account, tc.region_group, tc.region)
mock.ExpectQuery(regexp.QuoteMeta(`SELECT current_account AS "account", CASE WHEN CONTAINS(current_region, '.') THEN LEFT(current_region, POSITION('.' IN current_region) - 1) ELSE 'PUBLIC' END AS "region_group", CASE WHEN CONTAINS(current_region, '.') THEN RIGHT(current_region, LENGTH(current_region) - POSITION('.' IN current_region)) ELSE current_region END AS "region" FROM (SELECT CURRENT_ACCOUNT() AS current_account, CURRENT_REGION() AS current_region);`)).WillReturnRows(rows)

acc, err := snowflake.ReadCurrentAccount(sqlxDB.DB)
r.NoError(err)
r.Equal(tc.account, acc.Account)
r.Equal(tc.region_group, acc.RegionGroup)
r.Equal(tc.region, acc.Region)
url, err := acc.AccountURL()
r.NoError(err)
Expand Down