Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.9
1.0.10
2 changes: 1 addition & 1 deletion api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type Client interface {
// GetDatabase gets the database by instance resource id and the database name.
GetDatabase(ctx context.Context, databaseName string) (*v1pb.Database, error)
// ListDatabase list the databases.
ListDatabase(ctx context.Context, instanceID, filter string) (*v1pb.ListDatabasesResponse, error)
ListDatabase(ctx context.Context, instanceID, filter string) ([]*v1pb.Database, error)
// UpdateDatabase patches the database.
UpdateDatabase(ctx context.Context, patch *v1pb.Database, updateMasks []string) (*v1pb.Database, error)
// BatchUpdateDatabases batch updates databases.
Expand Down
37 changes: 31 additions & 6 deletions client/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,37 @@
return &res, nil
}

// ListDatabase list the databases.
func (c *client) ListDatabase(ctx context.Context, instanceID, filter string) (*v1pb.ListDatabasesResponse, error) {
requestURL := fmt.Sprintf("%s/%s/instances/%s/databases", c.url, c.version, instanceID)
if filter != "" {
requestURL = fmt.Sprintf("%s?filter=%s", requestURL, url.QueryEscape(filter))
}
// ListDatabase list all databases.
func (c *client) ListDatabase(ctx context.Context, instanceID, filter string) ([]*v1pb.Database, error) {
res := []*v1pb.Database{}
pageToken := ""

for true {

Check failure on line 34 in client/database.go

View workflow job for this annotation

GitHub Actions / golangci

S1006: should use for {} instead of for true {} (gosimple)
resp, err := c.listDatabase(ctx, instanceID, filter, pageToken, 500)
if err != nil {
return nil, err
}
res = append(res, resp.Databases...)
pageToken = resp.NextPageToken
if pageToken == "" {
break
}
}

return res, nil
}

// listDatabase list the databases.
func (c *client) listDatabase(ctx context.Context, instanceID, filter, pageToken string, pageSize int) (*v1pb.ListDatabasesResponse, error) {

Check failure on line 50 in client/database.go

View workflow job for this annotation

GitHub Actions / golangci

confusing-naming: Method 'listDatabase' differs only by capitalization to method 'ListDatabase' in the same source file (revive)
requestURL := fmt.Sprintf(
"%s/%s/instances/%s/databases?filter=%s&page_size=%d&page_token=%s",
c.url,
c.version,
instanceID,
url.QueryEscape(filter),
pageSize,
pageToken,
)

req, err := http.NewRequestWithContext(ctx, "GET", requestURL, nil)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Optional:
Optional:

- `database` (String) The accessible database full name in instances/{instance resource id}/databases/{database name} format
- `expire_timestamp` (String) The expiration timestamp in YYYY-MM-DDThh:mm:ss.000Z format
- `expire_timestamp` (String) The expiration timestamp in YYYY-MM-DDThh:mm:ssZ format
- `row_limit` (Number) The export row limit for exporter role
- `schema` (String) The accessible schema in the database
- `tables` (Set of String) The accessible table list
Expand Down
2 changes: 1 addition & 1 deletion examples/setup/project.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ resource "bytebase_project" "sample_project" {
database = "instances/test-sample-instance/databases/employee"
tables = ["dept_emp", "dept_manager"]
row_limit = 10000
expire_timestamp = "2027-03-09T16:17:49.637Z"
expire_timestamp = "2027-03-09T16:17:49Z"
}
}
}
6 changes: 3 additions & 3 deletions provider/data_source_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func getProjectMembersSchema(computed bool) *schema.Schema {
Type: schema.TypeString,
Computed: computed,
Optional: true,
Description: "The expiration timestamp in YYYY-MM-DDThh:mm:ss.000Z format",
Description: "The expiration timestamp in YYYY-MM-DDThh:mm:ssZ format",
},
},
},
Expand Down Expand Up @@ -300,7 +300,7 @@ func setProject(
project *v1pb.Project,
) diag.Diagnostics {
filter := fmt.Sprintf(`project == "%s"`, project.Name)
listDBResponse, err := client.ListDatabase(ctx, "-", filter)
databases, err := client.ListDatabase(ctx, "-", filter)
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -353,7 +353,7 @@ func setProject(
return diag.Errorf("cannot set postgres_database_tenant_mode for project: %s", err.Error())
}

databaseList := flattenDatabaseList(listDBResponse.Databases)
databaseList := flattenDatabaseList(databases)
if err := d.Set("databases", schema.NewSet(databaseHash, databaseList)); err != nil {
return diag.Errorf("cannot set databases for project: %s", err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions provider/data_source_project_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ func dataSourceProjectListRead(ctx context.Context, d *schema.ResourceData, m in
proj["postgres_database_tenant_mode"] = project.PostgresDatabaseTenantMode

filter := fmt.Sprintf(`project == "%s"`, project.Name)
response, err := c.ListDatabase(ctx, "-", filter)
databases, err := c.ListDatabase(ctx, "-", filter)
if err != nil {
return diag.FromErr(err)
}

proj["databases"] = flattenDatabaseList(response.Databases)
proj["databases"] = flattenDatabaseList(databases)

iamPolicy, err := c.GetProjectIAMPolicy(ctx, project.Name)
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions provider/internal/mock_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func (c *mockClient) GetDatabase(_ context.Context, databaseName string) (*v1pb.
}

// ListDatabase list the databases.
func (c *mockClient) ListDatabase(_ context.Context, instaceID, filter string) (*v1pb.ListDatabasesResponse, error) {
func (c *mockClient) ListDatabase(_ context.Context, instaceID, filter string) ([]*v1pb.Database, error) {
projectID := "-"
if strings.HasPrefix(filter, "project == ") {
projectID = strings.Split(filter, "project == ")[1]
Expand All @@ -391,9 +391,7 @@ func (c *mockClient) ListDatabase(_ context.Context, instaceID, filter string) (
databases = append(databases, db)
}

return &v1pb.ListDatabasesResponse{
Databases: databases,
}, nil
return databases, nil
}

// UpdateDatabase patches the database.
Expand Down
4 changes: 2 additions & 2 deletions provider/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,12 @@ func resourceProjectDelete(ctx context.Context, d *schema.ResourceData, m interf

func updateDatabasesInProject(ctx context.Context, d *schema.ResourceData, client api.Client, projectName string) diag.Diagnostics {
filter := fmt.Sprintf(`project == "%s"`, projectName)
listDB, err := client.ListDatabase(ctx, "-", filter)
databases, err := client.ListDatabase(ctx, "-", filter)
if err != nil {
return diag.Errorf("failed to list database with error: %v", err)
}
existedDBMap := map[string]*v1pb.Database{}
for _, db := range listDB.Databases {
for _, db := range databases {
existedDBMap[db.Name] = db
}

Expand Down
Loading