From a8eab5efd883f021302fc0d0a395c4f0249f5a7f Mon Sep 17 00:00:00 2001 From: Borys Pierov Date: Fri, 6 Jul 2018 00:00:52 -0400 Subject: [PATCH] Importer for consulacl_token --- CHANGELOG.md | 9 +++++- README.md | 37 +++++++++++++++++----- consulacl/resource_consulacl_token.go | 8 +++++ consulacl/resource_consulacl_token_test.go | 31 ++++++++++++++++++ 4 files changed, 76 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83e7ff1..78ae50a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ # Change Log -## 1.0 - 2018-07-05 +## 1.1.0 - 2018-07-06 + +### Added + +- Import functionality for `consulacl_token` + + +## 1.0.0 - 2018-07-05 ### Added diff --git a/README.md b/README.md index 7343c9e..d6980ec 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,6 @@ Provider is configurable with number of parameters: * `address` - String, host and port used to connect to Consul. Defaults to `localhost:8500`. Can be set via environment variables `CONSUL_ADDRESS` or `CONSUL_HTTP_ADDR`. -* `datacenter` - String, datacenter to operate in. Defaults to empty values which means local datacenter. Can be set via -environment variable `CONSUL_DATACENTER`. * `token` - String, ACL token to use for API calls to Consul. Must be a `management` token to manage ACLs. Defaults to empty value. Can be set via environment variables `CONSUL_TOKEN` or `CONSUL_HTTP_TOKEN`. * `scheme` - String, scheme to use to connect to Consul. Defaults to `http`. Can be set via environment variables @@ -89,7 +87,7 @@ resource "consulacl_token" "token" { ### Download ```bash -$ wget "https://github.com/ashald/terraform-provider-consulacl/releases/download/v1.0.0/terraform-provider-consulacl_v1.0.0-$(uname -s | tr '[:upper:]' '[:lower:]')-amd64" +$ wget "https://github.com/ashald/terraform-provider-consulacl/releases/download/v1.1.0/terraform-provider-consulacl_v1.1.0-$(uname -s | tr '[:upper:]' '[:lower:]')-amd64" $ chmod +x ./terraform-provider-transform* ``` @@ -97,7 +95,7 @@ $ chmod +x ./terraform-provider-transform* ```bash $ ls -1 main.tf - terraform-provider-consulacl_v1.0.0-linux-amd64 + terraform-provider-consulacl_v1.1.0-linux-amd64 $ terraform init @@ -180,6 +178,21 @@ $ terraform apply ``` +### Import + +```bash +$ terraform import consulacl_token.token "a694f2c0-20c8-902c-7d57-be10bd3edb1b" + consulacl_token.token: Importing from ID "a694f2c0-20c8-902c-7d57-be10bd3edb1b"... + consulacl_token.token: Import complete! + Imported consulacl_token (ID: 929a4284c36bdaa9ba4a96dbbcfd9839160258643e4d1beb9a15fff6c6bcd027) + consulacl_token.token: Refreshing state... (ID: 929a4284c36bdaa9ba4a96dbbcfd9839160258643e4d1beb9a15fff6c6bcd027) + + Import successful! + + The resources that were imported are shown above. These resources are now in + your Terraform state and will henceforth be managed by Terraform. +``` + ## Development @@ -216,8 +229,14 @@ $ make test ? github.com/ashald/terraform-provider-consulacl [no test files] === RUN TestProvider --- PASS: TestProvider (0.00s) + === RUN TestIntegrationToken + --- SKIP: TestIntegrationToken (0.00s) + testing.go:427: Acceptance tests skipped unless env 'TF_ACC' set + === RUN TestIntegrationTokenImport + --- SKIP: TestIntegrationTokenImport (0.00s) + testing.go:427: Acceptance tests skipped unless env 'TF_ACC' set PASS - ok github.com/ashald/terraform-provider-consulacl/consulacl 0.028s + ok github.com/ashald/terraform-provider-consulacl/consulacl (cached) go vet ./... ``` @@ -232,9 +251,11 @@ $ make test-integration === RUN TestProvider --- PASS: TestProvider (0.00s) === RUN TestIntegrationToken - --- PASS: TestIntegrationToken (0.29s) + --- PASS: TestIntegrationToken (0.30s) + === RUN TestIntegrationTokenImport + --- PASS: TestIntegrationTokenImport (0.06s) PASS - ok github.com/ashald/terraform-provider-consulacl/consulacl 0.350s + ok github.com/ashald/terraform-provider-consulacl/consulacl 0.391s ``` If you have [Docker](https://docs.docker.com/install/) installed, you can run Consul with the following command: @@ -263,7 +284,7 @@ environment variable. In order to build plugin for the current platform use [GNU]make: ```bash $ make build - go build -o terraform-provider-consulacl_v1.0.0 + go build -o terraform-provider-consulacl_v1.1.0 ``` diff --git a/consulacl/resource_consulacl_token.go b/consulacl/resource_consulacl_token.go index f7fecfd..775a965 100644 --- a/consulacl/resource_consulacl_token.go +++ b/consulacl/resource_consulacl_token.go @@ -37,6 +37,14 @@ func resourceConsulAclToken() *schema.Resource { Read: resourceConsulAclTokenRead, Delete: resourceConsulAclTokenDelete, + Importer: &schema.ResourceImporter{ + State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + d.Set(FieldToken, d.Id()) + d.SetId(getSHA256(d.Id())) + return []*schema.ResourceData{d}, nil + }, + }, + CustomizeDiff: diffResource, Schema: map[string]*schema.Schema{ diff --git a/consulacl/resource_consulacl_token_test.go b/consulacl/resource_consulacl_token_test.go index 95c141b..e733a2a 100644 --- a/consulacl/resource_consulacl_token_test.go +++ b/consulacl/resource_consulacl_token_test.go @@ -103,6 +103,37 @@ func TestIntegrationToken(t *testing.T) { }) } +const aclTokenImportConfig = ` +resource "consulacl_token" "imported" { + name = "Imported" + token = "my-imported-token" + type = "client" + + rule { scope="operator" policy="read" } +} +` + +func TestIntegrationTokenImport(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testPreCheck(t) }, + Providers: testProviders, + CheckDestroy: resource.ComposeTestCheckFunc( + testConsulAclTokenAbsent("my-imported-token"), + ), + Steps: []resource.TestStep{ + { + Config: aclTokenImportConfig, + }, + { + ResourceName: "consulacl_token.imported", + ImportState: true, + ImportStateVerify: true, + ImportStateId: "my-imported-token", + }, + }, + }) +} + func testConsulAclTokenAbsent(token string) resource.TestCheckFunc { return func(s *terraform.State) error { acl := aclProvider.Meta().(*consul.Client).ACL()