Skip to content

Commit

Permalink
Merge pull request #11 from mackerelio-labs/upgrade-plugin-sdk-v2
Browse files Browse the repository at this point in the history
upgrade terraform-plugin-sdk to v2
  • Loading branch information
xcezx committed Aug 23, 2021
2 parents 0ed3d26 + a51ab65 commit 662c708
Show file tree
Hide file tree
Showing 46 changed files with 885 additions and 1,074 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# all available settings of specific linters
linters-settings:
errcheck:
ignore: github.com/hashicorp/terraform-plugin-sdk/helper/schema:ForceNew|Set
ignore: github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema:Set

issues:
exclude:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ go 1.15

require (
github.com/golangci/golangci-lint v1.36.0
github.com/hashicorp/terraform-plugin-sdk v1.16.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.7.0
github.com/mackerelio/mackerel-client-go v0.14.0
)
247 changes: 103 additions & 144 deletions go.sum

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions mackerel/config.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package mackerel

import (
"fmt"
"net/http"

"github.com/hashicorp/terraform-plugin-sdk/helper/logging"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
"github.com/mackerelio/mackerel-client-go"
)

type Config struct {
APIKey string
}

func (c *Config) Client() (*mackerel.Client, error) {
func (c *Config) Client() (client *mackerel.Client, diags diag.Diagnostics) {
if c.APIKey == "" {
return nil, fmt.Errorf("no API Key for Mackerel")
return nil, diag.Errorf("no API Key for Mackerel")
}
client := mackerel.NewClient(c.APIKey)
client = mackerel.NewClient(c.APIKey)
client.HTTPClient.Transport = logging.NewTransport("Mackerel", http.DefaultTransport)
return client, nil
return client, diags
}
5 changes: 3 additions & 2 deletions mackerel/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ func TestConfigEmptyAPIKey(t *testing.T) {
config := Config{
APIKey: "",
}
if _, err := config.Client(); err == nil {
t.Fatalf("expected error, but got nil")
_, diags := config.Client()
if !diags.HasError() {
t.Error("expected diagnostics has an error, but get no error")
}
}
13 changes: 8 additions & 5 deletions mackerel/data_source_mackerel_alert_group_setting.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package mackerel

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mackerelio/mackerel-client-go"
)

func dataSourceMackerelAlertGroupSetting() *schema.Resource {
return &schema.Resource{
Read: dataSourceMackerelAlertGroupSettingRead,
ReadContext: dataSourceMackerelAlertGroupSettingRead,

Schema: map[string]*schema.Schema{
"id": {
Expand Down Expand Up @@ -45,14 +48,14 @@ func dataSourceMackerelAlertGroupSetting() *schema.Resource {
}
}

func dataSourceMackerelAlertGroupSettingRead(d *schema.ResourceData, meta interface{}) error {
func dataSourceMackerelAlertGroupSettingRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
id := d.Get("id").(string)

client := meta.(*mackerel.Client)
client := m.(*mackerel.Client)

group, err := client.GetAlertGroupSetting(id)
if err != nil {
return err
return diag.FromErr(err)
}
d.SetId(group.ID)
return flattenAlertGroupSetting(group, d)
Expand Down
8 changes: 4 additions & 4 deletions mackerel/data_source_mackerel_alert_group_setting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceMackerelAlertGroupSetting(t *testing.T) {
Expand All @@ -14,8 +14,8 @@ func TestAccDataSourceMackerelAlertGroupSetting(t *testing.T) {
name := fmt.Sprintf("tf-alert-group-setting-%s", rand)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourceMackerelAlertGroupSettingConfig(rand, name),
Expand Down
31 changes: 8 additions & 23 deletions mackerel/data_source_mackerel_channel.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package mackerel

import (
"fmt"
"context"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mackerelio/mackerel-client-go"
)

func dataSourceMackerelChannel() *schema.Resource {
return &schema.Resource{
Read: dataSourceMackerelChannelRead,
ReadContext: dataSourceMackerelChannelRead,

Schema: map[string]*schema.Schema{
"id": {
Expand Down Expand Up @@ -55,22 +56,6 @@ func dataSourceMackerelChannel() *schema.Resource {
"mentions": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ok": {
Type: schema.TypeString,
Computed: true,
},
"warning": {
Type: schema.TypeString,
Computed: true,
},
"critical": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"enabled_graph_image": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -105,14 +90,14 @@ func dataSourceMackerelChannel() *schema.Resource {
}
}

func dataSourceMackerelChannelRead(d *schema.ResourceData, meta interface{}) error {
func dataSourceMackerelChannelRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
id := d.Get("id").(string)

client := meta.(*mackerel.Client)
client := m.(*mackerel.Client)

channels, err := client.FindChannels()
if err != nil {
return err
return diag.FromErr(err)
}

var channel *mackerel.Channel
Expand All @@ -123,7 +108,7 @@ func dataSourceMackerelChannelRead(d *schema.ResourceData, meta interface{}) err
}
}
if channel == nil {
return fmt.Errorf(`the ID '%s' does not match any channel in mackerel.io`, id)
return diag.Errorf(`the ID '%s' does not match any channel in mackerel.io`, id)
}
d.SetId(channel.ID)
return flattenChannel(channel, d)
Expand Down
20 changes: 10 additions & 10 deletions mackerel/data_source_mackerel_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceMackerelChannelEmail(t *testing.T) {
dsName := "data.mackerel_channel.foo"
name := fmt.Sprintf("tf-channel-%s", acctest.RandString(5))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourceMackerelChannelConfigEmail(name),
Expand All @@ -41,8 +41,8 @@ func TestAccDataSourceMackerelChannelSlack(t *testing.T) {
name := fmt.Sprintf("tf-channel-%s", acctest.RandString(5))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourceMackerelChannelConfigSlack(name),
Expand Down Expand Up @@ -71,8 +71,8 @@ func TestAccDataSourceMackerelChannelWebhook(t *testing.T) {
name := fmt.Sprintf("tf-channel-%s", acctest.RandString(5))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourceMackerelChannelConfigWebhook(name),
Expand Down Expand Up @@ -160,8 +160,8 @@ data "mackerel_channel" "foo" {

func TestAccDataSourceMackerelChannelNotMatchAnyChannel(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: `data "mackerel_channel" "foo" { id = "not-found" }`,
Expand Down
15 changes: 8 additions & 7 deletions mackerel/data_source_mackerel_downtime.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package mackerel

import (
"fmt"
"context"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mackerelio/mackerel-client-go"
)

func dataSourceMackerelDowntime() *schema.Resource {
return &schema.Resource{
Read: dataSourceMackerelDowntimeRead,
ReadContext: dataSourceMackerelDowntimeRead,

Schema: map[string]*schema.Schema{
"id": {
Expand Down Expand Up @@ -91,14 +92,14 @@ func dataSourceMackerelDowntime() *schema.Resource {
}
}

func dataSourceMackerelDowntimeRead(d *schema.ResourceData, meta interface{}) error {
func dataSourceMackerelDowntimeRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
id := d.Get("id").(string)

client := meta.(*mackerel.Client)
client := m.(*mackerel.Client)

downtimes, err := client.FindDowntimes()
if err != nil {
return err
return diag.FromErr(err)
}
var downtime *mackerel.Downtime
for _, dt := range downtimes {
Expand All @@ -108,7 +109,7 @@ func dataSourceMackerelDowntimeRead(d *schema.ResourceData, meta interface{}) er
}
}
if downtime == nil {
return fmt.Errorf("the ID '%s' does not match any downtime in mackerel.io", id)
return diag.Errorf("the ID '%s' does not match any downtime in mackerel.io", id)
}
d.SetId(downtime.ID)
return flattenDowntime(downtime, d)
Expand Down
12 changes: 6 additions & 6 deletions mackerel/data_source_mackerel_downtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceMackerelDowntime(t *testing.T) {
Expand All @@ -15,8 +15,8 @@ func TestAccDataSourceMackerelDowntime(t *testing.T) {
name := fmt.Sprintf("tf-downtime-%s", rand)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourceMackerelDowntimeConfig(rand, name),
Expand Down Expand Up @@ -100,8 +100,8 @@ data "mackerel_downtime" "foo" {

func TestAccDataSourceMackerelDowntimeNotMatchAnyDowntime(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: `data "mackerel_downtime" "foo" { id = "not-found" }`,
Expand Down
13 changes: 8 additions & 5 deletions mackerel/data_source_mackerel_monitor.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package mackerel

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mackerelio/mackerel-client-go"
)

func dataSourceMackerelMonitor() *schema.Resource {
return &schema.Resource{
Read: dataSourceMackerelMonitorRead,
ReadContext: dataSourceMackerelMonitorRead,

Schema: map[string]*schema.Schema{
"id": {
Expand Down Expand Up @@ -254,13 +257,13 @@ func dataSourceMackerelMonitor() *schema.Resource {
}
}

func dataSourceMackerelMonitorRead(d *schema.ResourceData, meta interface{}) error {
func dataSourceMackerelMonitorRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
id := d.Get("id").(string)

client := meta.(*mackerel.Client)
client := m.(*mackerel.Client)
monitor, err := client.GetMonitor(id)
if err != nil {
return err
return diag.FromErr(err)
}
d.SetId(monitor.MonitorID())
return flattenMonitor(monitor, d)
Expand Down
Loading

0 comments on commit 662c708

Please sign in to comment.