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

feat: migrate to terraform-plugin-sdk #5

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
47 changes: 47 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
builds:
- env:
# goreleaser does not work with CGO, it could also complicate
# usage by users in CI/CD systems like Terraform Cloud where
# they are unable to install libraries.
- CGO_ENABLED=0
mod_timestamp: '{{ .CommitTimestamp }}'
flags:
- -mod=vendor
- -trimpath
ldflags:
- '-s -w -X main.version={{ .Version }} -X main.commit={{.Commit}}'
goos:
- windows
- linux
- darwin
goarch:
- amd64
ignore:
- goos: darwin
goarch: '386'
binary: '{{ .ProjectName }}_v{{ .Version }}'
archives:
- format: zip
name_template: '{{ .ProjectName }}_v{{ .Version }}_{{ .Os }}_{{ .Arch }}'
checksum:
name_template: '{{ .ProjectName }}_{{ .Version }}_SHA256SUMS'
algorithm: sha256
signs:
- artifacts: checksum
args:
# if you are using this is a GitHub action or some other automated pipeline, you
# need to pass the batch flag to indicate its not interactive.
- "--batch"
- "--local-user"
- "{{ .Env.GPG_FINGERPRINT }}" # set this environment variable for your signing key
- "--output"
- "${signature}"
- "--detach-sign"
- "${artifact}"
release:
draft: true
github:
owner: greut
name: terraform-provider-consulacl
changelog:
skip: true
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
# Change Log

## 1.6.2 - 2020-11-23

### Added

- Enable logging the HTTP requests when logging level if set to `debug` within Terraform.

## 1.6.1 - 2020-11-20

### Added

- Use `goreleaser` to publish to the Terraform registry.

## 1.6.0 - 2020-03-31

### Added

- Resource `consulacl_token` now resets `anonymous` token permissions upon deletion as it cannot be removed
- Resource `consulacl_token` now resets `anonymous` token permissions upon deletion as it cannot be removed

## 1.5.0 - 2019-10-04

Expand Down
28 changes: 17 additions & 11 deletions consulacl/common.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package consulacl

const FieldName = "name"
const FieldToken = "token"
const FieldAccessor = "accessor"
const FieldSecret = "secret"
const FieldType = "type"
const (
FieldName = "name"
FieldToken = "token"
FieldAccessor = "accessor"
FieldSecret = "secret"
FieldType = "type"
)

const FieldRule = "rule"

const FieldScope = "scope"
const FieldPrefix = "prefix"
const FieldPolicy = "policy"
const (
FieldScope = "scope"
FieldPrefix = "prefix"
FieldPolicy = "policy"
)

const FieldDescription = "description"
const FieldPolicies = "policies"
const FieldLocal = "local"
const (
FieldDescription = "description"
FieldPolicies = "policies"
FieldLocal = "local"
)
10 changes: 9 additions & 1 deletion consulacl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package consulacl

import (
"fmt"

consul "github.com/hashicorp/consul/api"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
)

