Skip to content

Commit e10a5eb

Browse files
authored
Merge pull request #1 from bytebase/feat/api
feat: add api for environment and instance
2 parents bfc9d9d + d00f6b4 commit e10a5eb

File tree

5 files changed

+309
-1
lines changed

5 files changed

+309
-1
lines changed

api/environment.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package api
2+
3+
// Environment is the API message for an environment.
4+
type Environment struct {
5+
ID int `json:"id"`
6+
7+
// Domain specific fields
8+
Name string `json:"name"`
9+
Order int `json:"order"`
10+
}
11+
12+
// EnvironmentCreate is the API message for creating an environment.
13+
type EnvironmentCreate struct {
14+
// Domain specific fields
15+
Name string `json:"name"`
16+
Order *int `json:"order"`
17+
}
18+
19+
// EnvironmentPatch is the API message for patching an environment.
20+
type EnvironmentPatch struct {
21+
// Domain specific fields
22+
Name string `json:"name,omitempty"`
23+
Order int `json:"order"`
24+
}

api/instance.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package api
2+
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+
10+
// Domain specific fields
11+
Name string `json:"name"`
12+
Engine string `json:"engine"`
13+
EngineVersion string `json:"engineVersion"`
14+
ExternalLink string `json:"externalLink"`
15+
Host string `json:"host"`
16+
Port string `json:"port"`
17+
Username string `json:"username"`
18+
}
19+
20+
// InstanceCreate is the API message for creating an instance.
21+
type InstanceCreate struct {
22+
// Related fields
23+
Environment string `json:"environment"`
24+
25+
// Domain specific fields
26+
Name string `json:"name"`
27+
Engine string `json:"engine"`
28+
ExternalLink string `json:"externalLink"`
29+
Host string `json:"host"`
30+
Port string `json:"port"`
31+
Username string `json:"username"`
32+
Password string `json:"password"`
33+
SslCa string `json:"sslCa"`
34+
SslCert string `json:"sslCert"`
35+
SslKey string `json:"sslKey"`
36+
}
37+
38+
// InstancePatch is the API message for patching an instance.
39+
type InstancePatch struct {
40+
// Domain specific fields
41+
Name string `json:"name,omitempty"`
42+
ExternalLink string `json:"externalLink,omitempty"`
43+
Host string `json:"host,omitempty"`
44+
Port string `json:"port,omitempty"`
45+
}
46+
47+
// HasChange returns if the patch struct has the value to update.
48+
func (p *InstancePatch) HasChange() bool {
49+
return p.Name != "" || p.ExternalLink != "" || p.Host != "" || p.Port != ""
50+
}

client/environment.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package client
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"strings"
8+
9+
"github.com/bytebase/terraform-provider-bytebase/api"
10+
)
11+
12+
// CreateEnvironment creates the environment.
13+
func (c *Client) CreateEnvironment(create *api.EnvironmentCreate) (*api.Environment, error) {
14+
payload, err := json.Marshal(create)
15+
if err != nil {
16+
return nil, err
17+
}
18+
19+
req, err := http.NewRequest("POST", fmt.Sprintf("%s/environment", c.HostURL), strings.NewReader(string(payload)))
20+
if err != nil {
21+
return nil, err
22+
}
23+
24+
body, err := c.doRequest(req)
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
var env api.Environment
30+
err = json.Unmarshal(body, &env)
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
return &env, nil
36+
}
37+
38+
// GetEnvironment gets the environment by id.
39+
func (c *Client) GetEnvironment(environmentID int) (*api.Environment, error) {
40+
req, err := http.NewRequest("GET", fmt.Sprintf("%s/environment/%d", c.HostURL, environmentID), nil)
41+
if err != nil {
42+
return nil, err
43+
}
44+
45+
body, err := c.doRequest(req)
46+
if err != nil {
47+
return nil, err
48+
}
49+
50+
var env api.Environment
51+
err = json.Unmarshal(body, &env)
52+
if err != nil {
53+
return nil, err
54+
}
55+
56+
return &env, nil
57+
}
58+
59+
// ListEnvironment finds all environments.
60+
func (c *Client) ListEnvironment() ([]*api.Environment, error) {
61+
req, err := http.NewRequest("GET", fmt.Sprintf("%s/environment", c.HostURL), nil)
62+
if err != nil {
63+
return nil, err
64+
}
65+
66+
body, err := c.doRequest(req)
67+
if err != nil {
68+
return nil, err
69+
}
70+
71+
res := []*api.Environment{}
72+
err = json.Unmarshal(body, &res)
73+
if err != nil {
74+
return nil, err
75+
}
76+
77+
return res, nil
78+
}
79+
80+
// UpdateEnvironment updates the environment.
81+
func (c *Client) UpdateEnvironment(environmentID int, patch *api.EnvironmentPatch) (*api.Environment, error) {
82+
payload, err := json.Marshal(patch)
83+
if err != nil {
84+
return nil, err
85+
}
86+
87+
req, err := http.NewRequest("PATCH", fmt.Sprintf("%s/environment/%d", c.HostURL, environmentID), strings.NewReader(string(payload)))
88+
if err != nil {
89+
return nil, err
90+
}
91+
92+
body, err := c.doRequest(req)
93+
if err != nil {
94+
return nil, err
95+
}
96+
97+
var env api.Environment
98+
err = json.Unmarshal(body, &env)
99+
if err != nil {
100+
return nil, err
101+
}
102+
103+
return &env, nil
104+
}
105+
106+
// DeleteEnvironment deletes the environment.
107+
func (c *Client) DeleteEnvironment(environmentID int) error {
108+
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/environment/%d", c.HostURL, environmentID), nil)
109+
if err != nil {
110+
return err
111+
}
112+
113+
if _, err := c.doRequest(req); err != nil {
114+
return err
115+
}
116+
return nil
117+
}

