-
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.
- Loading branch information
1 parent
a6ae969
commit 24b6cf7
Showing
11 changed files
with
293 additions
and
6 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_status_page_group Resource - terraform-provider-better-uptime" | ||
subcategory: "" | ||
description: |- | ||
https://betterstack.com/docs/uptime/api/status-page-groups/ | ||
--- | ||
|
||
# betteruptime_status_page_group (Resource) | ||
|
||
https://betterstack.com/docs/uptime/api/status-page-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 status page 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 status page group was created. | ||
- **id** (String) The ID of this status page group. | ||
- **updated_at** (String) The time when this status page 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
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 statusPageGroupSchema = 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 status page 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 status page 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 status page group was created.", | ||
Type: schema.TypeString, | ||
Optional: false, | ||
Computed: true, | ||
}, | ||
"updated_at": { | ||
Description: "The time when this status page group was updated.", | ||
Type: schema.TypeString, | ||
Optional: false, | ||
Computed: true, | ||
}, | ||
} | ||
|
||
func newStatusPageGroupResource() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: statusPageGroupCreate, | ||
ReadContext: statusPageGroupRead, | ||
UpdateContext: statusPageGroupUpdate, | ||
DeleteContext: statusPageGroupDelete, | ||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
Description: "https://betterstack.com/docs/uptime/api/status-page-groups/", | ||
Schema: statusPageGroupSchema, | ||
} | ||
} | ||
|
||
type statusPageGroup 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 statusPageGroupHTTPResponse struct { | ||
Data struct { | ||
ID string `json:"id"` | ||
Attributes statusPageGroup `json:"attributes"` | ||
} `json:"data"` | ||
} | ||
|
||
func statusPageGroupRef(in *statusPageGroup) []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 statusPageGroupCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var in statusPageGroup | ||
for _, e := range statusPageGroupRef(&in) { | ||
load(d, e.k, e.v) | ||
} | ||
load(d, "team_name", &in.TeamName) | ||
var out statusPageGroupHTTPResponse | ||
if err := resourceCreate(ctx, meta, "/api/v2/status-page-groups", &in, &out); err != nil { | ||
return err | ||
} | ||
d.SetId(out.Data.ID) | ||
return statusPageGroupCopyAttrs(d, &out.Data.Attributes) | ||
} | ||
|
||
func statusPageGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var out statusPageGroupHTTPResponse | ||
if err, ok := resourceRead(ctx, meta, fmt.Sprintf("/api/v2/status-page-groups/%s", url.PathEscape(d.Id())), &out); err != nil { | ||
return err | ||
} else if !ok { | ||
d.SetId("") // Force "create" on 404. | ||
return nil | ||
} | ||
return statusPageGroupCopyAttrs(d, &out.Data.Attributes) | ||
} | ||
|
||
func statusPageGroupCopyAttrs(d *schema.ResourceData, in *statusPageGroup) diag.Diagnostics { | ||
var derr diag.Diagnostics | ||
for _, e := range statusPageGroupRef(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 statusPageGroupUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var in statusPageGroup | ||
var out policyHTTPResponse | ||
for _, e := range statusPageGroupRef(&in) { | ||
if d.HasChange(e.k) { | ||
load(d, e.k, e.v) | ||
} | ||
} | ||
return resourceUpdate(ctx, meta, fmt.Sprintf("/api/v2/status-page-groups/%s", url.PathEscape(d.Id())), &in, &out) | ||
} | ||
|
||
func statusPageGroupDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
return resourceDelete(ctx, meta, fmt.Sprintf("/api/v2/status-page-groups/%s", url.PathEscape(d.Id()))) | ||
} |
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,84 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestResourceStatusPageGroup(t *testing.T) { | ||
server := newResourceServer(t, "/api/v2/status-page-groups", "1") | ||
defer server.Close() | ||
|
||
var name = "example" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
IsUnitTest: true, | ||
ProviderFactories: map[string]func() (*schema.Provider, error){ | ||
"betteruptime": func() (*schema.Provider, error) { | ||
return New(WithURL(server.URL)), nil | ||
}, | ||
}, | ||
Steps: []resource.TestStep{ | ||
// Step 1 - create. | ||
{ | ||
Config: fmt.Sprintf(` | ||
provider "betteruptime" { | ||
api_token = "foo" | ||
} | ||
resource "betteruptime_status_page_group" "this" { | ||
name = "%s" | ||
sort_index = 1 | ||
} | ||
`, name), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("betteruptime_status_page_group.this", "id"), | ||
resource.TestCheckResourceAttr("betteruptime_status_page_group.this", "name", name), | ||
resource.TestCheckResourceAttr("betteruptime_status_page_group.this", "sort_index", "1"), | ||
), | ||
}, | ||
// Step 2 - update. | ||
{ | ||
Config: fmt.Sprintf(` | ||
provider "betteruptime" { | ||
api_token = "foo" | ||
} | ||
resource "betteruptime_status_page_group" "this" { | ||
name = "%s" | ||
sort_index = 0 | ||
} | ||
`, name), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("betteruptime_status_page_group.this", "id"), | ||
resource.TestCheckResourceAttr("betteruptime_status_page_group.this", "name", name), | ||
resource.TestCheckResourceAttr("betteruptime_status_page_group.this", "sort_index", "0"), | ||
), | ||
}, | ||
// Step 3 - make no changes, check plan is empty. | ||
{ | ||
Config: fmt.Sprintf(` | ||
provider "betteruptime" { | ||
api_token = "foo" | ||
} | ||
resource "betteruptime_status_page_group" "this" { | ||
name = "%s" | ||
sort_index = 0 | ||
} | ||
`, name), | ||
PlanOnly: true, | ||
}, | ||
// Step 4 - destroy. | ||
{ | ||
ResourceName: "betteruptime_status_page_group.this", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} |