Skip to content

Commit 5d77cf8

Browse files
drebesmodular-magician
authored andcommitted
Terraform: Pubsub Topic labels update
Signed-off-by: Modular Magician <[email protected]>
1 parent 6a13ae2 commit 5d77cf8

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": {
@@ -137,6 +139,48 @@ func resourcePubsubTopicRead(d *schema.ResourceData, meta interface{}) error {
137139
return nil
138140
}
139141

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

@@ -202,3 +246,9 @@ func resourcePubsubTopicEncoder(d *schema.ResourceData, meta interface{}, obj ma
202246
delete(obj, "name")
203247
return obj, nil
204248
}
249+
250+
func resourcePubsubTopicUpdateEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {
251+
newObj := make(map[string]interface{})
252+
newObj["topic"] = obj
253+
return newObj, nil
254+
}

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)