Skip to content

Commit

Permalink
new: statusflare_integration resource
Browse files Browse the repository at this point in the history
  • Loading branch information
eidam committed May 17, 2021
1 parent e7dab33 commit 99ea7fc
Show file tree
Hide file tree
Showing 12 changed files with 335 additions and 89 deletions.
10 changes: 2 additions & 8 deletions docs/data-sources/integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@ description: |-
---

# Data Source statusflare_integration
# statusflare_integration (Data Source)


Use this data source to look up Integration in Statusflare..

## Example Usage

```terraform
data "statusflare_integration" "slack" {
name = "some-slack-integration"
}
```

<!-- schema generated by tfplugindocs -->
## Schema
Expand Down
10 changes: 5 additions & 5 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ description: |-
---

# Statusflare Provider
# statusflare Provider

The Statusflare provider is used to interact with Statusflare monitor & integration resources.


## Example Usage
Expand All @@ -35,6 +34,7 @@ provider "statusflare" {

### Optional

- **account_id** (String) Your Statusflare Account ID. This can also be specified with the `STATUSFLARE_ACCOUNT_ID` env. variable.
- **key_id** (String) Your token's key ID. This can also be specified with the `STATUSFLARE_KEY_ID` env. variable.
- **token** (String) Token's secret part. This can also be specified with the `STATUSFLARE_TOKEN` env. variable.
- **account_id** (String) Your Statusflare Account ID. This can also be specified with the `SF_ACCOUNT_ID` env. variable.
- **api_url** (String) Statusflare API URL.
- **key_id** (String) Your token's key ID. This can also be specified with the `SF_KEY_ID` env. variable.
- **token** (String) Token's secret part. This can also be specified with the `SF_TOKEN` env. variable.
28 changes: 28 additions & 0 deletions docs/resources/integration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "statusflare_integration Resource - terraform-provider-statusflare"
subcategory: ""
description: |-
---

# statusflare_integration (Resource)





<!-- schema generated by tfplugindocs -->
## Schema

### Required

- **name** (String) The name of the integration
- **secret** (String, Sensitive) The secret of the integration, e.g. webhook URL

### Optional

- **id** (String) The ID of this resource.
- **type** (String) Type of the integration, e.g. webhook


30 changes: 4 additions & 26 deletions docs/resources/monitor.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,10 @@ description: |-
---

# Resource statusflare_monitor

Provides a Monitor resource for Statusflare.

## Example Usage

```terraform
// example of simple monitor
resource "statusflare_monitor" "first" {
name = "hello-world"
url = "www.helloworld.com"
}
// example of simple monitor where incidents are forwarded to integration
data "statusflare_integration" "slack" {
name = "some-slack-integration"
}
resource "statusflare_monitor" "first" {
name = "hello-world"
url = "www.helloworld.com"
integrations = [
data.statusflare_integration.slack.id
]
}
```
# statusflare_monitor (Resource)





<!-- schema generated by tfplugindocs -->
Expand Down
11 changes: 6 additions & 5 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,34 @@ func New(version string) *schema.Provider {
configFields := map[string]*schema.Schema{
"api_url": {
Type: schema.TypeString,
Optional: true,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("SF_API_URL", "https://api.statusflare.com"),
Description: "Statusflare API URL.",
},
"account_id": {
Type: schema.TypeString,
Optional: true,
Required: true,
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,
Required: true,
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,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("SF_TOKEN", nil),
Description: "Token's secret part. This can also be specified with the `SF_TOKEN` env. variable.",
},
}

return &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
"statusflare_monitor": resourceMonitor(),
"statusflare_monitor": resourceMonitor(),
"statusflare_integration": resourceIntegration(),
},
DataSourcesMap: map[string]*schema.Resource{
"statusflare_integration": dataSourceIntegration(),
Expand Down
120 changes: 120 additions & 0 deletions internal/provider/resource_integration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package provider

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/statusflare-com/terraform-provider-statusflare/statusflare"
)

func resourceIntegration() *schema.Resource {

fields := map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the integration",
},
"type": {
Type: schema.TypeString,
Optional: true,
Default: "webhook",
Description: "Type of the integration, e.g. webhook",
},
"secret": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Sensitive: true,
Description: "The secret of the integration, e.g. webhook URL",
},
}

