-
Notifications
You must be signed in to change notification settings - Fork 273
feat: add datasource grafana_oncall_on_call_shift
#2255
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
Open
trotttrotttrott
wants to merge
4
commits into
main
Choose a base branch
from
ttt/oncall-datasource-shift
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,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 |
3 changes: 3 additions & 0 deletions
3
examples/data-sources/grafana_oncall_on_call_shift/data-source.tf
This file contains hidden or 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,3 @@ | ||
| data "grafana_oncall_on_call_shift" "shift" { | ||
| name = "example_shift" | ||
| } | ||
This file contains hidden or 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,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 | ||
| } |
This file contains hidden or 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,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) | ||
| } |
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be cool 👍
shiftis particularly odd. I initially implemented this asgrafana_oncall_shift, but realized the corresponding resource isgrafana_oncall_on_call_shiftand followed suit for consistency. Not sure why the extraon_callsegment. Other resource/datasource names do not have this.