-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add policy and severity groups (#110)
- Loading branch information
1 parent
24b6cf7
commit 3f87a50
Showing
14 changed files
with
584 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()))) | ||
} |
Oops, something went wrong.