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

Adding ForceNew and removing Computed from Seed to enable resource update. #25

Merged
merged 1 commit into from
May 21, 2018
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion random/resource_integer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func resourceInteger() *schema.Resource {
"seed": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"result": {
Expand Down
125 changes: 125 additions & 0 deletions random/resource_integer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,72 @@ func TestAccResourceIntegerBasic(t *testing.T) {
})
}

func TestAccResourceIntegerUpdate(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testRandomIntegerBasic,
Check: resource.ComposeTestCheckFunc(
testAccResourceIntegerBasic("random_integer.integer_1"),
),
},
{
Config: testRandomIntegerUpdate,
Check: resource.ComposeTestCheckFunc(
testAccResourceIntegerUpdate("random_integer.integer_1"),
),
},
},
})
}

func TestAccResourceIntegerSeedless_to_seeded(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testRandomIntegerSeedless,
Check: resource.ComposeTestCheckFunc(
testAccResourceIntegerSeedless("random_integer.integer_1"),
),
},
{
Config: testRandomIntegerUpdate,
Check: resource.ComposeTestCheckFunc(
testAccResourceIntegerUpdate("random_integer.integer_1"),
),
},
},
})
}

func TestAccResourceIntegerSeeded_to_seedless(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testRandomIntegerBasic,
Check: resource.ComposeTestCheckFunc(
testAccResourceIntegerBasic("random_integer.integer_1"),
),
},
{
Config: testRandomIntegerSeedless,
Check: resource.ComposeTestCheckFunc(
testAccResourceIntegerSeedless("random_integer.integer_1"),
),
},
},
})
}

func testAccResourceIntegerBasic(id string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[id]
Expand All @@ -53,12 +119,71 @@ func testAccResourceIntegerBasic(id string) resource.TestCheckFunc {
}
}

func testAccResourceIntegerUpdate(id string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, _ := s.RootModule().Resources[id]
// if !ok {
// return fmt.Errorf("Not found: %s", id)
// }
result := rs.Primary.Attributes["result"]

if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}

if result == "" {
return fmt.Errorf("Result not found")
}

if result != "2" {
return fmt.Errorf("Invalid result %s. Seed does not result in correct value", result)
}
return nil
}
}

// testAccResourceIntegerSeedless only checks that some result was returned, and does not validate the value.
func testAccResourceIntegerSeedless(id string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[id]
if !ok {
return fmt.Errorf("Not found: %s", id)
}
result := rs.Primary.Attributes["result"]

if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}

if result == "" {
return fmt.Errorf("Result not found")
}

return nil
}
}

const (
testRandomIntegerBasic = `
resource "random_integer" "integer_1" {
min = 1
max = 3
seed = "12345"
}
`

testRandomIntegerUpdate = `
resource "random_integer" "integer_1" {
min = 1
max = 3
seed = "123456"
}
`

testRandomIntegerSeedless = `
resource "random_integer" "integer_1" {
min = 1
max = 3
}
`
)