Skip to content

Commit 14b6d27

Browse files
authored
chore: instance with external_secret (#142)
* chore: support more settings * chore: multiple optimize * chore: update version * chore: update docs * fix: lint * fix: lint * fix: lint * chore: update dependency version * chore: update * chore: update * chore: instance with external_secret
1 parent 5fefe2c commit 14b6d27

File tree

12 files changed

+120
-45
lines changed

12 files changed

+120
-45
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.8.6
1+
3.8.7

docs/data-sources/instance.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ The instance data source.
3535
- `id` (String) The ID of this resource.
3636
- `maximum_connections` (Number) The maximum number of connections. The default value is 10.
3737
- `name` (String) The instance full name in instances/{resource id} format.
38+
- `sync_databases` (Set of String) Enable sync for following databases. Default empty, means sync all schemas & databases.
3839
- `sync_interval` (Number) How often the instance is synced in seconds. Default 0, means never sync.
3940
- `title` (String) The instance title.
4041

docs/data-sources/instance_list.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Read-Only:
4444
- `maximum_connections` (Number)
4545
- `name` (String)
4646
- `resource_id` (String)
47+
- `sync_databases` (Set of String)
4748
- `sync_interval` (Number)
4849
- `title` (String)
4950

docs/resources/instance.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ The instance resource.
3737
- `engine_version` (String) The engine version.
3838
- `id` (String) The ID of this resource.
3939
- `name` (String) The instance full name in instances/{resource id} format.
40+
- `sync_databases` (Set of String) Enable sync for following databases. Default empty, means sync all schemas & databases.
4041

4142
<a id="nestedblock--data_sources"></a>
4243
### Nested Schema for `data_sources`

examples/setup/instance.tf

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,59 @@ resource "bytebase_instance" "prod" {
6363
port = "54321"
6464
}
6565
}
66+
67+
# Instance with external_secret
68+
#
69+
# Option 1, use external_secret (recommend)
70+
# resource "bytebase_instance" "aws" {
71+
# resource_id = "instance-with-aws"
72+
# environment = "environments/test"
73+
# title = "Instance with AWS"
74+
# engine = "POSTGRES"
75+
# activation = true # activation is required to be true to enable the external_secret feature
76+
77+
# data_sources {
78+
# id = "admin data source"
79+
# type = "ADMIN"
80+
# host = "127.0.0.1"
81+
# port = "54321"
82+
# username = "bytebase"
83+
# external_secret {
84+
# aws_secrets_manager {
85+
# secret_name = "bytebase-external-secret"
86+
# password_key_name = "db_password"
87+
# }
88+
# }
89+
# }
90+
# }
91+
#
92+
# Option 2, work with aws provider
93+
# terraform {
94+
# required_providers {
95+
# aws = {
96+
# source = "hashicorp/aws"
97+
# version = "6.4.0"
98+
# }
99+
# }
100+
# }
101+
#
102+
# Retrieve the secret
103+
# data "aws_secretsmanager_secret_version" "bytebase" {
104+
# secret_id = "bytebase-external-secret"
105+
# }
106+
107+
# resource "bytebase_instance" "aws" {
108+
# resource_id = "instance-with-aws"
109+
# environment = "environments/test"
110+
# title = "Instance with AWS"
111+
# engine = "POSTGRES"
112+
113+
# data_sources {
114+
# id = "admin data source"
115+
# type = "ADMIN"
116+
# host = "127.0.0.1"
117+
# port = "54321"
118+
# username = "bytebase"
119+
# password = jsondecode(data.aws_secretsmanager_secret_version.bytebase.secret_string)["db_password"] # db_password is the secret key name in AWS Secret Manager
120+
# }
121+
# }

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 = "3.8.2"
4+
version = "3.8.7"
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
@@ -142,7 +142,8 @@ func dataSourceInstance() *schema.Resource {
142142
Default: false,
143143
Description: "List all databases in this instance. If false, will only list 500 databases.",
144144
},
145-
"databases": getDatabasesSchema(true),
145+
"databases": getDatabasesSchema(true),
146+
"sync_databases": getSyncDatabasesSchema(true),
146147
},
147148
}
148149
}

