From ad36b618ae81f3273eaadd239e53e31b77936e64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Jani=C5=A1?= Date: Mon, 17 May 2021 15:11:54 +0200 Subject: [PATCH] chore: configurable api url, api error responses --- Makefile | 10 ++-- README.md | 2 +- go.mod | 4 +- internal/provider/datasource_integration.go | 2 +- internal/provider/provider.go | 23 ++++++--- internal/provider/resource_monitor.go | 2 +- main.go | 2 +- statusflare/client.go | 57 +++++++++++++++------ statusflare/monitor.go | 6 +-- 9 files changed, 71 insertions(+), 37 deletions(-) diff --git a/Makefile b/Makefile index 5abaaa3..de5c5f8 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ -HOSTNAME=statusflare.com -NAMESPACE=statusflare +TEST?=$$(go list ./...) +HOSTNAME=github.com +NAMESPACE=statusflare-com NAME=statusflare BINARY=terraform-provider-${NAME} VERSION=1.0 @@ -14,7 +15,10 @@ install: build mkdir -p ~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/${OS_ARCH} mv ${BINARY} ~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/${OS_ARCH} +testacc: + TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m -parallel 1 + clean: rm -rf ${BINARY} rm -rf ./examples/.terraform - rm -rf ./examples/terraform.* + rm -rf ./examples/terraform.* \ No newline at end of file diff --git a/README.md b/README.md index 8c2fb30..55a3cb6 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ terraform { required_providers { statusflare = { version = "~> 1.0" - source = "statusflare.com/statusflare/statusflare" + source = "github.com/statusflare-com/terraform-provider-statusflare" } } } diff --git a/go.mod b/go.mod index 87905ce..8deb51b 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,9 @@ -module github.com/statusflare/terraform-provider-statusflare +module github.com/statusflare-com/terraform-provider-statusflare go 1.16 require ( github.com/hashicorp/terraform-plugin-docs v0.4.0 // indirect - github.com/hashicorp/terraform-plugin-sdk/v2 v2.6.1 // indirect + github.com/hashicorp/terraform-plugin-sdk/v2 v2.6.1 github.com/smartystreets/goconvey v1.6.4 // indirect ) diff --git a/internal/provider/datasource_integration.go b/internal/provider/datasource_integration.go index cae906e..7c1a871 100644 --- a/internal/provider/datasource_integration.go +++ b/internal/provider/datasource_integration.go @@ -5,7 +5,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/statusflare/terraform-provider-statusflare/statusflare" + "github.com/statusflare-com/terraform-provider-statusflare/statusflare" ) func dataSourceIntegration() *schema.Resource { diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 93b37ea..f3aefd0 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -5,30 +5,36 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/statusflare/terraform-provider-statusflare/statusflare" + "github.com/statusflare-com/terraform-provider-statusflare/statusflare" ) // This is provider's 'main' entry point. func New(version string) *schema.Provider { configFields := map[string]*schema.Schema{ + "api_url": { + Type: schema.TypeString, + Optional: true, + DefaultFunc: schema.EnvDefaultFunc("SF_API_URL", "https://api.statusflare.com"), + Description: "Statusflare API URL.", + }, "account_id": { Type: schema.TypeString, Optional: true, - DefaultFunc: schema.EnvDefaultFunc("STATUSFLARE_ACCOUNT_ID", nil), - Description: "Your Statusflare Account ID. This can also be specified with the `STATUSFLARE_ACCOUNT_ID` env. variable.", + DefaultFunc: schema.EnvDefaultFunc("SF_ACCOUNT_ID", nil), + Description: "Your Statusflare Account ID. This can also be specified with the `SF_ACCOUNT_ID` env. variable.", }, "key_id": { Type: schema.TypeString, Optional: true, - DefaultFunc: schema.EnvDefaultFunc("STATUSFLARE_KEY_ID", nil), - Description: "Your token's key ID. This can also be specified with the `STATUSFLARE_KEY_ID` env. variable.", + DefaultFunc: schema.EnvDefaultFunc("SF_KEY_ID", nil), + Description: "Your token's key ID. This can also be specified with the `SF_KEY_ID` env. variable.", }, "token": { Type: schema.TypeString, Optional: true, - DefaultFunc: schema.EnvDefaultFunc("STATUSFLARE_TOKEN", nil), - Description: "Token's secret part. This can also be specified with the `STATUSFLARE_TOKEN` env. variable.", + DefaultFunc: schema.EnvDefaultFunc("SF_TOKEN", nil), + Description: "Token's secret part. This can also be specified with the `SF_TOKEN` env. variable.", }, } @@ -46,10 +52,11 @@ func New(version string) *schema.Provider { // this function initialize and configure the Statusflare client func configure(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) { + apiUrl := d.Get("api_url").(string) accountId := d.Get("account_id").(string) keyId := d.Get("key_id").(string) token := d.Get("token").(string) - client := statusflare.NewClient(accountId, keyId, token) + client := statusflare.NewClient(apiUrl, accountId, keyId, token) return client, diag.Diagnostics{} } diff --git a/internal/provider/resource_monitor.go b/internal/provider/resource_monitor.go index a3b6fa3..75c7648 100644 --- a/internal/provider/resource_monitor.go +++ b/internal/provider/resource_monitor.go @@ -5,7 +5,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/statusflare/terraform-provider-statusflare/statusflare" + "github.com/statusflare-com/terraform-provider-statusflare/statusflare" ) func resourceMonitor() *schema.Resource { diff --git a/main.go b/main.go index b3d9dd0..cabd5b4 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/plugin" - "github.com/statusflare/terraform-provider-statusflare/internal/provider" + "github.com/statusflare-com/terraform-provider-statusflare/internal/provider" ) var ( diff --git a/statusflare/client.go b/statusflare/client.go index ae88b26..2f03135 100644 --- a/statusflare/client.go +++ b/statusflare/client.go @@ -3,6 +3,7 @@ package statusflare import ( "bytes" "encoding/json" + "errors" "fmt" "io" "io/ioutil" @@ -12,41 +13,50 @@ import ( // represent the session to statusflare type Client struct { - url string + apiUrl string accountID string keyID string token string http *http.Client } +type ErrorResponse struct { + Error struct { + Message string `json:"message"` + } `json:"error"` +} + // create default client where all needed data are // read from env. variables. The required env. variables // are: -// - STATUSFLARE_ACCOUNT_ID -// - STATUSFLARE_KEY_ID -// - STATUSFLARE_TOKEN +// - SF_ACCOUNT_ID +// - SF_KEY_ID +// - SF_TOKEN // // If none of the env. variables are set, the function returns // you nil client and error. func DefaultClient() (*Client, error) { + if os.Getenv("SF_API_URL") == "" { + return nil, fmt.Errorf("missing SF_API_URL") + } - if os.Getenv("STATUSFLARE_ACCOUNT_ID") == "" { - return nil, fmt.Errorf("missing STATUSFLARE_ACCOUNT_ID") + if os.Getenv("SF_ACCOUNT_ID") == "" { + return nil, fmt.Errorf("missing SF_ACCOUNT_ID") } - if os.Getenv("STATUSFLARE_KEY_ID") == "" { - return nil, fmt.Errorf("missing STATUSFLARE_KEY_ID") + if os.Getenv("SF_KEY_ID") == "" { + return nil, fmt.Errorf("missing SF_KEY_ID") } - if os.Getenv("STATUSFLARE_TOKEN") == "" { - return nil, fmt.Errorf("missing STATUSFLARE_TOKEN") + if os.Getenv("SF_TOKEN") == "" { + return nil, fmt.Errorf("missing SF_TOKEN") } client := &Client{ - url: "https://api.statusflare.com/", - accountID: os.Getenv("STATUSFLARE_ACCOUNT_ID"), - keyID: os.Getenv("STATUSFLARE_KEY_ID"), - token: os.Getenv("STATUSFLARE_TOKEN"), + apiUrl: os.Getenv("SF_API_URL"), + accountID: os.Getenv("SF_ACCOUNT_ID"), + keyID: os.Getenv("SF_KEY_ID"), + token: os.Getenv("SF_TOKEN"), http: &http.Client{}, } @@ -59,9 +69,9 @@ func DefaultClient() (*Client, error) { // // The account ID identify the whole account. Account might // have multiple key IDs with tokens. -func NewClient(accountID string, keyID string, token string) *Client { +func NewClient(apiUrl string, accountID string, keyID string, token string) *Client { return &Client{ - url: "https://api.statusflare.com/", + apiUrl: apiUrl, accountID: accountID, keyID: keyID, token: token, @@ -72,13 +82,14 @@ func NewClient(accountID string, keyID string, token string) *Client { // all API calls are performed via this function func (c *Client) makeAPICall(method string, endpoint string, body []byte) (*http.Response, error) { var err error + var errorResponse ErrorResponse var reader io.Reader if body != nil { reader = bytes.NewReader(body) } - url := c.url + endpoint + url := c.apiUrl + endpoint req, _ := http.NewRequest(method, url, reader) req.Header = map[string][]string{ "X-Statusflare-Token": {c.token}, @@ -86,6 +97,18 @@ func (c *Client) makeAPICall(method string, endpoint string, body []byte) (*http } resp, err := c.http.Do(req) + if err != nil { + return nil, err + } + + if resp.StatusCode != 200 { + err = unmarshallResp(resp, &errorResponse) + if err != nil { + return nil, fmt.Errorf("reading api error response failed, status code %d", resp.StatusCode) + } + return nil, errors.New(errorResponse.Error.Message) + } + return resp, err } diff --git a/statusflare/monitor.go b/statusflare/monitor.go index c7cbde8..f8d06ac 100644 --- a/statusflare/monitor.go +++ b/statusflare/monitor.go @@ -40,7 +40,7 @@ func (c *Client) CreateMonitor(m *Monitor) error { } if resp.StatusCode != 200 { - return fmt.Errorf("The server respond %d", resp.StatusCode) + return fmt.Errorf("the server respond %d", resp.StatusCode) } return unmarshallResp(resp, m) @@ -99,7 +99,7 @@ func (c *Client) SaveMonitor(m *Monitor) error { } if resp.StatusCode != 200 { - return fmt.Errorf("The server respond %d", resp.StatusCode) + return fmt.Errorf("the server respond %d", resp.StatusCode) } return unmarshallResp(resp, m) @@ -114,7 +114,7 @@ func (c *Client) DeleteMonitor(id string) error { } if resp.StatusCode != 200 { - return fmt.Errorf("The server respond %d", resp.StatusCode) + return fmt.Errorf("the server respond %d", resp.StatusCode) } return nil