Skip to content

Commit bfc9d9d

Browse files
committed
feat: initial the provider
0 parents  commit bfc9d9d

File tree

12 files changed

+662
-0
lines changed

12 files changed

+662
-0
lines changed

.github/workflows/tests.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: build
2+
3+
on:
4+
push:
5+
branches:
6+
- "main"
7+
pull_request:
8+
9+
jobs:
10+
golangci:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
- uses: actions/setup-go@v3
15+
with:
16+
go-version: 1.19
17+
- run: go generate ./...
18+
- name: golangci-lint
19+
uses: golangci/golangci-lint-action@v3
20+
with:
21+
version: v1.49.0
22+
args: -v
23+
skip-cache: true
24+
go-tidy:
25+
runs-on: ubuntu-latest
26+
steps:
27+
- uses: actions/checkout@v3
28+
- uses: actions/setup-go@v3
29+
with:
30+
go-version: 1.19
31+
- name: Verify tidy
32+
run: |
33+
go mod tidy
34+
git diff --exit-code
35+
go-tests:
36+
runs-on: ubuntu-latest
37+
steps:
38+
- name: Checkout
39+
uses: actions/checkout@v3
40+
with:
41+
fetch-depth: 0
42+
- uses: actions/setup-go@v3
43+
with:
44+
go-version: 1.19
45+
- run: |
46+
go generate ./...
47+
go test -v ./...

.golangci.yaml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
linters:
2+
enable:
3+
- errcheck
4+
- goimports
5+
- revive
6+
- govet
7+
- staticcheck
8+
- misspell
9+
- gocritic
10+
- sqlclosecheck
11+
- rowserrcheck
12+
- nilerr
13+
- godot
14+
- forbidigo
15+
16+
# golangci-lint run --exclude="Rollback,logger.Sync,pgInstance.Stop"
17+
issues:
18+
include:
19+
# https://golangci-lint.run/usage/configuration/#command-line-options
20+
- EXC0012
21+
- EXC0013
22+
- EXC0014
23+
- EXC0015
24+
exclude:
25+
- Rollback
26+
- logger.Sync
27+
- pgInstance.Stop
28+
- fmt.Printf
29+
- fmt.Print
30+
31+
run:
32+
timeout: 5m
33+
build-tags:
34+
- mysql
35+
36+
linters-settings:
37+
goimports:
38+
# Put imports beginning with prefix after 3rd-party packages.
39+
# It's a comma-separated list of prefixes.
40+
local-prefixes: github.com/bytebase/terraform-provider-bytebase
41+
revive:
42+
# Default to run all linters so that new rules in the future could automatically be added to the static check.
43+
enable-all-rules: true
44+
rules:
45+
# The following rules are too strict and make coding harder. We do not enable them for now.
46+
- name: file-header
47+
disabled: true
48+
- name: line-length-limit
49+
disabled: true
50+
- name: function-length
51+
disabled: true
52+
- name: max-public-structs
53+
disabled: true
54+
- name: function-result-limit
55+
disabled: true
56+
- name: banned-characters
57+
disabled: true
58+
- name: argument-limit
59+
disabled: true
60+
- name: cognitive-complexity
61+
disabled: true
62+
- name: cyclomatic
63+
disabled: true
64+
- name: confusing-results
65+
disabled: true
66+
- name: add-constant
67+
disabled: true
68+
- name: flag-parameter
69+
disabled: true
70+
- name: nested-structs
71+
disabled: true
72+
- name: import-shadowing
73+
disabled: true
74+
- name: early-return
75+
disabled: true
76+
gocritic:
77+
disabled-checks:
78+
- ifElseChain
79+
forbidigo:
80+
forbid:
81+
- 'fmt\.Errorf(# Please use errors\.Wrap\|Wrapf\|Errorf instead)?'
82+
- 'ioutil\.ReadDir(# Please use os\.ReadDir)?'

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Terraform Provider Bytebase
2+
3+
This repository is the Terraform provider for Bytebase.

api/auth.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package api
2+
3+
// AuthResponse is the API message for user login.
4+
type AuthResponse struct {
5+
UserID int `json:"userId"`
6+
Username string `json:"username"`
7+
Email string `json:"email"`
8+
Token string `json:"token"`
9+
}

api/doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package api is the API message for Bytebase Terraform provider.
2+
package api

