Skip to content

Commit a5b8a5f

Browse files
authored
feat: support environment resource (#2)
2 parents e10a5eb + b050dc6 commit a5b8a5f

File tree

19 files changed

+656
-36
lines changed

19 files changed

+656
-36
lines changed

.github/workflows/tests.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ jobs:
4242
- uses: actions/setup-go@v3
4343
with:
4444
go-version: 1.19
45+
- uses: hashicorp/setup-terraform@v2
46+
with:
47+
terraform_version: '1.3.*'
48+
terraform_wrapper: false
4549
- run: |
4650
go generate ./...
47-
go test -v ./...
51+
go test -v -cover ./...
52+
env:
53+
TF_ACC: '1'
54+
BYTEBASE_USER_EMAIL: 'test_email'
55+
BYTEBASE_USER_PASSWORD: 'test_pwd'
56+
BYTEBASE_URL: 'test_url'

.vscode/settings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"go.lintOnSave": "off",
3+
"go.lintTool": "golangci-lint",
4+
"go.buildTags": "mysql",
5+
"gopls": {
6+
"formatting.local": "github.com/bytebase/terraform-provider-bytebase"
7+
}
8+
}

Makefile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
TEST?=$$(go list ./... | grep -v 'vendor')
2+
HOSTNAME=registry.terraform.io
3+
NAMESPACE=bytebase
4+
NAME=bytebase
5+
BINARY=terraform-provider-${NAMESPACE}
6+
VERSION=0.0.1
7+
OS_ARCH=darwin_amd64
8+
9+
default: install
10+
11+
build:
12+
go build -o ${BINARY}
13+
14+
release:
15+
goreleaser release --rm-dist --snapshot --skip-publish --skip-sign
16+
17+
install: build
18+
mkdir -p ~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/${OS_ARCH}
19+
mv ${BINARY} ~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/${OS_ARCH}
20+
21+
test:
22+
go test -i $(TEST) || exit 1
23+
echo $(TEST) | xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4
24+
25+
testacc:
26+
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m

api/client.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package api
2+
3+
// Client is the API message for Bytebase OpenAPI client.
4+
type Client interface {
5+
// Auth
6+
// Login will login the user and get the response.
7+
Login() (*AuthResponse, error)
8+
9+
// Environment
10+
// CreateEnvironment creates the environment.
11+
CreateEnvironment(create *EnvironmentCreate) (*Environment, error)
12+
// GetEnvironment gets the environment by id.
13+
GetEnvironment(environmentID int) (*Environment, error)
14+
// ListEnvironment finds all environments.
15+
ListEnvironment() ([]*Environment, error)
16+
// UpdateEnvironment updates the environment.
17+
UpdateEnvironment(environmentID int, patch *EnvironmentPatch) (*Environment, error)
18+
// DeleteEnvironment deletes the environment.
19+
DeleteEnvironment(environmentID int) error
20+
21+
// Instance
22+
// ListInstance will return all instances.
23+
ListInstance() ([]*Instance, error)
24+
// CreateInstance creates the instance.
25+
CreateInstance(create *InstanceCreate) (*Instance, error)
26+
// GetInstance gets the instance by id.
27+
GetInstance(instanceID int) (*Instance, error)
28+
// UpdateInstance updates the instance.
29+
UpdateInstance(instanceID int, patch *InstancePatch) (*Instance, error)
30+
// DeleteInstance deletes the instance.
31+
DeleteInstance(instanceID int) error
32+
}

