Skip to content

Commit

Permalink
Merge pull request #67 from mercari/update-plugin-sdk-v2
Browse files Browse the repository at this point in the history
Update Terraform plugin sdk v2
  • Loading branch information
KeisukeYamashita authored Sep 3, 2020
2 parents 2c6d241 + 9728b2f commit d9f972d
Show file tree
Hide file tree
Showing 22 changed files with 406 additions and 379 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## v0.2.4 (Unreleased)
### Improvements
* Upgrade Terraform Plugin SDK to v2 ([#67](https://github.com/mercari/terraform-provider-spinnaker/pull/67)

### Bug fixes
* Fixed data source not showing in Terraform Registry. ([#66](https://github.com/mercari/terraform-provider-spinnaker/pull/66))
* Fixed broken `tgz` provider artifact. ([#67](https://github.com/mercari/terraform-provider-spinnaker/pull/68))
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ module github.com/mercari/terraform-provider-spinnaker
go 1.13

require (
github.com/armon/go-radix v1.0.0 // indirect
github.com/aws/aws-sdk-go v1.30.12 // indirect
github.com/ghodss/yaml v1.0.0
github.com/hashicorp/terraform v0.12.29
github.com/hashicorp/terraform-plugin-sdk v1.7.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.0.2-0.20200828083434-d39628234432
github.com/mattn/go-colorable v0.1.1 // indirect
github.com/mitchellh/mapstructure v1.2.2
github.com/posener/complete v1.2.1 // indirect
github.com/spf13/pflag v1.0.5
github.com/spinnaker/spin v0.0.0-20190530150642-535d2dc1b985
)
449 changes: 251 additions & 198 deletions go.sum

Large diffs are not rendered by default.

29 changes: 22 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
package main

import (
"github.com/hashicorp/terraform-plugin-sdk/plugin"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"context"
"flag"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
"github.com/mercari/terraform-provider-spinnaker/spinnaker"
)

func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: func() terraform.ResourceProvider {
return spinnaker.Provider()
},
})
var debugMode bool

flag.BoolVar(&debugMode, "debuggable", false, "set to true to run the provider with support for debuggers like delve")
flag.Parse()

if debugMode {
err := plugin.Debug(context.Background(), "registry.terraform.io/mercari/spinnaker",
&plugin.ServeOpts{
ProviderFunc: spinnaker.Provider,
})
if err != nil {
log.Println(err.Error())
}
} else {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: spinnaker.Provider})
}
}
2 changes: 1 addition & 1 deletion spinnaker/api/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mitchellh/mapstructure"
gate "github.com/spinnaker/spin/cmd/gateclient"
)
Expand Down
2 changes: 1 addition & 1 deletion spinnaker/api/canary_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"net/http"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mitchellh/mapstructure"
gate "github.com/spinnaker/spin/cmd/gateclient"
)
Expand Down
2 changes: 1 addition & 1 deletion spinnaker/api/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mitchellh/mapstructure"
gate "github.com/spinnaker/spin/cmd/gateclient"
)
Expand Down
5 changes: 3 additions & 2 deletions spinnaker/data_source_spinnaker_application.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package spinnaker

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

func datasourceApplication() *schema.Resource {
return &schema.Resource{
Description: "Provides a Spinnaker application resource",
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -35,6 +36,6 @@ func datasourceApplication() *schema.Resource {
Computed: true,
},
},
Read: resourceSpinnakerProjectRead,
ReadContext: resourceSpinnakerApplicationRead,
}
}
4 changes: 2 additions & 2 deletions spinnaker/data_source_spinnaker_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"testing"
"time"

"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 TestAccDataSourceSpinnakerApplication_basic(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions spinnaker/data_source_spinnaker_canary_config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package spinnaker

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

func datasourceCanaryConfig() *schema.Resource {
return &schema.Resource{
Description: "Provides a Spinnaker canary config resource",
Schema: map[string]*schema.Schema{
"id": {
Description: "Canary config id",
Expand Down Expand Up @@ -45,6 +46,6 @@ func datasourceCanaryConfig() *schema.Resource {
},
},
},
Read: resourceSpinnakerCanaryConfigRead,
ReadContext: resourceSpinnakerCanaryConfigRead,
}
}
3 changes: 2 additions & 1 deletion spinnaker/data_source_spinnaker_pipeline.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package spinnaker

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

