Skip to content

Commit

Permalink
Merge pull request #126 from uptime-com/givi-opsgenie-integration-sys…
Browse files Browse the repository at this point in the history
…-274

feat: sys-274 add opsgenie integration resource
  • Loading branch information
gigovich authored Dec 4, 2024
2 parents dcf8db1 + e5c15fb commit 7adcb7a
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 4 deletions.
36 changes: 36 additions & 0 deletions docs/resources/integration_opsgenie.md
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)


2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/shopspring/decimal v1.4.0
github.com/stretchr/testify v1.10.0
github.com/uptime-com/uptime-client-go/v2 v2.0.0-20241203124913-62cfc1744b9a
github.com/uptime-com/uptime-client-go/v2 v2.0.0-20241204093759-45854fa6a664
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/uptime-com/uptime-client-go/v2 v2.0.0-20241203124913-62cfc1744b9a h1:SUXIUdqiAvW42db1yTE+aIuYAdVyMuv7o3VcsCoInsM=
github.com/uptime-com/uptime-client-go/v2 v2.0.0-20241203124913-62cfc1744b9a/go.mod h1:jtWeB/tQ00fLX2r9OwKfTnxQ/PMR0YjmhTuc9RZH2h0=
github.com/uptime-com/uptime-client-go/v2 v2.0.0-20241204093759-45854fa6a664 h1:IphsKMPkm6/EQryzDZtfYxImHyk/HnCkJs1MtkB7NGU=
github.com/uptime-com/uptime-client-go/v2 v2.0.0-20241204093759-45854fa6a664/go.mod h1:jtWeB/tQ00fLX2r9OwKfTnxQ/PMR0YjmhTuc9RZH2h0=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ func (p *providerImpl) Resources(ctx context.Context) []func() resource.Resource
func() resource.Resource { return NewCheckWHOISResource(ctx, p) },
func() resource.Resource { return NewCheckWebhookResource(ctx, p) },
func() resource.Resource { return NewCheckMaintenanceResource(ctx, p) },

func() resource.Resource { return NewIntegrationOpsgenieResource(ctx, p) },
func() resource.Resource { return NewContactResource(ctx, p) },
func() resource.Resource { return NewCredentialResource(ctx, p) },
func() resource.Resource { return NewStatusPageResource(ctx, p) },
Expand Down
133 changes: 133 additions & 0 deletions internal/provider/resource_integration_opsgenie.go
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)
}
54 changes: 54 additions & 0 deletions internal/provider/resource_integration_opsgenie_test.go
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]),
),
},
}))
}
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
}
2 changes: 1 addition & 1 deletion internal/provider/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func testAccAPIClient(t testing.TB) upapi.API {
token := os.Getenv("UPTIME_TOKEN")
require.NotEmpty(t, token, "UPTIME_TOKEN must be set for acceptance tests")

api, err := upapi.New(upapi.WithToken(token), upapi.WithRateLimit(0.2))
api, err := upapi.New(upapi.WithToken(token), upapi.WithRateLimit(0.15))
require.NoError(t, err, "failed to initialize uptime.com api client")

return api
Expand Down

0 comments on commit 7adcb7a

Please sign in to comment.