api/environment.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ type Environment struct {
1313
type EnvironmentCreate struct {
1414
// Domain specific fields
1515
Name string `json:"name"`
16-
Order *int `json:"order"`
16+
Order *int `json:"order,omitempty"`
1717
}
1818

1919
// EnvironmentPatch is the API message for patching an environment.
2020
type EnvironmentPatch struct {
2121
// Domain specific fields
22-
Name string `json:"name,omitempty"`
23-
Order int `json:"order"`
22+
Name *string `json:"name,omitempty"`
23+
Order *int `json:"order,omitempty"`
2424
}

client/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
// Login will login the user and get the response.
15-
func (c *Client) Login() (*api.AuthResponse, error) {
15+
func (c *client) Login() (*api.AuthResponse, error) {
1616
if c.Auth.Email == "" || c.Auth.Password == "" {
1717
return nil, errors.Errorf("define username and password")
1818
}

client/client.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import (
88
"time"
99

1010
"github.com/pkg/errors"
11+
12+
"github.com/bytebase/terraform-provider-bytebase/api"
1113
)
1214

13-
// Client is the API message for Bytebase API client.
14-
type Client struct {
15+
// client is the API message for Bytebase API client.
16+
type client struct {
1517
HostURL string
1618
HTTPClient *http.Client
1719
Token string
@@ -24,8 +26,8 @@ type authStruct struct {
2426
}
2527

2628
// NewClient returns the new Bytebase API client.
27-
func NewClient(url, email, password string) (*Client, error) {
28-
c := Client{
29+
func NewClient(url, email, password string) (api.Client, error) {
30+
c := client{
2931
HTTPClient: &http.Client{Timeout: 10 * time.Second},
3032
HostURL: url,
3133
}
@@ -45,7 +47,7 @@ func NewClient(url, email, password string) (*Client, error) {
4547
return &c, nil
4648
}
4749

48-
func (c *Client) doRequest(req *http.Request) ([]byte, error) {
50+
func (c *client) doRequest(req *http.Request) ([]byte, error) {
4951
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.Token))
5052

5153
res, err := c.HTTPClient.Do(req)

client/environment.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
// CreateEnvironment creates the environment.
13-
func (c *Client) CreateEnvironment(create *api.EnvironmentCreate) (*api.Environment, error) {
13+
func (c *client) CreateEnvironment(create *api.EnvironmentCreate) (*api.Environment, error) {
1414
payload, err := json.Marshal(create)
1515
if err != nil {
1616
return nil, err
@@ -36,7 +36,7 @@ func (c *Client) CreateEnvironment(create *api.EnvironmentCreate) (*api.Environm
3636
}
3737

3838
// GetEnvironment gets the environment by id.
39-
func (c *Client) GetEnvironment(environmentID int) (*api.Environment, error) {
39+
func (c *client) GetEnvironment(environmentID int) (*api.Environment, error) {
4040
req, err := http.NewRequest("GET", fmt.Sprintf("%s/environment/%d", c.HostURL, environmentID), nil)
4141
if err != nil {
4242
return nil, err
@@ -57,7 +57,7 @@ func (c *Client) GetEnvironment(environmentID int) (*api.Environment, error) {
5757
}
5858

5959
// ListEnvironment finds all environments.
60-
func (c *Client) ListEnvironment() ([]*api.Environment, error) {
60+
func (c *client) ListEnvironment() ([]*api.Environment, error) {
6161
req, err := http.NewRequest("GET", fmt.Sprintf("%s/environment", c.HostURL), nil)
6262
if err != nil {
6363
return nil, err
@@ -78,7 +78,7 @@ func (c *Client) ListEnvironment() ([]*api.Environment, error) {
7878
}
7979

8080
// UpdateEnvironment updates the environment.
81-
func (c *Client) UpdateEnvironment(environmentID int, patch *api.EnvironmentPatch) (*api.Environment, error) {
81+
func (c *client) UpdateEnvironment(environmentID int, patch *api.EnvironmentPatch) (*api.Environment, error) {
8282
payload, err := json.Marshal(patch)
8383
if err != nil {
8484
return nil, err
@@ -104,7 +104,7 @@ func (c *Client) UpdateEnvironment(environmentID int, patch *api.EnvironmentPatc
104104
}
105105

106106
// DeleteEnvironment deletes the environment.
107-
func (c *Client) DeleteEnvironment(environmentID int) error {
107+
func (c *client) DeleteEnvironment(environmentID int) error {
108108
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/environment/%d", c.HostURL, environmentID), nil)
109109
if err != nil {
110110
return err

client/instance.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
// ListInstance will return all instances.
13-
func (c *Client) ListInstance() ([]*api.Instance, error) {
13+
func (c *client) ListInstance() ([]*api.Instance, error) {
1414
req, err := http.NewRequest("GET", fmt.Sprintf("%s/instance", c.HostURL), nil)
1515
if err != nil {
1616
return nil, err
@@ -31,7 +31,7 @@ func (c *Client) ListInstance() ([]*api.Instance, error) {
3131
}
3232

3333
// CreateInstance creates the instance.
34-
func (c *Client) CreateInstance(create *api.InstanceCreate) (*api.Instance, error) {
34+
func (c *client) CreateInstance(create *api.InstanceCreate) (*api.Instance, error) {
3535
payload, err := json.Marshal(create)
3636
if err != nil {
3737
return nil, err
@@ -57,7 +57,7 @@ func (c *Client) CreateInstance(create *api.InstanceCreate) (*api.Instance, erro
5757
}
5858

5959
// GetInstance gets the instance by id.
60-
func (c *Client) GetInstance(instanceID int) (*api.Instance, error) {
60+
func (c *client) GetInstance(instanceID int) (*api.Instance, error) {
6161
req, err := http.NewRequest("GET", fmt.Sprintf("%s/instance/%d", c.HostURL, instanceID), nil)
6262
if err != nil {
6363
return nil, err
@@ -78,7 +78,7 @@ func (c *Client) GetInstance(instanceID int) (*api.Instance, error) {
7878
}
7979

8080
// UpdateInstance updates the instance.
81-
func (c *Client) UpdateInstance(instanceID int, patch *api.InstancePatch) (*api.Instance, error) {
81+
func (c *client) UpdateInstance(instanceID int, patch *api.InstancePatch) (*api.Instance, error) {
8282
payload, err := json.Marshal(patch)
8383
if err != nil {
8484
return nil, err
@@ -104,7 +104,7 @@ func (c *Client) UpdateInstance(instanceID int, patch *api.InstancePatch) (*api.
104104
}
105105

106106
// DeleteInstance deletes the instance.
107-
func (c *Client) DeleteInstance(instanceID int) error {
107+
func (c *client) DeleteInstance(instanceID int) error {
108108
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/instance/%d", c.HostURL, instanceID), nil)
109109
if err != nil {
110110
return err

examples/main.tf

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# This is an example for using bytebase terraform provider to manage your resource.
2+
# To run this provider in your local machine,
3+
# 1. Run your bytebase service, then you can access the OpenAPI via http://localhost:8080/v1
4+
# 2. Replace the email and password with your account
5+
# 3. Run `make install` under terraform-provider-bytebase folder
6+
# 4. Run `terraform init` under terraform-provider-bytebase/examples folder
7+
# 5. Run terraform plan or terraform apply
8+
terraform {
9+
required_providers {
10+
bytebase = {
11+
version = "0.0.1"
12+
# The source is only used in this local example.
13+
source = "registry.terraform.io/bytebase/bytebase"
14+
}
15+
}
16+
}
17+
18+
provider "bytebase" {
19+
# You need to replace the email and password with your own bytebase account.
20+
21+
password = "ed"
22+
bytebase_url = "http://localhost:8080/v1"
23+
}
24+
25+
# Create a new environment named "dev"
26+
resource "bytebase_environment" "dev" {
27+
name = "dev"
28+
}
29+
30+
# Print the new environment
31+
output "staging_environment" {
32+
value = bytebase_environment.dev
33+
}

0 commit comments

Comments
 (0)