Skip to content

Commit 9762599

Browse files
Add Pub/Sub Lite reservation support to terraform. (#5284) (#3708)
* Add Pub/Sub Lite reservation support to terraform. Also fix doc links for Pub/Sub Lite. * Add Pub/Sub Lite reservation support to terraform. Also fix doc links for Pub/Sub Lite. * Add Pub/Sub Lite reservation support to terraform. Also fix doc links for Pub/Sub Lite. * Add Pub/Sub Lite reservation support to terraform. Also fix doc links for Pub/Sub Lite. * Update to expander to use 'locations' instead of 'regions'. Co-authored-by: Nathan Mckinley <[email protected]> Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Nathan Mckinley <[email protected]>
1 parent 48b28da commit 9762599

11 files changed

+762
-6
lines changed

.changelog/5284.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
```release-note:new-resource
2+
`google_pubsub_lite_reservation`
3+
```
4+
```release-note:enhancement
5+
pubsub: Added support for references to `google_pubsub_lite_reservation` to `google_pubsub_lite_topic`.
6+
```

google-beta/provider.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -894,9 +894,9 @@ func Provider() *schema.Provider {
894894
return provider
895895
}
896896

897-
// Generated resources: 239
897+
// Generated resources: 240
898898
// Generated IAM resources: 117
899-
// Total generated resources: 356
899+
// Total generated resources: 357
900900
func ResourceMap() map[string]*schema.Resource {
901901
resourceMap, _ := ResourceMapWithErrors()
902902
return resourceMap
@@ -1203,6 +1203,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
12031203
"google_pubsub_topic_iam_policy": ResourceIamPolicy(PubsubTopicIamSchema, PubsubTopicIamUpdaterProducer, PubsubTopicIdParseFunc),
12041204
"google_pubsub_subscription": resourcePubsubSubscription(),
12051205
"google_pubsub_schema": resourcePubsubSchema(),
1206+
"google_pubsub_lite_reservation": resourcePubsubLiteReservation(),
12061207
"google_pubsub_lite_topic": resourcePubsubLiteTopic(),
12071208
"google_pubsub_lite_subscription": resourcePubsubLiteSubscription(),
12081209
"google_redis_instance": resourceRedisInstance(),
Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
// ----------------------------------------------------------------------------
2+
//
3+
// *** AUTO GENERATED CODE *** Type: MMv1 ***
4+
//
5+
// ----------------------------------------------------------------------------
6+
//
7+
// This file is automatically generated by Magic Modules and manual
8+
// changes will be clobbered when the file is regenerated.
9+
//
10+
// Please read more about how to change this file in
11+
// .github/CONTRIBUTING.md.
12+
//
13+
// ----------------------------------------------------------------------------
14+
15+
package google
16+
17+
import (
18+
"fmt"
19+
"log"
20+
"reflect"
21+
"strconv"
22+
"strings"
23+
"time"
24+
25+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
26+
)
27+
28+
func resourcePubsubLiteReservation() *schema.Resource {
29+
return &schema.Resource{
30+
Create: resourcePubsubLiteReservationCreate,
31+
Read: resourcePubsubLiteReservationRead,
32+
Update: resourcePubsubLiteReservationUpdate,
33+
Delete: resourcePubsubLiteReservationDelete,
34+
35+
Importer: &schema.ResourceImporter{
36+
State: resourcePubsubLiteReservationImport,
37+
},
38+
39+
Timeouts: &schema.ResourceTimeout{
40+
Create: schema.DefaultTimeout(4 * time.Minute),
41+
Update: schema.DefaultTimeout(4 * time.Minute),
42+
Delete: schema.DefaultTimeout(4 * time.Minute),
43+
},
44+
45+
Schema: map[string]*schema.Schema{
46+
"name": {
47+
Type: schema.TypeString,
48+
Required: true,
49+
ForceNew: true,
50+
DiffSuppressFunc: compareSelfLinkOrResourceName,
51+
Description: `Name of the reservation.`,
52+
},
53+
"throughput_capacity": {
54+
Type: schema.TypeInt,
55+
Required: true,
56+
Description: `The reserved throughput capacity. Every unit of throughput capacity is
57+
equivalent to 1 MiB/s of published messages or 2 MiB/s of subscribed
58+
messages.`,
59+
},
60+
"region": {
61+
Type: schema.TypeString,
62+
Optional: true,
63+
Description: `The region of the pubsub lite reservation.`,
64+
},
65+
"project": {
66+
Type: schema.TypeString,
67+
Optional: true,
68+
Computed: true,
69+
ForceNew: true,
70+
},
71+
},
72+
UseJSONNumber: true,
73+
}
74+
}
75+
76+
func resourcePubsubLiteReservationCreate(d *schema.ResourceData, meta interface{}) error {
77+
config := meta.(*Config)
78+
userAgent, err := generateUserAgentString(d, config.userAgent)
79+
if err != nil {
80+
return err
81+
}
82+
83+
obj := make(map[string]interface{})
84+
throughputCapacityProp, err := expandPubsubLiteReservationThroughputCapacity(d.Get("throughput_capacity"), d, config)
85+
if err != nil {
86+
return err
87+
} else if v, ok := d.GetOkExists("throughput_capacity"); !isEmptyValue(reflect.ValueOf(throughputCapacityProp)) && (ok || !reflect.DeepEqual(v, throughputCapacityProp)) {
88+
obj["throughputCapacity"] = throughputCapacityProp
89+
}
90+
91+
url, err := replaceVars(d, config, "{{PubsubLiteBasePath}}projects/{{project}}/locations/{{region}}/reservations?reservationId={{name}}")
92+
if err != nil {
93+
return err
94+
}
95+
96+
log.Printf("[DEBUG] Creating new Reservation: %#v", obj)
97+
billingProject := ""
98+
99+
project, err := getProject(d, config)
100+
if err != nil {
101+
return fmt.Errorf("Error fetching project for Reservation: %s", err)
102+
}
103+
billingProject = project
104+
105+
// err == nil indicates that the billing_project value was found
106+
if bp, err := getBillingProject(d, config); err == nil {
107+
billingProject = bp
108+
}
109+
110+
res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate))
111+
if err != nil {
112+
return fmt.Errorf("Error creating Reservation: %s", err)
113+
}
114+
115+
// Store the ID now
116+
id, err := replaceVars(d, config, "projects/{{project}}/locations/{{region}}/reservations/{{name}}")
117+
if err != nil {
118+
return fmt.Errorf("Error constructing id: %s", err)
119+
}
120+
d.SetId(id)
121+
122+
log.Printf("[DEBUG] Finished creating Reservation %q: %#v", d.Id(), res)
123+
124+
return resourcePubsubLiteReservationRead(d, meta)
125+
}
126+
127+
func resourcePubsubLiteReservationRead(d *schema.ResourceData, meta interface{}) error {
128+
config := meta.(*Config)
129+
userAgent, err := generateUserAgentString(d, config.userAgent)
130+
if err != nil {
131+
return err
132+
}
133+
134+
url, err := replaceVars(d, config, "{{PubsubLiteBasePath}}projects/{{project}}/locations/{{region}}/reservations/{{name}}")
135+
if err != nil {
136+
return err
137+
}
138+
139+
billingProject := ""
140+
141+
project, err := getProject(d, config)
142+
if err != nil {
143+
return fmt.Errorf("Error fetching project for Reservation: %s", err)
144+
}
145+
billingProject = project
146+
147+
// err == nil indicates that the billing_project value was found
148+
if bp, err := getBillingProject(d, config); err == nil {
149+
billingProject = bp
150+
}
151+
152+
res, err := sendRequest(config, "GET", billingProject, url, userAgent, nil)
153+
if err != nil {
154+
return handleNotFoundError(err, d, fmt.Sprintf("PubsubLiteReservation %q", d.Id()))
155+
}
156+
157+
if err := d.Set("project", project); err != nil {
158+
return fmt.Errorf("Error reading Reservation: %s", err)
159+
}
160+
161+
if err := d.Set("throughput_capacity", flattenPubsubLiteReservationThroughputCapacity(res["throughputCapacity"], d, config)); err != nil {
162+
return fmt.Errorf("Error reading Reservation: %s", err)
163+
}
164+
165+
return nil
166+
}
167+
168+
func resourcePubsubLiteReservationUpdate(d *schema.ResourceData, meta interface{}) error {
169+
config := meta.(*Config)
170+
userAgent, err := generateUserAgentString(d, config.userAgent)
171+
if err != nil {
172+
return err
173+
}
174+
175+
billingProject := ""
176+
177+
project, err := getProject(d, config)
178+
if err != nil {
179+
return fmt.Errorf("Error fetching project for Reservation: %s", err)
180+
}
181+
billingProject = project
182+
183+
obj := make(map[string]interface{})
184+
throughputCapacityProp, err := expandPubsubLiteReservationThroughputCapacity(d.Get("throughput_capacity"), d, config)
185+
if err != nil {
186+
return err
187+
} else if v, ok := d.GetOkExists("throughput_capacity"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, throughputCapacityProp)) {
188+
obj["throughputCapacity"] = throughputCapacityProp
189+
}
190+
191+
url, err := replaceVars(d, config, "{{PubsubLiteBasePath}}projects/{{project}}/locations/{{region}}/reservations/{{name}}")
192+
if err != nil {
193+
return err
194+
}
195+
196+
log.Printf("[DEBUG] Updating Reservation %q: %#v", d.Id(), obj)
197+
updateMask := []string{}
198+
199+
if d.HasChange("throughput_capacity") {
200+
updateMask = append(updateMask, "throughputCapacity")
201+
}
202+
// updateMask is a URL parameter but not present in the schema, so replaceVars
203+
// won't set it
204+
url, err = addQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
205+
if err != nil {
206+
return err
207+
}
208+
209+
// err == nil indicates that the billing_project value was found
210+
if bp, err := getBillingProject(d, config); err == nil {
211+
billingProject = bp
212+
}
213+
214+
res, err := sendRequestWithTimeout(config, "PATCH", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutUpdate))
215+
216+
if err != nil {
217+
return fmt.Errorf("Error updating Reservation %q: %s", d.Id(), err)
218+
} else {
219+
log.Printf("[DEBUG] Finished updating Reservation %q: %#v", d.Id(), res)
220+
}
221+
222+
return resourcePubsubLiteReservationRead(d, meta)
223+
}
224+
225+
func resourcePubsubLiteReservationDelete(d *schema.ResourceData, meta interface{}) error {
226+
config := meta.(*Config)
227+
userAgent, err := generateUserAgentString(d, config.userAgent)
228+
if err != nil {
229+
return err
230+
}
231+
232+
billingProject := ""
233+
234+
project, err := getProject(d, config)
235+
if err != nil {
236+
return fmt.Errorf("Error fetching project for Reservation: %s", err)
237+
}
238+
billingProject = project
239+
240+
url, err := replaceVars(d, config, "{{PubsubLiteBasePath}}projects/{{project}}/locations/{{region}}/reservations/{{name}}")
241+
if err != nil {
242+
return err
243+
}
244+
245+
var obj map[string]interface{}
246+
log.Printf("[DEBUG] Deleting Reservation %q", d.Id())
247+
248+
// err == nil indicates that the billing_project value was found
249+
if bp, err := getBillingProject(d, config); err == nil {
250+
billingProject = bp
251+
}
252+
253+
res, err := sendRequestWithTimeout(config, "DELETE", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutDelete))
254+
if err != nil {
255+
return handleNotFoundError(err, d, "Reservation")
256+
}
257+
258+
log.Printf("[DEBUG] Finished deleting Reservation %q: %#v", d.Id(), res)
259+
return nil
260+
}
261+
262+
func resourcePubsubLiteReservationImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
263+
config := meta.(*Config)
264+
if err := parseImportId([]string{
265+
"projects/(?P<project>[^/]+)/locations/(?P<region>[^/]+)/reservations/(?P<name>[^/]+)",
266+
"(?P<project>[^/]+)/(?P<region>[^/]+)/(?P<name>[^/]+)",
267+
"(?P<region>[^/]+)/(?P<name>[^/]+)",
268+
"(?P<name>[^/]+)",
269+
}, d, config); err != nil {
270+
return nil, err
271+
}
272+
273+
// Replace import id for the resource id
274+
id, err := replaceVars(d, config, "projects/{{project}}/locations/{{region}}/reservations/{{name}}")
275+
if err != nil {
276+
return nil, fmt.Errorf("Error constructing id: %s", err)
277+
}
278+
d.SetId(id)
279+
280+
return []*schema.ResourceData{d}, nil
281+
}
282+
283+
func flattenPubsubLiteReservationThroughputCapacity(v interface{}, d *schema.ResourceData, config *Config) interface{} {
284+
// Handles the string fixed64 format
285+
if strVal, ok := v.(string); ok {
286+
if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
287+
return intVal
288+
}
289+
}
290+
291+
// number values are represented as float64
292+
if floatVal, ok := v.(float64); ok {
293+
intVal := int(floatVal)
294+
return intVal
295+
}
296+
297+
return v // let terraform core handle it otherwise
298+
}
299+
300+
func expandPubsubLiteReservationThroughputCapacity(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
301+
return v, nil
302+
}

0 commit comments

Comments
 (0)