-
Notifications
You must be signed in to change notification settings - Fork 5
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
Changes from 5 commits
57031ed
0ea077e
cfb15cf
5975e9c
4b1a7de
9728b2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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}) | ||
} | ||
} |
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, | ||
|
@@ -35,6 +36,6 @@ func datasourceApplication() *schema.Resource { | |
Computed: true, | ||
}, | ||
}, | ||
Read: resourceSpinnakerProjectRead, | ||
ReadContext: resourceSpinnakerApplicationRead, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just question, the previous implementation was incorrect, right? ( It should have been There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it was incorrect 🙇 |
||
} | ||
} |
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", | ||
|
@@ -45,6 +46,6 @@ func datasourceCanaryConfig() *schema.Resource { | |
}, | ||
}, | ||
}, | ||
Read: resourceSpinnakerCanaryConfigRead, | ||
ReadContext: resourceSpinnakerCanaryConfigRead, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [note]
|
||
} | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [note]
|
||
var testAccProvider *schema.Provider | ||
|
||
func init() { | ||
testAccProvider = Provider() | ||
testAccProviders = map[string]terraform.ResourceProvider{ | ||
testAccProviders = map[string]*schema.Provider{ | ||
"spinnaker": testAccProvider, | ||
} | ||
} | ||
|
@@ -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) | ||
} | ||
} | ||
|
||
|
@@ -33,7 +34,3 @@ func TestProvider(t *testing.T) { | |
t.Fatalf("err: %s", err) | ||
} | ||
} | ||
|
||
func TestProvider_impl(t *testing.T) { | ||
var _ terraform.ResourceProvider = Provider() | ||
} |
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" | ||
) | ||
|
||
|
@@ -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", | ||
|
@@ -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, | ||
}, | ||
} | ||
} | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [note]
|
||
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 { | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [note]
|
||
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 | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[note]
This is new implementation for Debuggable Provider Binary:
https://www.terraform.io/docs/extend/guides/v2-upgrade-guide.html#support-for-debuggable-provider-binaries