provider/data_source_instance_list.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ func dataSourceInstanceList() *schema.Resource {
126126
Computed: true,
127127
Description: "The maximum number of connections. The default value is 10.",
128128
},
129+
"sync_databases": getSyncDatabasesSchema(true),
129130
"data_sources": {
130131
Type: schema.TypeSet,
131132
Computed: true,
@@ -251,8 +252,11 @@ func dataSourceInstanceListRead(ctx context.Context, d *schema.ResourceData, m i
251252
ins["engine_version"] = instance.EngineVersion
252253
ins["external_link"] = instance.ExternalLink
253254
ins["environment"] = instance.Environment
254-
ins["sync_interval"] = instance.GetSyncInterval().GetSeconds()
255+
if v := instance.GetSyncInterval(); v != nil {
256+
ins["sync_interval"] = v.GetSeconds()
257+
}
255258
ins["maximum_connections"] = instance.GetMaximumConnections()
259+
ins["sync_databases"] = instance.SyncDatabases
256260

257261
dataSources, err := flattenDataSourceList(d, instance.DataSources)
258262
if err != nil {

provider/data_source_policy.go

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package provider
22

33
import (
4-
"bytes"
54
"context"
65
"fmt"
76
"strings"
@@ -143,7 +142,6 @@ func getMaskingExceptionPolicySchema(computed bool) *schema.Schema {
143142
},
144143
},
145144
},
146-
Set: exceptionHash,
147145
},
148146
},
149147
},
@@ -485,36 +483,7 @@ func flattenMaskingExceptionPolicy(p *v1pb.MaskingExceptionPolicy) ([]interface{
485483
exceptionList = append(exceptionList, raw)
486484
}
487485
policy := map[string]interface{}{
488-
"exceptions": schema.NewSet(exceptionHash, exceptionList),
486+
"exceptions": exceptionList,
489487
}
490488
return []interface{}{policy}, nil
491489
}
492-
493-
func exceptionHash(rawException interface{}) int {
494-
var buf bytes.Buffer
495-
exception := rawException.(map[string]interface{})
496-
497-
if v, ok := exception["database"].(string); ok {
498-
_, _ = buf.WriteString(fmt.Sprintf("%s-", v))
499-
}
500-
if v, ok := exception["schema"].(string); ok {
501-
_, _ = buf.WriteString(fmt.Sprintf("%s-", v))
502-
}
503-
if v, ok := exception["table"].(string); ok {
504-
_, _ = buf.WriteString(fmt.Sprintf("%s-", v))
505-
}
506-
if v, ok := exception["column"].(string); ok {
507-
_, _ = buf.WriteString(fmt.Sprintf("%s-", v))
508-
}
509-
if v, ok := exception["member"].(string); ok {
510-
_, _ = buf.WriteString(fmt.Sprintf("%s-", v))
511-
}
512-
if v, ok := exception["action"].(string); ok {
513-
_, _ = buf.WriteString(fmt.Sprintf("%s-", v))
514-
}
515-
if v, ok := exception["expire_timestamp"].(string); ok {
516-
_, _ = buf.WriteString(fmt.Sprintf("%s-", v))
517-
}
518-
519-
return internal.ToHashcodeInt(buf.String())
520-
}

provider/internal/resource.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ func ResourceDelete(ctx context.Context, d *schema.ResourceData, m interface{})
5353
var diags diag.Diagnostics
5454

5555
if err := c.DeleteResource(ctx, fullName); err != nil {
56-
return diag.FromErr(err)
56+
// Check if the resource was deleted outside of Terraform
57+
if !isNotFoundError(err) {
58+
return diag.FromErr(err)
59+
}
5760
}
5861

5962
d.SetId("")

0 commit comments

Comments
 (0)