client/instance.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package client
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"strings"
8+
9+
"github.com/bytebase/terraform-provider-bytebase/api"
10+
)
11+
12+
// ListInstance will return all instances.
13+
func (c *Client) ListInstance() ([]*api.Instance, error) {
14+
req, err := http.NewRequest("GET", fmt.Sprintf("%s/instance", c.HostURL), nil)
15+
if err != nil {
16+
return nil, err
17+
}
18+
19+
body, err := c.doRequest(req)
20+
if err != nil {
21+
return nil, err
22+
}
23+
24+
orders := []*api.Instance{}
25+
err = json.Unmarshal(body, &orders)
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
return orders, nil
31+
}
32+
33+
// CreateInstance creates the instance.
34+
func (c *Client) CreateInstance(create *api.InstanceCreate) (*api.Instance, error) {
35+
payload, err := json.Marshal(create)
36+
if err != nil {
37+
return nil, err
38+
}
39+
40+
req, err := http.NewRequest("POST", fmt.Sprintf("%s/instance", c.HostURL), strings.NewReader(string(payload)))
41+
if err != nil {
42+
return nil, err
43+
}
44+
45+
body, err := c.doRequest(req)
46+
if err != nil {
47+
return nil, err
48+
}
49+
50+
var instance api.Instance
51+
err = json.Unmarshal(body, &instance)
52+
if err != nil {
53+
return nil, err
54+
}
55+
56+
return &instance, nil
57+
}
58+
59+
// GetInstance gets the instance by id.
60+
func (c *Client) GetInstance(instanceID int) (*api.Instance, error) {
61+
req, err := http.NewRequest("GET", fmt.Sprintf("%s/instance/%d", c.HostURL, instanceID), nil)
62+
if err != nil {
63+
return nil, err
64+
}
65+
66+
body, err := c.doRequest(req)
67+
if err != nil {
68+
return nil, err
69+
}
70+
71+
var instance api.Instance
72+
err = json.Unmarshal(body, &instance)
73+
if err != nil {
74+
return nil, err
75+
}
76+
77+
return &instance, nil
78+
}
79+
80+
// UpdateInstance updates the instance.
81+
func (c *Client) UpdateInstance(instanceID int, patch *api.InstancePatch) (*api.Instance, error) {
82+
payload, err := json.Marshal(patch)
83+
if err != nil {
84+
return nil, err
85+
}
86+
87+
req, err := http.NewRequest("PATCH", fmt.Sprintf("%s/instance/%d", c.HostURL, instanceID), strings.NewReader(string(payload)))
88+
if err != nil {
89+
return nil, err
90+
}
91+
92+
body, err := c.doRequest(req)
93+
if err != nil {
94+
return nil, err
95+
}
96+
97+
var instance api.Instance
98+
err = json.Unmarshal(body, &instance)
99+
if err != nil {
100+
return nil, err
101+
}
102+
103+
return &instance, nil
104+
}
105+
106+
// DeleteInstance deletes the instance.
107+
func (c *Client) DeleteInstance(instanceID int) error {
108+
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/instance/%d", c.HostURL, instanceID), nil)
109+
if err != nil {
110+
return err
111+
}
112+
113+
if _, err := c.doRequest(req); err != nil {
114+
return err
115+
}
116+
return nil
117+
}

go.mod

Lines changed: 1 addition & 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/hashicorp/terraform-plugin-log v0.7.0
76
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.0
87
github.com/pkg/errors v0.9.1
98
)
@@ -24,6 +23,7 @@ require (
2423
github.com/hashicorp/hcl/v2 v2.14.1 // indirect
2524
github.com/hashicorp/logutils v1.0.0 // indirect
2625
github.com/hashicorp/terraform-plugin-go v0.14.0 // indirect
26+
github.com/hashicorp/terraform-plugin-log v0.7.0 // indirect
2727
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c // indirect
2828
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
2929
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect

0 commit comments

Comments
 (0)