Skip to content

Commit 1a42921

Browse files
authored
chore: database name list in instance and project (#95)
* chore: use set for unordered collection * chore: update doc * fix: list all databases in the project * chore: update * fix: lint * chore: add databases for instance * chore: more error log * chore: update * chore: update * feat: database resource * fix: lint * feat: support semantic type setting * fix: lint * chore: update * chore: database name list in instance and project * chore: update docs
1 parent 87a42a5 commit 1a42921

File tree

23 files changed

+208
-15
lines changed

23 files changed

+208
-15
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.15
1+
1.0.16

docs/data-sources/instance.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ The instance data source.
2222
### Read-Only
2323

2424
- `data_sources` (Set of Object) (see [below for nested schema](#nestedatt--data_sources))
25+
- `databases` (Set of String) The databases full name in the resource.
2526
- `engine` (String) The instance engine. Support MYSQL, POSTGRES, TIDB, SNOWFLAKE, CLICKHOUSE, MONGODB, SQLITE, REDIS, ORACLE, SPANNER, MSSQL, REDSHIFT, MARIADB, OCEANBASE.
2627
- `engine_version` (String) The engine version.
2728
- `environment` (String) The environment name for your instance in "environments/{resource id}" format.

docs/data-sources/instance_list.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ The instance data source list.
3030
Read-Only:
3131

3232
- `data_sources` (Set of Object) (see [below for nested schema](#nestedobjatt--instances--data_sources))
33+
- `databases` (Set of String)
3334
- `engine` (String)
3435
- `engine_version` (String)
3536
- `environment` (String)

docs/data-sources/project.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ The project data source.
2424
- `allow_modify_statement` (Boolean) Allow modifying statement after issue is created.
2525
- `auto_enable_backup` (Boolean) Whether to automatically enable backup.
2626
- `auto_resolve_issue` (Boolean) Enable auto resolve issue.
27+
- `databases` (Set of String) The databases full name in the resource.
2728
- `enforce_issue_title` (Boolean) Enforce issue title created by user instead of generated by Bytebase.
2829
- `id` (String) The ID of this resource.
2930
- `key` (String) The project key.

docs/data-sources/project_list.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Read-Only:
3232
- `allow_modify_statement` (Boolean)
3333
- `auto_enable_backup` (Boolean)
3434
- `auto_resolve_issue` (Boolean)
35+
- `databases` (Set of String)
3536
- `enforce_issue_title` (Boolean)
3637
- `key` (String)
3738
- `members` (Set of Object) (see [below for nested schema](#nestedobjatt--projects--members))

docs/resources/instance.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ The instance resource.
3131

3232
### Read-Only
3333

34+
- `databases` (Set of String) The databases full name in the resource.
3435
- `engine_version` (String) The engine version.
3536
- `id` (String) The ID of this resource.
3637
- `name` (String) The instance full name in instances/{resource id} format.

docs/resources/project.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ The project resource.
2626
- `allow_modify_statement` (Boolean) Allow modifying statement after issue is created.
2727
- `auto_enable_backup` (Boolean) Whether to automatically enable backup.
2828
- `auto_resolve_issue` (Boolean) Enable auto resolve issue.
29+
- `databases` (Set of String) The databases full name in the resource.
2930
- `enforce_issue_title` (Boolean) Enforce issue title created by user instead of generated by Bytebase.
3031
- `members` (Block Set) The members in the project. (see [below for nested schema](#nestedblock--members))
3132
- `postgres_database_tenant_mode` (Boolean) Whether to enable the database tenant mode for PostgreSQL. If enabled, the issue will be created with the pre-appended "set role <db_owner>" statement.

examples/environments/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
terraform {
33
required_providers {
44
bytebase = {
5-
version = "1.0.15"
5+
version = "1.0.16"
66
# For local development, please use "terraform.local/bytebase/bytebase" instead
77
source = "registry.terraform.io/bytebase/bytebase"
88
}

examples/groups/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
terraform {
22
required_providers {
33
bytebase = {
4-
version = "1.0.15"
4+
version = "1.0.16"
55
# For local development, please use "terraform.local/bytebase/bytebase" instead
66
source = "registry.terraform.io/bytebase/bytebase"
77
}

examples/instances/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
terraform {
33
required_providers {
44
bytebase = {
5-
version = "1.0.15"
5+
version = "1.0.16"
66
# For local development, please use "terraform.local/bytebase/bytebase" instead
77
source = "registry.terraform.io/bytebase/bytebase"
88
}

examples/policies/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
terraform {
22
required_providers {
33
bytebase = {
4-
version = "1.0.15"
4+
version = "1.0.16"
55
# For local development, please use "terraform.local/bytebase/bytebase" instead
66
source = "registry.terraform.io/bytebase/bytebase"
77
}

examples/projects/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
terraform {
33
required_providers {
44
bytebase = {
5-
version = "1.0.15"
5+
version = "1.0.16"
66
# For local development, please use "terraform.local/bytebase/bytebase" instead
77
source = "registry.terraform.io/bytebase/bytebase"
88
}

examples/settings/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
terraform {
22
required_providers {
33
bytebase = {
4-
version = "1.0.15"
4+
version = "1.0.16"
55
# For local development, please use "terraform.local/bytebase/bytebase" instead
66
source = "registry.terraform.io/bytebase/bytebase"
77
}

examples/setup/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
terraform {
22
required_providers {
33
bytebase = {
4-
version = "1.0.15"
4+
version = "1.0.16"
55
# For local development, please use "terraform.local/bytebase/bytebase" instead
66
source = "registry.terraform.io/bytebase/bytebase"
77
}

examples/setup/project.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ resource "bytebase_project" "sample_project" {
3131
expire_timestamp = "2027-03-09T16:17:49Z"
3232
}
3333
}
34+
35+
databases = bytebase_instance.prod.databases
3436
}

examples/users/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
terraform {
22
required_providers {
33
bytebase = {
4-
version = "1.0.15"
4+
version = "1.0.16"
55
# For local development, please use "terraform.local/bytebase/bytebase" instead
66
source = "registry.terraform.io/bytebase/bytebase"
77
}

examples/vcs/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
terraform {
22
required_providers {
33
bytebase = {
4-
version = "1.0.15"
4+
version = "1.0.16"
55
# For local development, please use "terraform.local/bytebase/bytebase" instead
66
source = "registry.terraform.io/bytebase/bytebase"
77
}

provider/data_source_instance.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func dataSourceInstance() *schema.Resource {
125125
},
126126
Set: dataSourceHash,
127127
},
128+
"databases": getDatabasesSchema(true),
128129
},
129130
}
130131
}
@@ -140,5 +141,5 @@ func dataSourceInstanceRead(ctx context.Context, d *schema.ResourceData, m inter
140141

141142
d.SetId(ins.Name)
142143

143-
return setInstanceMessage(ctx, d, ins)
144+
return setInstanceMessage(ctx, c, d, ins)
144145
}

provider/data_source_instance_list.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ func dataSourceInstanceList() *schema.Resource {
136136
},
137137
Set: dataSourceHash,
138138
},
139+
"databases": getDatabasesSchema(true),
139140
},
140141
},
141142
},
@@ -181,6 +182,12 @@ func dataSourceInstanceListRead(ctx context.Context, d *schema.ResourceData, m i
181182
}
182183
ins["data_sources"] = schema.NewSet(dataSourceHash, dataSources)
183184

185+
databases, err := c.ListDatabase(ctx, instance.Name, "")
186+
if err != nil {
187+
return diag.FromErr(err)
188+
}
189+
ins["databases"] = flattenDatabaseList(databases)
190+
184191
instances = append(instances, ins)
185192
}
186193

provider/data_source_project.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,20 @@ func dataSourceProject() *schema.Resource {
8080
Computed: true,
8181
Description: "Whether to enable the database tenant mode for PostgreSQL. If enabled, the issue will be created with the pre-appended \"set role <db_owner>\" statement.",
8282
},
83-
"members": getProjectMembersSchema(true),
83+
"members": getProjectMembersSchema(true),
84+
"databases": getDatabasesSchema(true),
85+
},
86+
}
87+
}
88+
89+
func getDatabasesSchema(computed bool) *schema.Schema {
90+
return &schema.Schema{
91+
Type: schema.TypeSet,
92+
Computed: computed,
93+
Optional: !computed,
94+
Description: "The databases full name in the resource.",
95+
Elem: &schema.Schema{
96+
Type: schema.TypeString,
8497
},
8598
}
8699
}
@@ -231,6 +244,14 @@ func flattenMemberList(iamPolicy *v1pb.IamPolicy) ([]interface{}, error) {
231244
return memberList, nil
232245
}
233246

247+
func flattenDatabaseList(databases []*v1pb.Database) []interface{} {
248+
dbList := []interface{}{}
249+
for _, database := range databases {
250+
dbList = append(dbList, database.Name)
251+
}
252+
return dbList
253+
}
254+
234255
func setProject(
235256
ctx context.Context,
236257
client api.Client,
@@ -241,6 +262,11 @@ func setProject(
241262
"project": project.Name,
242263
})
243264

265+
databases, err := client.ListDatabase(ctx, project.Name, "")
266+
if err != nil {
267+
return diag.FromErr(err)
268+
}
269+
244270
iamPolicy, err := client.GetProjectIAMPolicy(ctx, project.Name)
245271
if err != nil {
246272
return diag.Errorf("failed to get project iam with error: %v", err)
@@ -290,6 +316,17 @@ func setProject(
290316
}
291317

292318
startTime := time.Now()
319+
databaseList := flattenDatabaseList(databases)
320+
if err := d.Set("databases", databaseList); err != nil {
321+
return diag.Errorf("cannot set databases for project: %s", err.Error())
322+
}
323+
tflog.Debug(ctx, "[read project] set project databases", map[string]interface{}{
324+
"project": project.Name,
325+
"databases": len(databases),
326+
"ms": time.Since(startTime).Milliseconds(),
327+
})
328+
329+
startTime = time.Now()
293330
memberList, err := flattenMemberList(iamPolicy)
294331
if err != nil {
295332
return diag.FromErr(err)

provider/data_source_project_list.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ func dataSourceProjectList() *schema.Resource {
8383
Computed: true,
8484
Description: "Whether to enable the database tenant mode for PostgreSQL. If enabled, the issue will be created with the pre-appended \"set role <db_owner>\" statement.",
8585
},
86-
"members": getProjectMembersSchema(true),
86+
"members": getProjectMembersSchema(true),
87+
"databases": getDatabasesSchema(true),
8788
},
8889
},
8990
},
@@ -122,6 +123,14 @@ func dataSourceProjectListRead(ctx context.Context, d *schema.ResourceData, m in
122123
proj["skip_backup_errors"] = project.AllowModifyStatement
123124
proj["postgres_database_tenant_mode"] = project.PostgresDatabaseTenantMode
124125

126+
databases, err := c.ListDatabase(ctx, project.Name, "")
127+
if err != nil {
128+
return diag.FromErr(err)
129+
}
130+
131+
databaseList := flattenDatabaseList(databases)
132+
proj["databases"] = databaseList
133+
125134
iamPolicy, err := c.GetProjectIAMPolicy(ctx, project.Name)
126135
if err != nil {
127136
return diag.Errorf("failed to get project iam with error: %v", err)

provider/resource_instance.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ func resourceInstance() *schema.Resource {
181181
},
182182
Set: dataSourceHash,
183183
},
184+
"databases": getDatabasesSchema(true),
184185
},
185186
}
186187
}
@@ -338,7 +339,7 @@ func resourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interfa
338339
return diag.FromErr(err)
339340
}
340341

341-
resp := setInstanceMessage(ctx, d, instance)
342+
resp := setInstanceMessage(ctx, c, d, instance)
342343
tflog.Debug(ctx, "[read instance] read instance finished", map[string]interface{}{
343344
"instance": instance.Name,
344345
})
@@ -454,6 +455,7 @@ func resourceInstanceDelete(ctx context.Context, d *schema.ResourceData, m inter
454455

455456
func setInstanceMessage(
456457
ctx context.Context,
458+
client api.Client,
457459
d *schema.ResourceData,
458460
instance *v1pb.Instance,
459461
) diag.Diagnostics {
@@ -503,6 +505,19 @@ func setInstanceMessage(
503505
return diag.Errorf("cannot set data_sources for instance: %s", err.Error())
504506
}
505507

508+
tflog.Debug(ctx, "[read instance] start set instance databases", map[string]interface{}{
509+
"instance": instance.Name,
510+
})
511+
512+
databases, err := client.ListDatabase(ctx, instance.Name, "")
513+
if err != nil {
514+
return diag.FromErr(err)
515+
}
516+
databaseList := flattenDatabaseList(databases)
517+
if err := d.Set("databases", databaseList); err != nil {
518+
return diag.Errorf("cannot set databases for instance: %s", err.Error())
519+
}
520+
506521
return nil
507522
}
508523

0 commit comments

Comments
 (0)