Skip to content

Commit a5a7093

Browse files
authored
feat: migrate to new instance api (#32)
* chore: migrate to new instance api * chore: update example
1 parent 9d8ba16 commit a5a7093

22 files changed

+474
-832
lines changed

api/client.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ type Client interface {
2121
DeleteEnvironment(ctx context.Context, environmentID int) error
2222

2323
// Instance
24-
// ListInstance will return all instances.
25-
ListInstance(ctx context.Context, find *InstanceFind) ([]*Instance, error)
26-
// CreateInstance creates the instance.
27-
CreateInstance(ctx context.Context, create *InstanceCreate) (*Instance, error)
24+
// ListInstance will return instances in environment.
25+
ListInstance(ctx context.Context, find *InstanceFindMessage) (*ListInstanceMessage, error)
2826
// GetInstance gets the instance by id.
29-
GetInstance(ctx context.Context, instanceID int) (*Instance, error)
27+
GetInstance(ctx context.Context, find *InstanceFindMessage) (*InstanceMessage, error)
28+
// CreateInstance creates the instance.
29+
CreateInstance(ctx context.Context, environmentID, instanceID string, instance *InstanceMessage) (*InstanceMessage, error)
3030
// UpdateInstance updates the instance.
31-
UpdateInstance(ctx context.Context, instanceID int, patch *InstancePatch) (*Instance, error)
31+
UpdateInstance(ctx context.Context, environmentID, instanceID string, instance *InstanceMessage) (*InstanceMessage, error)
3232
// DeleteInstance deletes the instance.
33-
DeleteInstance(ctx context.Context, instanceID int) error
33+
DeleteInstance(ctx context.Context, environmentID, instanceID string) error
3434

3535
// Role
3636
// CreateRole creates the role in the instance.

api/common.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package api
2+
3+
// State is the state for a row.
4+
type State string
5+
6+
const (
7+
// Active is the state for a normal row.
8+
Active State = "ACTIVE"
9+
// Deleted is the state for an removed row.
10+
Deleted State = "DELETED"
11+
)

api/data_source.go

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,19 @@ const (
77
// DataSourceAdmin is the ADMIN type of data source.
88
DataSourceAdmin DataSourceType = "ADMIN"
99
// DataSourceRO is the read-only type of data source.
10-
DataSourceRO DataSourceType = "RO"
10+
DataSourceRO DataSourceType = "READ_ONLY"
1111
)
1212

13-
// DataSource is the API message for a data source.
14-
type DataSource struct {
15-
ID int `json:"id"`
16-
17-
// Related fields
18-
DatabaseID int `json:"databaseId"`
19-
20-
// Domain specific fields
21-
Name string `json:"name"`
13+
// DataSourceMessage is the API message for a data source.
14+
type DataSourceMessage struct {
15+
Title string `json:"title"`
2216
Type DataSourceType `json:"type"`
2317
Username string `json:"username"`
24-
25-
// HostOverride and PortOverride are only used for read-only data sources for user's read-replica instances.
26-
HostOverride string `json:"hostOverride"`
27-
PortOverride string `json:"portOverride"`
28-
}
29-
30-
// DataSourceCreate is the API message for creating a data source.
31-
type DataSourceCreate struct {
32-
// Domain specific fields
33-
Name string `json:"name"`
34-
Type DataSourceType `json:"type"`
35-
Username string `json:"username"`
36-
Password string `json:"password"`
37-
SslCa string `json:"sslCa"`
38-
SslCert string `json:"sslCert"`
39-
SslKey string `json:"sslKey"`
40-
HostOverride string `json:"hostOverride"`
41-
PortOverride string `json:"portOverride"`
18+
Password string `json:"password"`
19+
SslCa string `json:"sslCa"`
20+
SslCert string `json:"sslCert"`
21+
SslKey string `json:"sslKey"`
22+
Host string `json:"host"`
23+
Port string `json:"port"`
24+
Database string `json:"database"`
4225
}

api/instance.go

Lines changed: 18 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,25 @@
11
package api
22

3-
// Instance is the API message for an instance.
4-
type Instance struct {
5-
ID int `jsonapi:"primary,instance" json:"id"`
6-
7-
// Related fields
8-
Environment string `json:"environment"`
9-
DataSourceList []*DataSource `json:"dataSourceList"`
10-
11-
// Domain specific fields
12-
Name string `json:"name"`
13-
Engine string `json:"engine"`
14-
EngineVersion string `json:"engineVersion"`
15-
ExternalLink string `json:"externalLink"`
16-
Host string `json:"host"`
17-
Port string `json:"port"`
18-
Database string `json:"database"`
19-
}
20-
21-
// InstanceFind is the API message for finding instance.
22-
type InstanceFind struct {
23-
// Domain specific fields
24-
Name string `url:"name,omitempty"`
3+
// InstanceMessage is the API message for an instance.
4+
type InstanceMessage struct {
5+
UID string `json:"uid"`
6+
Name string `json:"name"`
7+
State State `json:"state,omitempty"`
8+
Title string `json:"title"`
9+
Engine string `json:"engine"`
10+
ExternalLink string `json:"externalLink"`
11+
DataSources []*DataSourceMessage `json:"dataSources"`
2512
}
2613

27-
// InstanceCreate is the API message for creating an instance.
28-
type InstanceCreate struct {
29-
// Related fields
30-
Environment string `json:"environment"`
31-
DataSourceList []*DataSourceCreate `json:"dataSourceList"`
32-
33-
// Domain specific fields
34-
Name string `json:"name"`
35-
Engine string `json:"engine"`
36-
ExternalLink string `json:"externalLink"`
37-
Host string `json:"host"`
38-
Port string `json:"port"`
39-
Database string `json:"database"`
40-
}
41-
42-
// InstancePatch is the API message for patching an instance.
43-
type InstancePatch struct {
44-
// Related fields
45-
DataSourceList []*DataSourceCreate `json:"dataSourceList"`
46-
47-
// Domain specific fields
48-
Name *string `json:"name,omitempty"`
49-
ExternalLink *string `json:"externalLink,omitempty"`
50-
Host *string `json:"host,omitempty"`
51-
Port *string `json:"port,omitempty"`
52-
Database *string `json:"database,omitempty"`
14+
// InstanceFindMessage is the API message for finding instance.
15+
type InstanceFindMessage struct {
16+
EnvironmentID string
17+
InstanceID string
18+
ShowDeleted bool
5319
}
5420

55-
// HasChange returns if the patch struct has the value to update.
56-
func (p *InstancePatch) HasChange() bool {
57-
return p.Name != nil ||
58-
p.ExternalLink != nil ||
59-
p.Host != nil ||
60-
p.Port != nil ||
61-
p.Database != nil ||
62-
p.DataSourceList != nil
21+
// ListInstanceMessage is the API message for list instance response.
22+
type ListInstanceMessage struct {
23+
Instances []*InstanceMessage `json:"instances"`
24+
NextPageToken string `json:"nextPageToken"`
6325
}

client/client.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ func NewClient(url, email, password string) (api.Client, error) {
4343
}
4444

4545
func (c *client) doRequest(req *http.Request) ([]byte, error) {
46-
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.Token))
46+
if c.Token != "" {
47+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.Token))
48+
}
4749

4850
res, err := c.HTTPClient.Do(req)
4951
if err != nil {

client/instance.go

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,12 @@ 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

15-
// ListInstance will return all instances.
16-
func (c *client) ListInstance(ctx context.Context, find *api.InstanceFind) ([]*api.Instance, error) {
17-
q, err := query.Values(find)
18-
if err != nil {
19-
return nil, err
20-
}
21-
22-
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/instance?%s", c.HostURL, q.Encode()), nil)
13+
// ListInstance will return instances in environment.
14+
func (c *client) ListInstance(ctx context.Context, find *api.InstanceFindMessage) (*api.ListInstanceMessage, error) {
15+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/environments/%s/instances?showDeleted=%v", c.HostURL, find.EnvironmentID, find.ShowDeleted), nil)
2316
if err != nil {
2417
return nil, err
2518
}
@@ -29,44 +22,45 @@ func (c *client) ListInstance(ctx context.Context, find *api.InstanceFind) ([]*a
2922
return nil, err
3023
}
3124

32-
orders := []*api.Instance{}
33-
err = json.Unmarshal(body, &orders)
25+
var res api.ListInstanceMessage
26+
err = json.Unmarshal(body, &res)
3427
if err != nil {
3528
return nil, err
3629
}
3730

38-
return orders, nil
31+
return &res, nil
3932
}
4033

41-
// CreateInstance creates the instance.
42-
func (c *client) CreateInstance(ctx context.Context, create *api.InstanceCreate) (*api.Instance, error) {
43-
payload, err := json.Marshal(create)
34+
// GetInstance gets the instance by id.
35+
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)
4437
if err != nil {
4538
return nil, err
4639
}
4740

48-
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/instance", c.HostURL), strings.NewReader(string(payload)))
41+
body, err := c.doRequest(req)
4942
if err != nil {
5043
return nil, err
5144
}
5245

53-
body, err := c.doRequest(req)
46+
var res api.InstanceMessage
47+
err = json.Unmarshal(body, &res)
5448
if err != nil {
5549
return nil, err
5650
}
5751

58-
var instance api.Instance
59-
err = json.Unmarshal(body, &instance)
52+
return &res, nil
53+
}
54+
55+
// CreateInstance creates the instance.
56+
func (c *client) CreateInstance(ctx context.Context, environmentID, instanceID string, instance *api.InstanceMessage) (*api.InstanceMessage, error) {
57+
payload, err := json.Marshal(instance)
6058
if err != nil {
6159
return nil, err
6260
}
6361

64-
return &instance, nil
65-
}
62+
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/environments/%s/instances?instanceId=%s", c.HostURL, environmentID, instanceID), strings.NewReader(string(payload)))
6663

67-
// GetInstance gets the instance by id.
68-
func (c *client) GetInstance(ctx context.Context, instanceID int) (*api.Instance, error) {
69-
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/instance/%d", c.HostURL, instanceID), nil)
7064
if err != nil {
7165
return nil, err
7266
}
@@ -76,23 +70,24 @@ func (c *client) GetInstance(ctx context.Context, instanceID int) (*api.Instance
7670
return nil, err
7771
}
7872

79-
var instance api.Instance
80-
err = json.Unmarshal(body, &instance)
73+
var res api.InstanceMessage
74+
err = json.Unmarshal(body, &res)
8175
if err != nil {
8276
return nil, err
8377
}
8478

85-
return &instance, nil
79+
return &res, nil
8680
}
8781

8882
// UpdateInstance updates the instance.
89-
func (c *client) UpdateInstance(ctx context.Context, instanceID int, patch *api.InstancePatch) (*api.Instance, error) {
90-
payload, err := json.Marshal(patch)
83+
func (c *client) UpdateInstance(ctx context.Context, environmentID, instanceID string, instance *api.InstanceMessage) (*api.InstanceMessage, error) {
84+
payload, err := json.Marshal(instance)
9185
if err != nil {
9286
return nil, err
9387
}
9488

95-
req, err := http.NewRequestWithContext(ctx, "PATCH", fmt.Sprintf("%s/instance/%d", c.HostURL, instanceID), strings.NewReader(string(payload)))
89+
req, err := http.NewRequestWithContext(ctx, "PATCH", fmt.Sprintf("%s/environments/%s/instances/%s", c.HostURL, environmentID, instanceID), strings.NewReader(string(payload)))
90+
9691
if err != nil {
9792
return nil, err
9893
}
@@ -102,18 +97,18 @@ func (c *client) UpdateInstance(ctx context.Context, instanceID int, patch *api.
10297
return nil, err
10398
}
10499

105-
var instance api.Instance
106-
err = json.Unmarshal(body, &instance)
100+
var res api.InstanceMessage
101+
err = json.Unmarshal(body, &res)
107102
if err != nil {
108103
return nil, err
109104
}
110105

111-
return &instance, nil
106+
return &res, nil
112107
}
113108

114109
// DeleteInstance deletes the instance.
115-
func (c *client) DeleteInstance(ctx context.Context, instanceID int) error {
116-
req, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("%s/instance/%d", c.HostURL, instanceID), nil)
110+
func (c *client) DeleteInstance(ctx context.Context, environmentID, instanceID string) error {
111+
req, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("%s/environments/%s/instances/%s", c.HostURL, environmentID, instanceID), nil)
117112
if err != nil {
118113
return err
119114
}

examples/instances/main.tf

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

2121
locals {
22-
instance_name_dev = "dev_instance_test"
23-
instance_name_prod = "prod_instance_test"
22+
environment_id_dev = "dev"
23+
environment_id_prod = "prod"
24+
instance_id_dev = "dev-instance"
25+
instance_id_prod = "prod-instance"
2426
}
2527

26-
# List all instance
28+
# List all instances in all environments
2729
data "bytebase_instance_list" "all" {}
2830

2931
output "all_instances" {
3032
value = data.bytebase_instance_list.all.instances
3133
}
3234

35+
# List all instances in dev environment
36+
data "bytebase_instance_list" "dev" {
37+
environment = local.environment_id_dev
38+
}
39+
40+
output "dev_instances" {
41+
value = data.bytebase_instance_list.dev.instances
42+
}
43+
3344
# Find a specific instance by name
3445
data "bytebase_instance" "dev" {
35-
name = local.instance_name_dev
46+
resource_id = local.instance_id_dev
47+
environment = local.environment_id_dev
3648
}
3749

3850

@@ -41,7 +53,8 @@ output "dev_instance" {
4153
}
4254

4355
data "bytebase_instance" "prod" {
44-
name = local.instance_name_prod
56+
resource_id = local.instance_id_prod
57+
environment = local.environment_id_prod
4558
}
4659

4760

0 commit comments

Comments
 (0)