Skip to content

Commit

Permalink
Self-contained oncall package (#1418)
Browse files Browse the repository at this point in the history
Just like what was done for `cloud`, `slo` and `ml` packages
This moves the mapping of resources and client validation to the oncall package. This will make it easier to support TF code generation + upgrade to the new TF plugin framework
  • Loading branch information
julienduchesne authored Mar 14, 2024
1 parent e731c71 commit 29cab68
Show file tree
Hide file tree
Showing 18 changed files with 146 additions and 203 deletions.
27 changes: 2 additions & 25 deletions internal/provider/legacy_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,6 @@ func Provider(version string) *schema.Provider {
"grafana_synthetic_monitoring_probe": syntheticmonitoring.ResourceProbe(),
})

// Resources that require the OnCall client to exist.
onCallClientResources = addResourcesMetadataValidation(onCallClientPresent, map[string]*schema.Resource{
"grafana_oncall_integration": oncall.ResourceIntegration(),
"grafana_oncall_route": oncall.ResourceRoute(),
"grafana_oncall_escalation_chain": oncall.ResourceEscalationChain(),
"grafana_oncall_escalation": oncall.ResourceEscalation(),
"grafana_oncall_on_call_shift": oncall.ResourceOnCallShift(),
"grafana_oncall_schedule": oncall.ResourceSchedule(),
"grafana_oncall_outgoing_webhook": oncall.ResourceOutgoingWebhook(),
})

// Datasources that require the Grafana client to exist.
grafanaClientDatasources = addCreateReadResourcesMetadataValidation(
readGrafanaClientValidation,
Expand All @@ -113,18 +102,6 @@ func Provider(version string) *schema.Provider {
"grafana_synthetic_monitoring_probe": syntheticmonitoring.DataSourceProbe(),
"grafana_synthetic_monitoring_probes": syntheticmonitoring.DataSourceProbes(),
})

// Datasources that require the OnCall client to exist.
onCallClientDatasources = addResourcesMetadataValidation(onCallClientPresent, map[string]*schema.Resource{
"grafana_oncall_user": oncall.DataSourceUser(),
"grafana_oncall_escalation_chain": oncall.DataSourceEscalationChain(),
"grafana_oncall_schedule": oncall.DataSourceSchedule(),
"grafana_oncall_slack_channel": oncall.DataSourceSlackChannel(),
"grafana_oncall_action": oncall.DataSourceAction(), // deprecated
"grafana_oncall_outgoing_webhook": oncall.DataSourceOutgoingWebhook(),
"grafana_oncall_user_group": oncall.DataSourceUserGroup(),
"grafana_oncall_team": oncall.DataSourceTeam(),
})
)

p := &schema.Provider{
Expand Down Expand Up @@ -249,7 +226,7 @@ func Provider(version string) *schema.Provider {
machinelearning.ResourcesMap,
slo.ResourcesMap,
smClientResources,
onCallClientResources,
oncall.ResourcesMap,
cloud.ResourcesMap,
),

Expand All @@ -258,7 +235,7 @@ func Provider(version string) *schema.Provider {
machinelearning.DatasourcesMap,
slo.DatasourcesMap,
smClientDatasources,
onCallClientDatasources,
oncall.DatasourcesMap,
cloud.DatasourcesMap,
),
}
Expand Down
7 changes: 0 additions & 7 deletions internal/provider/legacy_provider_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ func smClientPresent(resourceName string, d *schema.ResourceData, m interface{})
return nil
}

func onCallClientPresent(resourceName string, d *schema.ResourceData, m interface{}) error {
if m.(*common.Client).OnCallClient == nil {
return fmt.Errorf("the Oncall client is required for `%s`. Set the oncall_access_token provider attribute", resourceName)
}
return nil
}

func addResourcesMetadataValidation(validateFunc metadataValidation, resources map[string]*schema.Resource) map[string]*schema.Resource {
return addCreateReadResourcesMetadataValidation(validateFunc, validateFunc, resources)
}
Expand Down
8 changes: 3 additions & 5 deletions internal/resources/oncall/data_source_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

onCallAPI "github.com/grafana/amixr-api-go-client"
"github.com/grafana/terraform-provider-grafana/internal/common"
)

