-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from uptime-com/givi-opsgenie-integration-sys…
…-274 feat: sys-274 add opsgenie integration resource
- Loading branch information
Showing
8 changed files
with
246 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "uptime_integration_opsgenie Resource - terraform-provider-uptime" | ||
subcategory: "" | ||
description: |- | ||
Opsgenie integration resource | ||
--- | ||
|
||
# uptime_integration_opsgenie (Resource) | ||
|
||
Opsgenie integration resource | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `api_endpoint` (String) | ||
- `api_key` (String) | ||
- `name` (String) | ||
|
||
### Optional | ||
|
||
- `auto_resolve` (Boolean) Automatically resolve incident once the check is back up. | ||
- `contact_groups` (Set of String) | ||
- `tags` (String) A comma separated list of labels attached to the alert. You may overwrite the quiet hours setting for urgent alerts by adding the OverwriteQuietHours tag. Leave blank to automatically pull the tags from the check instead. | ||
- `teams` (String) A comma separated list of team names which will be responsible for the alert | ||
|
||
### Read-Only | ||
|
||
- `id` (Number) The ID of this resource. | ||
- `url` (String) | ||
|
||
|
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,133 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/uptime-com/uptime-client-go/v2/pkg/upapi" | ||
) | ||
|
||
func NewIntegrationOpsgenieResource(_ context.Context, p *providerImpl) resource.Resource { | ||
return APIResource[IntegrationOpsgenieResourceModel, upapi.IntegrationOpsgenie, upapi.Integration]{ | ||
api: IntegrationOpsgenieResourceAPI{provider: p}, | ||
mod: IntegrationOpsgenieResourceModelAdapter{}, | ||
meta: APIResourceMetadata{ | ||
TypeNameSuffix: "integration_opsgenie", | ||
Schema: schema.Schema{ | ||
Description: "Opsgenie integration resource", | ||
Attributes: map[string]schema.Attribute{ | ||
"id": IDSchemaAttribute(), | ||
"url": URLSchemaAttribute(), | ||
"name": NameSchemaAttribute(), | ||
"contact_groups": ContactGroupsSchemaAttribute(), | ||
"api_endpoint": schema.StringAttribute{ | ||
Required: true, | ||
}, | ||
"api_key": schema.StringAttribute{ | ||
Required: true, | ||
}, | ||
"teams": schema.StringAttribute{ | ||
Optional: true, | ||
Computed: true, | ||
Default: stringdefault.StaticString(""), | ||
Description: "A comma separated list of team names which will be responsible for the alert", | ||
}, | ||
"tags": schema.StringAttribute{ | ||
Optional: true, | ||
Computed: true, | ||
Default: stringdefault.StaticString(""), | ||
Description: ("A comma separated list of labels attached to the alert. " + | ||
"You may overwrite the quiet hours setting for urgent alerts by adding the OverwriteQuietHours tag. " + | ||
"Leave blank to automatically pull the tags from the check instead."), | ||
}, | ||
"auto_resolve": schema.BoolAttribute{ | ||
Optional: true, | ||
Computed: true, | ||
Description: "Automatically resolve incident once the check is back up.", | ||
Default: booldefault.StaticBool(false), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
type IntegrationOpsgenieResourceModel struct { | ||
ID types.Int64 `tfsdk:"id"` | ||
URL types.String `tfsdk:"url"` | ||
Name types.String `tfsdk:"name"` | ||
ContactGroups types.Set `tfsdk:"contact_groups"` | ||
APIEndpoint types.String `tfsdk:"api_endpoint"` | ||
APIKey types.String `tfsdk:"api_key"` | ||
Teams types.String `tfsdk:"teams"` | ||
Tags types.String `tfsdk:"tags"` | ||
AutoResolve types.Bool `tfsdk:"auto_resolve"` | ||
} | ||
|
||
func (m IntegrationOpsgenieResourceModel) PrimaryKey() upapi.PrimaryKey { | ||
return upapi.PrimaryKey(m.ID.ValueInt64()) | ||
} | ||
|
||
type IntegrationOpsgenieResourceModelAdapter struct { | ||
ContactGroupsAttributeAdapter | ||
} | ||
|
||
func (a IntegrationOpsgenieResourceModelAdapter) Get(ctx context.Context, sg StateGetter) (*IntegrationOpsgenieResourceModel, diag.Diagnostics) { | ||
model := *new(IntegrationOpsgenieResourceModel) | ||
diags := sg.Get(ctx, &model) | ||
if diags.HasError() { | ||
return nil, diags | ||
} | ||
return &model, nil | ||
} | ||
|
||
func (a IntegrationOpsgenieResourceModelAdapter) ToAPIArgument(model IntegrationOpsgenieResourceModel) (*upapi.IntegrationOpsgenie, error) { | ||
return &upapi.IntegrationOpsgenie{ | ||
Name: model.Name.ValueString(), | ||
ContactGroups: a.ContactGroups(model.ContactGroups), | ||
APIEndpoint: model.APIEndpoint.ValueString(), | ||
APIKey: model.APIKey.ValueString(), | ||
Teams: model.Teams.ValueString(), | ||
Tags: model.Tags.ValueString(), | ||
Autoresolve: model.AutoResolve.ValueBool(), | ||
}, nil | ||
} | ||
|
||
func (a IntegrationOpsgenieResourceModelAdapter) FromAPIResult(api upapi.Integration) (*IntegrationOpsgenieResourceModel, error) { | ||
return &IntegrationOpsgenieResourceModel{ | ||
ID: types.Int64Value(api.PK), | ||
URL: types.StringValue(api.URL), | ||
Name: types.StringValue(api.Name), | ||
ContactGroups: a.ContactGroupsValue(api.ContactGroups), | ||
APIEndpoint: types.StringValue(api.APIEndpoint), | ||
APIKey: types.StringValue(api.APIKey), | ||
Teams: types.StringValue(api.Teams), | ||
Tags: types.StringValue(api.Tags), | ||
AutoResolve: types.BoolValue(api.Autoresolve), | ||
}, nil | ||
} | ||
|
||
type IntegrationOpsgenieResourceAPI struct { | ||
provider *providerImpl | ||
} | ||
|
||
func (a IntegrationOpsgenieResourceAPI) Create(ctx context.Context, arg upapi.IntegrationOpsgenie) (*upapi.Integration, error) { | ||
return a.provider.api.Integrations().CreateOpsgenie(ctx, arg) | ||
} | ||
|
||
func (a IntegrationOpsgenieResourceAPI) Read(ctx context.Context, pk upapi.PrimaryKeyable) (*upapi.Integration, error) { | ||
return a.provider.api.Integrations().Get(ctx, pk) | ||
} | ||
|
||
func (a IntegrationOpsgenieResourceAPI) Update(ctx context.Context, pk upapi.PrimaryKeyable, arg upapi.IntegrationOpsgenie) (*upapi.Integration, error) { | ||
return a.provider.api.Integrations().UpdateOpsgenie(ctx, pk, arg) | ||
} | ||
|
||
func (a IntegrationOpsgenieResourceAPI) Delete(ctx context.Context, pk upapi.PrimaryKeyable) error { | ||
return a.provider.api.Integrations().Delete(ctx, pk) | ||
} |
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,54 @@ | ||
package provider | ||
|
||
import ( | ||
"testing" | ||
|
||
petname "github.com/dustinkirkland/golang-petname" | ||
"github.com/hashicorp/terraform-plugin-testing/config" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
) | ||
|
||
func TestAccIntegrationOpsgenieResource(t *testing.T) { | ||
names := [2]string{ | ||
petname.Generate(3, "-"), | ||
petname.Generate(3, "-"), | ||
} | ||
apiEndpoint := [2]string{ | ||
"https://api.opsgenie.com/v1/json/uptime1", | ||
"https://api.opsgenie.com/v1/json/uptime2", | ||
} | ||
apiKey := [2]string{ | ||
"16c8bfe0-b219-11ef-a5da-4b9e62fe7439", | ||
"274d1848-b219-11ef-a468-9f18e59bdc97", | ||
} | ||
resource.Test(t, testCaseFromSteps(t, []resource.TestStep{ | ||
{ | ||
ConfigVariables: config.Variables{ | ||
"name": config.StringVariable(names[0]), | ||
"api_endpoint": config.StringVariable(apiEndpoint[0]), | ||
"api_key": config.StringVariable(apiKey[0]), | ||
}, | ||
ConfigDirectory: config.StaticDirectory("testdata/resource_integration_opsgenie/_basic"), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("uptime_integration_opsgenie.test", "id"), | ||
resource.TestCheckResourceAttrSet("uptime_integration_opsgenie.test", "url"), | ||
resource.TestCheckResourceAttr("uptime_integration_opsgenie.test", "name", names[0]), | ||
resource.TestCheckResourceAttr("uptime_integration_opsgenie.test", "api_endpoint", apiEndpoint[0]), | ||
resource.TestCheckResourceAttr("uptime_integration_opsgenie.test", "api_key", apiKey[0]), | ||
), | ||
}, | ||
{ | ||
ConfigVariables: config.Variables{ | ||
"name": config.StringVariable(names[1]), | ||
"api_endpoint": config.StringVariable(apiEndpoint[1]), | ||
"api_key": config.StringVariable(apiKey[1]), | ||
}, | ||
ConfigDirectory: config.StaticDirectory("testdata/resource_integration_opsgenie/_basic"), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("uptime_integration_opsgenie.test", "name", names[1]), | ||
resource.TestCheckResourceAttr("uptime_integration_opsgenie.test", "api_endpoint", apiEndpoint[1]), | ||
resource.TestCheckResourceAttr("uptime_integration_opsgenie.test", "api_key", apiKey[1]), | ||
), | ||
}, | ||
})) | ||
} |
17 changes: 17 additions & 0 deletions
17
internal/provider/testdata/resource_integration_opsgenie/_basic/main.tf
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,17 @@ | ||
variable name { | ||
type = string | ||
} | ||
|
||
variable api_endpoint { | ||
type = string | ||
} | ||
|
||
variable api_key { | ||
type = string | ||
} | ||
|
||
resource uptime_integration_opsgenie test { | ||
name = var.name | ||
api_endpoint = var.api_endpoint | ||
api_key = var.api_key | ||
} |
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