Skip to content

Commit b87b6bf

Browse files
authored
chore: add databases for instance (#88)
* 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
1 parent c3b853d commit b87b6bf

File tree

12 files changed

+95
-15
lines changed

12 files changed

+95
-15
lines changed

docs/data-sources/instance.md

Lines changed: 15 additions & 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 Object) The databases in the resource. (see [below for nested schema](#nestedatt--databases))
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.
@@ -49,3 +50,17 @@ Read-Only:
4950
- `username` (String)
5051

5152

53+
<a id="nestedatt--databases"></a>
54+
### Nested Schema for `databases`
55+
56+
Read-Only:
57+
58+
- `environment` (String)
59+
- `labels` (Map of String)
60+
- `name` (String)
61+
- `project` (String)
62+
- `schema_version` (String)
63+
- `successful_sync_time` (String)
64+
- `sync_state` (String)
65+
66+

docs/data-sources/instance_list.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ The instance data source list.
2929

3030
Read-Only:
3131

32-
- `data_sources` (List of Object) (see [below for nested schema](#nestedobjatt--instances--data_sources))
32+
- `data_sources` (Set of Object) (see [below for nested schema](#nestedobjatt--instances--data_sources))
33+
- `databases` (Set of Object) (see [below for nested schema](#nestedobjatt--instances--databases))
3334
- `engine` (String)
3435
- `engine_version` (String)
3536
- `environment` (String)
@@ -57,3 +58,17 @@ Read-Only:
5758
- `username` (String)
5859

5960

61+
<a id="nestedobjatt--instances--databases"></a>
62+
### Nested Schema for `instances.databases`
63+
64+
Read-Only:
65+
66+
- `environment` (String)
67+
- `labels` (Map of String)
68+
- `name` (String)
69+
- `project` (String)
70+
- `schema_version` (String)
71+
- `successful_sync_time` (String)
72+
- `sync_state` (String)
73+
74+

docs/data-sources/project.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +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 Object) The databases in the project. (see [below for nested schema](#nestedatt--databases))
27+
- `databases` (Set of Object) The databases in the resource. (see [below for nested schema](#nestedatt--databases))
2828
- `enforce_issue_title` (Boolean) Enforce issue title created by user instead of generated by Bytebase.
2929
- `id` (String) The ID of this resource.
3030
- `key` (String) The project key.
@@ -43,6 +43,7 @@ Read-Only:
4343
- `environment` (String)
4444
- `labels` (Map of String)
4545
- `name` (String)
46+
- `project` (String)
4647
- `schema_version` (String)
4748
- `successful_sync_time` (String)
4849
- `sync_state` (String)

docs/data-sources/project_list.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Read-Only:
5151
- `environment` (String)
5252
- `labels` (Map of String)
5353
- `name` (String)
54+
- `project` (String)
5455
- `schema_version` (String)
5556
- `successful_sync_time` (String)
5657
- `sync_state` (String)

docs/resources/instance.md

Lines changed: 15 additions & 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 Object) The databases in the resource. (see [below for nested schema](#nestedatt--databases))
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.
@@ -55,3 +56,17 @@ Optional:
5556
- `username` (String) The connection user name used by Bytebase to perform DDL and DML operations.
5657

5758

59+
<a id="nestedatt--databases"></a>
60+
### Nested Schema for `databases`
61+
62+
Read-Only:
63+
64+
- `environment` (String)
65+
- `labels` (Map of String)
66+
- `name` (String)
67+
- `project` (String)
68+
- `schema_version` (String)
69+
- `successful_sync_time` (String)
70+
- `sync_state` (String)
71+
72+

docs/resources/project.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +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` (Block Set) The databases in the project. (see [below for nested schema](#nestedblock--databases))
29+
- `databases` (Block Set) The databases in the resource. (see [below for nested schema](#nestedblock--databases))
3030
- `enforce_issue_title` (Boolean) Enforce issue title created by user instead of generated by Bytebase.
3131
- `members` (Block Set) The members in the project. (see [below for nested schema](#nestedblock--members))
3232
- `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.
@@ -49,6 +49,7 @@ Optional:
4949
Read-Only:
5050

5151
- `environment` (String) The database environment.
52+
- `project` (String) The project full name for the database.
5253
- `schema_version` (String) The version of database schema.
5354
- `successful_sync_time` (String) The latest synchronization time.
5455
- `sync_state` (String) The existence of a database on latest sync.

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(d, ins)
144+
return setInstanceMessage(ctx, c, d, ins)
144145
}

provider/data_source_instance_list.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func dataSourceInstanceList() *schema.Resource {
7474
Description: "The maximum number of connections. The default value is 10.",
7575
},
7676
"data_sources": {
77-
Type: schema.TypeList,
77+
Type: schema.TypeSet,
7878
Computed: true,
7979
Elem: &schema.Resource{
8080
Schema: map[string]*schema.Schema{
@@ -134,7 +134,9 @@ func dataSourceInstanceList() *schema.Resource {
134134
},
135135
},
136136
},
137+
Set: dataSourceHash,
137138
},
139+
"databases": getDatabasesSchema(true),
138140
},
139141
},
140142
},
@@ -178,7 +180,14 @@ func dataSourceInstanceListRead(ctx context.Context, d *schema.ResourceData, m i
178180
if err != nil {
179181
return diag.FromErr(err)
180182
}
181-
ins["data_sources"] = dataSources
183+
ins["data_sources"] = schema.NewSet(dataSourceHash, dataSources)
184+
185+
databases, err := c.ListDatabase(ctx, instanceID, "")
186+
if err != nil {
187+
return diag.FromErr(err)
188+
}
189+
databaseList := flattenDatabaseList(databases)
190+
ins["databases"] = schema.NewSet(databaseHash, databaseList)
182191

183192
instances = append(instances, ins)
184193
}

provider/data_source_project.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,18 @@ func dataSourceProject() *schema.Resource {
7979
Computed: true,
8080
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.",
8181
},
82-
"databases": getProjectDatabasesSchema(true),
82+
"databases": getDatabasesSchema(true),
8383
"members": getProjectMembersSchema(true),
8484
},
8585
}
8686
}
8787

88-
func getProjectDatabasesSchema(computed bool) *schema.Schema {
88+
func getDatabasesSchema(computed bool) *schema.Schema {
8989
return &schema.Schema{
9090
Type: schema.TypeSet,
9191
Computed: computed,
9292
Optional: !computed,
93-
Description: "The databases in the project.",
93+
Description: "The databases in the resource.",
9494
Elem: &schema.Resource{
9595
Schema: map[string]*schema.Schema{
9696
"name": {
@@ -99,6 +99,11 @@ func getProjectDatabasesSchema(computed bool) *schema.Schema {
9999
Optional: !computed,
100100
Description: "The database full name in instances/{instance id}/databases/{db name} format.",
101101
},
102+
"project": {
103+
Type: schema.TypeString,
104+
Computed: true,
105+
Description: "The project full name for the database.",
106+
},
102107
"environment": {
103108
Type: schema.TypeString,
104109
Computed: true,
@@ -222,6 +227,7 @@ func flattenDatabaseList(databases []*v1pb.Database) []interface{} {
222227
for _, database := range databases {
223228
db := map[string]interface{}{}
224229
db["name"] = database.Name
230+
db["project"] = database.Project
225231
db["environment"] = database.Environment
226232
db["sync_state"] = database.SyncState.String()
227233
db["successful_sync_time"] = database.SuccessfulSyncTime.AsTime().UTC().Format(time.RFC3339)

provider/data_source_project_list.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func dataSourceProjectList() *schema.Resource {
8484
Computed: true,
8585
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.",
8686
},
87-
"databases": getProjectDatabasesSchema(true),
87+
"databases": getDatabasesSchema(true),
8888
"members": getProjectMembersSchema(true),
8989
},
9090
},
@@ -130,7 +130,8 @@ func dataSourceProjectListRead(ctx context.Context, d *schema.ResourceData, m in
130130
return diag.FromErr(err)
131131
}
132132

133-
proj["databases"] = flattenDatabaseList(databases)
133+
databaseList := flattenDatabaseList(databases)
134+
proj["databases"] = schema.NewSet(databaseHash, databaseList)
134135

135136
iamPolicy, err := c.GetProjectIAMPolicy(ctx, project.Name)
136137
if err != nil {
@@ -140,7 +141,7 @@ func dataSourceProjectListRead(ctx context.Context, d *schema.ResourceData, m in
140141
if err != nil {
141142
return diag.FromErr(err)
142143
}
143-
proj["members"] = memberList
144+
proj["members"] = schema.NewSet(memberHash, memberList)
144145

145146
projects = append(projects, proj)
146147
}

0 commit comments

Comments
 (0)