return &schema.Resource{
CreateContext: resourceIntegrationCreate,
ReadContext: resourceIntegrationRead,
UpdateContext: resourceIntegrationUpdate,
DeleteContext: resourceIntegrationDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: fields,
}
}

func resourceIntegrationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
var client *statusflare.Client = meta.(*statusflare.Client)

integration, err := client.GetIntegration(d.Id())
if err != nil {
return diag.FromErr(err)
}

integrationToData(integration, d)
return diags
}

func resourceIntegrationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
var client *statusflare.Client = meta.(*statusflare.Client)

integration := statusflare.Integration{}
dataToIntegration(d, &integration)

err := client.CreateIntegration(&integration)
if err != nil {
return diag.FromErr(err)
}

d.SetId(integration.Id)
d.Set("secret", integration.Secret)

resourceIntegrationRead(ctx, d, meta)
return diags
}

func resourceIntegrationUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var client *statusflare.Client = meta.(*statusflare.Client)

integration, err := client.GetIntegration(d.Id())
if err != nil {
return diag.FromErr(err)
}

dataToIntegration(d, integration)
err = client.SaveIntegration(integration)
if err != nil {
return diag.FromErr(err)
}

return resourceIntegrationRead(ctx, d, meta)
}

func resourceIntegrationDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var (
err error
diags diag.Diagnostics
client *statusflare.Client = m.(*statusflare.Client)
)

err = client.DeleteIntegration(d.Id())
if err != nil {
return diag.FromErr(err)
}

d.SetId("")
return diags
}

func dataToIntegration(src *schema.ResourceData, dst *statusflare.Integration) {
dst.Name = src.Get("name").(string)
dst.Type = src.Get("type").(string)
dst.Secret = src.Get("secret").(string)
}

func integrationToData(src *statusflare.Integration, dst *schema.ResourceData) {
dst.Set("name", src.Name)
dst.Set("type", src.Type)
}
2 changes: 1 addition & 1 deletion internal/provider/resource_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func resourceMonitorCreate(ctx context.Context, d *schema.ResourceData, meta int
return diag.FromErr(err)
}

d.SetId(monitor.ID)
d.SetId(monitor.Id)
resourceMonitorRead(ctx, d, meta)
return diags
}
Expand Down
16 changes: 8 additions & 8 deletions statusflare/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
// represent the session to statusflare
type Client struct {
apiUrl string
accountID string
keyID string
accountId string
keyId string
token string
http *http.Client
}
Expand Down Expand Up @@ -54,8 +54,8 @@ func DefaultClient() (*Client, error) {

client := &Client{
apiUrl: os.Getenv("SF_API_URL"),
accountID: os.Getenv("SF_ACCOUNT_ID"),
keyID: os.Getenv("SF_KEY_ID"),
accountId: os.Getenv("SF_ACCOUNT_ID"),
keyId: os.Getenv("SF_KEY_ID"),
token: os.Getenv("SF_TOKEN"),
http: &http.Client{},
}
Expand All @@ -69,11 +69,11 @@ func DefaultClient() (*Client, error) {
//
// The account ID identify the whole account. Account might
// have multiple key IDs with tokens.
func NewClient(apiUrl string, accountID string, keyID string, token string) *Client {
func NewClient(apiUrl string, accountId string, keyId string, token string) *Client {
return &Client{
apiUrl: apiUrl,
accountID: accountID,
keyID: keyID,
accountId: accountId,
keyId: keyId,
token: token,
http: &http.Client{},
}
Expand All @@ -93,7 +93,7 @@ func (c *Client) makeAPICall(method string, endpoint string, body []byte) (*http
req, _ := http.NewRequest(method, url, reader)
req.Header = map[string][]string{
"X-Statusflare-Token": {c.token},
"X-Statusflare-Token-Key-Id": {c.keyID},
"X-Statusflare-Token-Key-Id": {c.keyId},
}

resp, err := c.http.Do(req)
Expand Down
Loading

0 comments on commit 99ea7fc

Please sign in to comment.