Skip to content

Commit

Permalink
Grafana OnCall: routing_type attribute for grafana_oncall_route resou…
Browse files Browse the repository at this point in the history
…rce (support Jinja2 templates) (#892)

* ResourceRoute: routing_type attribute

* ResourceRoute: routing_type attribute docs

* ResourceRoute: fix routing_type attribute description

* fix lint & docs

* Upgrade github.com/grafana/amixr-api-go-client to v0.0.8

* Set `regex` as a default

* Generate docs

---------

Co-authored-by: Sergey Arefev <sergey-arefev@yandex-team.ru>
Co-authored-by: Ildar Iskhakov <ildar.iskhakov@grafana.com>
Co-authored-by: Julien Duchesne <julien.duchesne@grafana.com>
  • Loading branch information
4 people authored Jun 26, 2023
1 parent 569af5f commit 1326a83
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/resources/oncall_route.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ resource "grafana_oncall_route" "example_route" {
### Optional

- `msteams` (Block List, Max: 1) MS teams-specific settings for a route. (see [below for nested schema](#nestedblock--msteams))
- `routing_type` (String) The type of route. Can be jinja2, regex Defaults to `regex`.
- `slack` (Block List, Max: 1) Slack-specific settings for a route. (see [below for nested schema](#nestedblock--slack))
- `telegram` (Block List, Max: 1) Telegram-specific settings for a route. (see [below for nested schema](#nestedblock--telegram))

Expand Down
23 changes: 23 additions & 0 deletions internal/resources/oncall/resource_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@ package oncall

import (
"context"
"fmt"
"log"
"net/http"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

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

var routeTypeOptions = []string{
"jinja2",
"regex",
}

var routeTypeOptionsVerbal = strings.Join(routeTypeOptions, ", ")

func ResourceRoute() *schema.Resource {
return &schema.Resource{
Description: `
Expand Down Expand Up @@ -41,6 +52,13 @@ func ResourceRoute() *schema.Resource {
Required: true,
Description: "The position of the route (starts from 0).",
},
"routing_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(routeTypeOptions, false),
Default: "regex",
Description: fmt.Sprintf("The type of route. Can be %s", routeTypeOptionsVerbal),
},
"routing_regex": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -118,6 +136,7 @@ func ResourceRouteCreate(ctx context.Context, d *schema.ResourceData, m interfac

integrationID := d.Get("integration_id").(string)
escalationChainID := d.Get("escalation_chain_id").(string)
routingType := d.Get("routing_type").(string)
routingRegex := d.Get("routing_regex").(string)
position := d.Get("position").(int)
slack := d.Get("slack").([]interface{})
Expand All @@ -127,6 +146,7 @@ func ResourceRouteCreate(ctx context.Context, d *schema.ResourceData, m interfac
createOptions := &onCallAPI.CreateRouteOptions{
IntegrationId: integrationID,
EscalationChainId: escalationChainID,
RoutingType: routingType,
RoutingRegex: routingRegex,
Position: &position,
ManualOrder: true,
Expand Down Expand Up @@ -160,6 +180,7 @@ func ResourceRouteRead(ctx context.Context, d *schema.ResourceData, m interface{

d.Set("integration_id", route.IntegrationId)
d.Set("escalation_chain_id", route.EscalationChainId)
d.Set("routing_type", route.RoutingType)
d.Set("routing_regex", route.RoutingRegex)
d.Set("position", route.Position)

Expand All @@ -184,6 +205,7 @@ func ResourceRouteUpdate(ctx context.Context, d *schema.ResourceData, m interfac
client := m.(*common.Client).OnCallClient

escalationChainID := d.Get("escalation_chain_id").(string)
routingType := d.Get("routing_type").(string)
routingRegex := d.Get("routing_regex").(string)
position := d.Get("position").(int)
slack := d.Get("slack").([]interface{})
Expand All @@ -192,6 +214,7 @@ func ResourceRouteUpdate(ctx context.Context, d *schema.ResourceData, m interfac

updateOptions := &onCallAPI.UpdateRouteOptions{
EscalationChainId: escalationChainID,
RoutingType: routingType,
RoutingRegex: routingRegex,
Position: &position,
ManualOrder: true,
Expand Down

0 comments on commit 1326a83

Please sign in to comment.