Skip to content

Commit

Permalink
Merge pull request #104 from hashicorp/import-string
Browse files Browse the repository at this point in the history
Import string
  • Loading branch information
paultyng authored Mar 24, 2020
2 parents 0bf1a5b + 0f98710 commit 75d5afa
Show file tree
Hide file tree
Showing 69 changed files with 10,516 additions and 16,950 deletions.
2 changes: 1 addition & 1 deletion .go-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.11.5
1.14
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ services:
- docker
language: go
go:
- "1.11.x"
- "1.14.x"
- tip
env:
- GO111MODULE=on GOFLAGS=-mod=vendor TF_VERSION=0.12.20

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module github.com/terraform-providers/terraform-provider-random

go 1.14

require (
github.com/dustinkirkland/golang-petname v0.0.0-20170105215008-242afa0b4f8a
github.com/hashicorp/errwrap v1.0.0
Expand Down
3 changes: 3 additions & 0 deletions random/resource_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ func resourcePassword() *schema.Resource {
Read: readNil,
Delete: schema.RemoveFromState,
Schema: stringSchemaV1(true),
Importer: &schema.ResourceImporter{
State: importStringFunc(true),
},
}
}
36 changes: 36 additions & 0 deletions random/resource_pasword_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package random

import (
"fmt"
"regexp"
"strings"
"testing"

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

func TestAccResourcePassword(t *testing.T) {
Expand Down Expand Up @@ -35,6 +37,40 @@ func TestAccResourcePassword(t *testing.T) {
regexMatch("random_password.min", regexp.MustCompile(`([!#@])`), 1),
),
},
// TODO: for some reason unable to test import of a single resource here, broken out to test below
},
})
}

func TestAccResourcePassword_import(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: `
resource "random_password" "foo" {
length = 32
}`,
},
{
ResourceName: "random_password.foo",
ImportStateIdFunc: func(s *terraform.State) (string, error) {
id := "random_password.foo"
rs, ok := s.RootModule().Resources[id]
if !ok {
return "", fmt.Errorf("Not found: %s", id)
}
if rs.Primary.ID == "" {
return "", fmt.Errorf("No ID is set")
}

return rs.Primary.Attributes["result"], nil
},
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"length", "lower", "number", "special", "upper", "min_lower", "min_numeric", "min_special", "min_upper", "override_special"},
},
},
})
}
3 changes: 3 additions & 0 deletions random/resource_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ func resourceString() *schema.Resource {
MigrateState: resourceRandomStringMigrateState,
SchemaVersion: 1,
Schema: stringSchemaV1(false),
Importer: &schema.ResourceImporter{
State: importStringFunc(false),
},
}
}
25 changes: 24 additions & 1 deletion random/resource_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ func TestAccResourceString(t *testing.T) {
regexMatch("random_string.min", regexp.MustCompile(`([!#@])`), 1),
),
},
{
ResourceName: "random_string.foo",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"length", "lower", "number", "special", "upper", "min_lower", "min_numeric", "min_special", "min_upper", "override_special"},
},
{
ResourceName: "random_string.bar",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"length", "lower", "number", "special", "upper", "min_lower", "min_numeric", "min_special", "min_upper", "override_special"},
},
{
ResourceName: "random_string.three",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"length", "lower", "number", "special", "upper", "min_lower", "min_numeric", "min_special", "min_upper", "override_special"},
},
{
ResourceName: "random_string.min",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"length", "lower", "number", "special", "upper", "min_lower", "min_numeric", "min_special", "min_upper", "override_special"},
},
},
})
}
Expand Down Expand Up @@ -128,6 +152,5 @@ resource "random_string" "min" {
min_special = 1
min_numeric = 4
}
`
)
11 changes: 11 additions & 0 deletions random/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,14 @@ func generateRandomBytes(charSet *string, length int) ([]byte, error) {
func readNil(d *schema.ResourceData, meta interface{}) error {
return nil
}

func importStringFunc(sensitive bool) schema.StateFunc {
return func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
val := d.Id()
if sensitive {
d.SetId("none")
}
d.Set("result", val)
return []*schema.ResourceData{d}, nil
}
}
Loading

0 comments on commit 75d5afa

Please sign in to comment.