client/auth.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package client
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"strings"
8+
9+
"github.com/pkg/errors"
10+
11+
"github.com/bytebase/terraform-provider-bytebase/api"
12+
)
13+
14+
// Login will login the user and get the response.
15+
func (c *Client) Login() (*api.AuthResponse, error) {
16+
if c.Auth.Email == "" || c.Auth.Password == "" {
17+
return nil, errors.Errorf("define username and password")
18+
}
19+
rb, err := json.Marshal(c.Auth)
20+
if err != nil {
21+
return nil, err
22+
}
23+
24+
req, err := http.NewRequest("POST", fmt.Sprintf("%s/auth/login", c.HostURL), strings.NewReader(string(rb)))
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
body, err := c.doRequest(req)
30+
if err != nil {
31+
return nil, err
32+
}
33+
34+
ar := api.AuthResponse{}
35+
err = json.Unmarshal(body, &ar)
36+
if err != nil {
37+
return nil, err
38+
}
39+
40+
return &ar, nil
41+
}

client/client.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Package client is the API message for Bytebase API client.
2+
package client
3+
4+
import (
5+
"fmt"
6+
"io"
7+
"net/http"
8+
"time"
9+
10+
"github.com/pkg/errors"
11+
)
12+
13+
// Client is the API message for Bytebase API client.
14+
type Client struct {
15+
HostURL string
16+
HTTPClient *http.Client
17+
Token string
18+
Auth *authStruct
19+
}
20+
21+
type authStruct struct {
22+
Email string `json:"email"`
23+
Password string `json:"password"`
24+
}
25+
26+
// NewClient returns the new Bytebase API client.
27+
func NewClient(url, email, password string) (*Client, error) {
28+
c := Client{
29+
HTTPClient: &http.Client{Timeout: 10 * time.Second},
30+
HostURL: url,
31+
}
32+
33+
c.Auth = &authStruct{
34+
Email: email,
35+
Password: password,
36+
}
37+
38+
ar, err := c.Login()
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
c.Token = ar.Token
44+
45+
return &c, nil
46+
}
47+
48+
func (c *Client) doRequest(req *http.Request) ([]byte, error) {
49+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.Token))
50+
51+
res, err := c.HTTPClient.Do(req)
52+
if err != nil {
53+
return nil, err
54+
}
55+
defer res.Body.Close()
56+
57+
body, err := io.ReadAll(res.Body)
58+
if err != nil {
59+
return nil, err
60+
}
61+
62+
if res.StatusCode != http.StatusOK {
63+
return nil, errors.Errorf("status: %d, body: %s", res.StatusCode, body)
64+
}
65+
66+
return body, err
67+
}

go.mod

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module github.com/bytebase/terraform-provider-bytebase
2+
3+
go 1.19
4+
5+
require (
6+
github.com/hashicorp/terraform-plugin-log v0.7.0
7+
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.0
8+
github.com/pkg/errors v0.9.1
9+
)
10+
11+
require (
12+
github.com/agext/levenshtein v1.2.2 // indirect
13+
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
14+
github.com/fatih/color v1.13.0 // indirect
15+
github.com/golang/protobuf v1.5.2 // indirect
16+
github.com/google/go-cmp v0.5.9 // indirect
17+
github.com/hashicorp/errwrap v1.1.0 // indirect
18+
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
19+
github.com/hashicorp/go-hclog v1.2.1 // indirect
20+
github.com/hashicorp/go-multierror v1.1.1 // indirect
21+
github.com/hashicorp/go-plugin v1.4.4 // indirect
22+
github.com/hashicorp/go-uuid v1.0.3 // indirect
23+
github.com/hashicorp/go-version v1.6.0 // indirect
24+
github.com/hashicorp/hcl/v2 v2.14.1 // indirect
25+
github.com/hashicorp/logutils v1.0.0 // indirect
26+
github.com/hashicorp/terraform-plugin-go v0.14.0 // indirect
27+
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c // indirect
28+
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
29+
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
30+
github.com/kr/pretty v0.3.0 // indirect
31+
github.com/mattn/go-colorable v0.1.12 // indirect
32+
github.com/mattn/go-isatty v0.0.14 // indirect
33+
github.com/mitchellh/copystructure v1.2.0 // indirect
34+
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
35+
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
36+
github.com/mitchellh/mapstructure v1.5.0 // indirect
37+
github.com/mitchellh/reflectwalk v1.0.2 // indirect
38+
github.com/oklog/run v1.0.0 // indirect
39+
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
40+
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
41+
github.com/vmihailenco/tagparser v0.1.1 // indirect
42+
github.com/zclconf/go-cty v1.11.0 // indirect
43+
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
44+
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b // indirect
45+
golang.org/x/text v0.3.7 // indirect
46+
google.golang.org/appengine v1.6.6 // indirect
47+
google.golang.org/genproto v0.0.0-20200711021454-869866162049 // indirect
48+
google.golang.org/grpc v1.48.0 // indirect
49+
google.golang.org/protobuf v1.28.1 // indirect
50+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
51+
)

0 commit comments

Comments
 (0)