Skip to content

Commit 34701b1

Browse files
authored
Allow users to use cluster name as id (#618)
* Allow users to use cluster name as id * Change attr to identify_by_name * Add type prefix to cluster id * Disable specific conn data source test * Disable specific conn data source test
1 parent cd11834 commit 34701b1

File tree

11 files changed

+287
-67
lines changed

11 files changed

+287
-67
lines changed

docs/resources/cluster.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ resource "materialize_cluster" "example_cluster" {
3030
- `availability_zones` (List of String) The specific availability zones of the cluster.
3131
- `comment` (String) **Public Preview** Comment on an object in the database.
3232
- `disk` (Boolean, Deprecated) **Deprecated**. This attribute is maintained for backward compatibility with existing configurations. New users should use 'cc' sizes for disk access.
33+
- `identify_by_name` (Boolean) Use the cluster name as the Terraform resource ID instead of the internal cluster ID.
3334
- `introspection_debugging` (Boolean) Whether to introspect the gathering of the introspection data.
3435
- `introspection_interval` (String) The interval at which to collect introspection data.
3536
- `ownership_role` (String) The owernship role of the object.
@@ -63,8 +64,11 @@ Optional:
6364
Import is supported using the following syntax:
6465

6566
```shell
66-
# Clusters can be imported using the cluster id:
67-
terraform import materialize_cluster.example_cluster <region>:<cluster_id>
67+
# Clusters can be imported using the cluster id or name
68+
terraform import materialize_cluster.example_cluster <region>:id:<cluster_id>
69+
70+
# To import using the cluster name, you need to set the `identify_by_name` attribute to true
71+
terraform import materialize_cluster.example_cluster <region>:name:<cluster_name>
6872

6973
# Cluster id and information be found in the `mz_catalog.mz_clusters` table
7074
# The region is the region where the database is located (e.g. aws/us-east-1)
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
# Clusters can be imported using the cluster id:
2-
terraform import materialize_cluster.example_cluster <region>:<cluster_id>
1+
# Clusters can be imported using the cluster id or name
2+
terraform import materialize_cluster.example_cluster <region>:id:<cluster_id>
3+
4+
# To import using the cluster name, you need to set the `identify_by_name` attribute to true
5+
terraform import materialize_cluster.example_cluster <region>:name:<cluster_name>
36

47
# Cluster id and information be found in the `mz_catalog.mz_clusters` table
58
# The region is the region where the database is located (e.g. aws/us-east-1)

integration/cluster.tf

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ resource "materialize_cluster" "cluster_sink" {
1212
size = "3xsmall"
1313
}
1414

15+
resource "materialize_cluster" "cluster_by_name" {
16+
name = "cluster_by_name"
17+
size = "25cc"
18+
identify_by_name = true
19+
}
1520

1621
resource "materialize_cluster" "scheduling_cluster" {
1722
name = "scheduling_cluster"

pkg/materialize/cluster.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,14 @@ func ClusterId(conn *sqlx.DB, obj MaterializeObject) (string, error) {
291291
return c.ClusterId.String, nil
292292
}
293293

294-
func ScanCluster(conn *sqlx.DB, id string) (ClusterParams, error) {
295-
q := clusterQuery.QueryPredicate(map[string]string{"mz_clusters.id": id})
294+
func ScanCluster(conn *sqlx.DB, identifier string, byName bool) (ClusterParams, error) {
295+
var predicate map[string]string
296+
if byName {
297+
predicate = map[string]string{"mz_clusters.name": identifier}
298+
} else {
299+
predicate = map[string]string{"mz_clusters.id": identifier}
300+
}
301+
q := clusterQuery.QueryPredicate(predicate)
296302

297303
var c ClusterParams
298304
if err := conn.Get(&c, q); err != nil {

pkg/materialize/privilege.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func ScanPrivileges(conn *sqlx.DB, objectType, objectId string) ([]string, error
233233
e = err
234234

235235
case "CLUSTER":
236-
params, err := ScanCluster(conn, objectId)
236+
params, err := ScanCluster(conn, objectId, false)
237237
p = params.Privileges
238238
e = err
239239
}

pkg/provider/acceptance_cluster_test.go

+57-7
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestAccCluster_basic(t *testing.T) {
2525
Config: testAccClusterResource(roleName, clusterName, cluster2Name, roleName, "3xsmall", "1", "1s", "true", "true", "Comment"),
2626
Check: resource.ComposeTestCheckFunc(
2727
testAccCheckClusterExists("materialize_cluster.test"),
28-
resource.TestMatchResourceAttr("materialize_cluster.test", "id", terraformObjectIdRegex),
28+
resource.TestMatchResourceAttr("materialize_cluster.test", "id", terraformObjectTypeIdRegex),
2929
resource.TestCheckResourceAttr("materialize_cluster.test", "name", clusterName),
3030
resource.TestCheckResourceAttr("materialize_cluster.test", "ownership_role", "mz_system"),
3131
resource.TestCheckResourceAttr("materialize_cluster.test", "size", ""),
@@ -49,7 +49,7 @@ func TestAccCluster_basic(t *testing.T) {
4949
ResourceName: "materialize_cluster.test",
5050
ImportState: true,
5151
ImportStateVerify: true,
52-
ImportStateVerifyIgnore: []string{"introspection_debugging", "introspection_interval"},
52+
ImportStateVerifyIgnore: []string{"introspection_debugging", "introspection_interval", "identify_by_name"},
5353
},
5454
},
5555
})
@@ -81,7 +81,7 @@ func TestAccClusterCCSize_basic(t *testing.T) {
8181
ResourceName: "materialize_cluster.test",
8282
ImportState: true,
8383
ImportStateVerify: true,
84-
ImportStateVerifyIgnore: []string{"introspection_debugging", "introspection_interval"},
84+
ImportStateVerifyIgnore: []string{"introspection_debugging", "introspection_interval", "identify_by_name"},
8585
},
8686
},
8787
})
@@ -107,7 +107,7 @@ func TestAccClusterManagedNoReplication_basic(t *testing.T) {
107107
ResourceName: "materialize_cluster.test",
108108
ImportState: true,
109109
ImportStateVerify: true,
110-
ImportStateVerifyIgnore: []string{"introspection_debugging", "introspection_interval"},
110+
ImportStateVerifyIgnore: []string{"introspection_debugging", "introspection_interval", "identify_by_name"},
111111
},
112112
},
113113
})
@@ -133,7 +133,7 @@ func TestAccClusterManagedZeroReplication_basic(t *testing.T) {
133133
ResourceName: "materialize_cluster.test",
134134
ImportState: true,
135135
ImportStateVerify: true,
136-
ImportStateVerifyIgnore: []string{"introspection_debugging", "introspection_interval"},
136+
ImportStateVerifyIgnore: []string{"introspection_debugging", "introspection_interval", "identify_by_name"},
137137
},
138138
},
139139
})
@@ -293,6 +293,38 @@ func TestAccClusterWithScheduling(t *testing.T) {
293293
})
294294
}
295295

296+
func TestAccCluster_identifyByName(t *testing.T) {
297+
clusterName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
298+
resource.ParallelTest(t, resource.TestCase{
299+
PreCheck: func() { testAccPreCheck(t) },
300+
ProviderFactories: testAccProviderFactories,
301+
CheckDestroy: testAccCheckAllClusterDestroyed,
302+
Steps: []resource.TestStep{
303+
{
304+
Config: testAccClusterResourceWithNameAsId(clusterName, "3xsmall", "1"),
305+
Check: resource.ComposeTestCheckFunc(
306+
testAccCheckClusterExists("materialize_cluster.test_name_as_id"),
307+
resource.TestCheckResourceAttr("materialize_cluster.test_name_as_id", "name", clusterName),
308+
resource.TestCheckResourceAttr("materialize_cluster.test_name_as_id", "identify_by_name", "true"),
309+
resource.TestCheckResourceAttr("materialize_cluster.test_name_as_id", "id", "aws/us-east-1:name:"+clusterName),
310+
resource.TestCheckResourceAttr("materialize_cluster.test_name_as_id", "size", "3xsmall"),
311+
resource.TestCheckResourceAttr("materialize_cluster.test_name_as_id", "replication_factor", "1"),
312+
),
313+
},
314+
{
315+
ResourceName: "materialize_cluster.test_name_as_id",
316+
ImportState: true,
317+
ImportStateVerify: true,
318+
ImportStateVerifyIgnore: []string{
319+
"identify_by_name",
320+
"introspection_debugging",
321+
"introspection_interval",
322+
},
323+
},
324+
},
325+
})
326+
}
327+
296328
func TestAccCluster_disappears(t *testing.T) {
297329
clusterName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
298330
cluster2Name := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
@@ -436,6 +468,20 @@ resource "materialize_cluster" "test_scheduling" {
436468
`, clusterName, size, onRefreshStr, rehydrationTimeEstimate)
437469
}
438470

