forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_pubsub_subscription.go
144 lines (118 loc) · 3.44 KB
/
resource_pubsub_subscription.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package google
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/pubsub/v1"
)
func resourcePubsubSubscription() *schema.Resource {
return &schema.Resource{
Create: resourcePubsubSubscriptionCreate,
Read: resourcePubsubSubscriptionRead,
Delete: resourcePubsubSubscriptionDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"topic": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"ack_deadline_seconds": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"push_config": &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"attributes": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Elem: schema.TypeString,
},
"push_endpoint": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
},
},
},
}
}
func cleanAdditionalArgs(args map[string]interface{}) map[string]string {
cleaned_args := make(map[string]string)
for k, v := range args {
cleaned_args[k] = v.(string)
}
return cleaned_args
}
func resourcePubsubSubscriptionCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
name := fmt.Sprintf("projects/%s/subscriptions/%s", project, d.Get("name").(string))
computed_topic_name := fmt.Sprintf("projects/%s/topics/%s", project, d.Get("topic").(string))
// process optional parameters
var ackDeadlineSeconds int64
ackDeadlineSeconds = 10
if v, ok := d.GetOk("ack_deadline_seconds"); ok {
ackDeadlineSeconds = int64(v.(int))
}
var subscription *pubsub.Subscription
if v, ok := d.GetOk("push_config"); ok {
push_configs := v.([]interface{})
if len(push_configs) > 1 {
return fmt.Errorf("At most one PushConfig is allowed per subscription!")
}
push_config := push_configs[0].(map[string]interface{})
attributes := push_config["attributes"].(map[string]interface{})
attributesClean := cleanAdditionalArgs(attributes)
pushConfig := &pubsub.PushConfig{Attributes: attributesClean, PushEndpoint: push_config["push_endpoint"].(string)}
subscription = &pubsub.Subscription{AckDeadlineSeconds: ackDeadlineSeconds, Topic: computed_topic_name, PushConfig: pushConfig}
} else {
subscription = &pubsub.Subscription{AckDeadlineSeconds: ackDeadlineSeconds, Topic: computed_topic_name}
}
call := config.clientPubsub.Projects.Subscriptions.Create(name, subscription)
res, err := call.Do()
if err != nil {
return err
}
d.SetId(res.Name)
return nil
}
func resourcePubsubSubscriptionRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
name := d.Id()
call := config.clientPubsub.Projects.Subscriptions.Get(name)
_, err := call.Do()
if err != nil {
return err
}
return nil
}
func resourcePubsubSubscriptionDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
name := d.Id()
call := config.clientPubsub.Projects.Subscriptions.Delete(name)
_, err := call.Do()
if err != nil {
return err
}
return nil
}