Skip to content

Grafana oncall integration data source #1532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/data-sources/oncall_integration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "grafana_oncall_integration Data Source - terraform-provider-grafana"
subcategory: "OnCall"
description: |-
HTTP API https://grafana.com/docs/oncall/latest/oncall-api-reference/integrations/
---

# grafana_oncall_integration (Data Source)

* [HTTP API](https://grafana.com/docs/oncall/latest/oncall-api-reference/integrations/)

## Example Usage

```terraform
data "grafana_oncall_integration" "example_integration" {
id = "CEXAMPLEID123"
}
```

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

### Required

- `id` (String) The integration ID.

### Read-Only

- `name` (String) The integration name.
2 changes: 2 additions & 0 deletions docs/resources/oncall_route.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ resource "grafana_oncall_escalation_chain" "default" {
resource "grafana_oncall_integration" "example_integration" {
name = "Grafana Integration"
type = "grafana"
default_route {
}
}

resource "grafana_oncall_route" "example_route" {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "grafana_oncall_integration" "example_integration" {
id = "CEXAMPLEID123"
}
4 changes: 3 additions & 1 deletion examples/resources/grafana_oncall_route/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ resource "grafana_oncall_escalation_chain" "default" {
resource "grafana_oncall_integration" "example_integration" {
name = "Grafana Integration"
type = "grafana"
default_route {
}
}

resource "grafana_oncall_route" "example_route" {
Expand All @@ -28,4 +30,4 @@ resource "grafana_oncall_route" "example_route" {
id = "ONCALLMSTEAMSID"
enabled = false
}
}
}
50 changes: 50 additions & 0 deletions internal/resources/oncall/data_source_integration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package oncall

import (
"context"
"strings"

onCallAPI "github.com/grafana/amixr-api-go-client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceIntegration() *schema.Resource {
return &schema.Resource{
Description: `
* [HTTP API](https://grafana.com/docs/oncall/latest/oncall-api-reference/integrations/)
`,
ReadContext: withClient[schema.ReadContextFunc](dataSourceIntegrationRead),
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
Description: "The integration ID.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The integration name.",
},
},
}
}

func dataSourceIntegrationRead(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
options := &onCallAPI.GetIntegrationOptions{}
integrationID := d.Get("id").(string)

integrationResponse, _, err := client.Integrations.GetIntegration(integrationID, options)
if err != nil {
if strings.HasPrefix(err.Error(), "status: 404") {
return diag.Errorf("no integration exists with ID %q", integrationID)
}
return diag.FromErr(err)
}

d.SetId(integrationResponse.ID)
d.Set("id", integrationResponse.ID)
d.Set("name", integrationResponse.Name)

return nil
}
52 changes: 52 additions & 0 deletions internal/resources/oncall/data_source_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package oncall_test

import (
"fmt"
"testing"

"github.com/grafana/terraform-provider-grafana/v2/internal/testutils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceIntegration_Basic(t *testing.T) {
testutils.CheckCloudInstanceTestsEnabled(t)

randomName := fmt.Sprintf("test-name-%s", acctest.RandString(10))

integrationPath := "grafana_oncall_integration.test_integration"
dataSourcePath := "data.grafana_oncall_integration.test_integration_ds"

resource.Test(t, resource.TestCase{
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckOnCallIntegrationResourceDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceIntegration(randomName),
Check: resource.ComposeTestCheckFunc(
testAccCheckOnCallIntegrationResourceExists(integrationPath),
resource.TestCheckResourceAttr(integrationPath, "name", randomName),
resource.TestCheckResourceAttrPair(
integrationPath, "id",
dataSourcePath, "id",
),
),
},
},
})
}

func testAccDataSourceIntegration(randomName string) string {
return fmt.Sprintf(`
resource "grafana_oncall_integration" "test_integration" {
name = "%[1]s"
type = "grafana"
default_route {
}
}

data "grafana_oncall_integration" "test_integration_ds" {
id = grafana_oncall_integration.test_integration.id
}
`, randomName)
}
1 change: 1 addition & 0 deletions internal/resources/oncall/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var DatasourcesMap = map[string]*schema.Resource{
"grafana_oncall_outgoing_webhook": dataSourceOutgoingWebhook(),
"grafana_oncall_user_group": dataSourceUserGroup(),
"grafana_oncall_team": dataSourceTeam(),
"grafana_oncall_integration": dataSourceIntegration(),
}

var Resources = []*common.Resource{
Expand Down
1 change: 1 addition & 0 deletions tools/subcategories.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"data-sources/users": "Grafana OSS",
"data-sources/oncall_action": "OnCall",
"data-sources/oncall_escalation_chain": "OnCall",
"data-sources/oncall_integration": "OnCall",
"data-sources/oncall_outgoing_webhook": "OnCall",
"data-sources/oncall_schedule": "OnCall",
"data-sources/oncall_slack_channel": "OnCall",
Expand Down
Loading