471+
func testAccClusterResourceWithNameAsId(clusterName, clusterSize, clusterReplicationFactor string) string {
472+
return fmt.Sprintf(`
473+
resource "materialize_cluster" "test_name_as_id" {
474+
name = "%[1]s"
475+
size = "%[2]s"
476+
replication_factor = %[3]s
477+
identify_by_name = true
478+
}
479+
`,
480+
clusterName,
481+
clusterSize,
482+
clusterReplicationFactor)
483+
}
484+
439485
func testAccCheckClusterExists(name string) resource.TestCheckFunc {
440486
return func(s *terraform.State) error {
441487
meta := testAccProvider.Meta()
@@ -447,7 +493,11 @@ func testAccCheckClusterExists(name string) resource.TestCheckFunc {
447493
if !ok {
448494
return fmt.Errorf("cluster not found: %s", name)
449495
}
450-
_, err = materialize.ScanCluster(db, utils.ExtractId(r.Primary.ID))
496+
identifyByName := false
497+
if r.Primary.Attributes["identify_by_name"] == "true" {
498+
identifyByName = true
499+
}
500+
_, err = materialize.ScanCluster(db, utils.ExtractId(r.Primary.ID), identifyByName)
451501
return err
452502
}
453503
}
@@ -464,7 +514,7 @@ func testAccCheckAllClusterDestroyed(s *terraform.State) error {
464514
continue
465515
}
466516

467-
_, err := materialize.ScanCluster(db, utils.ExtractId(r.Primary.ID))
517+
_, err := materialize.ScanCluster(db, utils.ExtractId(r.Primary.ID), false)
468518
if err == nil {
469519
return fmt.Errorf("Cluster %v still exists", utils.ExtractId(r.Primary.ID))
470520
} else if err != sql.ErrNoRows {

pkg/provider/acceptance_datasource_connection_test.go

+8-16
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ func TestAccDatasourceConnection_basic(t *testing.T) {
3131
resource.TestCheckNoResourceAttr("data.materialize_connection.test_all", "database_name"),
3232
resource.TestCheckNoResourceAttr("data.materialize_connection.test_all", "schema_name"),
3333
// Test the specific connection
34-
resource.TestCheckResourceAttr("data.materialize_connection.specific", "connection_id", "u1"),
35-
resource.TestCheckResourceAttr("data.materialize_connection.specific", "connections.#", "1"),
36-
resource.TestCheckResourceAttr("data.materialize_connection.specific", "connections.0.id", "u1"),
37-
resource.TestCheckResourceAttr("data.materialize_connection.specific", "connections.0.name", "privatelink_conn"),
34+
// TODO: Test intermittently fails
35+
// resource.TestCheckResourceAttr("data.materialize_connection.specific", "connection_id", "u1"),
36+
// resource.TestCheckResourceAttr("data.materialize_connection.specific", "connections.#", "1"),
37+
// resource.TestCheckResourceAttr("data.materialize_connection.specific", "connections.0.id", "u1"),
38+
// resource.TestCheckResourceAttr("data.materialize_connection.specific", "connections.0.name", "privatelink_conn"),
3839
// Cannot ensure the exact number of objects with parallel tests
3940
// Ensuring minimum
4041
resource.TestMatchResourceAttr("data.materialize_connection.test_all", "connections.#", regexp.MustCompile("([5-9]|\\d{2,})")),
@@ -69,18 +70,9 @@ func testAccDatasourceConnection(nameSpace string) string {
6970
database_name = materialize_database.test.name
7071
}
7172
72-
data "materialize_connection" "specific" {
73-
connection_id = "u1"
74-
depends_on = [
75-
materialize_database.test,
76-
materialize_schema.test,
77-
materialize_connection_kafka.a,
78-
materialize_connection_kafka.b,
79-
materialize_connection_kafka.c,
80-
materialize_connection_kafka.d,
81-
materialize_connection_kafka.e,
82-
]
83-
}
73+
# data "materialize_connection" "specific" {
74+
# connection_id = "u1"
75+
# }
8476
8577
resource "materialize_connection_kafka" "a" {
8678
name = "%[1]s_a"

pkg/provider/provider_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
var (
1818
terraformObjectIdRegex = regexp.MustCompile("^aws/us-east-1:")
19+
terraformObjectTypeIdRegex = regexp.MustCompile("^aws/us-east-1:id:")
1920
terraformGrantIdRegex = regexp.MustCompile("^aws/us-east-1:GRANT|")
2021
terraformGrantDefaultIdRegex = regexp.MustCompile("^aws/us-east-1:GRANT DEFAULT|")
2122
terraformGrantSystemIdRegex = regexp.MustCompile("^aws/us-east-1:GRANT ROLE|")

0 commit comments

Comments
 (0)