func datasourcePipeline() *schema.Resource {
return &schema.Resource{
DeprecationMessage: "Pipeline deprecated because is not HCL native. We plan to support in the next major release",
Schema: map[string]*schema.Schema{
"application": {
Type: schema.TypeString,
Expand Down
5 changes: 3 additions & 2 deletions spinnaker/data_source_spinnaker_project.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package spinnaker

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

func datasourceProject() *schema.Resource {
return &schema.Resource{
Description: "Provides a Spinnaker project resource",
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Expand All @@ -29,6 +30,6 @@ func datasourceProject() *schema.Resource {
},
},
},
Read: resourceSpinnakerProjectRead,
ReadContext: resourceSpinnakerProjectRead,
}
}
2 changes: 1 addition & 1 deletion spinnaker/provider.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package spinnaker

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/spf13/pflag"
gate "github.com/spinnaker/spin/cmd/gateclient"
)
Expand Down
17 changes: 7 additions & 10 deletions spinnaker/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package spinnaker

import (
"context"
"os"
"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
var testAccProviders map[string]*schema.Provider
var testAccProvider *schema.Provider

func init() {
testAccProvider = Provider()
testAccProviders = map[string]terraform.ResourceProvider{
testAccProviders = map[string]*schema.Provider{
"spinnaker": testAccProvider,
}
}
Expand All @@ -22,9 +23,9 @@ func testAccPreCheck(t *testing.T) {
if os.Getenv("GATE_ENDPOINT") == "" {
t.Fatal("GATE_ENDPOINT must be set for acceptance tests")
}
err := testAccProvider.Configure(terraform.NewResourceConfigRaw(nil))
err := testAccProvider.Configure(context.Background(), terraform.NewResourceConfigRaw(nil))
if err != nil {
t.Fatalf("err: %s", err)
t.Fatalf("err: %v", err)
}
}

Expand All @@ -33,7 +34,3 @@ func TestProvider(t *testing.T) {
t.Fatalf("err: %s", err)
}
}

func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
}
77 changes: 31 additions & 46 deletions spinnaker/resource_application.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package spinnaker

import (
"context"
"fmt"
"regexp"
"strings"

"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/mercari/terraform-provider-spinnaker/spinnaker/api"
)

Expand All @@ -15,6 +18,7 @@ const (

func resourceSpinnakerApplication() *schema.Resource {
return &schema.Resource{
Description: "Provides a Spinnaker application resource",
Schema: map[string]*schema.Schema{
"application": {
Description: "Name of the Application",
Expand Down Expand Up @@ -58,13 +62,12 @@ func resourceSpinnakerApplication() *schema.Resource {
},
},
},
Create: resourceSpinnakerApplicationCreate,
Read: resourceSpinnakerApplicationRead,
Update: resourceSpinnakerApplicationUpdate,
Delete: resourceSpinnakerApplicationDelete,
Exists: resourceSpinnakerApplicationExists,
CreateContext: resourceSpinnakerApplicationCreate,
ReadContext: resourceSpinnakerApplicationRead,
UpdateContext: resourceSpinnakerApplicationUpdate,
DeleteContext: resourceSpinnakerApplicationDelete,
Importer: &schema.ResourceImporter{
State: resourceSpinnakerApplicationImport,
StateContext: resourceSpinnakerApplicationImport,
},
}
}
Expand All @@ -88,32 +91,34 @@ type Permissions struct {
Write []string `json:"WRITE"`
}

func resourceSpinnakerApplicationCreate(d *schema.ResourceData, meta interface{}) error {
func resourceSpinnakerApplicationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
clientConfig := meta.(gateConfig)
var diags diag.Diagnostics
client := clientConfig.client
appName := api.GetApplicationName(d)

task, err := api.NewCreateApplicationTask(d)
if err != nil {
return err
return diag.FromErr(err)
}

if err := api.CreateApplication(client, task); err != nil {
return err
return diag.FromErr(err)
}

d.SetId(appName)
return resourceSpinnakerApplicationRead(d, meta)
return diags
}

func resourceSpinnakerApplicationRead(d *schema.ResourceData, meta interface{}) error {
func resourceSpinnakerApplicationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
clientConfig := meta.(gateConfig)
var diags diag.Diagnostics
client := clientConfig.client
appName := api.GetApplicationName(d)

app := &applicationRead{}
if err := api.GetApplication(client, appName, app); err != nil {
return err
return diag.FromErr(err)
}

if app == nil {
Expand Down Expand Up @@ -143,66 +148,46 @@ func resourceSpinnakerApplicationRead(d *schema.ResourceData, meta interface{})
if v := app.Attributes.Permissions; v != nil {
tfPermissions, err := buildTerraformPermissions(v)
if err != nil {
return err
return diag.FromErr(err)
}

d.Set("permissions", tfPermissions)
}

return nil
return diags
}

func resourceSpinnakerApplicationUpdate(d *schema.ResourceData, meta interface{}) error {
func resourceSpinnakerApplicationUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
clientConfig := meta.(gateConfig)
client := clientConfig.client
task, err := api.NewCreateApplicationTask(d)
if err != nil {
return err
return diag.FromErr(err)
}

if err := api.CreateApplication(client, task); err != nil {
return err
return diag.FromErr(err)
}
return resourceSpinnakerApplicationRead(d, meta)
return resourceSpinnakerApplicationRead(ctx, d, meta)
}

func resourceSpinnakerApplicationDelete(d *schema.ResourceData, meta interface{}) error {
func resourceSpinnakerApplicationDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
clientConfig := meta.(gateConfig)
var diags diag.Diagnostics
client := clientConfig.client
appName := api.GetApplicationName(d)

if err := api.DeleteApplication(client, appName); err != nil {
return err
return diag.FromErr(err)
}

d.SetId("")
return nil
}

func resourceSpinnakerApplicationExists(d *schema.ResourceData, meta interface{}) (bool, error) {
clientConfig := meta.(gateConfig)
client := clientConfig.client
appName := api.GetApplicationName(d)

var app applicationRead
if err := api.GetApplication(client, appName, &app); err != nil {
errmsg := err.Error()
if strings.Contains(errmsg, "not found") {
return false, nil
}
return false, err
}

if app.Name == "" {
return false, nil
}

return true, nil
return diags
}

func resourceSpinnakerApplicationImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
if err := resourceSpinnakerApplicationRead(d, meta); err != nil {
return nil, err
func resourceSpinnakerApplicationImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
if diags := resourceSpinnakerApplicationRead(context.Background(), d, meta); diags.HasError() {
return nil, fmt.Errorf("failed to read spinnaker application")
}
return []*schema.ResourceData{d}, nil
}
Expand Down
Loading

0 comments on commit d9f972d

Please sign in to comment.