Skip to content

Commit

Permalink
Add policy and severity groups (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarwynNelson authored Jul 29, 2024
1 parent 24b6cf7 commit 3f87a50
Show file tree
Hide file tree
Showing 14 changed files with 584 additions and 10 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/betteruptime_policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Policy lookup.

- **id** (String) The ID of this Policy.
- **incident_token** (String) Incident token that can be used for manually reporting incidents.
- **policy_group_id** (Number) Set this attribute if you want to add this policy to a policy group.
- **repeat_count** (Number) How many times should the entire policy be repeated if no one acknowledges the incident.
- **repeat_delay** (Number) How long in seconds to wait before each repetition.
- **steps** (List of Object) An array of escalation policy steps (see [below for nested schema](#nestedatt--steps))
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/betteruptime_severity.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Severity lookup.
- **email** (Boolean) Whether to send email when a new incident is created.
- **id** (String) The ID of this Severity.
- **push** (Boolean) Whether to send push notification when a new incident is created.
- **severity_group_id** (Number) Set this attribute if you want to add this severity to a severity group.
- **sms** (Boolean) Whether to send SMS when a new incident is created.


1 change: 1 addition & 0 deletions docs/resources/betteruptime_policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ https://betterstack.com/docs/uptime/api/list-all-escalation-policies/

### Optional

- **policy_group_id** (Number) Set this attribute if you want to add this policy to a policy group.
- **repeat_count** (Number) How many times should the entire policy be repeated if no one acknowledges the incident.
- **repeat_delay** (Number) How long in seconds to wait before each repetition.
- **team_name** (String) Used to specify the team the resource should be created in when using global tokens.
Expand Down
33 changes: 33 additions & 0 deletions docs/resources/betteruptime_policy_group.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "betteruptime_policy_group Resource - terraform-provider-better-uptime"
subcategory: ""
description: |-
https://betterstack.com/docs/uptime/api/policy-groups/
---

# betteruptime_policy_group (Resource)

https://betterstack.com/docs/uptime/api/policy-groups/



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

### Required

- **name** (String) A name of the group that you can see in the dashboard.

### Optional

- **sort_index** (Number) Set sort_index to specify how to sort your policy groups.
- **team_name** (String) Used to specify the team the resource should be created in when using global tokens.

### Read-Only

- **created_at** (String) The time when this policy group was created.
- **id** (String) The ID of this policy group.
- **updated_at** (String) The time when this policy group was updated.


1 change: 1 addition & 0 deletions docs/resources/betteruptime_severity.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ https://betterstack.com/docs/uptime/api/list-all-severities/
- **call** (Boolean) Whether to call when a new incident is created.
- **email** (Boolean) Whether to send email when a new incident is created.
- **push** (Boolean) Whether to send push notification when a new incident is created.
- **severity_group_id** (Number) Set this attribute if you want to add this severity to a severity group.
- **sms** (Boolean) Whether to send SMS when a new incident is created.
- **team_name** (String) Used to specify the team the resource should be created in when using global tokens.

Expand Down
33 changes: 33 additions & 0 deletions docs/resources/betteruptime_severity_group.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "betteruptime_severity_group Resource - terraform-provider-better-uptime"
subcategory: ""
description: |-
https://betterstack.com/docs/uptime/api/urgency-groups/
---

# betteruptime_severity_group (Resource)

https://betterstack.com/docs/uptime/api/urgency-groups/



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

### Required

- **name** (String) A name of the group that you can see in the dashboard.

### Optional

- **sort_index** (Number) Set sort_index to specify how to sort your severity groups.
- **team_name** (String) Used to specify the team the resource should be created in when using global tokens.

### Read-Only

- **created_at** (String) The time when this severity group was created.
- **id** (String) The ID of this severity group.
- **updated_at** (String) The time when this severity group was updated.


25 changes: 22 additions & 3 deletions examples/advanced/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,15 @@ data "betteruptime_severity" "this" {
name = var.betteruptime_severity_name
}

resource "betteruptime_policy_group" "this" {
name = "Policies from Terraform"
}

resource "betteruptime_policy" "this" {
name = "Standard Escalation Policy"
repeat_count = 3
repeat_delay = 60
name = "Standard Escalation Policy"
repeat_count = 3
repeat_delay = 60
policy_group_id = betteruptime_policy_group.this.id

steps {
type = "escalation"
Expand Down Expand Up @@ -270,3 +275,17 @@ resource "betteruptime_policy" "this" {
step_members { type = "entire_team" }
}
}

resource "betteruptime_severity_group" "this" {
name = "Severities from Terraform"
}

resource "betteruptime_severity" "this" {
name = "Terraform"
call = false
email = false
push = false
sms = false

severity_group_id = betteruptime_severity_group.this.id
}
2 changes: 2 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ func New(opts ...Option) *schema.Provider {
"betteruptime_monitor": newMonitorResource(),
"betteruptime_monitor_group": newMonitorGroupResource(),
"betteruptime_policy": newPolicyResource(),
"betteruptime_policy_group": newPolicyGroupResource(),
"betteruptime_severity": newSeverityResource(),
"betteruptime_severity_group": newSeverityGroupResource(),
"betteruptime_status_page": newStatusPageResource(),
"betteruptime_status_page_group": newStatusPageGroupResource(),
"betteruptime_status_page_section": newStatusPageSectionResource(),
Expand Down
7 changes: 7 additions & 0 deletions internal/provider/resource_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ var policySchema = map[string]*schema.Schema{
Elem: &schema.Resource{Schema: policyStepSchema},
Required: true,
},
"policy_group_id": {
Description: "Set this attribute if you want to add this policy to a policy group.",
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
}

func newPolicyResource() *schema.Resource {
Expand Down Expand Up @@ -195,6 +201,7 @@ type policy struct {
IncidentToken *string `json:"incident_token,omitempty"`
Steps *[]policyStep `json:"steps"`
TeamName *string `json:"team_name,omitempty"`
PolicyGroupID *int `json:"policy_group_id,omitempty"`
}

type policyHTTPResponse struct {
Expand Down
150 changes: 150 additions & 0 deletions internal/provider/resource_policy_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package provider

import (
"context"
"fmt"
"net/url"
"reflect"

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

var policyGroupSchema = map[string]*schema.Schema{
"team_name": {
Description: "Used to specify the team the resource should be created in when using global tokens.",
Type: schema.TypeString,
Optional: true,
Default: nil,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return d.Id() != ""
},
},
"id": {
Description: "The ID of this policy group.",
Type: schema.TypeString,
Optional: false,
Computed: true,
},
"name": {
Description: "A name of the group that you can see in the dashboard.",
Type: schema.TypeString,
Required: true,
},
"sort_index": {
Description: "Set sort_index to specify how to sort your policy groups.",
Type: schema.TypeInt,
Optional: true,
Computed: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return !d.HasChange(k)
},
},
"created_at": {
Description: "The time when this policy group was created.",
Type: schema.TypeString,
Optional: false,
Computed: true,
},
"updated_at": {
Description: "The time when this policy group was updated.",
Type: schema.TypeString,
Optional: false,
Computed: true,
},
}

func newPolicyGroupResource() *schema.Resource {
return &schema.Resource{
CreateContext: policyGroupCreate,
ReadContext: policyGroupRead,
UpdateContext: policyGroupUpdate,
DeleteContext: policyGroupDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Description: "https://betterstack.com/docs/uptime/api/policy-groups/",
Schema: policyGroupSchema,
}
}

type policyGroup struct {
Name *string `json:"name,omitempty"`
SortIndex *int `json:"sort_index,omitempty"`
CreatedAt *string `json:"created_at,omitempty"`
UpdatedAt *string `json:"updated_at,omitempty"`
TeamName *string `json:"team_name,omitempty"`
}

type policyGroupHTTPResponse struct {
Data struct {
ID string `json:"id"`
Attributes policyGroup `json:"attributes"`
} `json:"data"`
}

func policyGroupRef(in *policyGroup) []struct {
k string
v interface{}
} {
// TODO: if reflect.TypeOf(in).NumField() != len([]struct)
return []struct {
k string
v interface{}
}{
{k: "name", v: &in.Name},
{k: "sort_index", v: &in.SortIndex},
{k: "created_at", v: &in.CreatedAt},
{k: "updated_at", v: &in.UpdatedAt},
}
}

func policyGroupCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var in policyGroup
for _, e := range policyGroupRef(&in) {
load(d, e.k, e.v)
}
load(d, "team_name", &in.TeamName)
var out policyGroupHTTPResponse
if err := resourceCreate(ctx, meta, "/api/v2/policy-groups", &in, &out); err != nil {
return err
}
d.SetId(out.Data.ID)
return policyGroupCopyAttrs(d, &out.Data.Attributes)
}

func policyGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var out policyGroupHTTPResponse
if err, ok := resourceRead(ctx, meta, fmt.Sprintf("/api/v2/policy-groups/%s", url.PathEscape(d.Id())), &out); err != nil {
return err
} else if !ok {
d.SetId("") // Force "create" on 404.
return nil
}
return policyGroupCopyAttrs(d, &out.Data.Attributes)
}

func policyGroupCopyAttrs(d *schema.ResourceData, in *policyGroup) diag.Diagnostics {
var derr diag.Diagnostics
for _, e := range policyGroupRef(in) {
if err := d.Set(e.k, reflect.Indirect(reflect.ValueOf(e.v)).Interface()); err != nil {
derr = append(derr, diag.FromErr(err)[0])
}
}
return derr
}

func policyGroupUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var in policyGroup
var out policyHTTPResponse
for _, e := range policyGroupRef(&in) {
if d.HasChange(e.k) {
load(d, e.k, e.v)
}
}
return resourceUpdate(ctx, meta, fmt.Sprintf("/api/v2/policy-groups/%s", url.PathEscape(d.Id())), &in, &out)
}

func policyGroupDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return resourceDelete(ctx, meta, fmt.Sprintf("/api/v2/policy-groups/%s", url.PathEscape(d.Id())))
}
Loading

0 comments on commit 3f87a50

Please sign in to comment.