Skip to content

Commit e044506

Browse files
authored
chore: optimize (#45)
* feat: support policy data source * chore: support CRUD policy resource * chore: support config sql review rules * chore: update docs * chore: update version * chore: update docs * chore: update docs * chore: update docs * chore: optimize * chore: update * chore: update version
1 parent 6529ab4 commit e044506

21 files changed

+163
-57
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.7-alpha.5
1+
0.0.7-alpha.6

api/client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ type Client interface {
3535
DeleteInstance(ctx context.Context, environmentID, instanceID string) error
3636
// UndeleteInstance undeletes the instance.
3737
UndeleteInstance(ctx context.Context, environmentID, instanceID string) (*InstanceMessage, error)
38+
// SyncInstanceSchema will trigger the schema sync for an instance.
39+
SyncInstanceSchema(ctx context.Context, instanceUID string) error
3840

3941
// Role
4042
// CreateRole creates the role in the instance.

api/instance.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
package api
22

3+
// EngineType is the type of the instance engine.
4+
type EngineType string
5+
6+
const (
7+
// EngineTypeMySQL is the database type for MYSQL.
8+
EngineTypeMySQL EngineType = "MYSQL"
9+
// EngineTypePostgres is the database type for POSTGRES.
10+
EngineTypePostgres EngineType = "POSTGRES"
11+
// EngineTypeTiDB is the database type for TiDB.
12+
EngineTypeTiDB EngineType = "TIDB"
13+
// EngineTypeSnowflake is the database type for SNOWFLAKE.
14+
EngineTypeSnowflake EngineType = "SNOWFLAKE"
15+
// EngineTypeClickHouse is the database type for CLICKHOUSE.
16+
EngineTypeClickHouse EngineType = "CLICKHOUSE"
17+
// EngineTypeMongoDB is the database type for MongoDB.
18+
EngineTypeMongoDB EngineType = "MONGODB"
19+
// EngineTypeSQLite is the database type for SQLite.
20+
EngineTypeSQLite EngineType = "SQLITE"
21+
)
22+
323
// InstanceMessage is the API message for an instance.
424
type InstanceMessage struct {
525
UID string `json:"uid"`
626
Name string `json:"name"`
727
State State `json:"state,omitempty"`
828
Title string `json:"title"`
9-
Engine string `json:"engine"`
29+
Engine EngineType `json:"engine"`
1030
ExternalLink string `json:"externalLink"`
1131
DataSources []*DataSourceMessage `json:"dataSources"`
1232
}

client/auth.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ import (
1313

1414
// Login will login the user and get the response.
1515
func (c *client) Login() (*api.AuthResponse, error) {
16-
if c.Auth.Email == "" || c.Auth.Password == "" {
16+
if c.auth.Email == "" || c.auth.Password == "" {
1717
return nil, errors.Errorf("define username and password")
1818
}
19-
rb, err := json.Marshal(c.Auth)
19+
rb, err := json.Marshal(c.auth)
2020
if err != nil {
2121
return nil, err
2222
}
2323

24-
req, err := http.NewRequest("POST", fmt.Sprintf("%s/auth/login", c.HostURL), strings.NewReader(string(rb)))
24+
req, err := http.NewRequest("POST", fmt.Sprintf("%s/%s/auth/login", c.url, c.version), strings.NewReader(string(rb)))
2525
if err != nil {
2626
return nil, err
2727
}

client/client.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,22 @@ import (
1414

1515
// client is the API message for Bytebase API client.
1616
type client struct {
17-
HostURL string
18-
HTTPClient *http.Client
19-
Token string
20-
Auth *api.Login
17+
url string
18+
version string
19+
client *http.Client
20+
token string
21+
auth *api.Login
2122
}
2223

2324
// NewClient returns the new Bytebase API client.
24-
func NewClient(url, email, password string) (api.Client, error) {
25+
func NewClient(url, version, email, password string) (api.Client, error) {
2526
c := client{
26-
HTTPClient: &http.Client{Timeout: 10 * time.Second},
27-
HostURL: url,
27+
client: &http.Client{Timeout: 10 * time.Second},
28+
url: url,
29+
version: version,
2830
}
2931

30-
c.Auth = &api.Login{
32+
c.auth = &api.Login{
3133
Email: email,
3234
Password: password,
3335
}
@@ -37,17 +39,17 @@ func NewClient(url, email, password string) (api.Client, error) {
3739
return nil, err
3840
}
3941

40-
c.Token = ar.Token
42+
c.token = ar.Token
4143

4244
return &c, nil
4345
}
4446

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

50-
res, err := c.HTTPClient.Do(req)
52+
res, err := c.client.Do(req)
5153
if err != nil {
5254
return nil, err
5355
}

client/database_role.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func (c *client) CreateRole(ctx context.Context, environmentID, instanceID strin
1717
return nil, err
1818
}
1919

20-
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/environments/%s/instances/%s/roles", c.HostURL, environmentID, instanceID), strings.NewReader(string(payload)))
20+
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/%s/environments/%s/instances/%s/roles", c.url, c.version, environmentID, instanceID), strings.NewReader(string(payload)))
2121
if err != nil {
2222
return nil, err
2323
}
@@ -38,7 +38,7 @@ func (c *client) CreateRole(ctx context.Context, environmentID, instanceID strin
3838

3939
// GetRole gets the role by instance id and role name.
4040
func (c *client) GetRole(ctx context.Context, environmentID, instanceID, roleName string) (*api.Role, error) {
41-
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/environments/%s/instances/%s/roles/%s", c.HostURL, environmentID, instanceID, roleName), nil)
41+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/environments/%s/instances/%s/roles/%s", c.url, c.version, environmentID, instanceID, roleName), nil)
4242
if err != nil {
4343
return nil, err
4444
}
@@ -59,7 +59,7 @@ func (c *client) GetRole(ctx context.Context, environmentID, instanceID, roleNam
5959

6060
// ListRole lists the role in instance.
6161
func (c *client) ListRole(ctx context.Context, environmentID, instanceID string) ([]*api.Role, error) {
62-
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/environments/%s/instances/%s/roles", c.HostURL, environmentID, instanceID), nil)
62+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/environments/%s/instances/%s/roles", c.url, c.version, environmentID, instanceID), nil)
6363
if err != nil {
6464
return nil, err
6565
}
@@ -105,7 +105,7 @@ func (c *client) UpdateRole(ctx context.Context, environmentID, instanceID, role
105105
paths = append(paths, "role.attribute")
106106
}
107107

108-
req, err := http.NewRequestWithContext(ctx, "PATCH", fmt.Sprintf("%s/environments/%s/instances/%s/roles/%s?update_mask=%s", c.HostURL, environmentID, instanceID, roleName, strings.Join(paths, ",")), strings.NewReader(string(payload)))
108+
req, err := http.NewRequestWithContext(ctx, "PATCH", fmt.Sprintf("%s/%s/environments/%s/instances/%s/roles/%s?update_mask=%s", c.url, c.version, environmentID, instanceID, roleName, strings.Join(paths, ",")), strings.NewReader(string(payload)))
109109
if err != nil {
110110
return nil, err
111111
}
@@ -126,7 +126,7 @@ func (c *client) UpdateRole(ctx context.Context, environmentID, instanceID, role
126126

127127
// DeleteRole deletes the role in the instance.
128128
func (c *client) DeleteRole(ctx context.Context, environmentID, instanceID, roleName string) error {
129-
req, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("%s/environments/%s/instances/%s/roles/%s", c.HostURL, environmentID, instanceID, roleName), nil)
129+
req, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("%s/%s/environments/%s/instances/%s/roles/%s", c.url, c.version, environmentID, instanceID, roleName), nil)
130130
if err != nil {
131131
return err
132132
}

client/environment.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func (c *client) CreateEnvironment(ctx context.Context, environmentID string, cr
1717
return nil, err
1818
}
1919

20-
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/environments?environmentId=%s", c.HostURL, environmentID), strings.NewReader(string(payload)))
20+
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/%s/environments?environmentId=%s", c.url, c.version, environmentID), strings.NewReader(string(payload)))
2121
if err != nil {
2222
return nil, err
2323
}
@@ -38,7 +38,7 @@ func (c *client) CreateEnvironment(ctx context.Context, environmentID string, cr
3838

3939
// GetEnvironment gets the environment by id.
4040
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)
41+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/environments/%s", c.url, c.version, environmentID), nil)
4242
if err != nil {
4343
return nil, err
4444
}
@@ -59,7 +59,7 @@ func (c *client) GetEnvironment(ctx context.Context, environmentID string) (*api
5959

6060
// ListEnvironment finds all environments.
6161
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)
62+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/environments?showDeleted=%v", c.url, c.version, showDeleted), nil)
6363
if err != nil {
6464
return nil, err
6565
}
@@ -96,7 +96,7 @@ func (c *client) UpdateEnvironment(ctx context.Context, environmentID string, pa
9696
paths = append(paths, "environment.tier")
9797
}
9898

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)))
99+
req, err := http.NewRequestWithContext(ctx, "PATCH", fmt.Sprintf("%s/%s/environments/%s?update_mask=%s", c.url, c.version, environmentID, strings.Join(paths, ",")), strings.NewReader(string(payload)))
100100
if err != nil {
101101
return nil, err
102102
}
@@ -117,7 +117,7 @@ func (c *client) UpdateEnvironment(ctx context.Context, environmentID string, pa
117117

118118
// DeleteEnvironment deletes the environment.
119119
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)
120+
req, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("%s/%s/environments/%s", c.url, c.version, environmentID), nil)
121121
if err != nil {
122122
return err
123123
}
@@ -130,7 +130,7 @@ func (c *client) DeleteEnvironment(ctx context.Context, environmentID string) er
130130

131131
// UndeleteEnvironment undeletes the environment.
132132
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)
133+
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/%s/environments/%s:undelete", c.url, c.version, environmentID), nil)
134134
if err != nil {
135135
return nil, err
136136
}

client/instance.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
// ListInstance will return instances in environment.
1414
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)
15+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/environments/%s/instances?showDeleted=%v", c.url, c.version, find.EnvironmentID, find.ShowDeleted), nil)
1616
if err != nil {
1717
return nil, err
1818
}
@@ -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", c.HostURL, find.EnvironmentID, find.InstanceID), nil)
36+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/environments/%s/instances/%s", c.url, c.version, find.EnvironmentID, find.InstanceID), nil)
3737
if err != nil {
3838
return nil, err
3939
}
@@ -59,7 +59,7 @@ func (c *client) CreateInstance(ctx context.Context, environmentID, instanceID s
5959
return nil, err
6060
}
6161

62-
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/environments/%s/instances?instanceId=%s", c.HostURL, environmentID, instanceID), strings.NewReader(string(payload)))
62+
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/%s/environments/%s/instances?instanceId=%s", c.url, c.version, environmentID, instanceID), strings.NewReader(string(payload)))
6363

6464
if err != nil {
6565
return nil, err
@@ -97,7 +97,7 @@ func (c *client) UpdateInstance(ctx context.Context, environmentID, instanceID s
9797
paths = append(paths, "instance.data_sources")
9898
}
9999

100-
req, err := http.NewRequestWithContext(ctx, "PATCH", fmt.Sprintf("%s/environments/%s/instances/%s?update_mask=%s", c.HostURL, environmentID, instanceID, strings.Join(paths, ",")), strings.NewReader(string(payload)))
100+
req, err := http.NewRequestWithContext(ctx, "PATCH", fmt.Sprintf("%s/%s/environments/%s/instances/%s?update_mask=%s", c.url, c.version, environmentID, instanceID, strings.Join(paths, ",")), strings.NewReader(string(payload)))
101101

102102
if err != nil {
103103
return nil, err
@@ -119,7 +119,7 @@ func (c *client) UpdateInstance(ctx context.Context, environmentID, instanceID s
119119

120120
// DeleteInstance deletes the instance.
121121
func (c *client) DeleteInstance(ctx context.Context, environmentID, instanceID string) error {
122-
req, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("%s/environments/%s/instances/%s", c.HostURL, environmentID, instanceID), nil)
122+
req, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("%s/%s/environments/%s/instances/%s", c.url, c.version, environmentID, instanceID), nil)
123123
if err != nil {
124124
return err
125125
}
@@ -132,7 +132,7 @@ func (c *client) DeleteInstance(ctx context.Context, environmentID, instanceID s
132132

133133
// UndeleteInstance undeletes the instance.
134134
func (c *client) UndeleteInstance(ctx context.Context, environmentID, instanceID string) (*api.InstanceMessage, error) {
135-
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/environments/%s/instances/%s:undelete", c.HostURL, environmentID, instanceID), nil)
135+
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/%s/environments/%s/instances/%s:undelete", c.url, c.version, environmentID, instanceID), nil)
136136
if err != nil {
137137
return nil, err
138138
}
@@ -150,3 +150,19 @@ func (c *client) UndeleteInstance(ctx context.Context, environmentID, instanceID
150150

151151
return &res, nil
152152
}
153+
154+
// SyncInstanceSchema will trigger the schema sync for an instance.
155+
func (c *client) SyncInstanceSchema(ctx context.Context, instanceUID string) error {
156+
payload := fmt.Sprintf(`{"data":{"type":"sqlSyncSchema","attributes":{"instanceId":%s}}}`, instanceUID)
157+
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/api/sql/sync-schema", c.url), strings.NewReader(string(payload)))
158+
159+
if err != nil {
160+
return err
161+
}
162+
163+
if _, err := c.doRequest(req); err != nil {
164+
return err
165+
}
166+
167+
return nil
168+
}

client/policy.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (c *client) ListPolicies(ctx context.Context, find *api.PolicyFindMessage)
1818
return nil, errors.Errorf("invalid request, list policies cannot specific the policy type")
1919
}
2020

21-
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s?showDeleted=%v", c.HostURL, getPolicyRequestName(find), find.ShowDeleted), nil)
21+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/%s?showDeleted=%v", c.url, c.version, getPolicyRequestName(find), find.ShowDeleted), nil)
2222
if err != nil {
2323
return nil, err
2424
}
@@ -43,7 +43,7 @@ func (c *client) GetPolicy(ctx context.Context, find *api.PolicyFindMessage) (*a
4343
return nil, errors.Errorf("invalid request, get policy must specific the policy type")
4444
}
4545

46-
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s", c.HostURL, getPolicyRequestName(find)), nil)
46+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/%s", c.url, c.version, getPolicyRequestName(find)), nil)
4747
if err != nil {
4848
return nil, err
4949
}
@@ -85,7 +85,7 @@ func (c *client) UpsertPolicy(ctx context.Context, find *api.PolicyFindMessage,
8585
paths = append(paths, "policy.payload")
8686
}
8787

88-
req, err := http.NewRequestWithContext(ctx, "PATCH", fmt.Sprintf("%s/%s?allow_missing=true&update_mask=%s", c.HostURL, getPolicyRequestName(find), strings.Join(paths, ",")), strings.NewReader(string(payload)))
88+
req, err := http.NewRequestWithContext(ctx, "PATCH", fmt.Sprintf("%s/%s/%s?allow_missing=true&update_mask=%s", c.url, c.version, getPolicyRequestName(find), strings.Join(paths, ",")), strings.NewReader(string(payload)))
8989
if err != nil {
9090
return nil, err
9191
}
@@ -110,7 +110,7 @@ func (c *client) DeletePolicy(ctx context.Context, find *api.PolicyFindMessage)
110110
return errors.Errorf("invalid request, get policy must specific the policy type")
111111
}
112112

113-
req, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("%s/%s", c.HostURL, getPolicyRequestName(find)), nil)
113+
req, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("%s/%s/%s", c.url, c.version, getPolicyRequestName(find)), nil)
114114
if err != nil {
115115
return err
116116
}

examples/environments/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
terraform {
33
required_providers {
44
bytebase = {
5-
version = "0.0.7-alpha.3"
5+
version = "0.0.7-alpha.6"
66
# For local development, please use "terraform.local/bytebase/bytebase" instead
77
source = "registry.terraform.io/bytebase/bytebase"
88
}

0 commit comments

Comments
 (0)