Skip to content

Commit

Permalink
Merge branch 'main' into application-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-swinkler authored Jan 23, 2024
2 parents d0e33d7 + d2e5ffd commit 1060c36
Show file tree
Hide file tree
Showing 61 changed files with 2,242 additions and 699 deletions.
50 changes: 24 additions & 26 deletions pkg/datasources/storage_integrations.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package datasources

import (
"context"
"database/sql"
"errors"
"fmt"
"log"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/snowflake"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -49,40 +48,39 @@ func StorageIntegrations() *schema.Resource {

func ReadStorageIntegrations(d *schema.ResourceData, meta interface{}) error {
db := meta.(*sql.DB)
ctx := context.Background()
client := sdk.NewClientFromDB(db)

account, err := snowflake.ReadCurrentAccount(db)
account, err := client.ContextFunctions.CurrentAccount(ctx)
if err != nil {
log.Print("[DEBUG] unable to retrieve current account")
d.SetId("")
return nil
return fmt.Errorf("[DEBUG] unable to retrieve current account")
}

d.SetId(fmt.Sprintf("%s.%s", account.Account, account.Region))

currentStorageIntegrations, err := snowflake.ListStorageIntegrations(db)
if errors.Is(err, sql.ErrNoRows) {
// If not found, mark resource to be removed from state file during apply or refresh
log.Printf("[DEBUG] no storage integrations found in account (%s)", d.Id())
d.SetId("")
return nil
} else if err != nil {
log.Printf("[DEBUG] unable to parse storage integrations in account (%s)", d.Id())
region, err := client.ContextFunctions.CurrentRegion(ctx)
if err != nil {
d.SetId("")
return nil
return fmt.Errorf("[DEBUG] unable to retrieve current region")
}

storageIntegrations := []map[string]interface{}{}
d.SetId(fmt.Sprintf("%s.%s", account, region))

for _, storageIntegration := range currentStorageIntegrations {
storageIntegrationMap := map[string]interface{}{}
storageIntegrations, err := client.StorageIntegrations.Show(ctx, sdk.NewShowStorageIntegrationRequest())
if err != nil {
d.SetId("")
return fmt.Errorf("unable to retrieve storage integrations in account (%s), err = %w", d.Id(), err)
}

storageIntegrationMap["name"] = storageIntegration.Name.String
storageIntegrationMap["type"] = storageIntegration.IntegrationType.String
storageIntegrationMap["comment"] = storageIntegration.Comment.String
storageIntegrationMap["enabled"] = storageIntegration.Enabled.Bool
storageIntegrationMaps := make([]map[string]any, len(storageIntegrations))

storageIntegrations = append(storageIntegrations, storageIntegrationMap)
for i, storageIntegration := range storageIntegrations {
storageIntegrationMaps[i] = map[string]any{
"name": storageIntegration.Name,
"type": storageIntegration.StorageType,
"enabled": storageIntegration.Enabled,
"comment": storageIntegration.Comment,
}
}

return d.Set("storage_integrations", storageIntegrations)
return d.Set("storage_integrations", storageIntegrationMaps)
}
82 changes: 62 additions & 20 deletions pkg/datasources/storage_integrations_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,84 @@ package datasources_test

import (
"fmt"
"strings"
"strconv"
"testing"

acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance"
"github.com/hashicorp/terraform-plugin-testing/config"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-plugin-testing/tfversion"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAcc_StorageIntegrations(t *testing.T) {
storageIntegrationName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
resource.ParallelTest(t, resource.TestCase{
Providers: providers(),
CheckDestroy: nil,
func TestAcc_StorageIntegrations_basic(t *testing.T) {
name := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() { acc.TestAccPreCheck(t) },
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
Steps: []resource.TestStep{
{
Config: storageIntegrations(storageIntegrationName),
ConfigVariables: config.Variables{
"name": config.StringVariable(name),
"allowed_locations": config.SetVariable(
config.StringVariable("gcs://foo/"),
config.StringVariable("gcs://bar/"),
),
"blocked_locations": config.SetVariable(
config.StringVariable("gcs://foo/"),
config.StringVariable("gcs://bar/"),
),
"comment": config.StringVariable("some comment"),
},
ConfigDirectory: config.TestNameDirectory(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.snowflake_storage_integrations.s", "storage_integrations.#"),
resource.TestCheckResourceAttrSet("data.snowflake_storage_integrations.s", "storage_integrations.0.name"),
resource.TestCheckResourceAttrSet("data.snowflake_storage_integrations.test", "storage_integrations.#"),
containsStorageIntegration(name, true, "some comment"),
),
},
},
})
}

func storageIntegrations(storageIntegrationName string) string {
return fmt.Sprintf(`
func containsStorageIntegration(name string, enabled bool, comment string) resource.TestCheckFunc {
return func(state *terraform.State) error {
for _, rs := range state.RootModule().Resources {
if rs.Type != "snowflake_storage_integrations" {
continue
}
iter, err := strconv.ParseInt(rs.Primary.Attributes["storage_integrations.#"], 10, 32)
if err != nil {
return err
}

resource snowflake_storage_integration i {
name = "%v"
storage_allowed_locations = ["s3://foo/"]
storage_provider = "S3"
storage_aws_role_arn = "arn:aws:iam::000000000001:/role/test"
}
for i := 0; i < int(iter); i++ {
if rs.Primary.Attributes[fmt.Sprintf("storage_integrations.%d.name", i)] == name {
actualEnabled, err := strconv.ParseBool(rs.Primary.Attributes[fmt.Sprintf("storage_integrations.%d.enabled", i)])
if err != nil {
return err
}

if actualEnabled != enabled {
return fmt.Errorf("expected comment: %v, but got: %v", enabled, actualEnabled)
}

actualComment := rs.Primary.Attributes[fmt.Sprintf("storage_integrations.%d.comment", i)]
if actualComment != comment {
return fmt.Errorf("expected comment: %s, but got: %s", comment, actualComment)
}

return nil
}
}

data snowflake_storage_integrations "s" {
depends_on = [snowflake_storage_integration.i]
return fmt.Errorf("storage integration (%s) not found", name)
}
return nil
}
`, storageIntegrationName)
}
12 changes: 12 additions & 0 deletions pkg/datasources/testdata/TestAcc_StorageIntegrations_basic/test.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource "snowflake_storage_integration" "test" {
name = var.name
enabled = true
storage_provider = "GCS"
comment = var.comment
storage_allowed_locations = var.allowed_locations
storage_blocked_locations = var.blocked_locations
}

data "snowflake_storage_integrations" "test" {
depends_on = [snowflake_storage_integration.test]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
variable "name" {
type = string
}

variable "comment" {
type = string
}

variable "allowed_locations" {
type = set(string)
}

variable "blocked_locations" {
type = set(string)
}
21 changes: 21 additions & 0 deletions pkg/resources/helpers_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resources_test

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -21,6 +22,26 @@ const (
onAll
)

var (
awsBucketUrl, awsBucketUrlIsSet = os.LookupEnv("TEST_SF_TF_AWS_EXTERNAL_BUCKET_URL")
awsKeyId, awsKeyIdIsSet = os.LookupEnv("TEST_SF_TF_AWS_EXTERNAL_KEY_ID")
awsSecretKey, awsSecretKeyIsSet = os.LookupEnv("TEST_SF_TF_AWS_EXTERNAL_SECRET_KEY")
awsRoleARN, awsRoleARNIsSet = os.LookupEnv("TEST_SF_TF_AWS_EXTERNAL_ROLE_ARN")

gcsBucketUrl, gcsBucketUrlIsSet = os.LookupEnv("TEST_SF_TF_GCS_EXTERNAL_BUCKET_URL")

azureBucketUrl, azureBucketUrlIsSet = os.LookupEnv("TEST_SF_TF_AZURE_EXTERNAL_BUCKET_URL")
azureTenantId, azureTenantIdIsSet = os.LookupEnv("TEST_SF_TF_AZURE_EXTERNAL_TENANT_ID")

hasExternalEnvironmentVariablesSet = awsBucketUrlIsSet &&
awsKeyIdIsSet &&
awsSecretKeyIsSet &&
awsRoleARNIsSet &&
gcsBucketUrlIsSet &&
azureBucketUrlIsSet &&
azureTenantIdIsSet
)

func TestGetPropertyAsPointer(t *testing.T) {
d := schema.TestResourceDataRaw(t, map[string]*schema.Schema{
"integer": {
Expand Down
Loading

0 comments on commit 1060c36

Please sign in to comment.