Skip to content

Commit 8a9674a

Browse files
drebesmodular-magician
authored andcommitted
Terraform: Pubsub Topic labels update
Signed-off-by: Modular Magician <[email protected]>
1 parent 80df1bf commit 8a9674a

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed

google/resource_pubsub_topic.go

+51-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"log"
2020
"reflect"
21+
"strings"
2122
"time"
2223

2324
"github.com/hashicorp/terraform/helper/schema"
@@ -27,6 +28,7 @@ func resourcePubsubTopic() *schema.Resource {
2728
return &schema.Resource{
2829
Create: resourcePubsubTopicCreate,
2930
Read: resourcePubsubTopicRead,
31+
Update: resourcePubsubTopicUpdate,
3032
Delete: resourcePubsubTopicDelete,
3133

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

3638
Timeouts: &schema.ResourceTimeout{
3739
Create: schema.DefaultTimeout(240 * time.Second),
40+
Update: schema.DefaultTimeout(240 * time.Second),
3841
Delete: schema.DefaultTimeout(240 * time.Second),
3942
},
4043

@@ -48,7 +51,6 @@ func resourcePubsubTopic() *schema.Resource {
4851
"labels": {
4952
Type: schema.TypeMap,
5053
Optional: true,
51-
ForceNew: true,
5254
Elem: &schema.Schema{Type: schema.TypeString},
5355
},
5456
"project": {
@@ -132,6 +134,48 @@ func resourcePubsubTopicRead(d *schema.ResourceData, meta interface{}) error {
132134
return nil
133135
}
134136

137+
func resourcePubsubTopicUpdate(d *schema.ResourceData, meta interface{}) error {
138+
config := meta.(*Config)
139+
140+
obj := make(map[string]interface{})
141+
labelsProp, err := expandPubsubTopicLabels(d.Get("labels"), d, config)
142+
if err != nil {
143+
return err
144+
} else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
145+
obj["labels"] = labelsProp
146+
}
147+
148+
obj, err = resourcePubsubTopicUpdateEncoder(d, meta, obj)
149+
if err != nil {
150+
return err
151+
}
152+
153+
url, err := replaceVars(d, config, "https://pubsub.googleapis.com/v1/projects/{{project}}/topics/{{name}}")
154+
if err != nil {
155+
return err
156+
}
157+
158+
log.Printf("[DEBUG] Updating Topic %q: %#v", d.Id(), obj)
159+
updateMask := []string{}
160+
161+
if d.HasChange("labels") {
162+
updateMask = append(updateMask, "labels")
163+
}
164+
// updateMask is a URL parameter but not present in the schema, so replaceVars
165+
// won't set it
166+
url, err = addQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
167+
if err != nil {
168+
return err
169+
}
170+
_, err = sendRequestWithTimeout(config, "PATCH", url, obj, d.Timeout(schema.TimeoutUpdate))
171+
172+
if err != nil {
173+
return fmt.Errorf("Error updating Topic %q: %s", d.Id(), err)
174+
}
175+
176+
return resourcePubsubTopicRead(d, meta)
177+
}
178+
135179
func resourcePubsubTopicDelete(d *schema.ResourceData, meta interface{}) error {
136180
config := meta.(*Config)
137181

@@ -192,3 +236,9 @@ func expandPubsubTopicLabels(v interface{}, d TerraformResourceData, config *Con
192236
}
193237
return m, nil
194238
}
239+
240+
func resourcePubsubTopicUpdateEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {
241+
newObj := make(map[string]interface{})
242+
newObj["topic"] = obj
243+
return newObj, nil
244+
}

google/resource_pubsub_topic_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/acctest"
8+
"github.com/hashicorp/terraform/helper/resource"
9+
)
10+
11+
func TestAccPubsubTopic_update(t *testing.T) {
12+
t.Parallel()
13+
14+
topic := fmt.Sprintf("tf-test-topic-%s", acctest.RandString(10))
15+
16+
resource.Test(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
Providers: testAccProviders,
19+
CheckDestroy: testAccCheckPubsubTopicDestroy,
20+
Steps: []resource.TestStep{
21+
{
22+
Config: testAccPubsubTopic_update(topic, "foo", "bar"),
23+
},
24+
{
25+
ResourceName: "google_pubsub_topic.foo",
26+
ImportStateId: topic,
27+
ImportState: true,
28+
ImportStateVerify: true,
29+
},
30+
{
31+
Config: testAccPubsubTopic_update(topic, "wibble", "wobble"),
32+
},
33+
{
34+
ResourceName: "google_pubsub_topic.foo",
35+
ImportStateId: topic,
36+
ImportState: true,
37+
ImportStateVerify: true,
38+
},
39+
},
40+
})
41+
}
42+
43+
func testAccPubsubTopic_update(topic, key, value string) string {
44+
return fmt.Sprintf(`
45+
resource "google_pubsub_topic" "foo" {
46+
name = "%s"
47+
labels = {
48+
%s = "%s"
49+
}
50+
}
51+
`, topic, key, value)
52+
}

website/docs/r/pubsub_topic.html.markdown

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ This resource provides the following
7676
[Timeouts](/docs/configuration/resources.html#timeouts) configuration options:
7777

7878
- `create` - Default is 4 minutes.
79+
- `update` - Default is 4 minutes.
7980
- `delete` - Default is 4 minutes.
8081

8182
## Import

0 commit comments

Comments
 (0)