Skip to content
Open
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
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/3-bug-report-enhanced.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ body:
- grafana_oncall_integration (data source)
- grafana_oncall_integration (resource)
- grafana_oncall_label (data source)
- grafana_oncall_on_call_shift (data source)
- grafana_oncall_on_call_shift (resource)
- grafana_oncall_outgoing_webhook (data source)
- grafana_oncall_outgoing_webhook (resource)
Expand Down
46 changes: 46 additions & 0 deletions docs/data-sources/oncall_on_call_shift.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "grafana_oncall_on_call_shift Data Source - terraform-provider-grafana"
subcategory: "OnCall"
description: |-
HTTP API https://grafana.com/docs/oncall/latest/oncall-api-reference/on_call_shifts/
---

# grafana_oncall_on_call_shift (Data Source)

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

## Example Usage

```terraform
data "grafana_oncall_on_call_shift" "shift" {
name = "example_shift"
}
```

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

### Required

- `name` (String) The shift's name.

### Read-Only

- `by_day` (Set of String) This parameter takes a list of days in iCal format. Can be MO, TU, WE, TH, FR, SA, SU
- `by_month` (Set of Number) This parameter takes a list of months. Valid values are 1 to 12
- `by_monthday` (Set of Number) This parameter takes a list of days of the month. Valid values are 1 to 31 or -31 to -1
- `duration` (Number) The duration of the event.
- `frequency` (String) The frequency of the event. Can be hourly, daily, weekly, monthly
- `id` (String) The ID of this resource.
- `interval` (Number) The positive integer representing at which intervals the recurrence rule repeats.
- `level` (Number) The priority level. The higher the value, the higher the priority.
- `rolling_users` (List of Set of String) The list of lists with on-call users (for rolling_users event type)
- `start` (String) The start time of the on-call shift. This parameter takes a date format as yyyy-MM-dd'T'HH:mm:ss (for example "2020-09-05T08:00:00")
- `start_rotation_from_user_index` (Number) The index of the list of users in rolling_users, from which on-call rotation starts.
- `team_id` (String) The ID of the OnCall team (using the `grafana_oncall_team` datasource).
- `time_zone` (String) The shift's timezone. Overrides schedule's timezone.
- `type` (String) The shift's type. Can be rolling_users, recurrent_event, single_event
- `until` (String) The end time of recurrent on-call shifts (endless if null). This parameter takes a date format as yyyy-MM-dd'T'HH:mm:ss (for example "2020-09-05T08:00:00")
- `users` (Set of String) The list of on-call users (for single_event and recurrent_event event type).
- `week_start` (String) Start day of the week in iCal format. Can be MO, TU, WE, TH, FR, SA, SU
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "grafana_oncall_on_call_shift" "shift" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wonder if this is the time to start using grafana_irm_* as a prefix. It would help with the name oddity here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be cool 👍

shift is particularly odd. I initially implemented this as grafana_oncall_shift, but realized the corresponding resource is grafana_oncall_on_call_shift and followed suit for consistency. Not sure why the extra on_call segment. Other resource/datasource names do not have this.

name = "example_shift"
}
68 changes: 68 additions & 0 deletions internal/resources/oncall/data_source_shift.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package oncall

import (
"context"

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

func dataSourceOnCallShift() *common.DataSource {
schema := &schema.Resource{
Description: `
* [HTTP API](https://grafana.com/docs/oncall/latest/oncall-api-reference/on_call_shifts/)
`,
ReadContext: withClient[schema.ReadContextFunc](dataSourceOnCallShiftRead),
Schema: common.CloneResourceSchemaForDatasource(resourceOnCallShift().Schema, map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "The shift's name.",
},
}),
}
return common.NewLegacySDKDataSource(common.CategoryOnCall, "grafana_oncall_on_call_shift", schema)
}

func dataSourceOnCallShiftRead(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
options := &onCallAPI.ListOnCallShiftOptions{}
nameData := d.Get("name").(string)

options.Name = nameData

shiftsResponse, _, err := client.OnCallShifts.ListOnCallShifts(options)
if err != nil {
return diag.FromErr(err)
}

if len(shiftsResponse.OnCallShifts) == 0 {
return diag.Errorf("couldn't find a shift matching: %s", options.Name)
} else if len(shiftsResponse.OnCallShifts) != 1 {
return diag.Errorf("more than one shift found matching: %s", options.Name)
}

shift := shiftsResponse.OnCallShifts[0]

d.SetId(shift.ID)
d.Set("team_id", shift.TeamId)
d.Set("name", shift.Name)
d.Set("type", shift.Type)
d.Set("level", shift.Level)
d.Set("start", shift.Start)
d.Set("until", shift.Until)
d.Set("duration", shift.Duration)
d.Set("frequency", shift.Frequency)
d.Set("week_start", shift.WeekStart)
d.Set("interval", shift.Interval)
d.Set("users", shift.Users)
d.Set("rolling_users", shift.RollingUsers)
d.Set("by_day", shift.ByDay)
d.Set("by_month", shift.ByMonth)
d.Set("by_monthday", shift.ByMonthday)
d.Set("time_zone", shift.TimeZone)
d.Set("start_rotation_from_user_index", shift.StartRotationFromUserIndex)

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

import (
"fmt"
"testing"

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

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

shiftName := fmt.Sprintf("test-acc-%s", acctest.RandString(8))

resource.ParallelTest(t, resource.TestCase{
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourceOnCallShiftConfig(shiftName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.grafana_oncall_on_call_shift.test-acc-shift", "id"),
resource.TestCheckResourceAttr("data.grafana_oncall_on_call_shift.test-acc-shift", "name", shiftName),
),
},
},
})
}

func testAccDataSourceOnCallShiftConfig(shiftName string) string {
return fmt.Sprintf(`
data "grafana_oncall_on_call_shift" "test-acc-shift" {
name = "%s"
}
`, shiftName)
}
1 change: 1 addition & 0 deletions internal/resources/oncall/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func withClient[T schema.CreateContextFunc | schema.UpdateContextFunc | schema.R
var DataSources = []*common.DataSource{
dataSourceEscalationChain(),
dataSourceSchedule(),
dataSourceOnCallShift(),
dataSourceSlackChannel(),
dataSourceOutgoingWebhook(),
dataSourceUserGroup(),
Expand Down
2 changes: 1 addition & 1 deletion provider_schema.json

Large diffs are not rendered by default.

Loading