-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added Several Datasources Part 2 (#622)
- Loading branch information
1 parent
c03656f
commit 2a99ea9
Showing
49 changed files
with
2,161 additions
and
4 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
examples/data-sources/snowflake_external_functions/data-source.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
data "snowflake_external_functions" "current" { | ||
database = "MYDB" | ||
schema = "MYSCHEMA" | ||
} |
4 changes: 4 additions & 0 deletions
4
examples/data-sources/snowflake_external_tables/data-source.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
data "snowflake_external_tables" "current" { | ||
database = "MYDB" | ||
schema = "MYSCHEMA" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
data "snowflake_file_formats" "current" { | ||
database = "MYDB" | ||
schema = "MYSCHEMA" | ||
} |
4 changes: 4 additions & 0 deletions
4
examples/data-sources/snowflake_masking_policies/data-source.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
data "snowflake_masking_policies" "current" { | ||
database = "MYDB" | ||
schema = "MYSCHEMA" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
data "snowflake_pipes" "current" { | ||
database = "MYDB" | ||
schema = "MYSCHEMA" | ||
} |
2 changes: 2 additions & 0 deletions
2
examples/data-sources/snowflake_resource_monitors/data-source.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
data "snowflake_resource_monitors" "current" { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
data "snowflake_sequences" "current" { | ||
database = "MYDB" | ||
schema = "MYSCHEMA" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
data "snowflake_stages" "current" { | ||
database = "MYDB" | ||
schema = "MYSCHEMA" | ||
} |
2 changes: 2 additions & 0 deletions
2
examples/data-sources/snowflake_storage_integrations/data-source.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
data "snowflake_storage_integrations" "current" { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
data "snowflake_streams" "current" { | ||
database = "MYDB" | ||
schema = "MYSCHEMA" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
data "snowflake_tasks" "current" { | ||
database = "MYDB" | ||
schema = "MYSCHEMA" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
data "snowflake_warehouses" "current" { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package datasources | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/chanzuckerberg/terraform-provider-snowflake/pkg/snowflake" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
var externalFunctionsSchema = map[string]*schema.Schema{ | ||
"database": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The database from which to return the schemas from.", | ||
}, | ||
"schema": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The schema from which to return the external functions from.", | ||
}, | ||
"external_functions": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: "The external functions in the schema", | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"database": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"schema": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"comment": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"language": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
func ExternalFunctions() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: ReadExternalFunctions, | ||
Schema: externalFunctionsSchema, | ||
} | ||
} | ||
|
||
func ReadExternalFunctions(d *schema.ResourceData, meta interface{}) error { | ||
db := meta.(*sql.DB) | ||
databaseName := d.Get("database").(string) | ||
schemaName := d.Get("schema").(string) | ||
|
||
currentExternalFunctions, err := snowflake.ListExternalFunctions(databaseName, schemaName, db) | ||
if err == sql.ErrNoRows { | ||
// If not found, mark resource to be removed from statefile during apply or refresh | ||
log.Printf("[DEBUG] external functions in schema (%s) not found", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} else if err != nil { | ||
log.Printf("[DEBUG] unable to parse external functions in schema (%s)", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
externalFunctions := []map[string]interface{}{} | ||
|
||
for _, externalFunction := range currentExternalFunctions { | ||
externalFunctionMap := map[string]interface{}{} | ||
|
||
externalFunctionMap["name"] = externalFunction.ExternalFunctionName.String | ||
externalFunctionMap["database"] = externalFunction.DatabaseName.String | ||
externalFunctionMap["schema"] = externalFunction.SchemaName.String | ||
externalFunctionMap["comment"] = externalFunction.Comment.String | ||
externalFunctionMap["language"] = externalFunction.Language.String | ||
|
||
externalFunctions = append(externalFunctions, externalFunctionMap) | ||
} | ||
|
||
d.SetId(fmt.Sprintf(`%v|%v`, databaseName, schemaName)) | ||
return d.Set("external_functions", externalFunctions) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package datasources_test | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccExternalFunctions(t *testing.T) { | ||
databaseName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) | ||
schemaName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) | ||
apiName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) | ||
externalFunctionName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) | ||
resource.ParallelTest(t, resource.TestCase{ | ||
Providers: providers(), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: externalFunctions(databaseName, schemaName, apiName, externalFunctionName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.snowflake_external_functions.t", "database", databaseName), | ||
resource.TestCheckResourceAttr("data.snowflake_external_functions.t", "schema", schemaName), | ||
resource.TestCheckResourceAttrSet("data.snowflake_external_functions.t", "external_functions.#"), | ||
resource.TestCheckResourceAttr("data.snowflake_external_functions.t", "external_functions.#", "1"), | ||
resource.TestCheckResourceAttr("data.snowflake_external_functions.t", "external_functions.0.name", externalFunctionName), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func externalFunctions(databaseName string, schemaName string, apiName string, externalFunctionName string) string { | ||
return fmt.Sprintf(` | ||
resource snowflake_database "test_database" { | ||
name = "%v" | ||
} | ||
resource snowflake_schema "test_schema"{ | ||
name = "%v" | ||
database = snowflake_database.test_database.name | ||
} | ||
resource "snowflake_api_integration" "test_api_int" { | ||
name = "%v" | ||
api_provider = "aws_api_gateway" | ||
api_aws_role_arn = "arn:aws:iam::000000000001:/role/test" | ||
api_allowed_prefixes = ["https://123456.execute-api.us-west-2.amazonaws.com/prod/"] | ||
enabled = true | ||
} | ||
resource "snowflake_external_function" "test_func" { | ||
name = "%v" | ||
database = snowflake_database.test_database.name | ||
schema = snowflake_schema.test_schema.name | ||
arg { | ||
name = "arg1" | ||
type = "varchar" | ||
} | ||
arg { | ||
name = "arg2" | ||
type = "varchar" | ||
} | ||
comment = "Terraform acceptance test" | ||
return_type = "varchar" | ||
return_behavior = "IMMUTABLE" | ||
api_integration = snowflake_api_integration.test_api_int.name | ||
url_of_proxy_and_resource = "https://123456.execute-api.us-west-2.amazonaws.com/prod/test_func" | ||
} | ||
data snowflake_external_functions "t" { | ||
database = snowflake_external_function.test_func.database | ||
schema = snowflake_external_function.test_func.schema | ||
depends_on = [snowflake_external_function.test_func] | ||
} | ||
`, databaseName, schemaName, apiName, externalFunctionName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package datasources | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/chanzuckerberg/terraform-provider-snowflake/pkg/snowflake" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
var externalTablesSchema = map[string]*schema.Schema{ | ||
"database": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The database from which to return the schemas from.", | ||
}, | ||
"schema": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The schema from which to return the external tables from.", | ||
}, | ||
"external_tables": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: "The external tables in the schema", | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"database": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"schema": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"comment": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
func ExternalTables() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: ReadExternalTables, | ||
Schema: externalTablesSchema, | ||
} | ||
} | ||
|
||
func ReadExternalTables(d *schema.ResourceData, meta interface{}) error { | ||
db := meta.(*sql.DB) | ||
databaseName := d.Get("database").(string) | ||
schemaName := d.Get("schema").(string) | ||
|
||
currentExternalTables, err := snowflake.ListExternalTables(databaseName, schemaName, db) | ||
if err == sql.ErrNoRows { | ||
// If not found, mark resource to be removed from statefile during apply or refresh | ||
log.Printf("[DEBUG] external tables in schema (%s) not found", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} else if err != nil { | ||
log.Printf("[DEBUG] unable to parse external tables in schema (%s)", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
externalTables := []map[string]interface{}{} | ||
|
||
for _, externalTable := range currentExternalTables { | ||
externalTableMap := map[string]interface{}{} | ||
|
||
externalTableMap["name"] = externalTable.ExternalTableName.String | ||
externalTableMap["database"] = externalTable.DatabaseName.String | ||
externalTableMap["schema"] = externalTable.SchemaName.String | ||
externalTableMap["comment"] = externalTable.Comment.String | ||
|
||
externalTables = append(externalTables, externalTableMap) | ||
} | ||
|
||
d.SetId(fmt.Sprintf(`%v|%v`, databaseName, schemaName)) | ||
return d.Set("external_tables", externalTables) | ||
} |
Oops, something went wrong.