Skip to content

Commit

Permalink
Merge pull request #1 from statusflare-com/e/api-errors
Browse files Browse the repository at this point in the history
chore: configurable api url, api error responses
  • Loading branch information
eidam authored May 17, 2021
2 parents 4c102c9 + ad36b61 commit e7dab33
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 37 deletions.
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ terraform {
required_providers {
statusflare = {
version = "~> 1.0"
source = "statusflare.com/statusflare/statusflare"
source = "github.com/statusflare-com/terraform-provider-statusflare"
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
2 changes: 1 addition & 1 deletion internal/provider/datasource_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 15 additions & 8 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
},
}

Expand All @@ -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{}
}
2 changes: 1 addition & 1 deletion internal/provider/resource_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
57 changes: 40 additions & 17 deletions statusflare/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package statusflare
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -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{},
}

Expand All @@ -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,
Expand All @@ -72,20 +82,33 @@ 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},
"X-Statusflare-Token-Key-Id": {c.keyID},
}

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
}

Expand Down
6 changes: 3 additions & 3 deletions statusflare/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit e7dab33

Please sign in to comment.