Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6ee12bb
chore: use set for unordered collection
ecmadao Feb 5, 2025
92bff81
chore: update doc
ecmadao Feb 5, 2025
c0edb6c
fix: list all databases in the project
ecmadao Feb 6, 2025
dee5b4b
Merge branch 'main' of github.com:bytebase/terraform-provider-bytebas…
ecmadao Feb 6, 2025
404484c
chore: update
ecmadao Feb 6, 2025
78b4069
fix: lint
ecmadao Feb 6, 2025
19d7ec2
chore: add databases for instance
ecmadao Feb 6, 2025
c9914af
Merge branch 'main' of github.com:bytebase/terraform-provider-bytebas…
ecmadao Feb 6, 2025
e6ed605
chore: more error log
ecmadao Feb 7, 2025
220cd85
Merge branch 'main' of github.com:bytebase/terraform-provider-bytebas…
ecmadao Feb 7, 2025
e84bd40
Merge branch 'main' of github.com:bytebase/terraform-provider-bytebas…
ecmadao Feb 8, 2025
b9335a0
chore: update
ecmadao Feb 8, 2025
8998132
chore: update
ecmadao Feb 8, 2025
7f3344c
feat: database resource
ecmadao Feb 11, 2025
c3959db
Merge branch 'main' of github.com:bytebase/terraform-provider-bytebas…
ecmadao Feb 11, 2025
c312d64
fix: lint
ecmadao Feb 11, 2025
5b0f1c2
Merge branch 'main' of github.com:bytebase/terraform-provider-bytebas…
ecmadao Feb 11, 2025
732443a
feat: support semantic type setting
ecmadao Feb 11, 2025
cdd4374
fix: lint
ecmadao Feb 11, 2025
609eb05
chore: update
ecmadao Feb 11, 2025
6e86168
chore: database name list in instance and project
ecmadao Feb 13, 2025
bd8ea2f
chore: update docs
ecmadao Feb 13, 2025
a1dd92d
Merge branch 'main' of github.com:bytebase/terraform-provider-bytebas…
ecmadao Feb 13, 2025
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
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.15
1.0.16
1 change: 1 addition & 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 String) The databases full name in the resource.
- `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
1 change: 1 addition & 0 deletions docs/data-sources/instance_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ The instance data source list.
Read-Only:

- `data_sources` (Set of Object) (see [below for nested schema](#nestedobjatt--instances--data_sources))
- `databases` (Set of String)
- `engine` (String)
- `engine_version` (String)
- `environment` (String)
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +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 String) The databases full name in the resource.
- `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 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 @@ -32,6 +32,7 @@ Read-Only:
- `allow_modify_statement` (Boolean)
- `auto_enable_backup` (Boolean)
- `auto_resolve_issue` (Boolean)
- `databases` (Set of String)
- `enforce_issue_title` (Boolean)
- `key` (String)
- `members` (Set of Object) (see [below for nested schema](#nestedobjatt--projects--members))
Expand Down
1 change: 1 addition & 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 String) The databases full name in the resource.
- `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 Down
1 change: 1 addition & 0 deletions docs/resources/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +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` (Set of String) The databases full name in the resource.
- `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 Down
2 changes: 1 addition & 1 deletion examples/environments/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
terraform {
required_providers {
bytebase = {
version = "1.0.15"
version = "1.0.16"
# For local development, please use "terraform.local/bytebase/bytebase" instead
source = "registry.terraform.io/bytebase/bytebase"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/groups/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
bytebase = {
version = "1.0.15"
version = "1.0.16"
# For local development, please use "terraform.local/bytebase/bytebase" instead
source = "registry.terraform.io/bytebase/bytebase"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/instances/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
terraform {
required_providers {
bytebase = {
version = "1.0.15"
version = "1.0.16"
# For local development, please use "terraform.local/bytebase/bytebase" instead
source = "registry.terraform.io/bytebase/bytebase"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/policies/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
bytebase = {
version = "1.0.15"
version = "1.0.16"
# For local development, please use "terraform.local/bytebase/bytebase" instead
source = "registry.terraform.io/bytebase/bytebase"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/projects/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
terraform {
required_providers {
bytebase = {
version = "1.0.15"
version = "1.0.16"
# For local development, please use "terraform.local/bytebase/bytebase" instead
source = "registry.terraform.io/bytebase/bytebase"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/settings/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
bytebase = {
version = "1.0.15"
version = "1.0.16"
# For local development, please use "terraform.local/bytebase/bytebase" instead
source = "registry.terraform.io/bytebase/bytebase"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/setup/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
bytebase = {
version = "1.0.15"
version = "1.0.16"
# For local development, please use "terraform.local/bytebase/bytebase" instead
source = "registry.terraform.io/bytebase/bytebase"
}
Expand Down
2 changes: 2 additions & 0 deletions examples/setup/project.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ resource "bytebase_project" "sample_project" {
expire_timestamp = "2027-03-09T16:17:49Z"
}
}

databases = bytebase_instance.prod.databases
}
2 changes: 1 addition & 1 deletion examples/users/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
bytebase = {
version = "1.0.15"
version = "1.0.16"
# For local development, please use "terraform.local/bytebase/bytebase" instead
source = "registry.terraform.io/bytebase/bytebase"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/vcs/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
bytebase = {
version = "1.0.15"
version = "1.0.16"
# For local development, please use "terraform.local/bytebase/bytebase" instead
source = "registry.terraform.io/bytebase/bytebase"
}
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(ctx, d, ins)
return setInstanceMessage(ctx, c, d, ins)
}
7 changes: 7 additions & 0 deletions provider/data_source_instance_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func dataSourceInstanceList() *schema.Resource {
},
Set: dataSourceHash,
},
"databases": getDatabasesSchema(true),
},
},
},
Expand Down Expand Up @@ -181,6 +182,12 @@ func dataSourceInstanceListRead(ctx context.Context, d *schema.ResourceData, m i
}
ins["data_sources"] = schema.NewSet(dataSourceHash, dataSources)

databases, err := c.ListDatabase(ctx, instance.Name, "")
if err != nil {
return diag.FromErr(err)
}
ins["databases"] = flattenDatabaseList(databases)

instances = append(instances, ins)
}

Expand Down
39 changes: 38 additions & 1 deletion provider/data_source_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,20 @@ 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.",
},
"members": getProjectMembersSchema(true),
"members": getProjectMembersSchema(true),
"databases": getDatabasesSchema(true),
},
}
}

func getDatabasesSchema(computed bool) *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Computed: computed,
Optional: !computed,
Description: "The databases full name in the resource.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
}
}
Expand Down Expand Up @@ -231,6 +244,14 @@ func flattenMemberList(iamPolicy *v1pb.IamPolicy) ([]interface{}, error) {
return memberList, nil
}

func flattenDatabaseList(databases []*v1pb.Database) []interface{} {
dbList := []interface{}{}
for _, database := range databases {
dbList = append(dbList, database.Name)
}
return dbList
}

func setProject(
ctx context.Context,
client api.Client,
Expand All @@ -241,6 +262,11 @@ func setProject(
"project": project.Name,
})

databases, err := client.ListDatabase(ctx, project.Name, "")
if err != nil {
return diag.FromErr(err)
}

iamPolicy, err := client.GetProjectIAMPolicy(ctx, project.Name)
if err != nil {
return diag.Errorf("failed to get project iam with error: %v", err)
Expand Down Expand Up @@ -290,6 +316,17 @@ func setProject(
}

startTime := time.Now()
databaseList := flattenDatabaseList(databases)
if err := d.Set("databases", databaseList); err != nil {
return diag.Errorf("cannot set databases for project: %s", err.Error())
}
tflog.Debug(ctx, "[read project] set project databases", map[string]interface{}{
"project": project.Name,
"databases": len(databases),
"ms": time.Since(startTime).Milliseconds(),
})

startTime = time.Now()
memberList, err := flattenMemberList(iamPolicy)
if err != nil {
return diag.FromErr(err)
Expand Down
11 changes: 10 additions & 1 deletion provider/data_source_project_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ 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.",
},
"members": getProjectMembersSchema(true),
"members": getProjectMembersSchema(true),
"databases": getDatabasesSchema(true),
},
},
},
Expand Down Expand Up @@ -122,6 +123,14 @@ func dataSourceProjectListRead(ctx context.Context, d *schema.ResourceData, m in
proj["skip_backup_errors"] = project.AllowModifyStatement
proj["postgres_database_tenant_mode"] = project.PostgresDatabaseTenantMode

databases, err := c.ListDatabase(ctx, project.Name, "")
if err != nil {
return diag.FromErr(err)
}

databaseList := flattenDatabaseList(databases)
proj["databases"] = databaseList

iamPolicy, err := c.GetProjectIAMPolicy(ctx, project.Name)
if err != nil {
return diag.Errorf("failed to get project iam with error: %v", err)
Expand Down
17 changes: 16 additions & 1 deletion provider/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func resourceInstance() *schema.Resource {
},
Set: dataSourceHash,
},
"databases": getDatabasesSchema(true),
},
}
}
Expand Down Expand Up @@ -338,7 +339,7 @@ func resourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interfa
return diag.FromErr(err)
}

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

func setInstanceMessage(
ctx context.Context,
client api.Client,
d *schema.ResourceData,
instance *v1pb.Instance,
) diag.Diagnostics {
Expand Down Expand Up @@ -503,6 +505,19 @@ func setInstanceMessage(
return diag.Errorf("cannot set data_sources for instance: %s", err.Error())
}

tflog.Debug(ctx, "[read instance] start set instance databases", map[string]interface{}{
"instance": instance.Name,
})

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

return nil
}

Expand Down
Loading
Loading