type Config struct {
Expand Down Expand Up @@ -43,8 +45,14 @@ func (c *Config) Client() (*consul.Client, error) {
config.Token = c.Token
}

client, err := consul.NewClient(config)
if logging.IsDebugOrHigher() {
config.HttpClient.Transport = logging.NewTransport(
"consulacl",
config.HttpClient.Transport,
)
}

client, err := consul.NewClient(config)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion consulacl/data_source_consulacl_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package consulacl

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

func dataSourceConsulAclToken() *schema.Resource {
Expand Down
32 changes: 15 additions & 17 deletions consulacl/data_source_consulacl_token_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
package consulacl_test

import (
"context"
"fmt"
"github.com/ashald/terraform-provider-consulacl/consulacl"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/terraform"
"os"
"testing"

consul "github.com/hashicorp/consul/api"
"github.com/hashicorp/terraform/helper/resource"
"os"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

"github.com/ashald/terraform-provider-consulacl/consulacl"
)

const dataSourceAclTokenAccessor = "65150ab9-1ba8-4538-a1cd-b0f64780ffb6"
const dataSourceAclTokenSecret = "b7723bf9-cf63-4c69-96bf-dccb924e4734"
const dataSourceAclTokenConfig = `
const (
dataSourceAclTokenAccessor = "65150ab9-1ba8-4538-a1cd-b0f64780ffb6"
dataSourceAclTokenSecret = "b7723bf9-cf63-4c69-96bf-dccb924e4734"
dataSourceAclTokenConfig = `
data "consulacl_token" "test" {
accessor = "65150ab9-1ba8-4538-a1cd-b0f64780ffb6"
}
`
)

func TestIntegrationDataSourceToken(t *testing.T) {
resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -57,22 +60,17 @@ func testDataSourceConsulAclTokenPreConfig(t *testing.T) {

raw := map[string]interface{}{}

rawConfig, err := config.NewRawConfig(raw)
if err != nil {
t.Fatalf("err: %s", err)
}

err = rp.Configure(terraform.NewResourceConfig(rawConfig))
if err != nil {
t.Fatalf("err: %s", err)
diags := rp.Configure(context.TODO(), terraform.NewResourceConfigRaw(raw))
if diags.HasError() {
t.Fatalf("err: %#v", diags)
}

token := &consul.ACLToken{
AccessorID: dataSourceAclTokenAccessor,
SecretID: dataSourceAclTokenSecret,
}

_, _, err = testClient.ACL().TokenCreate(token, nil)
_, _, err := testClient.ACL().TokenCreate(token, nil)
if err != nil {
t.Fatal("Cannot provision a test token for consulacl_token datasource test", err)
}
Expand Down
5 changes: 2 additions & 3 deletions consulacl/provider.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package consulacl

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

func Provider() terraform.ResourceProvider {
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
// Destination
Expand Down
32 changes: 15 additions & 17 deletions consulacl/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
package consulacl_test

import (
"context"
"fmt"
"github.com/ashald/terraform-provider-consulacl/consulacl"
"github.com/hashicorp/terraform/config"
"os"
"testing"

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

"github.com/ashald/terraform-provider-consulacl/consulacl"
)

var testProviders map[string]terraform.ResourceProvider
var testProviders map[string]*schema.Provider

var aclProvider *schema.Provider
var testClient *consul.Client
var (
aclProvider *schema.Provider
testClient *consul.Client
)

func init() {
aclProvider = consulacl.Provider().(*schema.Provider)
aclProvider = consulacl.Provider()

testProviders = map[string]terraform.ResourceProvider{
testProviders = map[string]*schema.Provider{
"consulacl": aclProvider,
}

Expand All @@ -38,14 +41,9 @@ func init() {

raw := map[string]interface{}{}

rawConfig, err := config.NewRawConfig(raw)
if err != nil {
panic(fmt.Sprintf("error initializing config for the test provider instance: %s", err))
}

err = aclProvider.Configure(terraform.NewResourceConfig(rawConfig))
if err != nil {
panic(fmt.Sprintf("error configuring the test provider instance: %s", err))
diags := aclProvider.Configure(context.TODO(), terraform.NewResourceConfigRaw(raw))
if diags != nil {
panic(fmt.Sprintf("error configuring the test provider instance: %#v", diags))
}

testClient = aclProvider.Meta().(*consul.Client)
Expand Down
3 changes: 2 additions & 1 deletion consulacl/resource_consulacl_policy_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package consulacl

import (
"fmt"

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

func resourceConsulAclPolicyBinding() *schema.Resource {
Expand Down
22 changes: 9 additions & 13 deletions consulacl/resource_consulacl_policy_binding_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package consulacl_test

import (
"context"
"fmt"
"github.com/ashald/terraform-provider-consulacl/consulacl"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/terraform"
"os"
"testing"

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

"github.com/ashald/terraform-provider-consulacl/consulacl"
)

const resourcePolicyBinding = `
Expand Down Expand Up @@ -57,14 +58,9 @@ func testResourcePolicyBindingPreConfig(t *testing.T) {

raw := map[string]interface{}{}

rawConfig, err := config.NewRawConfig(raw)
if err != nil {
t.Fatalf("err: %s", err)
}

err = rp.Configure(terraform.NewResourceConfig(rawConfig))
if err != nil {
t.Fatalf("err: %s", err)
diags := rp.Configure(context.TODO(), terraform.NewResourceConfigRaw(raw))
if diags.HasError() {
t.Fatalf("err: %#v", diags)
}
}

Expand Down
21 changes: 12 additions & 9 deletions consulacl/resource_consulacl_token.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package consulacl

import (
"context"
"crypto/sha256"
"fmt"
consul "github.com/hashicorp/consul/api"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/hcl"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"reflect"
"sort"
"strings"

consul "github.com/hashicorp/consul/api"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/hcl"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

var prefixedScopes = []string{"agent", "event", "key", "node", "query", "service", "session"}
var singletonScopes = []string{"keyring", "operator"}
var (
prefixedScopes = []string{"agent", "event", "key", "node", "query", "service", "session"}
singletonScopes = []string{"keyring", "operator"}
)

const anonymousToken = "anonymous"

Expand Down Expand Up @@ -224,7 +228,6 @@ func decodeRules(raw string) ([]map[string]string, error) {
simplePolicy := map[string]string{FieldScope: scope, FieldPolicy: simplePolicyValue}
result = append(result, simplePolicy)
} else {

for i := 0; i < defRef.Len(); i++ {
scopePolicyRef := defRef.Index(i)

Expand Down Expand Up @@ -321,7 +324,7 @@ func stringInSlice(str string, list []string) bool {
}

// We only need this to run manual validation on fields
func diffResource(d *schema.ResourceDiff, m interface{}) error {
func diffResource(_ context.Context, d *schema.ResourceDiff, m interface{}) error {
_, newRules := d.GetChange(FieldRule)

_, err := extractRules(newRules.(*schema.Set).List())
Expand Down
5 changes: 3 additions & 2 deletions consulacl/resource_consulacl_token14.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package consulacl

import (
"fmt"

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

func resourceConsulAclToken14() *schema.Resource {
Expand All @@ -13,7 +14,7 @@ func resourceConsulAclToken14() *schema.Resource {
Update: resourceConsulAclToken14Update,
Delete: resourceConsulAclToken14Delete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
Expand Down
Loading