Skip to content

Commit

Permalink
Refactor Resources (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
dehume authored May 25, 2023
1 parent 1c81f69 commit 9235b4b
Show file tree
Hide file tree
Showing 65 changed files with 1,300 additions and 1,359 deletions.
2 changes: 1 addition & 1 deletion pkg/datasources/datasource_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestConnectionDatasource(t *testing.T) {
ON mz_connections.schema_id = mz_schemas.id
JOIN mz_databases
ON mz_schemas.database_id = mz_databases.id
WHERE mz_schemas.name = 'schema' AND mz_databases.name = 'database';`).WillReturnRows(ir)
WHERE mz_databases.name = 'database' AND mz_schemas.name = 'schema'`).WillReturnRows(ir)

if err := connectionRead(context.TODO(), d, db); err != nil {
t.Fatal(err)
Expand Down
26 changes: 5 additions & 21 deletions pkg/datasources/datasource_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package datasources

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

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"

Expand Down Expand Up @@ -41,30 +38,17 @@ func Database() *schema.Resource {
func databaseRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*sqlx.DB)

q := materialize.ReadDatabaseDatasource()

rows, err := conn.Query(q)
if errors.Is(err, sql.ErrNoRows) {
log.Printf("[DEBUG] no databases found in account")
d.SetId("")
return diag.FromErr(err)
} else if err != nil {
log.Println("[DEBUG] failed to list databases")
d.SetId("")
dataSource, err := materialize.ListDatabases(meta.(*sqlx.DB))
if err != nil {
return diag.FromErr(err)
}

databaseFormats := []map[string]interface{}{}
for rows.Next() {
var id, name string
rows.Scan(&id, &name)

for _, p := range dataSource {
databaseMap := map[string]interface{}{}

databaseMap["id"] = id
databaseMap["name"] = name
databaseMap["id"] = p.DatabaseId.String
databaseMap["name"] = p.DatabaseName.String

databaseFormats = append(databaseFormats, databaseMap)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/datasources/datasource_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func TestDatabaseDatasource(t *testing.T) {
r.NotNil(d)

testhelpers.WithMockDb(t, func(db *sqlx.DB, mock sqlmock.Sqlmock) {
ir := mock.NewRows([]string{"id", "name"}).
ir := mock.NewRows([]string{"id", "database_name"}).
AddRow("u1", "database")
mock.ExpectQuery(`SELECT id, name FROM mz_databases;`).WillReturnRows(ir)
mock.ExpectQuery(`SELECT id, name AS database_name FROM mz_databases;`).WillReturnRows(ir)

if err := databaseRead(context.TODO(), d, db); err != nil {
t.Fatal(err)
Expand Down
36 changes: 10 additions & 26 deletions pkg/datasources/datasource_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package datasources

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

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"

Expand Down Expand Up @@ -62,37 +59,25 @@ func Index() *schema.Resource {
}

func indexRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*sqlx.DB)

schemaName := d.Get("schema_name").(string)
databaseName := d.Get("database_name").(string)
q := materialize.ReadIndexDatasource(databaseName, schemaName)

rows, err := conn.Query(q)
if errors.Is(err, sql.ErrNoRows) {
log.Printf("[DEBUG] no indexes found in account")
d.SetId("")
return diag.FromErr(err)
} else if err != nil {
log.Println("[DEBUG] failed to list indexes")
d.SetId("")
var diags diag.Diagnostics

dataSource, err := materialize.ListIndexes(meta.(*sqlx.DB), schemaName, databaseName)
if err != nil {
return diag.FromErr(err)
}

indexFormats := []map[string]interface{}{}
for rows.Next() {
var id, name, obj, schema, database string
rows.Scan(&id, &name, &obj, &schema, &database)

for _, p := range dataSource {
indexMap := map[string]interface{}{}

indexMap["id"] = id
indexMap["name"] = name
indexMap["obj_name"] = obj
indexMap["obj_schema"] = schema
indexMap["obj_database"] = database
indexMap["id"] = p.IndexId.String
indexMap["name"] = p.IndexName.String
indexMap["obj_name"] = p.Object.String
indexMap["obj_schema"] = p.SchemaName.String
indexMap["obj_database"] = p.DatabaseName.String

indexFormats = append(indexFormats, indexMap)
}
Expand All @@ -102,6 +87,5 @@ func indexRead(ctx context.Context, d *schema.ResourceData, meta interface{}) di
}

SetId("indexes", databaseName, schemaName, d)

return diags
}
15 changes: 8 additions & 7 deletions pkg/datasources/datasource_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,25 @@ func TestIndexDatasource(t *testing.T) {
r.NotNil(d)

testhelpers.WithMockDb(t, func(db *sqlx.DB, mock sqlmock.Sqlmock) {
ir := mock.NewRows([]string{"id", "name", "obj", "schema", "database"}).
ir := mock.NewRows([]string{"id", "index_name", "obj_name", "obj_schema_name", "obj_database_name"}).
AddRow("u1", "index", "obj", "schema", "database")
mock.ExpectQuery(`
SELECT
mz_indexes.id,
mz_indexes.name,
mz_objects.name,
mz_schemas.name,
mz_databases.name
mz_indexes.name AS index_name,
mz_objects.name AS obj_name,
mz_schemas.name AS obj_schema_name,
mz_databases.name AS obj_database_name
FROM mz_indexes
JOIN mz_objects
ON mz_indexes.on_id = mz_objects.id
LEFT JOIN mz_schemas
ON mz_objects.schema_id = mz_schemas.id
LEFT JOIN mz_databases
ON mz_schemas.database_id = mz_databases.id
WHERE mz_objects.type IN \('source', 'view', 'materialized-view'\)
AND mz_databases.name = 'database' AND mz_schemas.name = 'schema'
WHERE mz_databases.name = 'database'
AND mz_objects.type IN \('source', 'view', 'materialized-view'\)
AND mz_schemas.name = 'schema';
`).WillReturnRows(ir)

if err := indexRead(context.TODO(), d, db); err != nil {
Expand Down
34 changes: 9 additions & 25 deletions pkg/datasources/datasource_materialized_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package datasources

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

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"

Expand Down Expand Up @@ -58,36 +55,24 @@ func MaterializedView() *schema.Resource {
}

func materializedViewRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*sqlx.DB)

schemaName := d.Get("schema_name").(string)
databaseName := d.Get("database_name").(string)
q := materialize.ReadMaterializedViewDatasource(databaseName, schemaName)

rows, err := conn.Query(q)
if errors.Is(err, sql.ErrNoRows) {
log.Printf("[DEBUG] no materialized views found in account")
d.SetId("")
return diag.FromErr(err)
} else if err != nil {
log.Println("[DEBUG] failed to list materialized views")
d.SetId("")
var diags diag.Diagnostics

dataSource, err := materialize.ListMaterializedViews(meta.(*sqlx.DB), schemaName, databaseName)
if err != nil {
return diag.FromErr(err)
}

materizliedViewFormats := []map[string]interface{}{}
for rows.Next() {
var id, name, schema_name, database_name string
rows.Scan(&id, &name, &schema_name, &database_name)

for _, p := range dataSource {
materizliedViewMap := map[string]interface{}{}

materizliedViewMap["id"] = id
materizliedViewMap["name"] = name
materizliedViewMap["schema_name"] = schema_name
materizliedViewMap["database_name"] = database_name
materizliedViewMap["id"] = p.MaterializedViewId.String
materizliedViewMap["name"] = p.MaterializedViewName.String
materizliedViewMap["schema_name"] = p.SchemaName.String
materizliedViewMap["database_name"] = p.DatabaseName.String

materizliedViewFormats = append(materizliedViewFormats, materizliedViewMap)
}
Expand All @@ -97,6 +82,5 @@ func materializedViewRead(ctx context.Context, d *schema.ResourceData, meta inte
}

SetId("materialized_views", databaseName, schemaName, d)

return diags
}
16 changes: 10 additions & 6 deletions pkg/datasources/datasource_materialized_view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,24 @@ func TestMaterializedViewDatasource(t *testing.T) {
r.NotNil(d)

testhelpers.WithMockDb(t, func(db *sqlx.DB, mock sqlmock.Sqlmock) {
ir := mock.NewRows([]string{"id", "name", "schema", "database"}).
AddRow("u1", "view", "schema", "database")
ir := mock.NewRows([]string{"id", "materialized_view_name", "schema_name", "database_name", "cluster_name"}).
AddRow("u1", "view", "schema", "database", "cluster")
mock.ExpectQuery(`
SELECT
mz_materialized_views.id,
mz_materialized_views.name,
mz_schemas.name,
mz_databases.name
mz_materialized_views.name AS materialized_view_name,
mz_schemas.name AS schema_name,
mz_databases.name AS database_name,
mz_clusters.name AS cluster_name
FROM mz_materialized_views
JOIN mz_schemas
ON mz_materialized_views.schema_id = mz_schemas.id
JOIN mz_databases
ON mz_schemas.database_id = mz_databases.id
WHERE mz_databases.name = 'database' AND mz_schemas.name = 'schema'`).WillReturnRows(ir)
LEFT JOIN mz_clusters
ON mz_materialized_views.cluster_id = mz_clusters.id
WHERE mz_databases.name = 'database'
AND mz_schemas.name = 'schema'`).WillReturnRows(ir)

if err := materializedViewRead(context.TODO(), d, db); err != nil {
t.Fatal(err)
Expand Down
26 changes: 5 additions & 21 deletions pkg/datasources/datasource_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package datasources

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

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"

Expand Down Expand Up @@ -41,30 +38,17 @@ func Role() *schema.Resource {
func roleRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*sqlx.DB)

q := materialize.ReadRoleDatasource()

rows, err := conn.Query(q)
if errors.Is(err, sql.ErrNoRows) {
log.Printf("[DEBUG] no roles found in account")
d.SetId("")
return diag.FromErr(err)
} else if err != nil {
log.Println("[DEBUG] failed to list roles")
d.SetId("")
dataSource, err := materialize.ListRoles(meta.(*sqlx.DB))
if err != nil {
return diag.FromErr(err)
}

roleFormats := []map[string]interface{}{}
for rows.Next() {
var id, name string
rows.Scan(&id, &name)

for _, p := range dataSource {
roleMap := map[string]interface{}{}

roleMap["id"] = id
roleMap["name"] = name
roleMap["id"] = p.RoleId.String
roleMap["name"] = p.RoleName.String

roleFormats = append(roleFormats, roleMap)
}
Expand Down
14 changes: 11 additions & 3 deletions pkg/datasources/datasource_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@ func TestRoleDatasource(t *testing.T) {
r.NotNil(d)

testhelpers.WithMockDb(t, func(db *sqlx.DB, mock sqlmock.Sqlmock) {
ir := mock.NewRows([]string{"id", "name"}).
AddRow("u1", "role")
mock.ExpectQuery(`SELECT id, name FROM mz_roles;`).WillReturnRows(ir)
ir := mock.NewRows([]string{"id", "role_name", "inherit", "create_role", "create_db", "create_cluster"}).
AddRow("u1", "role", true, true, true, true)
mock.ExpectQuery(`
SELECT
id,
name AS role_name,
inherit,
create_role,
create_db,
create_cluster
FROM mz_roles;`).WillReturnRows(ir)

if err := roleRead(context.TODO(), d, db); err != nil {
t.Fatal(err)
Expand Down
31 changes: 8 additions & 23 deletions pkg/datasources/datasource_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package datasources

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

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"

Expand Down Expand Up @@ -49,34 +46,22 @@ func Schema() *schema.Resource {
}

func schemaRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*sqlx.DB)

databaseName := d.Get("database_name").(string)
q := materialize.ReadSchemaDatasource(databaseName)

rows, err := conn.Query(q)
if errors.Is(err, sql.ErrNoRows) {
log.Printf("[DEBUG] no schemas found in account")
d.SetId("")
return diag.FromErr(err)
} else if err != nil {
log.Println("[DEBUG] failed to list schemas")
d.SetId("")
var diags diag.Diagnostics

dataSource, err := materialize.ListSchemas(meta.(*sqlx.DB), databaseName)
if err != nil {
return diag.FromErr(err)
}

schemasFormats := []map[string]interface{}{}
for rows.Next() {
var id, name, database_name string
rows.Scan(&id, &name, &database_name)

for _, p := range dataSource {
schemaMap := map[string]interface{}{}

schemaMap["id"] = id
schemaMap["name"] = name
schemaMap["database_name"] = database_name
schemaMap["id"] = p.SchemaId.String
schemaMap["name"] = p.SchemaName.String
schemaMap["database_name"] = p.DatabaseName.String

schemasFormats = append(schemasFormats, schemaMap)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/datasources/datasource_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ func TestSchemaDatasource(t *testing.T) {
r.NotNil(d)

testhelpers.WithMockDb(t, func(db *sqlx.DB, mock sqlmock.Sqlmock) {
ir := mock.NewRows([]string{"id", "schema", "database"}).
ir := mock.NewRows([]string{"id", "schema_name", "database_name"}).
AddRow("u1", "schema", "database")
mock.ExpectQuery(`
SELECT
mz_schemas.id,
mz_schemas.name,
mz_databases.name
mz_schemas.name AS schema_name,
mz_databases.name AS database_name
FROM mz_schemas JOIN mz_databases
ON mz_schemas.database_id = mz_databases.id
WHERE mz_databases.name = 'database`).WillReturnRows(ir)
Expand Down
Loading

0 comments on commit 9235b4b

Please sign in to comment.