Skip to content

Commit

Permalink
Terraform: Pubsub Topic labels update
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
drebes authored and modular-magician committed Jun 14, 2019
1 parent 6a13ae2 commit 5d77cf8
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
52 changes: 51 additions & 1 deletion google/resource_pubsub_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"log"
"reflect"
"strings"
"time"

"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -27,6 +28,7 @@ func resourcePubsubTopic() *schema.Resource {
return &schema.Resource{
Create: resourcePubsubTopicCreate,
Read: resourcePubsubTopicRead,
Update: resourcePubsubTopicUpdate,
Delete: resourcePubsubTopicDelete,

Importer: &schema.ResourceImporter{
Expand All @@ -35,6 +37,7 @@ func resourcePubsubTopic() *schema.Resource {

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(240 * time.Second),
Update: schema.DefaultTimeout(240 * time.Second),
Delete: schema.DefaultTimeout(240 * time.Second),
},

Expand All @@ -48,7 +51,6 @@ func resourcePubsubTopic() *schema.Resource {
"labels": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"project": {
Expand Down Expand Up @@ -137,6 +139,48 @@ func resourcePubsubTopicRead(d *schema.ResourceData, meta interface{}) error {
return nil
}

func resourcePubsubTopicUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

obj := make(map[string]interface{})
labelsProp, err := expandPubsubTopicLabels(d.Get("labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}

obj, err = resourcePubsubTopicUpdateEncoder(d, meta, obj)
if err != nil {
return err
}

url, err := replaceVars(d, config, "{{PubsubBasePath}}projects/{{project}}/topics/{{name}}")
if err != nil {
return err
}

log.Printf("[DEBUG] Updating Topic %q: %#v", d.Id(), obj)
updateMask := []string{}

if d.HasChange("labels") {
updateMask = append(updateMask, "labels")
}
// updateMask is a URL parameter but not present in the schema, so replaceVars
// won't set it
url, err = addQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
if err != nil {
return err
}
_, err = sendRequestWithTimeout(config, "PATCH", url, obj, d.Timeout(schema.TimeoutUpdate))

if err != nil {
return fmt.Errorf("Error updating Topic %q: %s", d.Id(), err)
}

return resourcePubsubTopicRead(d, meta)
}

func resourcePubsubTopicDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

Expand Down Expand Up @@ -202,3 +246,9 @@ func resourcePubsubTopicEncoder(d *schema.ResourceData, meta interface{}, obj ma
delete(obj, "name")
return obj, nil
}

func resourcePubsubTopicUpdateEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {
newObj := make(map[string]interface{})
newObj["topic"] = obj
return newObj, nil
}
52 changes: 52 additions & 0 deletions google/resource_pubsub_topic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package google

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccPubsubTopic_update(t *testing.T) {
t.Parallel()

topic := fmt.Sprintf("tf-test-topic-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPubsubTopicDestroy,
Steps: []resource.TestStep{
{
Config: testAccPubsubTopic_update(topic, "foo", "bar"),
},
{
ResourceName: "google_pubsub_topic.foo",
ImportStateId: topic,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccPubsubTopic_update(topic, "wibble", "wobble"),
},
{
ResourceName: "google_pubsub_topic.foo",
ImportStateId: topic,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccPubsubTopic_update(topic, key, value string) string {
return fmt.Sprintf(`
resource "google_pubsub_topic" "foo" {
name = "%s"
labels = {
%s = "%s"
}
}
`, topic, key, value)
}
1 change: 1 addition & 0 deletions website/docs/r/pubsub_topic.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ This resource provides the following
[Timeouts](/docs/configuration/resources.html#timeouts) configuration options:

- `create` - Default is 4 minutes.
- `update` - Default is 4 minutes.
- `delete` - Default is 4 minutes.

## Import
Expand Down

0 comments on commit 5d77cf8

Please sign in to comment.