func DataSourceAction() *schema.Resource {
func dataSourceAction() *schema.Resource {
return &schema.Resource{
Description: `
**Note:** This data source is going to be deprecated, please use outgoing webhook data source instead.
* [HTTP API](https://grafana.com/docs/oncall/latest/oncall-api-reference/outgoing_webhooks/)
!> Deprecated: Use the ` + "`grafana_oncall_outgoing_webhook`" + ` data source instead.
`,
ReadContext: DataSourceActionRead,
ReadContext: withClient[schema.ReadContextFunc](dataSourceActionRead),
DeprecationMessage: "This data source is going to be deprecated, please use outgoing webhook data source instead.",
Schema: map[string]*schema.Schema{
"name": {
Expand All @@ -30,8 +29,7 @@ func DataSourceAction() *schema.Resource {
}
}

func DataSourceActionRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*common.Client).OnCallClient
func dataSourceActionRead(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
options := &onCallAPI.ListCustomActionOptions{}
nameData := d.Get("name").(string)

Expand Down
8 changes: 3 additions & 5 deletions internal/resources/oncall/data_source_escalation_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import (
"context"

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"
)

func DataSourceEscalationChain() *schema.Resource {
func dataSourceEscalationChain() *schema.Resource {
return &schema.Resource{
Description: `
* [HTTP API](https://grafana.com/docs/oncall/latest/oncall-api-reference/escalation_chains/)
`,
ReadContext: dataSourceEscalationChainRead,
ReadContext: withClient[schema.ReadContextFunc](dataSourceEscalationChainRead),
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand All @@ -25,8 +24,7 @@ func DataSourceEscalationChain() *schema.Resource {
}
}

func dataSourceEscalationChainRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*common.Client).OnCallClient
func dataSourceEscalationChainRead(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
options := &onCallAPI.ListEscalationChainOptions{}
nameData := d.Get("name").(string)

Expand Down
8 changes: 3 additions & 5 deletions internal/resources/oncall/data_source_outgoing_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

onCallAPI "github.com/grafana/amixr-api-go-client"
"github.com/grafana/terraform-provider-grafana/internal/common"
)

func DataSourceOutgoingWebhook() *schema.Resource {
func dataSourceOutgoingWebhook() *schema.Resource {
return &schema.Resource{
Description: `
* [HTTP API](https://grafana.com/docs/oncall/latest/oncall-api-reference/outgoing_webhooks/)
`,
ReadContext: DataSourceOutgoingWebhookRead,
ReadContext: withClient[schema.ReadContextFunc](dataSourceOutgoingWebhookRead),
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand All @@ -26,8 +25,7 @@ func DataSourceOutgoingWebhook() *schema.Resource {
}
}

func DataSourceOutgoingWebhookRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*common.Client).OnCallClient
func dataSourceOutgoingWebhookRead(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
options := &onCallAPI.ListWebhookOptions{}
name := d.Get("name").(string)

Expand Down
8 changes: 3 additions & 5 deletions internal/resources/oncall/data_source_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ import (
"context"

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"
)

func DataSourceSchedule() *schema.Resource {
func dataSourceSchedule() *schema.Resource {
return &schema.Resource{
Description: `
* [Official documentation](https://grafana.com/docs/oncall/latest/manage/on-call-schedules/)
* [HTTP API](https://grafana.com/docs/oncall/latest/oncall-api-reference/schedules/)
`,
ReadContext: DataSourceScheduleRead,
ReadContext: withClient[schema.ReadContextFunc](dataSourceScheduleRead),
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand All @@ -31,8 +30,7 @@ func DataSourceSchedule() *schema.Resource {
}
}

func DataSourceScheduleRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*common.Client).OnCallClient
func dataSourceScheduleRead(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
options := &onCallAPI.ListScheduleOptions{}
nameData := d.Get("name").(string)

Expand Down
8 changes: 3 additions & 5 deletions internal/resources/oncall/data_source_slack_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import (
"context"

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"
)

func DataSourceSlackChannel() *schema.Resource {
func dataSourceSlackChannel() *schema.Resource {
return &schema.Resource{
Description: `
* [HTTP API](https://grafana.com/docs/oncall/latest/oncall-api-reference/slack_channels/)
`,
ReadContext: DataSourceSlackChannelRead,
ReadContext: withClient[schema.ReadContextFunc](dataSourceSlackChannelRead),
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand All @@ -30,8 +29,7 @@ func DataSourceSlackChannel() *schema.Resource {
}
}

func DataSourceSlackChannelRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*common.Client).OnCallClient
func dataSourceSlackChannelRead(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
options := &onCallAPI.ListSlackChannelOptions{}
nameData := d.Get("name").(string)

Expand Down
8 changes: 3 additions & 5 deletions internal/resources/oncall/data_source_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"context"

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"
)

func DataSourceTeam() *schema.Resource {
func dataSourceTeam() *schema.Resource {
return &schema.Resource{
ReadContext: DataSourceTeamRead,
ReadContext: withClient[schema.ReadContextFunc](dataSourceTeamRead),
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand All @@ -30,8 +29,7 @@ func DataSourceTeam() *schema.Resource {
}
}

func DataSourceTeamRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*common.Client).OnCallClient
func dataSourceTeamRead(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
options := &onCallAPI.ListTeamOptions{}
nameData := d.Get("name").(string)

Expand Down
8 changes: 3 additions & 5 deletions internal/resources/oncall/data_source_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import (
"context"

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"
)

func DataSourceUser() *schema.Resource {
func dataSourceUser() *schema.Resource {
return &schema.Resource{
Description: `
* [HTTP API](https://grafana.com/docs/oncall/latest/oncall-api-reference/users/)
`,
ReadContext: DataSourceUserRead,
ReadContext: withClient[schema.ReadContextFunc](dataSourceUserRead),
Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Expand All @@ -35,8 +34,7 @@ func DataSourceUser() *schema.Resource {
}
}

func DataSourceUserRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*common.Client).OnCallClient
func dataSourceUserRead(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
options := &onCallAPI.ListUserOptions{}
usernameData := d.Get("username").(string)

Expand Down
8 changes: 3 additions & 5 deletions internal/resources/oncall/data_source_user_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import (
"context"

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"
)

func DataSourceUserGroup() *schema.Resource {
func dataSourceUserGroup() *schema.Resource {
return &schema.Resource{
Description: `
* [HTTP API](https://grafana.com/docs/oncall/latest/oncall-api-reference/user_groups/)
`,
ReadContext: DataSourceUserGroupRead,
ReadContext: withClient[schema.ReadContextFunc](dataSourceUserGroupRead),
Schema: map[string]*schema.Schema{
"slack_handle": {
Type: schema.TypeString,
Expand All @@ -28,8 +27,7 @@ func DataSourceUserGroup() *schema.Resource {
}
}

func DataSourceUserGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*common.Client).OnCallClient
func dataSourceUserGroupRead(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
options := &onCallAPI.ListUserGroupOptions{}
slackHandleData := d.Get("slack_handle").(string)

Expand Down
30 changes: 11 additions & 19 deletions internal/resources/oncall/resource_escalation.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ var durationOptions = []int{
3600,
}

func ResourceEscalation() *schema.Resource {
func resourceEscalation() *schema.Resource {
return &schema.Resource{
Description: `
* [Official documentation](https://grafana.com/docs/oncall/latest/configure/escalation-chains-and-routes/)
* [HTTP API](https://grafana.com/docs/oncall/latest/oncall-api-reference/escalation_policies/)
`,
CreateContext: resourceEscalationCreate,
ReadContext: resourceEscalationRead,
UpdateContext: resourceEscalationUpdate,
DeleteContext: resourceEscalationDelete,
CreateContext: withClient[schema.CreateContextFunc](resourceEscalationCreate),
ReadContext: withClient[schema.ReadContextFunc](resourceEscalationRead),
UpdateContext: withClient[schema.UpdateContextFunc](resourceEscalationUpdate),
DeleteContext: withClient[schema.DeleteContextFunc](resourceEscalationDelete),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand Down Expand Up @@ -199,9 +199,7 @@ func ResourceEscalation() *schema.Resource {
}
}

func resourceEscalationCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*common.Client).OnCallClient

func resourceEscalationCreate(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
escalationChainIDData := d.Get("escalation_chain_id").(string)

createOptions := &onCallAPI.CreateEscalationOptions{
Expand Down Expand Up @@ -302,12 +300,10 @@ func resourceEscalationCreate(ctx context.Context, d *schema.ResourceData, m int

d.SetId(escalation.ID)

return resourceEscalationRead(ctx, d, m)
return resourceEscalationRead(ctx, d, client)
}

func resourceEscalationRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*common.Client).OnCallClient

func resourceEscalationRead(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
escalation, r, err := client.Escalations.GetEscalation(d.Id(), &onCallAPI.GetEscalationOptions{})
if err != nil {
if r != nil && r.StatusCode == http.StatusNotFound {
Expand All @@ -334,9 +330,7 @@ func resourceEscalationRead(ctx context.Context, d *schema.ResourceData, m inter
return nil
}

func resourceEscalationUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*common.Client).OnCallClient

func resourceEscalationUpdate(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
updateOptions := &onCallAPI.UpdateEscalationOptions{
ManualOrder: true,
}
Expand Down Expand Up @@ -417,12 +411,10 @@ func resourceEscalationUpdate(ctx context.Context, d *schema.ResourceData, m int
}

d.SetId(escalation.ID)
return resourceEscalationRead(ctx, d, m)
return resourceEscalationRead(ctx, d, client)
}

func resourceEscalationDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*common.Client).OnCallClient

func resourceEscalationDelete(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
_, err := client.Escalations.DeleteEscalation(d.Id(), &onCallAPI.DeleteEscalationOptions{})
if err != nil {
return diag.FromErr(err)
Expand Down
Loading

0 comments on commit 29cab68

Please sign in to comment.