Skip to content

Commit a279e40

Browse files
Feature/add pubsub schema resource (#4741) (#3243)
Co-authored-by: Scott Suarez <[email protected]> Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Scott Suarez <[email protected]>
1 parent 50b6b3d commit a279e40

11 files changed

+850
-3
lines changed

.changelog/4741.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
pubsub: Add schema resource as `google_pubsub_schema` to google pubsub
3+
```

google-beta/provider.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -839,9 +839,9 @@ func Provider() *schema.Provider {
839839
return provider
840840
}
841841

842-
// Generated resources: 221
842+
// Generated resources: 222
843843
// Generated IAM resources: 117
844-
// Total generated resources: 338
844+
// Total generated resources: 339
845845
func ResourceMap() map[string]*schema.Resource {
846846
resourceMap, _ := ResourceMapWithErrors()
847847
return resourceMap
@@ -1134,6 +1134,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
11341134
"google_pubsub_topic_iam_member": ResourceIamMember(PubsubTopicIamSchema, PubsubTopicIamUpdaterProducer, PubsubTopicIdParseFunc),
11351135
"google_pubsub_topic_iam_policy": ResourceIamPolicy(PubsubTopicIamSchema, PubsubTopicIamUpdaterProducer, PubsubTopicIdParseFunc),
11361136
"google_pubsub_subscription": resourcePubsubSubscription(),
1137+
"google_pubsub_schema": resourcePubsubSchema(),
11371138
"google_pubsub_lite_topic": resourcePubsubLiteTopic(),
11381139
"google_pubsub_lite_subscription": resourcePubsubLiteSubscription(),
11391140
"google_redis_instance": resourceRedisInstance(),

google-beta/resource_dataflow_flex_template_job_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
11-
compute "google.golang.org/api/compute/v1"
11+
"google.golang.org/api/compute/v1"
1212
)
1313

1414
func TestAccDataflowFlexTemplateJob_basic(t *testing.T) {
Lines changed: 320 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,320 @@
1+
// ----------------------------------------------------------------------------
2+
//
3+
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
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+
"time"
22+
23+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
24+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
25+
)
26+
27+
func resourcePubsubSchema() *schema.Resource {
28+
return &schema.Resource{
29+
Create: resourcePubsubSchemaCreate,
30+
Read: resourcePubsubSchemaRead,
31+
Update: resourcePubsubSchemaUpdate,
32+
Delete: resourcePubsubSchemaDelete,
33+
34+
Importer: &schema.ResourceImporter{
35+
State: resourcePubsubSchemaImport,
36+
},
37+
38+
Timeouts: &schema.ResourceTimeout{
39+
Create: schema.DefaultTimeout(4 * time.Minute),
40+
Update: schema.DefaultTimeout(4 * time.Minute),
41+
Delete: schema.DefaultTimeout(4 * time.Minute),
42+
},
43+
44+
Schema: map[string]*schema.Schema{
45+
"name": {
46+
Type: schema.TypeString,
47+
Required: true,
48+
ForceNew: true,
49+
DiffSuppressFunc: compareSelfLinkOrResourceName,
50+
Description: `The ID to use for the schema, which will become the final component of the schema's resource name.`,
51+
},
52+
"definition": {
53+
Type: schema.TypeString,
54+
Optional: true,
55+
Description: `The definition of the schema.
56+
This should contain a string representing the full definition of the schema
57+
that is a valid schema definition of the type specified in type.`,
58+
},
59+
"type": {
60+
Type: schema.TypeString,
61+
Optional: true,
62+
ValidateFunc: validation.StringInSlice([]string{"TYPE_UNSPECIFIED", "PROTOCOL_BUFFER", "AVRO", ""}, false),
63+
Description: `The type of the schema definition Default value: "TYPE_UNSPECIFIED" Possible values: ["TYPE_UNSPECIFIED", "PROTOCOL_BUFFER", "AVRO"]`,
64+
Default: "TYPE_UNSPECIFIED",
65+
},
66+
"project": {
67+
Type: schema.TypeString,
68+
Optional: true,
69+
Computed: true,
70+
ForceNew: true,
71+
},
72+
},
73+
UseJSONNumber: true,
74+
}
75+
}
76+
77+
func resourcePubsubSchemaCreate(d *schema.ResourceData, meta interface{}) error {
78+
config := meta.(*Config)
79+
userAgent, err := generateUserAgentString(d, config.userAgent)
80+
if err != nil {
81+
return err
82+
}
83+
84+
obj := make(map[string]interface{})
85+
typeProp, err := expandPubsubSchemaType(d.Get("type"), d, config)
86+
if err != nil {
87+
return err
88+
} else if v, ok := d.GetOkExists("type"); !isEmptyValue(reflect.ValueOf(typeProp)) && (ok || !reflect.DeepEqual(v, typeProp)) {
89+
obj["type"] = typeProp
90+
}
91+
definitionProp, err := expandPubsubSchemaDefinition(d.Get("definition"), d, config)
92+
if err != nil {
93+
return err
94+
} else if v, ok := d.GetOkExists("definition"); !isEmptyValue(reflect.ValueOf(definitionProp)) && (ok || !reflect.DeepEqual(v, definitionProp)) {
95+
obj["definition"] = definitionProp
96+
}
97+
nameProp, err := expandPubsubSchemaName(d.Get("name"), d, config)
98+
if err != nil {
99+
return err
100+
} else if v, ok := d.GetOkExists("name"); !isEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) {
101+
obj["name"] = nameProp
102+
}
103+
104+
url, err := replaceVars(d, config, "{{PubsubBasePath}}projects/{{project}}/schemas?schemaId={{name}}")
105+
if err != nil {
106+
return err
107+
}
108+
109+
log.Printf("[DEBUG] Creating new Schema: %#v", obj)
110+
billingProject := ""
111+
112+
project, err := getProject(d, config)
113+
if err != nil {
114+
return fmt.Errorf("Error fetching project for Schema: %s", err)
115+
}
116+
billingProject = project
117+
118+
// err == nil indicates that the billing_project value was found
119+
if bp, err := getBillingProject(d, config); err == nil {
120+
billingProject = bp
121+
}
122+
123+
res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate))
124+
if err != nil {
125+
return fmt.Errorf("Error creating Schema: %s", err)
126+
}
127+
128+
// Store the ID now
129+
id, err := replaceVars(d, config, "projects/{{project}}/schemas/{{name}}")
130+
if err != nil {
131+
return fmt.Errorf("Error constructing id: %s", err)
132+
}
133+
d.SetId(id)
134+
135+
log.Printf("[DEBUG] Finished creating Schema %q: %#v", d.Id(), res)
136+
137+
return resourcePubsubSchemaRead(d, meta)
138+
}
139+
140+
func resourcePubsubSchemaRead(d *schema.ResourceData, meta interface{}) error {
141+
config := meta.(*Config)
142+
userAgent, err := generateUserAgentString(d, config.userAgent)
143+
if err != nil {
144+
return err
145+
}
146+
147+
url, err := replaceVars(d, config, "{{PubsubBasePath}}projects/{{project}}/schemas/{{name}}")
148+
if err != nil {
149+
return err
150+
}
151+
152+
billingProject := ""
153+
154+
project, err := getProject(d, config)
155+
if err != nil {
156+
return fmt.Errorf("Error fetching project for Schema: %s", err)
157+
}
158+
billingProject = project
159+
160+
// err == nil indicates that the billing_project value was found
161+
if bp, err := getBillingProject(d, config); err == nil {
162+
billingProject = bp
163+
}
164+
165+
res, err := sendRequest(config, "GET", billingProject, url, userAgent, nil)
166+
if err != nil {
167+
return handleNotFoundError(err, d, fmt.Sprintf("PubsubSchema %q", d.Id()))
168+
}
169+
170+
if err := d.Set("project", project); err != nil {
171+
return fmt.Errorf("Error reading Schema: %s", err)
172+
}
173+
174+
if err := d.Set("type", flattenPubsubSchemaType(res["type"], d, config)); err != nil {
175+
return fmt.Errorf("Error reading Schema: %s", err)
176+
}
177+
if err := d.Set("name", flattenPubsubSchemaName(res["name"], d, config)); err != nil {
178+
return fmt.Errorf("Error reading Schema: %s", err)
179+
}
180+
181+
return nil
182+
}
183+
184+
func resourcePubsubSchemaUpdate(d *schema.ResourceData, meta interface{}) error {
185+
config := meta.(*Config)
186+
userAgent, err := generateUserAgentString(d, config.userAgent)
187+
if err != nil {
188+
return err
189+
}
190+
191+
billingProject := ""
192+
193+
project, err := getProject(d, config)
194+
if err != nil {
195+
return fmt.Errorf("Error fetching project for Schema: %s", err)
196+
}
197+
billingProject = project
198+
199+
obj := make(map[string]interface{})
200+
typeProp, err := expandPubsubSchemaType(d.Get("type"), d, config)
201+
if err != nil {
202+
return err
203+
} else if v, ok := d.GetOkExists("type"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, typeProp)) {
204+
obj["type"] = typeProp
205+
}
206+
definitionProp, err := expandPubsubSchemaDefinition(d.Get("definition"), d, config)
207+
if err != nil {
208+
return err
209+
} else if v, ok := d.GetOkExists("definition"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, definitionProp)) {
210+
obj["definition"] = definitionProp
211+
}
212+
nameProp, err := expandPubsubSchemaName(d.Get("name"), d, config)
213+
if err != nil {
214+
return err
215+
} else if v, ok := d.GetOkExists("name"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, nameProp)) {
216+
obj["name"] = nameProp
217+
}
218+
219+
url, err := replaceVars(d, config, "{{PubsubBasePath}}projects/{{project}}/schemas/{{name}}")
220+
if err != nil {
221+
return err
222+
}
223+
224+
log.Printf("[DEBUG] Updating Schema %q: %#v", d.Id(), obj)
225+
226+
// err == nil indicates that the billing_project value was found
227+
if bp, err := getBillingProject(d, config); err == nil {
228+
billingProject = bp
229+
}
230+
231+
res, err := sendRequestWithTimeout(config, "PUT", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutUpdate))
232+
233+
if err != nil {
234+
return fmt.Errorf("Error updating Schema %q: %s", d.Id(), err)
235+
} else {
236+
log.Printf("[DEBUG] Finished updating Schema %q: %#v", d.Id(), res)
237+
}
238+
239+
return resourcePubsubSchemaRead(d, meta)
240+
}
241+
242+
func resourcePubsubSchemaDelete(d *schema.ResourceData, meta interface{}) error {
243+
config := meta.(*Config)
244+
userAgent, err := generateUserAgentString(d, config.userAgent)
245+
if err != nil {
246+
return err
247+
}
248+
249+
billingProject := ""
250+
251+
project, err := getProject(d, config)
252+
if err != nil {
253+
return fmt.Errorf("Error fetching project for Schema: %s", err)
254+
}
255+
billingProject = project
256+
257+
url, err := replaceVars(d, config, "{{PubsubBasePath}}projects/{{project}}/schemas/{{name}}")
258+
if err != nil {
259+
return err
260+
}
261+
262+
var obj map[string]interface{}
263+
log.Printf("[DEBUG] Deleting Schema %q", d.Id())
264+
265+
// err == nil indicates that the billing_project value was found
266+
if bp, err := getBillingProject(d, config); err == nil {
267+
billingProject = bp
268+
}
269+
270+
res, err := sendRequestWithTimeout(config, "DELETE", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutDelete))
271+
if err != nil {
272+
return handleNotFoundError(err, d, "Schema")
273+
}
274+
275+
log.Printf("[DEBUG] Finished deleting Schema %q: %#v", d.Id(), res)
276+
return nil
277+
}
278+
279+
func resourcePubsubSchemaImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
280+
config := meta.(*Config)
281+
if err := parseImportId([]string{
282+
"projects/(?P<project>[^/]+)/schemas/(?P<name>[^/]+)",
283+
"(?P<project>[^/]+)/(?P<name>[^/]+)",
284+
"(?P<name>[^/]+)",
285+
}, d, config); err != nil {
286+
return nil, err
287+
}
288+
289+
// Replace import id for the resource id
290+
id, err := replaceVars(d, config, "projects/{{project}}/schemas/{{name}}")
291+
if err != nil {
292+
return nil, fmt.Errorf("Error constructing id: %s", err)
293+
}
294+
d.SetId(id)
295+
296+
return []*schema.ResourceData{d}, nil
297+
}
298+
299+
func flattenPubsubSchemaType(v interface{}, d *schema.ResourceData, config *Config) interface{} {
300+
return v
301+
}
302+
303+
func flattenPubsubSchemaName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
304+
if v == nil {
305+
return v
306+
}
307+
return NameFromSelfLinkStateFunc(v)
308+
}
309+
310+
func expandPubsubSchemaType(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
311+
return v, nil
312+
}
313+
314+
func expandPubsubSchemaDefinition(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
315+
return v, nil
316+
}
317+
318+
func expandPubsubSchemaName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
319+
return GetResourceNameFromSelfLink(v.(string)), nil
320+
}

0 commit comments

Comments
 (0)