Skip to content
This repository was archived by the owner on Aug 1, 2021. It is now read-only.

Upgrade terraform plugin sdk to v2 #46

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
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: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
sudo: false
language: go
go:
- 1.14
- 1.15

cache:
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module github.com/kjmkznr/terraform-provider-mackerel

go 1.14
go 1.15

require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform v0.13.5
github.com/hashicorp/terraform-plugin-sdk v1.16.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.0.3
github.com/mackerelio/mackerel-client-go v0.12.0
golang.org/x/tools v0.0.0-20200904185747-39188db58858 // indirect
)
12 changes: 5 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -295,6 +295,8 @@ github.com/hashicorp/go-checkpoint v0.5.0/go.mod h1:7nfLNL10NsxqO4iWuW6tWW0HjZuD
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 h1:1/D3zfFHttUKaCaGKZ/dR2roBXv0vKbSCnssIldfQdI=
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320/go.mod h1:EiZBMaudVLy8fmjf9Npq1dq9RalhveqZG5w/yz3mHWs=
github.com/hashicorp/go-getter v1.4.0/go.mod h1:7qxyCd8rBfcShwsvxgIguu4KbS3l8bUCwg2Umn7RjeY=
github.com/hashicorp/go-getter v1.4.2-0.20200106182914-9813cbd4eb02 h1:l1KB3bHVdvegcIf5upQ5mjcHjs2qsWnKh4Yr9xgIuu8=
github.com/hashicorp/go-getter v1.4.2-0.20200106182914-9813cbd4eb02/go.mod h1:7qxyCd8rBfcShwsvxgIguu4KbS3l8bUCwg2Umn7RjeY=
@@ -330,9 +332,8 @@ github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f h1:UdxlrJz4JOnY8W+DbLISwf2B8WXEolNRA8BGCwI9jws=
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f/go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/hcl/v2 v2.0.0/go.mod h1:oVVDG71tEinNGYCxinCYadcmKU9bglqW9pV3txagJ90=
github.com/hashicorp/hcl/v2 v2.3.0/go.mod h1:d+FwDBbOLvpAM3Z6J7gPj/VoAGkNe/gm352ZhjJ/Zv8=
github.com/hashicorp/hcl/v2 v2.6.0 h1:3krZOfGY6SziUXa6H9PJU6TyohHn7I+ARYnhbeNBz+o=
@@ -346,15 +347,14 @@ github.com/hashicorp/serf v0.0.0-20160124182025-e4ec8cc423bb h1:ZbgmOQt8DOg796fi
github.com/hashicorp/serf v0.0.0-20160124182025-e4ec8cc423bb/go.mod h1:h/Ru6tmZazX7WO/GDmwdpS975F019L4t5ng5IgwbNrE=
github.com/hashicorp/terraform v0.13.5 h1:QgPgb/pOBclUMymDji9255FlseySf9dRcUMzJws9BAQ=
github.com/hashicorp/terraform v0.13.5/go.mod h1:1H1qcnppNc/bBGc7poOfnmmBeQMlF0stEN3haY3emCU=
github.com/hashicorp/terraform-config-inspect v0.0.0-20191115094559-17f92b0546e8/go.mod h1:p+ivJws3dpqbp1iP84+npOyAmTTOLMgCzrXd3GSdn/A=
github.com/hashicorp/terraform-config-inspect v0.0.0-20191212124732-c6ae6269b9d7 h1:Pc5TCv9mbxFN6UVX0LH6CpQrdTM5YjbVI2w15237Pjk=
github.com/hashicorp/terraform-config-inspect v0.0.0-20191212124732-c6ae6269b9d7/go.mod h1:p+ivJws3dpqbp1iP84+npOyAmTTOLMgCzrXd3GSdn/A=
github.com/hashicorp/terraform-exec v0.10.0 h1:3nh/1e3u9gYRUQGOKWp/8wPR7ABlL2F14sZMZBrp+dM=
github.com/hashicorp/terraform-exec v0.10.0/go.mod h1:tOT8j1J8rP05bZBGWXfMyU3HkLi1LWyqL3Bzsc3CJjo=
github.com/hashicorp/terraform-json v0.5.0 h1:7TV3/F3y7QVSuN4r9BEXqnWqrAyeOtON8f0wvREtyzs=
github.com/hashicorp/terraform-json v0.5.0/go.mod h1:eAbqb4w0pSlRmdvl8fOyHAi/+8jnkVYN28gJkSJrLhU=
github.com/hashicorp/terraform-plugin-sdk v1.16.0 h1:NrkXMRjHErUPPTHQkZ6JIn6bByiJzGnlJzH1rVdNEuE=
github.com/hashicorp/terraform-plugin-sdk v1.16.0/go.mod h1:5sVxrwW6/xzFhZyql+Q9zXCUEJaGWcBIxBbZFLpVXOI=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.0.3 h1:X7VmKpcIxq+rIbuqe5TPN27KLzbO9aXQcjG4c5iC3tk=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.0.3/go.mod h1:oz4kkpfTJ/hA2VMD0WpITTd3yPDGpT4uN7CiKdre/YI=
github.com/hashicorp/terraform-plugin-test/v2 v2.1.2 h1:p96IIn+XpvVjw7AtN8y9MKxn0x69S7wtbGf7JgDJoIk=
github.com/hashicorp/terraform-plugin-test/v2 v2.1.2/go.mod h1:jerO5mrd+jVNALy8aiq+VZOg/CR8T2T1QR3jd6JKGOI=
github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596 h1:hjyO2JsNZUKT1ym+FAdlBEkGPevazYsmVgIMw7dVELg=
@@ -582,8 +582,6 @@ github.com/zclconf/go-cty v1.2.1 h1:vGMsygfmeCl4Xb6OA5U5XVAaQZ69FvoG7X2jUtQujb8=
github.com/zclconf/go-cty v1.2.1/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
github.com/zclconf/go-cty v1.5.1 h1:oALUZX+aJeEBUe2a1+uD2+UTaYfEjnKFDEMRydkGvWE=
github.com/zclconf/go-cty v1.5.1/go.mod h1:nHzOclRkoj++EU9ZjSrZvRG0BXIWt8c7loYc0qXAFGQ=
github.com/zclconf/go-cty-yaml v1.0.1 h1:up11wlgAaDvlAGENcFDnZgkn0qUJurso7k6EpURKNF8=
github.com/zclconf/go-cty-yaml v1.0.1/go.mod h1:IP3Ylp0wQpYm50IHK8OZWKMu6sPJIUgKa8XhiVHura0=
github.com/zclconf/go-cty-yaml v1.0.2 h1:dNyg4QLTrv2IfJpm7Wtxi55ed5gLGOlPrZ6kMd51hY0=
github.com/zclconf/go-cty-yaml v1.0.2/go.mod h1:IP3Ylp0wQpYm50IHK8OZWKMu6sPJIUgKa8XhiVHura0=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
19 changes: 13 additions & 6 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package provider

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

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

