Skip to content

Commit

Permalink
upgrade to Terraform SDK v2 (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevholditch-f3 authored Jul 26, 2021
1 parent d91ea9c commit f86940b
Show file tree
Hide file tree
Showing 28 changed files with 589 additions and 250 deletions.
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ module github.com/kevholditch/terraform-provider-kong

go 1.16

replace "github.com/kevholditch/terraform-provider-kong/kong" => "./kong"
replace github.com/kevholditch/terraform-provider-kong/kong => ./kong

require (
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/Microsoft/go-winio v0.4.14 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/containerd/continuity v0.0.0-20200413184840-d3ef23f19fbb // indirect
Expand All @@ -16,8 +15,8 @@ require (
github.com/elazarl/goproxy/ext v0.0.0-20190421051319-9d40249d3c2f // indirect
github.com/gotestyourself/gotestyourself v2.2.0+incompatible // indirect
github.com/hashicorp/hcl/v2 v2.3.0 // indirect
github.com/hashicorp/terraform v0.12.24 // indirect
github.com/hashicorp/terraform-plugin-sdk v1.7.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.7.0 // indirect
github.com/kong/go-kong v0.20.0
github.com/lib/pq v1.0.0
github.com/opencontainers/image-spec v1.0.1 // indirect
Expand Down
298 changes: 298 additions & 0 deletions go.sum

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions kong/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package kong
import (
"os"

"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/kong/go-kong/kong"
)

Expand All @@ -14,7 +13,7 @@ type config struct {
strictConsumerPlugins bool
}

func Provider() terraform.ResourceProvider {
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"kong_admin_uri": &schema.Schema{
Expand Down
28 changes: 17 additions & 11 deletions kong/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package kong

import (
"context"
"log"
"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"
"github.com/kevholditch/terraform-provider-kong/kong/containers"
)

Expand All @@ -16,34 +17,39 @@ const EnvKongAdminUsername = "KONG_ADMIN_USERNAME"
const EnvKongAdminPassword = "KONG_ADMIN_PASSWORD"
const defaultKongRepository = "kong"
const defaultKongLicense = ""
const providerNameKong = "kong"

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

func init() {
testAccProvider = Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{
"kong": testAccProvider,
testAccProvider = Provider()
testAccProviders = map[string]*schema.Provider{
providerNameKong: testAccProvider,
}
testAccProviderFactories = map[string]func() (*schema.Provider, error){
providerNameKong: func() (*schema.Provider, error) { return Provider(), nil }, //nolint:unparam
}
}

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()
var _ = Provider()
}

func TestProvider_configure(t *testing.T) {

rc := terraform.NewResourceConfigRaw(map[string]interface{}{})
p := Provider()
err := p.Configure(rc)
err := p.Configure(context.Background(), rc)
if err != nil {
t.Fatal(err)
}
Expand All @@ -55,7 +61,7 @@ func TestProvider_configure_strict(t *testing.T) {
"strict_plugins_match": "true",
})
p := Provider()
err := p.Configure(rc)
err := p.Configure(context.Background(), rc)
if err != nil {
t.Fatal(err)
}
Expand Down
45 changes: 24 additions & 21 deletions kong/resource_kong_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package kong
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

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

func resourceKongCertificate() *schema.Resource {
return &schema.Resource{
Create: resourceKongCertificateCreate,
Read: resourceKongCertificateRead,
Delete: resourceKongCertificateDelete,
Update: resourceKongCertificateUpdate,
CreateContext: resourceKongCertificateCreate,
ReadContext: resourceKongCertificateRead,
DeleteContext: resourceKongCertificateDelete,
UpdateContext: resourceKongCertificateUpdate,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Expand Down Expand Up @@ -41,7 +42,7 @@ func resourceKongCertificate() *schema.Resource {
}
}

func resourceKongCertificateCreate(d *schema.ResourceData, meta interface{}) error {
func resourceKongCertificateCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {

certificateRequest := &kong.Certificate{
Cert: kong.String(d.Get("certificate").(string)),
Expand All @@ -51,18 +52,18 @@ func resourceKongCertificateCreate(d *schema.ResourceData, meta interface{}) err

client := meta.(*config).adminClient.Certificates

certificate, err := client.Create(context.Background(), certificateRequest)
certificate, err := client.Create(ctx, certificateRequest)

if err != nil {
return fmt.Errorf("failed to create kong certificate: %v error: %v", certificateRequest, err)
return diag.FromErr(fmt.Errorf("failed to create kong certificate: %v error: %v", certificateRequest, err))
}

d.SetId(*certificate.ID)

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

func resourceKongCertificateUpdate(d *schema.ResourceData, meta interface{}) error {
func resourceKongCertificateUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
d.Partial(false)

certificateRequest := &kong.Certificate{
Expand All @@ -74,23 +75,24 @@ func resourceKongCertificateUpdate(d *schema.ResourceData, meta interface{}) err

client := meta.(*config).adminClient.Certificates

_, err := client.Update(context.Background(), certificateRequest)
_, err := client.Update(ctx, certificateRequest)

if err != nil {
return fmt.Errorf("error updating kong certificate: %s", err)
return diag.FromErr(fmt.Errorf("error updating kong certificate: %s", err))
}

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

func resourceKongCertificateRead(d *schema.ResourceData, meta interface{}) error {
func resourceKongCertificateRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {

var diags diag.Diagnostics
client := meta.(*config).adminClient.Certificates

certificate, err := client.Get(context.Background(), kong.String(d.Id()))
certificate, err := client.Get(ctx, kong.String(d.Id()))

if !kong.IsNotFoundErr(err) && err != nil {
return fmt.Errorf("could not find kong certificate: %v", err)
return diag.FromErr(fmt.Errorf("could not find kong certificate: %v", err))
}

if certificate == nil {
Expand All @@ -109,18 +111,19 @@ func resourceKongCertificateRead(d *schema.ResourceData, meta interface{}) error
}
}

return nil
return diags
}

func resourceKongCertificateDelete(d *schema.ResourceData, meta interface{}) error {
func resourceKongCertificateDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {

var diags diag.Diagnostics
client := meta.(*config).adminClient.Certificates

err := client.Delete(context.Background(), kong.String(d.Id()))
err := client.Delete(ctx, kong.String(d.Id()))

if err != nil {
return fmt.Errorf("could not delete kong certificate: %v", err)
return diag.FromErr(fmt.Errorf("could not delete kong certificate: %v", err))
}

return nil
return diags
}
4 changes: 2 additions & 2 deletions kong/resource_kong_certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"fmt"
"testing"

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

Expand Down
2 changes: 1 addition & 1 deletion kong/resource_kong_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"

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

Expand Down
49 changes: 26 additions & 23 deletions kong/resource_kong_consumer_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package kong
import (
"context"
"fmt"
"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/kong/go-kong/kong"
)

func resourceKongConsumerACL() *schema.Resource {
return &schema.Resource{
Create: resourceKongConsumerACLCreate,
Read: resourceKongConsumerACLRead,
Delete: resourceKongConsumerACLDelete,
Update: resourceKongConsumerACLUpdate,
CreateContext: resourceKongConsumerACLCreate,
ReadContext: resourceKongConsumerACLRead,
DeleteContext: resourceKongConsumerACLDelete,
UpdateContext: resourceKongConsumerACLUpdate,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Expand All @@ -37,7 +38,7 @@ func resourceKongConsumerACL() *schema.Resource {
}
}

func resourceKongConsumerACLCreate(d *schema.ResourceData, meta interface{}) error {
func resourceKongConsumerACLCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
ACLGroupRequest := &kong.ACLGroup{
Group: kong.String(d.Get("group").(string)),
Tags: readStringArrayPtrFromResource(d, "tags"),
Expand All @@ -46,18 +47,18 @@ func resourceKongConsumerACLCreate(d *schema.ResourceData, meta interface{}) err
consumerId := kong.String(d.Get("consumer_id").(string))

client := meta.(*config).adminClient.ACLs
aclGroup, err := client.Create(context.Background(), consumerId, ACLGroupRequest)
aclGroup, err := client.Create(ctx, consumerId, ACLGroupRequest)

if err != nil {
return fmt.Errorf("failed to create kong ACL Group: %v error: %v", ACLGroupRequest, err)
return diag.FromErr(fmt.Errorf("failed to create kong ACL Group: %v error: %v", ACLGroupRequest, err))
}

d.SetId(buildConsumerPairID(*aclGroup.ID, *consumerId))

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

func resourceKongConsumerACLUpdate(d *schema.ResourceData, meta interface{}) error {
func resourceKongConsumerACLUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
id, err := splitConsumerID(d.Id())

ACLGroupRequest := &kong.ACLGroup{
Expand All @@ -69,28 +70,29 @@ func resourceKongConsumerACLUpdate(d *schema.ResourceData, meta interface{}) err
consumerId := kong.String(d.Get("consumer_id").(string))

client := meta.(*config).adminClient.ACLs
_, err = client.Update(context.Background(), consumerId, ACLGroupRequest)
_, err = client.Update(ctx, consumerId, ACLGroupRequest)

if err != nil {
return fmt.Errorf("error updating kong ACL Group: %s", err)
return diag.FromErr(fmt.Errorf("error updating kong ACL Group: %s", err))
}

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

func resourceKongConsumerACLRead(d *schema.ResourceData, meta interface{}) error {
func resourceKongConsumerACLRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
id, err := splitConsumerID(d.Id())
if err != nil {
return err
return diag.FromErr(err)
}

client := meta.(*config).adminClient.ACLs
ACLGroup, err := client.Get(context.Background(), kong.String(id.ConsumerID), kong.String(id.ID))
ACLGroup, err := client.Get(ctx, kong.String(id.ConsumerID), kong.String(id.ID))

if kong.IsNotFoundErr(err) {
d.SetId("")
} else if err != nil {
return fmt.Errorf("could not find kong ACLGroup with id: %s error: %v", id, err)
return diag.FromErr(fmt.Errorf("could not find kong ACLGroup with id: %s error: %v", id, err))
}

if ACLGroup == nil {
Expand All @@ -101,20 +103,21 @@ func resourceKongConsumerACLRead(d *schema.ResourceData, meta interface{}) error
d.Set("tags", ACLGroup.Tags)
}

return nil
return diags
}

func resourceKongConsumerACLDelete(d *schema.ResourceData, meta interface{}) error {
func resourceKongConsumerACLDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
id, err := splitConsumerID(d.Id())
if err != nil {
return err
return diag.FromErr(err)
}
client := meta.(*config).adminClient.ACLs
err = client.Delete(context.Background(), kong.String(id.ConsumerID), kong.String(id.ID))
err = client.Delete(ctx, kong.String(id.ConsumerID), kong.String(id.ID))

if err != nil {
return fmt.Errorf("could not delete kong ACL Group: %v", err)
return diag.FromErr(fmt.Errorf("could not delete kong ACL Group: %v", err))
}

return nil
return diags
}
4 changes: 2 additions & 2 deletions kong/resource_kong_consumer_acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/kong/go-kong/kong"
"testing"

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

func TestAccConsumerACL(t *testing.T) {
Expand Down
Loading

0 comments on commit f86940b

Please sign in to comment.