Skip to content

Commit

Permalink
Cleanup testing-related code by using ConflictsWith
Browse files Browse the repository at this point in the history
Also adds better comments around how update works
  • Loading branch information
selmanj committed Aug 14, 2017
1 parent 9d1f331 commit 01514c6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
4 changes: 3 additions & 1 deletion google/resource_runtimeconfig_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,13 @@ func resourceRuntimeconfigConfigRead(d *schema.ResourceData, meta interface{}) e
func resourceRuntimeconfigConfigUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

// Update works more like an 'overwrite' method - we build a new runtimeconfig.RuntimeConfig struct and it becomes
// the new config. This means our Update logic looks an awful lot like Create (and hence, doesn't use
// schema.ResourceData.hasChange()).
fullName := d.Id()
runtimeConfig := runtimeconfig.RuntimeConfig{
Name: fullName,
}
// Update the description (currently its the only thing that could have changed)
if v, ok := d.GetOk("description"); ok {
runtimeConfig.Description = v.(string)
}
Expand Down
36 changes: 36 additions & 0 deletions google/resource_runtimeconfig_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,35 @@ func TestAccRuntimeconfig_update(t *testing.T) {
})
}

func TestAccRuntimeconfig_updateEmptyDescription(t *testing.T) {
var runtimeConfig runtimeconfig.RuntimeConfig
configName := fmt.Sprintf("runtimeconfig-test-%s", acctest.RandString(10))
description := "my test description"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRuntimeconfigConfigDestroy,
Steps: []resource.TestStep{
{
Config: testAccRuntimeconfigConfig_basicDescription(configName, description),
Check: resource.ComposeTestCheckFunc(
testAccCheckRuntimeConfigExists(
"google_runtimeconfig_config.foobar", &runtimeConfig),
testAccCheckRuntimeConfigDescription(&runtimeConfig, description),
),
}, {
Config: testAccRuntimeconfigConfig_emptyDescription(configName),
Check: resource.ComposeTestCheckFunc(
testAccCheckRuntimeConfigExists(
"google_runtimeconfig_config.foobar", &runtimeConfig),
testAccCheckRuntimeConfigDescription(&runtimeConfig, ""),
),
},
},
})
}

func testAccCheckRuntimeConfigDescription(runtimeConfig *runtimeconfig.RuntimeConfig, description string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if runtimeConfig.Description != description {
Expand Down Expand Up @@ -121,3 +150,10 @@ resource "google_runtimeconfig_config" "foobar" {
description = "%s"
}`, name, description)
}

func testAccRuntimeconfigConfig_emptyDescription(name string) string {
return fmt.Sprintf(`
resource "google_runtimeconfig_config" "foobar" {
name = "%s"
}`, name)
}
16 changes: 11 additions & 5 deletions google/resource_runtimeconfig_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ func resourceRuntimeconfigVariable() *schema.Resource {
},

"value": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"text"},
},

"text": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"value"},
},

"update_time": {
Expand Down Expand Up @@ -95,6 +97,10 @@ func resourceRuntimeconfigVariableUpdate(d *schema.ResourceData, meta interface{
return err
}

// Update works more like an 'overwrite' method - we build a new runtimeconfig.Variable struct and it becomes the
// new config. This means our Update logic looks an awful lot like Create (and hence, doesn't use
// schema.ResourceData.hasChange()).

variable, _, err := newRuntimeconfigVariableFromResourceData(d, project)
if err != nil {
return err
Expand Down Expand Up @@ -146,7 +152,7 @@ func newRuntimeconfigVariableFromResourceData(d *schema.ResourceData, project st
text, textSet := d.GetOk("text")
value, valueSet := d.GetOk("value")

if (textSet && valueSet) || (!textSet && !valueSet) {
if !textSet && !valueSet {
return nil, "", fmt.Errorf("You must specify one of value or text.")
}

Expand Down
4 changes: 2 additions & 2 deletions google/resource_runtimeconfig_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestAccRuntimeconfigVariable_errorsOnBothValueAndText(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccRuntimeconfigVariable_invalidBothTextValue(),
ExpectError: regexp.MustCompile("You must specify one of value or text"),
ExpectError: regexp.MustCompile("conflicts with"),
},
},
})
Expand Down Expand Up @@ -265,6 +265,6 @@ resource "google_runtimeconfig_config" "foobar" {
resource "google_runtimeconfig_variable" "foobar" {
parent = "${google_runtimeconfig_config.foobar.name}"
name = "%s"
name = "my-variable-namespace/%s"
}`, acctest.RandString(10), acctest.RandString(10))
}

0 comments on commit 01514c6

Please sign in to comment.