Skip to content

Commit d275546

Browse files
OnCall: Use ReadContext instead of Read (#614)
`Read` is deprecated
1 parent ef134d0 commit d275546

8 files changed

+56
-80
lines changed

grafana/data_source_oncall_action.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package grafana
22

33
import (
4-
"errors"
5-
"fmt"
4+
"context"
65

6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
88

99
onCallAPI "github.com/grafana/amixr-api-go-client"
@@ -15,7 +15,7 @@ func DataSourceOnCallAction() *schema.Resource {
1515
**Note:** This data source is going to be deprecated, please use outgoing webhook data source instead.
1616
* [HTTP API](https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/outgoing_webhooks/)
1717
`,
18-
Read: dataSourceOnCallActionRead,
18+
ReadContext: dataSourceOnCallActionRead,
1919
DeprecationMessage: "This data source is going to be deprecated, please use outgoing webhook data source instead.",
2020
Schema: map[string]*schema.Schema{
2121
"name": {
@@ -27,25 +27,22 @@ func DataSourceOnCallAction() *schema.Resource {
2727
}
2828
}
2929

30-
func dataSourceOnCallActionRead(d *schema.ResourceData, m interface{}) error {
30+
func dataSourceOnCallActionRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
3131
client := m.(*client).onCallAPI
32-
if client == nil {
33-
return errors.New("grafana OnCall api client is not configured")
34-
}
3532
options := &onCallAPI.ListCustomActionOptions{}
3633
nameData := d.Get("name").(string)
3734

3835
options.Name = nameData
3936

4037
customActionsResponse, _, err := client.CustomActions.ListCustomActions(options)
4138
if err != nil {
42-
return err
39+
return diag.FromErr(err)
4340
}
4441

4542
if len(customActionsResponse.CustomActions) == 0 {
46-
return fmt.Errorf("couldn't find an action matching: %s", options.Name)
43+
return diag.Errorf("couldn't find an action matching: %s", options.Name)
4744
} else if len(customActionsResponse.CustomActions) != 1 {
48-
return fmt.Errorf("more than one action found matching: %s", options.Name)
45+
return diag.Errorf("more than one action found matching: %s", options.Name)
4946
}
5047

5148
customAction := customActionsResponse.CustomActions[0]

grafana/data_source_oncall_escalation_chain.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package grafana
22

33
import (
4-
"errors"
5-
"fmt"
4+
"context"
65

76
onCallAPI "github.com/grafana/amixr-api-go-client"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
99
)
1010

@@ -13,7 +13,7 @@ func DataSourceOnCallEscalationChain() *schema.Resource {
1313
Description: `
1414
* [HTTP API](https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/escalation_chains/)
1515
`,
16-
Read: dataSourceEscalationChainRead,
16+
ReadContext: dataSourceEscalationChainRead,
1717
Schema: map[string]*schema.Schema{
1818
"name": {
1919
Type: schema.TypeString,
@@ -24,25 +24,22 @@ func DataSourceOnCallEscalationChain() *schema.Resource {
2424
}
2525
}
2626

27-
func dataSourceEscalationChainRead(d *schema.ResourceData, m interface{}) error {
27+
func dataSourceEscalationChainRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
2828
client := m.(*client).onCallAPI
29-
if client == nil {
30-
return errors.New("grafana OnCall api client is not configured")
31-
}
3229
options := &onCallAPI.ListEscalationChainOptions{}
3330
nameData := d.Get("name").(string)
3431

3532
options.Name = nameData
3633

3734
escalationChainsResponse, _, err := client.EscalationChains.ListEscalationChains(options)
3835
if err != nil {
39-
return err
36+
return diag.FromErr(err)
4037
}
4138

4239
if len(escalationChainsResponse.EscalationChains) == 0 {
43-
return fmt.Errorf("couldn't find an escalation chain matching: %s", options.Name)
40+
return diag.Errorf("couldn't find an escalation chain matching: %s", options.Name)
4441
} else if len(escalationChainsResponse.EscalationChains) != 1 {
45-
return fmt.Errorf("more than one escalation chain found matching: %s", options.Name)
42+
return diag.Errorf("more than one escalation chain found matching: %s", options.Name)
4643
}
4744

4845
escalationChain := escalationChainsResponse.EscalationChains[0]

grafana/data_source_oncall_outgoing_webhook.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package grafana
22

33
import (
4-
"errors"
5-
"fmt"
4+
"context"
65

6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
88

99
onCallAPI "github.com/grafana/amixr-api-go-client"
@@ -14,7 +14,7 @@ func DataSourceOnCallOutgoingWebhook() *schema.Resource {
1414
Description: `
1515
* [HTTP API](https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/outgoing_webhooks/)
1616
`,
17-
Read: dataSourceOnCallOutgoingWebhookRead,
17+
ReadContext: dataSourceOnCallOutgoingWebhookRead,
1818
Schema: map[string]*schema.Schema{
1919
"name": {
2020
Type: schema.TypeString,
@@ -25,25 +25,22 @@ func DataSourceOnCallOutgoingWebhook() *schema.Resource {
2525
}
2626
}
2727

28-
func dataSourceOnCallOutgoingWebhookRead(d *schema.ResourceData, m interface{}) error {
28+
func dataSourceOnCallOutgoingWebhookRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
2929
client := m.(*client).onCallAPI
30-
if client == nil {
31-
return errors.New("grafana OnCall api client is not configured")
32-
}
3330
options := &onCallAPI.ListCustomActionOptions{}
3431
name := d.Get("name").(string)
3532

3633
options.Name = name
3734

3835
outgoingWebhookResponse, _, err := client.CustomActions.ListCustomActions(options)
3936
if err != nil {
40-
return err
37+
return diag.FromErr(err)
4138
}
4239

4340
if len(outgoingWebhookResponse.CustomActions) == 0 {
44-
return fmt.Errorf("couldn't find an outgoing webhook matching: %s", options.Name)
41+
return diag.Errorf("couldn't find an outgoing webhook matching: %s", options.Name)
4542
} else if len(outgoingWebhookResponse.CustomActions) != 1 {
46-
return fmt.Errorf("more than one outgoing webhook found matching: %s", options.Name)
43+
return diag.Errorf("more than one outgoing webhook found matching: %s", options.Name)
4744
}
4845

4946
outgoingWebhook := outgoingWebhookResponse.CustomActions[0]

grafana/data_source_oncall_schedule.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package grafana
22

33
import (
4-
"errors"
5-
"fmt"
4+
"context"
65

76
onCallAPI "github.com/grafana/amixr-api-go-client"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
99
)
1010

@@ -14,7 +14,7 @@ func DataSourceOnCallSchedule() *schema.Resource {
1414
* [Official documentation](https://grafana.com/docs/grafana-cloud/oncall/calendar-schedules/)
1515
* [HTTP API](https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/schedules/)
1616
`,
17-
Read: dataSourceOnCallScheduleRead,
17+
ReadContext: dataSourceOnCallScheduleRead,
1818
Schema: map[string]*schema.Schema{
1919
"name": {
2020
Type: schema.TypeString,
@@ -30,25 +30,22 @@ func DataSourceOnCallSchedule() *schema.Resource {
3030
}
3131
}
3232

33-
func dataSourceOnCallScheduleRead(d *schema.ResourceData, m interface{}) error {
33+
func dataSourceOnCallScheduleRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
3434
client := m.(*client).onCallAPI
35-
if client == nil {
36-
return errors.New("grafana OnCall api client is not configured")
37-
}
3835
options := &onCallAPI.ListScheduleOptions{}
3936
nameData := d.Get("name").(string)
4037

4138
options.Name = nameData
4239

4340
schedulesResponse, _, err := client.Schedules.ListSchedules(options)
4441
if err != nil {
45-
return err
42+
return diag.FromErr(err)
4643
}
4744

4845
if len(schedulesResponse.Schedules) == 0 {
49-
return fmt.Errorf("couldn't find a schedule matching: %s", options.Name)
46+
return diag.Errorf("couldn't find a schedule matching: %s", options.Name)
5047
} else if len(schedulesResponse.Schedules) != 1 {
51-
return fmt.Errorf("more than one schedule found matching: %s", options.Name)
48+
return diag.Errorf("more than one schedule found matching: %s", options.Name)
5249
}
5350

5451
schedule := schedulesResponse.Schedules[0]

grafana/data_source_slack_channel.go renamed to grafana/data_source_oncall_slack_channel.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package grafana
22

33
import (
4-
"errors"
5-
"fmt"
4+
"context"
65

76
onCallAPI "github.com/grafana/amixr-api-go-client"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
99
)
1010

@@ -13,7 +13,7 @@ func DataSourceOnCallSlackChannel() *schema.Resource {
1313
Description: `
1414
* [HTTP API](https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/slack_channels/)
1515
`,
16-
Read: dataSourceOnCallSlackChannelRead,
16+
ReadContext: dataSourceOnCallSlackChannelRead,
1717
Schema: map[string]*schema.Schema{
1818
"name": {
1919
Type: schema.TypeString,
@@ -29,25 +29,22 @@ func DataSourceOnCallSlackChannel() *schema.Resource {
2929
}
3030
}
3131

32-
func dataSourceOnCallSlackChannelRead(d *schema.ResourceData, m interface{}) error {
32+
func dataSourceOnCallSlackChannelRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
3333
client := m.(*client).onCallAPI
34-
if client == nil {
35-
return errors.New("grafana OnCall api client is not configured")
36-
}
3734
options := &onCallAPI.ListSlackChannelOptions{}
3835
nameData := d.Get("name").(string)
3936

4037
options.ChannelName = nameData
4138

4239
slackChannelsResponse, _, err := client.SlackChannels.ListSlackChannels(options)
4340
if err != nil {
44-
return err
41+
return diag.FromErr(err)
4542
}
4643

4744
if len(slackChannelsResponse.SlackChannels) == 0 {
48-
return fmt.Errorf("couldn't find a slack_channel matching: %s", options.ChannelName)
45+
return diag.Errorf("couldn't find a slack_channel matching: %s", options.ChannelName)
4946
} else if len(slackChannelsResponse.SlackChannels) != 1 {
50-
return fmt.Errorf("more than one slack_channel found matching: %s", options.ChannelName)
47+
return diag.Errorf("more than one slack_channel found matching: %s", options.ChannelName)
5148
}
5249

5350
slackChannel := slackChannelsResponse.SlackChannels[0]

grafana/data_source_oncall_team.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package grafana
22

33
import (
4-
"errors"
5-
"fmt"
4+
"context"
65

76
onCallAPI "github.com/grafana/amixr-api-go-client"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
99
)
1010

1111
func DataSourceOnCallTeam() *schema.Resource {
1212
return &schema.Resource{
13-
Read: dataSourceOnCallTeamRead,
13+
ReadContext: dataSourceOnCallTeamRead,
1414
Schema: map[string]*schema.Schema{
1515
"name": {
1616
Type: schema.TypeString,
@@ -29,25 +29,22 @@ func DataSourceOnCallTeam() *schema.Resource {
2929
}
3030
}
3131

32-
func dataSourceOnCallTeamRead(d *schema.ResourceData, m interface{}) error {
32+
func dataSourceOnCallTeamRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
3333
client := m.(*client).onCallAPI
34-
if client == nil {
35-
return errors.New("grafana OnCall api client is not configured")
36-
}
3734
options := &onCallAPI.ListTeamOptions{}
3835
nameData := d.Get("name").(string)
3936

4037
options.Name = nameData
4138

4239
teamsResponse, _, err := client.Teams.ListTeams(options)
4340
if err != nil {
44-
return err
41+
return diag.FromErr(err)
4542
}
4643

4744
if len(teamsResponse.Teams) == 0 {
48-
return fmt.Errorf("couldn't find a team matching: %s", options.Name)
45+
return diag.Errorf("couldn't find a team matching: %s", options.Name)
4946
} else if len(teamsResponse.Teams) != 1 {
50-
return fmt.Errorf("more than one team found matching: %s", options.Name)
47+
return diag.Errorf("more than one team found matching: %s", options.Name)
5148
}
5249

5350
team := teamsResponse.Teams[0]

grafana/data_source_oncall_user.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package grafana
22

33
import (
4-
"errors"
5-
"fmt"
4+
"context"
65

76
onCallAPI "github.com/grafana/amixr-api-go-client"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
99
)
1010

@@ -13,7 +13,7 @@ func DataSourceOnCallUser() *schema.Resource {
1313
Description: `
1414
* [HTTP API](https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/users/)
1515
`,
16-
Read: dataSourceOnCallUserRead,
16+
ReadContext: dataSourceOnCallUserRead,
1717
Schema: map[string]*schema.Schema{
1818
"username": {
1919
Type: schema.TypeString,
@@ -34,25 +34,22 @@ func DataSourceOnCallUser() *schema.Resource {
3434
}
3535
}
3636

37-
func dataSourceOnCallUserRead(d *schema.ResourceData, m interface{}) error {
37+
func dataSourceOnCallUserRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
3838
client := m.(*client).onCallAPI
39-
if client == nil {
40-
return errors.New("grafana OnCall api client is not configured")
41-
}
4239
options := &onCallAPI.ListUserOptions{}
4340
usernameData := d.Get("username").(string)
4441

4542
options.Username = usernameData
4643

4744
usersResponse, _, err := client.Users.ListUsers(options)
4845
if err != nil {
49-
return err
46+
return diag.FromErr(err)
5047
}
5148

5249
if len(usersResponse.Users) == 0 {
53-
return fmt.Errorf("couldn't find a user matching: %s", options.Username)
50+
return diag.Errorf("couldn't find a user matching: %s", options.Username)
5451
} else if len(usersResponse.Users) != 1 {
55-
return fmt.Errorf("more than one user found matching: %s", options.Username)
52+
return diag.Errorf("more than one user found matching: %s", options.Username)
5653
}
5754

5855
user := usersResponse.Users[0]

0 commit comments

Comments
 (0)