const (
MackerelAPIKeyParamName = "MACKEREL_API_KEY"
)

// Provider returns a terraform.ResourceProvider.
func Provider() terraform.ResourceProvider {
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"api_key": {
@@ -29,14 +31,19 @@ func Provider() terraform.ResourceProvider {
"mackerel_service": resourceMackerelService(),
"mackerel_channel": resourceMackerelChannel(),
},
ConfigureFunc: providerConfigure,
ConfigureContextFunc: providerConfigure,
}
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
func providerConfigure(_ context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
var diags diag.Diagnostics
config := Config{
ApiKey: d.Get("api_key").(string),
}

return config.NewClient()
c, err := config.NewClient()
if err != nil {
diag.FromErr(err)
}
return c, diags
}
38 changes: 24 additions & 14 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,45 @@
package provider

import (
"context"
"os"
"sync"
"testing"

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

var testAccProviders map[string]terraform.ResourceProvider
const providerName = "mackerel"

var testAccProvider *schema.Provider
var testAccProviderFactories map[string]func() (*schema.Provider, error)

var testAccProviderConfigure sync.Once

func init() {
testAccProvider = Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{
"mackerel": testAccProvider,
testAccProvider = Provider()
testAccProviderFactories = map[string]func() (*schema.Provider, error){
providerName: func() (*schema.Provider, error) {
return Provider(), nil
},
}
}

func TestProvider(t *testing.T) {
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
if err := Provider().InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}

func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
}

func testAccPreCheck(t *testing.T) {
if v := os.Getenv("MACKEREL_API_KEY"); v == "" {
t.Fatal("MACKEREL_API_KEY must be set for acceptance tests")
}
testAccProviderConfigure.Do(func() {
if v := os.Getenv("MACKEREL_API_KEY"); v == "" {
t.Fatal("MACKEREL_API_KEY must be set for acceptance tests")
}
err := testAccProvider.Configure(context.Background(), terraform.NewResourceConfigRaw(nil))
if err != nil {
t.Fatal(err)
}
})
}
68 changes: 34 additions & 34 deletions internal/provider/resource_mackerel_channel.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
package provider

import (
"context"
"encoding/json"
"fmt"
"log"

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

func resourceMackerelChannel() *schema.Resource {
return &schema.Resource{
Create: resourceMackerelChannelCreate,
Read: resourceMackerelChannelRead,
Delete: resourceMackerelChannelDelete,
Exists: resourceMackerelChannelExists,
CreateContext: resourceMackerelChannelCreate,
ReadContext: resourceMackerelChannelRead,
DeleteContext: resourceMackerelChannelDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
ValidateDiagFunc: stringInSliceDiag([]string{
"email",
"slack",
"webhook",
@@ -76,77 +76,77 @@ func resourceMackerelChannel() *schema.Resource {
}
}

func resourceMackerelChannelCreate(d *schema.ResourceData, meta interface{}) error {
func resourceMackerelChannelCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*mackerel.Client)

input, err := buildChannelParameter(d)
if err != nil {
return err
return diag.FromErr(fmt.Errorf("error build channel parameter %w", err))
}

channel, err := client.CreateChannel(input)
if err != nil {
return err
return diag.FromErr(fmt.Errorf("error create channel %w", err))
}

log.Printf("[DEBUG] mackerel channel %q created.", channel.ID)
d.SetId(channel.ID)

return resourceMackerelChannelRead(d, meta)
return resourceMackerelChannelRead(ctx, d, meta)
}

func resourceMackerelChannelRead(d *schema.ResourceData, meta interface{}) error {
func resourceMackerelChannelRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*mackerel.Client)

log.Printf("[DEBUG] Reading mackerel channel: %q", d.Id())
channels, err := client.FindChannels()
if err != nil {
return err
return diag.FromErr(fmt.Errorf("error read channel %w", err))
}

exists := false
for _, channel := range channels {
if channel.ID == d.Id() {
_ = d.Set("id", channel.ID)
exists = true
_ = d.Set("name", channel.Name)
_ = d.Set("type", channel.Type)
_ = d.Set("url", channel.URL)
_ = d.Set("enabled_graph_image", channel.EnabledGraphImage)
_ = d.Set("user_ids", channel.UserIDs)
_ = d.Set("mentions", channel.Mentions)

var mentions = map[string]string{}
if ok := channel.Mentions.OK; ok != "" {
mentions["ok"] = ok
}
if crit := channel.Mentions.Critical; crit != "" {
mentions["critical"] = crit
}
if warn := channel.Mentions.Warning; warn != "" {
mentions["warning"] = warn
}
_ = d.Set("mentions", mentions)
_ = d.Set("events", channel.Events)
break
}
}

return nil
}

func resourceMackerelChannelExists(d *schema.ResourceData, meta interface{}) (b bool, e error) {
client := meta.(*mackerel.Client)
channels, err := client.FindChannels()
if err != nil {
return false, err
}

for _, c := range channels {
if c.ID == d.Id() {
return true, nil
if !exists {
// channel is gone
d.SetId("")
}
}

return false, nil
return nil
}

func resourceMackerelChannelDelete(d *schema.ResourceData, meta interface{}) error {
func resourceMackerelChannelDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*mackerel.Client)

_, err := client.DeleteChannel(d.Id())
if err != nil {
return err
return diag.FromErr(fmt.Errorf("error delete channel %w", err))
}

log.Printf("[DEBUG] mackerel channel %q deleted.", d.Id())
d.SetId("")

return nil
}
Loading