Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Terraform plugin sdk v2 #67

Merged
merged 6 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
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))

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})
}
Comment on lines +13 to +29
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just question, the previous implementation was incorrect, right? ( It should have been Application, not Project )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it was incorrect 🙇

}
}
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note]
More Support for context.Context

for example, the helper/schema.CreateFunc, helper/schema.UpdateFunc, helper/schema.ReadFunc, and helper/schema.DeleteFunc types all got context-aware versions (helper/schema.CreateContextFunc, helper/schema.UpdateContextFunc, helper/schema.ReadContextFunc, helper/schema.DeleteContextFunc, respectively). The old versions are now deprecated, and we recommend you upgrade to the context-aware versions to get their improvements, ...

}
}
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note]
Removal of the terraform.ResourceProvider Interface

The terraform.ResourceProvider interface has been removed. The concrete *helper/schema.Provider type should be used in its place.

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note]
Support for Diagnostics

A number of new functions have been added that return a diag.Diagnostics type. This type can be used to return multiple errors and warnings to Terraform, and to associate those errors or warnings with specific fields.

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note]
Deprecation of helper/schema.ExistsFunc

The helper/schema.ExistsFunc (usually set as the Exists property on helper/schema.Resource) is now deprecated.

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