Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/data-sources/instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The instance data source.
### Read-Only

- `data_sources` (Set of Object) (see [below for nested schema](#nestedatt--data_sources))
- `databases` (Set of Object) The databases in the resource. (see [below for nested schema](#nestedatt--databases))
- `engine` (String) The instance engine. Support MYSQL, POSTGRES, TIDB, SNOWFLAKE, CLICKHOUSE, MONGODB, SQLITE, REDIS, ORACLE, SPANNER, MSSQL, REDSHIFT, MARIADB, OCEANBASE.
- `engine_version` (String) The engine version.
- `environment` (String) The environment name for your instance in "environments/{resource id}" format.
Expand Down Expand Up @@ -49,3 +50,17 @@ Read-Only:
- `username` (String)


<a id="nestedatt--databases"></a>
### Nested Schema for `databases`

Read-Only:

- `environment` (String)
- `labels` (Map of String)
- `name` (String)
- `project` (String)
- `schema_version` (String)
- `successful_sync_time` (String)
- `sync_state` (String)


17 changes: 16 additions & 1 deletion docs/data-sources/instance_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ The instance data source list.

Read-Only:

- `data_sources` (List of Object) (see [below for nested schema](#nestedobjatt--instances--data_sources))
- `data_sources` (Set of Object) (see [below for nested schema](#nestedobjatt--instances--data_sources))
- `databases` (Set of Object) (see [below for nested schema](#nestedobjatt--instances--databases))
- `engine` (String)
- `engine_version` (String)
- `environment` (String)
Expand Down Expand Up @@ -57,3 +58,17 @@ Read-Only:
- `username` (String)


<a id="nestedobjatt--instances--databases"></a>
### Nested Schema for `instances.databases`

Read-Only:

- `environment` (String)
- `labels` (Map of String)
- `name` (String)
- `project` (String)
- `schema_version` (String)
- `successful_sync_time` (String)
- `sync_state` (String)


3 changes: 2 additions & 1 deletion docs/data-sources/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The project data source.
- `allow_modify_statement` (Boolean) Allow modifying statement after issue is created.
- `auto_enable_backup` (Boolean) Whether to automatically enable backup.
- `auto_resolve_issue` (Boolean) Enable auto resolve issue.
- `databases` (Set of Object) The databases in the project. (see [below for nested schema](#nestedatt--databases))
- `databases` (Set of Object) The databases in the resource. (see [below for nested schema](#nestedatt--databases))
- `enforce_issue_title` (Boolean) Enforce issue title created by user instead of generated by Bytebase.
- `id` (String) The ID of this resource.
- `key` (String) The project key.
Expand All @@ -43,6 +43,7 @@ Read-Only:
- `environment` (String)
- `labels` (Map of String)
- `name` (String)
- `project` (String)
- `schema_version` (String)
- `successful_sync_time` (String)
- `sync_state` (String)
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/project_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Read-Only:
- `environment` (String)
- `labels` (Map of String)
- `name` (String)
- `project` (String)
- `schema_version` (String)
- `successful_sync_time` (String)
- `sync_state` (String)
Expand Down
15 changes: 15 additions & 0 deletions docs/resources/instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ The instance resource.

### Read-Only

- `databases` (Set of Object) The databases in the resource. (see [below for nested schema](#nestedatt--databases))
- `engine_version` (String) The engine version.
- `id` (String) The ID of this resource.
- `name` (String) The instance full name in instances/{resource id} format.
Expand All @@ -55,3 +56,17 @@ Optional:
- `username` (String) The connection user name used by Bytebase to perform DDL and DML operations.


<a id="nestedatt--databases"></a>
### Nested Schema for `databases`

Read-Only:

- `environment` (String)
- `labels` (Map of String)
- `name` (String)
- `project` (String)
- `schema_version` (String)
- `successful_sync_time` (String)
- `sync_state` (String)


3 changes: 2 additions & 1 deletion docs/resources/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ The project resource.
- `allow_modify_statement` (Boolean) Allow modifying statement after issue is created.
- `auto_enable_backup` (Boolean) Whether to automatically enable backup.
- `auto_resolve_issue` (Boolean) Enable auto resolve issue.
- `databases` (Block Set) The databases in the project. (see [below for nested schema](#nestedblock--databases))
- `databases` (Block Set) The databases in the resource. (see [below for nested schema](#nestedblock--databases))
- `enforce_issue_title` (Boolean) Enforce issue title created by user instead of generated by Bytebase.
- `members` (Block Set) The members in the project. (see [below for nested schema](#nestedblock--members))
- `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.
Expand All @@ -49,6 +49,7 @@ Optional:
Read-Only:

- `environment` (String) The database environment.
- `project` (String) The project full name for the database.
- `schema_version` (String) The version of database schema.
- `successful_sync_time` (String) The latest synchronization time.
- `sync_state` (String) The existence of a database on latest sync.
Expand Down
3 changes: 2 additions & 1 deletion provider/data_source_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func dataSourceInstance() *schema.Resource {
},
Set: dataSourceHash,
},
"databases": getDatabasesSchema(true),
},
}
}
Expand All @@ -140,5 +141,5 @@ func dataSourceInstanceRead(ctx context.Context, d *schema.ResourceData, m inter

d.SetId(ins.Name)

return setInstanceMessage(d, ins)
return setInstanceMessage(ctx, c, d, ins)
}
13 changes: 11 additions & 2 deletions provider/data_source_instance_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func dataSourceInstanceList() *schema.Resource {
Description: "The maximum number of connections. The default value is 10.",
},
"data_sources": {
Type: schema.TypeList,
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -134,7 +134,9 @@ func dataSourceInstanceList() *schema.Resource {
},
},
},
Set: dataSourceHash,
},
"databases": getDatabasesSchema(true),
},
},
},
Expand Down Expand Up @@ -178,7 +180,14 @@ func dataSourceInstanceListRead(ctx context.Context, d *schema.ResourceData, m i
if err != nil {
return diag.FromErr(err)
}
ins["data_sources"] = dataSources
ins["data_sources"] = schema.NewSet(dataSourceHash, dataSources)

databases, err := c.ListDatabase(ctx, instanceID, "")
if err != nil {
return diag.FromErr(err)
}
databaseList := flattenDatabaseList(databases)
ins["databases"] = schema.NewSet(databaseHash, databaseList)

instances = append(instances, ins)
}
Expand Down
12 changes: 9 additions & 3 deletions provider/data_source_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,18 @@ func dataSourceProject() *schema.Resource {
Computed: true,
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.",
},
"databases": getProjectDatabasesSchema(true),
"databases": getDatabasesSchema(true),
"members": getProjectMembersSchema(true),
},
}
}

func getProjectDatabasesSchema(computed bool) *schema.Schema {
func getDatabasesSchema(computed bool) *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Computed: computed,
Optional: !computed,
Description: "The databases in the project.",
Description: "The databases in the resource.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Expand All @@ -99,6 +99,11 @@ func getProjectDatabasesSchema(computed bool) *schema.Schema {
Optional: !computed,
Description: "The database full name in instances/{instance id}/databases/{db name} format.",
},
"project": {
Type: schema.TypeString,
Computed: true,
Description: "The project full name for the database.",
},
"environment": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -222,6 +227,7 @@ func flattenDatabaseList(databases []*v1pb.Database) []interface{} {
for _, database := range databases {
db := map[string]interface{}{}
db["name"] = database.Name
db["project"] = database.Project
db["environment"] = database.Environment
db["sync_state"] = database.SyncState.String()
db["successful_sync_time"] = database.SuccessfulSyncTime.AsTime().UTC().Format(time.RFC3339)
Expand Down
7 changes: 4 additions & 3 deletions provider/data_source_project_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func dataSourceProjectList() *schema.Resource {
Computed: true,
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.",
},
"databases": getProjectDatabasesSchema(true),
"databases": getDatabasesSchema(true),
"members": getProjectMembersSchema(true),
},
},
Expand Down Expand Up @@ -130,7 +130,8 @@ func dataSourceProjectListRead(ctx context.Context, d *schema.ResourceData, m in
return diag.FromErr(err)
}

proj["databases"] = flattenDatabaseList(databases)
databaseList := flattenDatabaseList(databases)
proj["databases"] = schema.NewSet(databaseHash, databaseList)

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

projects = append(projects, proj)
}
Expand Down
19 changes: 17 additions & 2 deletions provider/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func resourceInstance() *schema.Resource {
},
Set: dataSourceHash,
},
"databases": getDatabasesSchema(true),
},
}
}
Expand Down Expand Up @@ -325,7 +326,7 @@ func resourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interfa
return diag.FromErr(err)
}

return setInstanceMessage(d, instance)
return setInstanceMessage(ctx, c, d, instance)
}

func resourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down Expand Up @@ -436,7 +437,12 @@ func resourceInstanceDelete(ctx context.Context, d *schema.ResourceData, m inter
return diags
}

func setInstanceMessage(d *schema.ResourceData, instance *v1pb.Instance) diag.Diagnostics {
func setInstanceMessage(
ctx context.Context,
client api.Client,
d *schema.ResourceData,
instance *v1pb.Instance,
) diag.Diagnostics {
instanceID, err := internal.GetInstanceID(instance.Name)
if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -479,6 +485,15 @@ func setInstanceMessage(d *schema.ResourceData, instance *v1pb.Instance) diag.Di
return diag.Errorf("cannot set data_sources for instance: %s", err.Error())
}

databases, err := client.ListDatabase(ctx, instanceID, "")
if err != nil {
return diag.FromErr(err)
}
databaseList := flattenDatabaseList(databases)
if err := d.Set("databases", schema.NewSet(databaseHash, databaseList)); err != nil {
return diag.Errorf("cannot set databases for instance: %s", err.Error())
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion provider/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func resourceProjct() *schema.Resource {
Default: false,
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.",
},
"databases": getProjectDatabasesSchema(false),
"databases": getDatabasesSchema(false),
"members": getProjectMembersSchema(false),
},
}
Expand Down