Skip to content

Commit

Permalink
refactor: fix invalid style in go (#1265)
Browse files Browse the repository at this point in the history
docs: update document

Co-authored-by: Scott Winkler <scott.winkler@snowflake.com>
  • Loading branch information
mnagaa and sfc-gh-swinkler authored Oct 11, 2022
1 parent e9595f2 commit aede29f
Show file tree
Hide file tree
Showing 66 changed files with 376 additions and 377 deletions.
2 changes: 1 addition & 1 deletion docs/resources/tag_masking_policy_association.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ resource "snowflake_tag_masking_policy_association" "name" {

### Required

- `masking_policy_id` (String) The the resource id of the masking policy
- `masking_policy_id` (String) The resource id of the masking policy
- `tag_id` (String) Specifies the identifier for the tag. Note: format must follow: "databaseName"."schemaName"."tagName" or "databaseName.schemaName.tagName" or "databaseName|schemaName.tagName" (snowflake_tag.tag.id)

### Read-Only
Expand Down
4 changes: 2 additions & 2 deletions pkg/datasources/masking_policies_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestAcc_MaskingPolicies(t *testing.T) {
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: masking_policies(databaseName, schemaName, maskingPolicyName),
Config: maskingPolicies(databaseName, schemaName, maskingPolicyName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.snowflake_masking_policies.t", "database", databaseName),
resource.TestCheckResourceAttr("data.snowflake_masking_policies.t", "schema", schemaName),
Expand All @@ -31,7 +31,7 @@ func TestAcc_MaskingPolicies(t *testing.T) {
})
}

func masking_policies(databaseName string, schemaName string, maskingPolicyName string) string {
func maskingPolicies(databaseName string, schemaName string, maskingPolicyName string) string {
return fmt.Sprintf(`
resource snowflake_database "test" {
Expand Down
24 changes: 12 additions & 12 deletions pkg/datasources/system_get_privatelink_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,26 @@ func ReadSystemGetPrivateLinkConfig(d *schema.ResourceData, meta interface{}) er
if accNameErr != nil {
return accNameErr
}
accUrlErr := d.Set("account_url", config.AccountURL)
if accUrlErr != nil {
return accUrlErr
accURLErr := d.Set("account_url", config.AccountURL)
if accURLErr != nil {
return accURLErr
}
ocspUrlErr := d.Set("ocsp_url", config.OCSPURL)
if ocspUrlErr != nil {
return ocspUrlErr
ocspURLErr := d.Set("ocsp_url", config.OCSPURL)
if ocspURLErr != nil {
return ocspURLErr
}

if config.AwsVpceID != "" {
awsVpceIdErr := d.Set("aws_vpce_id", config.AwsVpceID)
if awsVpceIdErr != nil {
return awsVpceIdErr
awsVpceIDErr := d.Set("aws_vpce_id", config.AwsVpceID)
if awsVpceIDErr != nil {
return awsVpceIDErr
}
}

if config.AzurePrivateLinkServiceID != "" {
azurePlsIdErr := d.Set("azure_pls_id", config.AzurePrivateLinkServiceID)
if azurePlsIdErr != nil {
return azurePlsIdErr
azurePlsIDErr := d.Set("azure_pls_id", config.AzurePrivateLinkServiceID)
if azurePlsIDErr != nil {
return azurePlsIDErr
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/helpers/list_to_string_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func ListToSnowflakeString(list []string) string {
return fmt.Sprintf("%v", strings.Join(list, ", "))
}

// IpListToString formats a list of IPs into a Snowflake-DDL friendly string, e.g. ('192.168.1.0', '192.168.1.100').
func IpListToSnowflakeString(ips []string) string {
// IPListToString formats a list of IPs into a Snowflake-DDL friendly string, e.g. ('192.168.1.0', '192.168.1.100').
func IPListToSnowflakeString(ips []string) string {
for index, element := range ips {
ips[index] = fmt.Sprintf(`'%v'`, element)
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,12 +444,12 @@ func ReadPrivateKeyFile(privateKeyPath string) ([]byte, error) {
func ParsePrivateKey(privateKeyBytes []byte, passhrase []byte) (*rsa.PrivateKey, error) {
privateKeyBlock, _ := pem.Decode(privateKeyBytes)
if privateKeyBlock == nil {
return nil, fmt.Errorf("Could not parse private key, key is not in PEM format")
return nil, fmt.Errorf("could not parse private key, key is not in PEM format")
}

if privateKeyBlock.Type == "ENCRYPTED PRIVATE KEY" {
if len(passhrase) == 0 {
return nil, fmt.Errorf("Private key requires a passphrase, but private_key_passphrase was not supplied")
return nil, fmt.Errorf("private key requires a passphrase, but private_key_passphrase was not supplied")
}
privateKey, err := pkcs8.ParsePKCS8PrivateKeyRSA(privateKeyBlock.Bytes, passhrase)
if err != nil {
Expand Down Expand Up @@ -479,32 +479,32 @@ type Result struct {
ExpiresIn int `json:"expires_in"`
}

func GetOauthData(refreshToken, redirectUrl string) url.Values {
func GetOauthData(refreshToken, redirectURL string) url.Values {
data := url.Values{}
data.Set("grant_type", "refresh_token")
data.Set("refresh_token", refreshToken)
data.Set("redirect_uri", redirectUrl)
data.Set("redirect_uri", redirectURL)
return data
}

func GetOauthRequest(dataContent io.Reader, endPoint, clientId, clientSecret string) (*http.Request, error) {
func GetOauthRequest(dataContent io.Reader, endPoint, clientID, clientSecret string) (*http.Request, error) {
request, err := http.NewRequest("POST", endPoint, dataContent)
if err != nil {
return nil, errors.Wrap(err, "Request to the endpoint could not be completed")
}
request.SetBasicAuth(clientId, clientSecret)
request.SetBasicAuth(clientID, clientSecret)
request.Header.Set("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
return request, nil
}

func GetOauthAccessToken(
endPoint,
client_id,
client_secret string,
clientID,
clientSecret string,
data url.Values) (string, error) {

client := &http.Client{}
request, err := GetOauthRequest(strings.NewReader(data.Encode()), endPoint, client_id, client_secret)
request, err := GetOauthRequest(strings.NewReader(data.Encode()), endPoint, clientID, clientSecret)
if err != nil {
return "", errors.Wrap(err, "Oauth request returned an error:")
}
Expand Down
24 changes: 12 additions & 12 deletions pkg/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,16 @@ func TestOAuthDSN(t *testing.T) {
protocol string
port int
}
pseudorandom_access_token := "ETMsjLOLvQ-C/bzGmmdvbEM/RSQFFX-a+sefbQeQoJqwdFNXZ+ftBIdwlasApA+/MItZLNRRW-rYJiEZMvAAdzpGLxaghIoww+vDOuIeAFBDUxTAY-I+qGbQOXipkNcmzwuAaugjYtlTjPXGjqKw-OSsVacQXzsQyAMnbMyUrbdhRQEETIqTAdMuDqJBeaSj+LMsKDXzLd-guSlm-mmv+="
pseudorandomAccessToken := "ETMsjLOLvQ-C/bzGmmdvbEM/RSQFFX-a+sefbQeQoJqwdFNXZ+ftBIdwlasApA+/MItZLNRRW-rYJiEZMvAAdzpGLxaghIoww+vDOuIeAFBDUxTAY-I+qGbQOXipkNcmzwuAaugjYtlTjPXGjqKw-OSsVacQXzsQyAMnbMyUrbdhRQEETIqTAdMuDqJBeaSj+LMsKDXzLd-guSlm-mmv+="
tests := []struct {
name string
args args
want string
wantErr bool
}{
{"simple_oauth", args{"acct", "user", pseudorandom_access_token, "region", "role", "", "https", 443},
{"simple_oauth", args{"acct", "user", pseudorandomAccessToken, "region", "role", "", "https", 443},
"user:@acct.region.snowflakecomputing.com:443?application=terraform-provider-snowflake&authenticator=oauth&ocspFailOpen=true&region=region&role=role&token=ETMsjLOLvQ-C%2FbzGmmdvbEM%2FRSQFFX-a%2BsefbQeQoJqwdFNXZ%2BftBIdwlasApA%2B%2FMItZLNRRW-rYJiEZMvAAdzpGLxaghIoww%2BvDOuIeAFBDUxTAY-I%2BqGbQOXipkNcmzwuAaugjYtlTjPXGjqKw-OSsVacQXzsQyAMnbMyUrbdhRQEETIqTAdMuDqJBeaSj%2BLMsKDXzLd-guSlm-mmv%2B%3D&validateDefaultParameters=true", false},
{"oauth_over_password", args{"acct", "user", pseudorandom_access_token, "region", "role", "", "https", 443},
{"oauth_over_password", args{"acct", "user", pseudorandomAccessToken, "region", "role", "", "https", 443},
"user:@acct.region.snowflakecomputing.com:443?application=terraform-provider-snowflake&authenticator=oauth&ocspFailOpen=true&region=region&role=role&token=ETMsjLOLvQ-C%2FbzGmmdvbEM%2FRSQFFX-a%2BsefbQeQoJqwdFNXZ%2BftBIdwlasApA%2B%2FMItZLNRRW-rYJiEZMvAAdzpGLxaghIoww%2BvDOuIeAFBDUxTAY-I%2BqGbQOXipkNcmzwuAaugjYtlTjPXGjqKw-OSsVacQXzsQyAMnbMyUrbdhRQEETIqTAdMuDqJBeaSj%2BLMsKDXzLd-guSlm-mmv%2B%3D&validateDefaultParameters=true", false},
{"empty_token_no_password_errors_out", args{"acct", "user", "", "region", "role", "", "https", 443},
"", true},
Expand All @@ -106,27 +106,27 @@ func TestOAuthDSN(t *testing.T) {

func TestGetOauthDATA(t *testing.T) {
type param struct {
refresh_token,
redirect_url string
refreshToken,
redirectURL string
}
refresh_token := "ETMsDgAAAXdeJNwXABRBRVMvQ0JDL1BLQ1M1UGFwPu1hHM3UoUexZBtXW+0cE7KJx2yoUV0ysWu3HKwhJ1v/iEa1Np5EdjGDsBqedR15aFb8NstLTWDUoTJPuQNZRJTjJeuxrX/JUM3/wzcrKt2zDf6QIpkfLXuSlDH4VABeqsaRdl5z6bE9VJVgAUKgZwizwedHAt6pcJgFcQffYZPaY="
redirect_url := "https://localhost.com"
refreshToken := "ETMsDgAAAXdeJNwXABRBRVMvQ0JDL1BLQ1M1UGFwPu1hHM3UoUexZBtXW+0cE7KJx2yoUV0ysWu3HKwhJ1v/iEa1Np5EdjGDsBqedR15aFb8NstLTWDUoTJPuQNZRJTjJeuxrX/JUM3/wzcrKt2zDf6QIpkfLXuSlDH4VABeqsaRdl5z6bE9VJVgAUKgZwizwedHAt6pcJgFcQffYZPaY="
redirectURL := "https://localhost.com"
cases := []struct {
name string
param param
want string
wantErr bool
}{
{"simpleData", param{refresh_token, redirect_url},
{"simpleData", param{refreshToken, redirectURL},
"grant_type=refresh_token&redirect_uri=https%3A%2F%2Flocalhost.com&refresh_token=ETMsDgAAAXdeJNwXABRBRVMvQ0JDL1BLQ1M1UGFwPu1hHM3UoUexZBtXW%2B0cE7KJx2yoUV0ysWu3HKwhJ1v%2FiEa1Np5EdjGDsBqedR15aFb8NstLTWDUoTJPuQNZRJTjJeuxrX%2FJUM3%2FwzcrKt2zDf6QIpkfLXuSlDH4VABeqsaRdl5z6bE9VJVgAUKgZwizwedHAt6pcJgFcQffYZPaY%3D",
false},
{"errorData", param{"no_refresh_token", redirect_url},
{"errorData", param{"no_refresh_token", redirectURL},
"grant_type=refresh_token&redirect_uri=https%3A%2F%2Flocalhost.com&refresh_token=no_refresh_token",
false},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
got := provider.GetOauthData(tt.param.refresh_token, tt.param.redirect_url)
got := provider.GetOauthData(tt.param.refreshToken, tt.param.redirectURL)
want, err := url.ParseQuery(tt.want)
if (err != nil) != tt.wantErr {
t.Errorf("GetData() error = %v, dsn = %v, wantErr %v", err, got, tt.wantErr)
Expand Down Expand Up @@ -229,11 +229,11 @@ func TestGetOauthAccessToken(t *testing.T) {
Header: make(http.Header),
}
})
req_got, err := provider.GetOauthRequest(strings.NewReader(tt.param.dataStuff), tt.param.endpoint, tt.param.clientid, tt.param.clientsecret)
reqGot, err := provider.GetOauthRequest(strings.NewReader(tt.param.dataStuff), tt.param.endpoint, tt.param.clientid, tt.param.clientsecret)
if err != nil {
t.Errorf("GetOauthRequest() %v", err)
}
body, err := client.Do(req_got)
body, err := client.Do(reqGot)
if err != nil {
t.Errorf("Body was not returned %v", err)
}
Expand Down
26 changes: 13 additions & 13 deletions pkg/resources/api_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func CreateAPIIntegration(d *schema.ResourceData, meta interface{}) error {
db := meta.(*sql.DB)
name := d.Get("name").(string)

stmt := snowflake.ApiIntegration(name).Create()
stmt := snowflake.APIIntegration(name).Create()

// Set required fields
stmt.SetBool(`ENABLED`, d.Get("enabled").(bool))
Expand Down Expand Up @@ -143,25 +143,25 @@ func ReadAPIIntegration(d *schema.ResourceData, meta interface{}) error {
db := meta.(*sql.DB)
id := d.Id()

stmt := snowflake.ApiIntegration(id).Show()
stmt := snowflake.APIIntegration(id).Show()
row := snowflake.QueryRow(db, stmt)

// Some properties can come from the SHOW INTEGRATION call

s, err := snowflake.ScanApiIntegration(row)
s, err := snowflake.ScanAPIIntegration(row)
if err != nil {
// If no such resource exists, it is not an error but rather not exist
if err.Error() == snowflake.ErrNoRowInRS {
d.SetId("")
return nil
} else {
return fmt.Errorf("Could not show api integration: %w", err)
return fmt.Errorf("could not show api integration: %w", err)
}
}

// Note: category must be API or something is broken
if c := s.Category.String; c != "API" {
return fmt.Errorf("Expected %v to be an api integration, got %v", id, c)
return fmt.Errorf("expected %v to be an api integration, got %v", id, c)
}

if err := d.Set("name", s.Name.String); err != nil {
Expand All @@ -180,10 +180,10 @@ func ReadAPIIntegration(d *schema.ResourceData, meta interface{}) error {
// We need to grab them in a loop
var k, pType string
var v, unused interface{}
stmt = snowflake.ApiIntegration(id).Describe()
stmt = snowflake.APIIntegration(id).Describe()
rows, err := db.Query(stmt)
if err != nil {
return fmt.Errorf("Could not describe api integration: %w", err)
return fmt.Errorf("could not describe api integration: %w", err)
}
defer rows.Close()
for rows.Next() {
Expand Down Expand Up @@ -236,7 +236,7 @@ func UpdateAPIIntegration(d *schema.ResourceData, meta interface{}) error {
db := meta.(*sql.DB)
id := d.Id()

stmt := snowflake.ApiIntegration(id).Alter()
stmt := snowflake.APIIntegration(id).Alter()

var runSetStatement bool

Expand Down Expand Up @@ -296,7 +296,7 @@ func UpdateAPIIntegration(d *schema.ResourceData, meta interface{}) error {

// DeleteAPIIntegration implements schema.DeleteFunc.
func DeleteAPIIntegration(d *schema.ResourceData, meta interface{}) error {
return DeleteResource("", snowflake.ApiIntegration)(d, meta)
return DeleteResource("", snowflake.APIIntegration)(d, meta)
}

func setAPIProviderSettings(data *schema.ResourceData, stmt snowflake.SettingBuilder) error {
Expand All @@ -307,23 +307,23 @@ func setAPIProviderSettings(data *schema.ResourceData, stmt snowflake.SettingBui
case "aws_api_gateway", "aws_private_api_gateway", "aws_gov_api_gateway", "aws_gov_private_api_gateway":
v, ok := data.GetOk("api_aws_role_arn")
if !ok {
return fmt.Errorf("If you use AWS api provider you must specify an api_aws_role_arn")
return fmt.Errorf("if you use AWS api provider you must specify an api_aws_role_arn")
}
stmt.SetString(`API_AWS_ROLE_ARN`, v.(string))
case "azure_api_management":
v, ok := data.GetOk("azure_tenant_id")
if !ok {
return fmt.Errorf("If you use the Azure api provider you must specify an azure_tenant_id")
return fmt.Errorf("if you use the Azure api provider you must specify an azure_tenant_id")
}
stmt.SetString(`AZURE_TENANT_ID`, v.(string))

v, ok = data.GetOk("azure_ad_application_id")
if !ok {
return fmt.Errorf("If you use the Azure api provider you must specify an azure_ad_application_id")
return fmt.Errorf("if you use the Azure api provider you must specify an azure_ad_application_id")
}
stmt.SetString(`AZURE_AD_APPLICATION_ID`, v.(string))
default:
return fmt.Errorf("Unexpected provider %v", apiProvider)
return fmt.Errorf("unexpected provider %v", apiProvider)
}

return nil
Expand Down
8 changes: 4 additions & 4 deletions pkg/resources/api_integration_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestAcc_ApiIntegration(t *testing.T) {
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: apiIntegrationConfig_aws(apiIntName, []string{"https://123456.execute-api.us-west-2.amazonaws.com/prod/"}),
Config: apiIntegrationConfigAWS(apiIntName, []string{"https://123456.execute-api.us-west-2.amazonaws.com/prod/"}),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_api_integration.test_aws_int", "name", apiIntName),
resource.TestCheckResourceAttr("snowflake_api_integration.test_aws_int", "api_provider", "aws_api_gateway"),
Expand All @@ -33,7 +33,7 @@ func TestAcc_ApiIntegration(t *testing.T) {
),
},
{
Config: apiIntegrationConfig_azure(apiIntName2, []string{"https://apim-hello-world.azure-api.net/"}),
Config: apiIntegrationConfigAzure(apiIntName2, []string{"https://apim-hello-world.azure-api.net/"}),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_api_integration.test_azure_int", "name", apiIntName2),
resource.TestCheckResourceAttr("snowflake_api_integration.test_azure_int", "api_provider", "azure_api_management"),
Expand All @@ -46,7 +46,7 @@ func TestAcc_ApiIntegration(t *testing.T) {
})
}

func apiIntegrationConfig_aws(name string, prefixes []string) string {
func apiIntegrationConfigAWS(name string, prefixes []string) string {
return fmt.Sprintf(`
resource "snowflake_api_integration" "test_aws_int" {
name = "%s"
Expand All @@ -58,7 +58,7 @@ func apiIntegrationConfig_aws(name string, prefixes []string) string {
`, name, prefixes)
}

func apiIntegrationConfig_azure(name string, prefixes []string) string {
func apiIntegrationConfigAzure(name string, prefixes []string) string {
return fmt.Sprintf(`
resource "snowflake_api_integration" "test_azure_int" {
name = "%s"
Expand Down
10 changes: 5 additions & 5 deletions pkg/resources/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ func CreateDatabase(d *schema.ResourceData, meta interface{}) error {

func enableReplication(d *schema.ResourceData, meta interface{}, replicationConfig map[string]interface{}) error {
db := meta.(*sql.DB)
primaryDbName := d.Get("name").(string)
primaryDBName := d.Get("name").(string)
accounts := replicationConfig["accounts"].([]interface{})
accountsToEnableReplication := strings.Join(expandStringList(accounts), ", ")
enableReplicationStmt := fmt.Sprintf(`ALTER DATABASE "%s" ENABLE REPLICATION TO ACCOUNTS %s`, primaryDbName, accountsToEnableReplication)
enableReplicationStmt := fmt.Sprintf(`ALTER DATABASE "%s" ENABLE REPLICATION TO ACCOUNTS %s`, primaryDBName, accountsToEnableReplication)
return snowflake.Exec(db, enableReplicationStmt)
}

Expand Down Expand Up @@ -203,15 +203,15 @@ func createDatabaseFromShare(d *schema.ResourceData, meta interface{}) error {
}

func createDatabaseFromReplica(d *schema.ResourceData, meta interface{}) error {
sourceDb := d.Get("from_replica").(string)
sourceDB := d.Get("from_replica").(string)

db := meta.(*sql.DB)
name := d.Get("name").(string)
builder := snowflake.DatabaseFromReplica(name, sourceDb)
builder := snowflake.DatabaseFromReplica(name, sourceDB)

err := snowflake.Exec(db, builder.Create())
if err != nil {
return errors.Wrapf(err, "error creating a secondary database %v from database %v", name, sourceDb)
return errors.Wrapf(err, "error creating a secondary database %v from database %v", name, sourceDB)
}

d.SetId(name)
Expand Down
4 changes: 2 additions & 2 deletions pkg/resources/external_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func externalFunctionIDFromString(stringID string) (*externalFunctionID, error)
reader.Comma = externalFunctionIDDelimiter
lines, err := reader.ReadAll()
if err != nil {
return nil, fmt.Errorf("Not CSV compatible")
return nil, fmt.Errorf("not CSV compatible")
}

if len(lines) != 1 {
Expand Down Expand Up @@ -352,7 +352,7 @@ func ReadExternalFunction(d *schema.ResourceData, meta interface{}) error {

// Note: 'language' must be EXTERNAL and 'is_external_function' set to Y
if externalFunction.Language.String != "EXTERNAL" || externalFunction.IsExternalFunction.String != "Y" {
return fmt.Errorf("Expected %v to be an external function, got 'language=%v' and 'is_external_function=%v'", d.Id(), externalFunction.Language.String, externalFunction.IsExternalFunction.String)
return fmt.Errorf("expected %v to be an external function, got 'language=%v' and 'is_external_function=%v'", d.Id(), externalFunction.Language.String, externalFunction.IsExternalFunction.String)
}

if err := d.Set("name", externalFunction.ExternalFunctionName.String); err != nil {
Expand Down
Loading

0 comments on commit aede29f

Please sign in to comment.