Skip to content

Commit 69ea947

Browse files
authored
chore: migrate environment api (#35)
* chore: migrate environment api * fix: go mod tidy
1 parent 12c2687 commit 69ea947

16 files changed

+316
-418
lines changed

api/client.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ type Client interface {
1010

1111
// Environment
1212
// CreateEnvironment creates the environment.
13-
CreateEnvironment(ctx context.Context, create *EnvironmentUpsert) (*Environment, error)
13+
CreateEnvironment(ctx context.Context, environmentID string, create *EnvironmentMessage) (*EnvironmentMessage, error)
1414
// GetEnvironment gets the environment by id.
15-
GetEnvironment(ctx context.Context, environmentID int) (*Environment, error)
15+
GetEnvironment(ctx context.Context, environmentID string) (*EnvironmentMessage, error)
1616
// ListEnvironment finds all environments.
17-
ListEnvironment(ctx context.Context, find *EnvironmentFind) ([]*Environment, error)
17+
ListEnvironment(ctx context.Context, showDeleted bool) (*ListEnvironmentMessage, error)
1818
// UpdateEnvironment updates the environment.
19-
UpdateEnvironment(ctx context.Context, environmentID int, patch *EnvironmentUpsert) (*Environment, error)
19+
UpdateEnvironment(ctx context.Context, environmentID string, patch *EnvironmentPatchMessage) (*EnvironmentMessage, error)
2020
// DeleteEnvironment deletes the environment.
21-
DeleteEnvironment(ctx context.Context, environmentID int) error
21+
DeleteEnvironment(ctx context.Context, environmentID string) error
22+
// UndeleteEnvironment undeletes the environment.
23+
UndeleteEnvironment(ctx context.Context, environmentID string) (*EnvironmentMessage, error)
2224

2325
// Instance
2426
// ListInstance will return instances in environment.

api/environment.go

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,26 @@
11
package api
22

3-
// Environment is the API message for an environment.
4-
type Environment struct {
5-
ID int `json:"id"`
6-
7-
// Related fields
8-
EnvironmentTierPolicy *EnvironmentTierPolicy `json:"environmentTierPolicy,omitempty"`
9-
PipelineApprovalPolicy *PipelineApprovalPolicy `json:"pipelineApprovalPolicy,omitempty"`
10-
BackupPlanPolicy *BackupPlanPolicy `json:"backupPlanPolicy,omitempty"`
3+
// EnvironmentMessage is the API message for an environment.
4+
type EnvironmentMessage struct {
5+
UID string `json:"uid"`
116

127
// Domain specific fields
138
Name string `json:"name"`
9+
Title string `json:"title"`
1410
Order int `json:"order"`
11+
State State `json:"state,omitempty"`
12+
Tier string `json:"tier"`
1513
}
1614

17-
// EnvironmentFind is the API message for finding environment.
18-
type EnvironmentFind struct {
19-
// Domain specific fields
20-
Name string `url:"name,omitempty"`
15+
// ListEnvironmentMessage is the API message for list environment response.
16+
type ListEnvironmentMessage struct {
17+
Environments []*EnvironmentMessage `json:"environments"`
18+
NextPageToken string `json:"nextPageToken"`
2119
}
2220

23-
// EnvironmentUpsert is the API message for upserting an environment.
24-
type EnvironmentUpsert struct {
25-
// Related fields
26-
EnvironmentTierPolicy *EnvironmentTierPolicy `json:"environmentTierPolicy,omitempty"`
27-
PipelineApprovalPolicy *PipelineApprovalPolicy `json:"pipelineApprovalPolicy,omitempty"`
28-
BackupPlanPolicy *BackupPlanPolicy `json:"backupPlanPolicy,omitempty"`
29-
30-
// Domain specific fields
31-
Name *string `json:"name,omitempty"`
21+
// EnvironmentPatchMessage is the API message to patch the environment.
22+
type EnvironmentPatchMessage struct {
23+
Title *string `json:"title,omitempty"`
3224
Order *int `json:"order,omitempty"`
33-
}
34-
35-
// HasChange returns if the patch struct has the value to update.
36-
func (e *EnvironmentUpsert) HasChange() bool {
37-
return e.Name != nil || e.Order != nil || e.EnvironmentTierPolicy != nil || e.PipelineApprovalPolicy != nil || e.BackupPlanPolicy != nil
25+
Tier *string `json:"tier,omitempty"`
3826
}

client/environment.go

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,17 @@ import (
77
"net/http"
88
"strings"
99

10-
"github.com/google/go-querystring/query"
11-
1210
"github.com/bytebase/terraform-provider-bytebase/api"
1311
)
1412

1513
// CreateEnvironment creates the environment.
16-
func (c *client) CreateEnvironment(ctx context.Context, create *api.EnvironmentUpsert) (*api.Environment, error) {
14+
func (c *client) CreateEnvironment(ctx context.Context, environmentID string, create *api.EnvironmentMessage) (*api.EnvironmentMessage, error) {
1715
payload, err := json.Marshal(create)
1816
if err != nil {
1917
return nil, err
2018
}
2119

22-
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/environment", c.HostURL), strings.NewReader(string(payload)))
20+
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/environments?environmentId=%s", c.HostURL, environmentID), strings.NewReader(string(payload)))
2321
if err != nil {
2422
return nil, err
2523
}
@@ -29,7 +27,7 @@ func (c *client) CreateEnvironment(ctx context.Context, create *api.EnvironmentU
2927
return nil, err
3028
}
3129

32-
var env api.Environment
30+
var env api.EnvironmentMessage
3331
err = json.Unmarshal(body, &env)
3432
if err != nil {
3533
return nil, err
@@ -39,8 +37,8 @@ func (c *client) CreateEnvironment(ctx context.Context, create *api.EnvironmentU
3937
}
4038

4139
// GetEnvironment gets the environment by id.
42-
func (c *client) GetEnvironment(ctx context.Context, environmentID int) (*api.Environment, error) {
43-
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/environment/%d", c.HostURL, environmentID), nil)
40+
func (c *client) GetEnvironment(ctx context.Context, environmentID string) (*api.EnvironmentMessage, error) {
41+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/environments/%s", c.HostURL, environmentID), nil)
4442
if err != nil {
4543
return nil, err
4644
}
@@ -50,7 +48,7 @@ func (c *client) GetEnvironment(ctx context.Context, environmentID int) (*api.En
5048
return nil, err
5149
}
5250

53-
var env api.Environment
51+
var env api.EnvironmentMessage
5452
err = json.Unmarshal(body, &env)
5553
if err != nil {
5654
return nil, err
@@ -60,13 +58,8 @@ func (c *client) GetEnvironment(ctx context.Context, environmentID int) (*api.En
6058
}
6159

6260
// ListEnvironment finds all environments.
63-
func (c *client) ListEnvironment(ctx context.Context, find *api.EnvironmentFind) ([]*api.Environment, error) {
64-
q, err := query.Values(find)
65-
if err != nil {
66-
return nil, err
67-
}
68-
69-
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/environment?%s", c.HostURL, q.Encode()), nil)
61+
func (c *client) ListEnvironment(ctx context.Context, showDeleted bool) (*api.ListEnvironmentMessage, error) {
62+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/environments?showDeleted=%v", c.HostURL, showDeleted), nil)
7063
if err != nil {
7164
return nil, err
7265
}
@@ -76,23 +69,34 @@ func (c *client) ListEnvironment(ctx context.Context, find *api.EnvironmentFind)
7669
return nil, err
7770
}
7871

79-
res := []*api.Environment{}
72+
var res api.ListEnvironmentMessage
8073
err = json.Unmarshal(body, &res)
8174
if err != nil {
8275
return nil, err
8376
}
8477

85-
return res, nil
78+
return &res, nil
8679
}
8780

8881
// UpdateEnvironment updates the environment.
89-
func (c *client) UpdateEnvironment(ctx context.Context, environmentID int, patch *api.EnvironmentUpsert) (*api.Environment, error) {
82+
func (c *client) UpdateEnvironment(ctx context.Context, environmentID string, patch *api.EnvironmentPatchMessage) (*api.EnvironmentMessage, error) {
9083
payload, err := json.Marshal(patch)
9184
if err != nil {
9285
return nil, err
9386
}
9487

95-
req, err := http.NewRequestWithContext(ctx, "PATCH", fmt.Sprintf("%s/environment/%d", c.HostURL, environmentID), strings.NewReader(string(payload)))
88+
paths := []string{}
89+
if patch.Title != nil {
90+
paths = append(paths, "environment.title")
91+
}
92+
if patch.Order != nil {
93+
paths = append(paths, "environment.order")
94+
}
95+
if patch.Tier != nil {
96+
paths = append(paths, "environment.tier")
97+
}
98+
99+
req, err := http.NewRequestWithContext(ctx, "PATCH", fmt.Sprintf("%s/environments/%s?update_mask=%s", c.HostURL, environmentID, strings.Join(paths, ",")), strings.NewReader(string(payload)))
96100
if err != nil {
97101
return nil, err
98102
}
@@ -102,7 +106,7 @@ func (c *client) UpdateEnvironment(ctx context.Context, environmentID int, patch
102106
return nil, err
103107
}
104108

105-
var env api.Environment
109+
var env api.EnvironmentMessage
106110
err = json.Unmarshal(body, &env)
107111
if err != nil {
108112
return nil, err
@@ -112,8 +116,8 @@ func (c *client) UpdateEnvironment(ctx context.Context, environmentID int, patch
112116
}
113117

114118
// DeleteEnvironment deletes the environment.
115-
func (c *client) DeleteEnvironment(ctx context.Context, environmentID int) error {
116-
req, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("%s/environment/%d", c.HostURL, environmentID), nil)
119+
func (c *client) DeleteEnvironment(ctx context.Context, environmentID string) error {
120+
req, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("%s/environments/%s", c.HostURL, environmentID), nil)
117121
if err != nil {
118122
return err
119123
}
@@ -123,3 +127,24 @@ func (c *client) DeleteEnvironment(ctx context.Context, environmentID int) error
123127
}
124128
return nil
125129
}
130+
131+
// UndeleteEnvironment undeletes the environment.
132+
func (c *client) UndeleteEnvironment(ctx context.Context, environmentID string) (*api.EnvironmentMessage, error) {
133+
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/environments/%s:undelete", c.HostURL, environmentID), nil)
134+
if err != nil {
135+
return nil, err
136+
}
137+
138+
body, err := c.doRequest(req)
139+
if err != nil {
140+
return nil, err
141+
}
142+
143+
var res api.EnvironmentMessage
144+
err = json.Unmarshal(body, &res)
145+
if err != nil {
146+
return nil, err
147+
}
148+
149+
return &res, nil
150+
}

client/instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (c *client) ListInstance(ctx context.Context, find *api.InstanceFindMessage
3333

3434
// GetInstance gets the instance by id.
3535
func (c *client) GetInstance(ctx context.Context, find *api.InstanceFindMessage) (*api.InstanceMessage, error) {
36-
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/environments/%s/instances/%s?showDeleted=%v", c.HostURL, find.EnvironmentID, find.InstanceID, find.ShowDeleted), nil)
36+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/environments/%s/instances/%s", c.HostURL, find.EnvironmentID, find.InstanceID), nil)
3737
if err != nil {
3838
return nil, err
3939
}

examples/environments/main.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,28 @@ provider "bytebase" {
1919
}
2020

2121
locals {
22-
environment_name_dev = "dev"
23-
environment_name_prod = "prod"
22+
environment_id_dev = "dev"
23+
environment_id_prod = "prod"
2424
}
2525

2626
# List all environment
2727
data "bytebase_environment_list" "all" {}
2828

2929
output "all_environments" {
30-
value = data.bytebase_environment_list.all.environments
30+
value = data.bytebase_environment_list.all
3131
}
3232

3333
// Find a specific environment by the name
3434
data "bytebase_environment" "dev" {
35-
name = local.environment_name_dev
35+
resource_id = local.environment_id_dev
3636
}
3737

3838
output "dev_environment" {
3939
value = data.bytebase_environment.dev
4040
}
4141

4242
data "bytebase_environment" "prod" {
43-
name = local.environment_name_prod
43+
resource_id = local.environment_id_prod
4444
}
4545

4646
output "prod_environment" {

examples/setup/main.tf

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,18 @@ locals {
2727

2828
# Create a new environment named "dev"
2929
resource "bytebase_environment" "dev" {
30-
name = local.environment_id_dev
31-
order = 0
32-
environment_tier_policy = "UNPROTECTED"
33-
pipeline_approval_policy = "MANUAL_APPROVAL_NEVER"
34-
backup_plan_policy = "UNSET"
30+
resource_id = local.environment_id_dev
31+
title = local.environment_id_dev
32+
order = 0
33+
environment_tier_policy = "UNPROTECTED"
3534
}
3635

3736
# Create another environment named "prod"
3837
resource "bytebase_environment" "prod" {
39-
name = local.environment_id_prod
40-
order = 1
41-
environment_tier_policy = "PROTECTED"
42-
pipeline_approval_policy = "MANUAL_APPROVAL_BY_WORKSPACE_OWNER_OR_DBA"
43-
backup_plan_policy = "DAILY"
38+
resource_id = local.environment_id_prod
39+
title = local.environment_id_prod
40+
order = 1
41+
environment_tier_policy = "PROTECTED"
4442
}
4543

4644
# Create a new instance named "dev_instance_test"

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/bytebase/terraform-provider-bytebase
33
go 1.19
44

55
require (
6-
github.com/google/go-querystring v1.1.0
76
github.com/hashicorp/terraform-plugin-docs v0.13.0
87
github.com/hashicorp/terraform-plugin-log v0.7.0
98
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.0

go.sum

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,11 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
9292
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
9393
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
9494
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
95-
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
9695
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
9796
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
9897
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
9998
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
10099
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
101-
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
102-
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
103100
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
104101
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
105102
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=

0 commit comments

Comments
 (0)