Skip to content

Commit e1fe2ff

Browse files
Add support for Logical Views (#13306) (#22499)
[upstream:3a4b9125332bc24ec450398a1f137cfb75477aa6] Signed-off-by: Modular Magician <[email protected]>
1 parent d8d1db3 commit e1fe2ff

7 files changed

+746
-2
lines changed

.changelog/13306.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+
`google_bigtable_logical_view`
3+
```

google/provider/provider_mmv1_resources.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,9 @@ var handwrittenIAMDatasources = map[string]*schema.Resource{
517517
}
518518

519519
// Resources
520-
// Generated resources: 591
520+
// Generated resources: 592
521521
// Generated IAM resources: 297
522-
// Total generated resources: 888
522+
// Total generated resources: 889
523523
var generatedResources = map[string]*schema.Resource{
524524
"google_folder_access_approval_settings": accessapproval.ResourceAccessApprovalFolderSettings(),
525525
"google_organization_access_approval_settings": accessapproval.ResourceAccessApprovalOrganizationSettings(),
@@ -638,6 +638,7 @@ var generatedResources = map[string]*schema.Resource{
638638
"google_bigquery_reservation": bigqueryreservation.ResourceBigqueryReservationReservation(),
639639
"google_bigquery_reservation_assignment": bigqueryreservation.ResourceBigqueryReservationReservationAssignment(),
640640
"google_bigtable_app_profile": bigtable.ResourceBigtableAppProfile(),
641+
"google_bigtable_logical_view": bigtable.ResourceBigtableLogicalView(),
641642
"google_bigtable_materialized_view": bigtable.ResourceBigtableMaterializedView(),
642643
"google_billing_budget": billing.ResourceBillingBudget(),
643644
"google_binary_authorization_attestor": binaryauthorization.ResourceBinaryAuthorizationAttestor(),
Lines changed: 355 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,355 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
// ----------------------------------------------------------------------------
5+
//
6+
// *** AUTO GENERATED CODE *** Type: MMv1 ***
7+
//
8+
// ----------------------------------------------------------------------------
9+
//
10+
// This code is generated by Magic Modules using the following:
11+
//
12+
// Configuration: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/products/bigtable/LogicalView.yaml
13+
// Template: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/templates/terraform/resource.go.tmpl
14+
//
15+
// DO NOT EDIT this file directly. Any changes made to this file will be
16+
// overwritten during the next generation cycle.
17+
//
18+
// ----------------------------------------------------------------------------
19+
20+
package bigtable
21+
22+
import (
23+
"fmt"
24+
"log"
25+
"net/http"
26+
"reflect"
27+
"strings"
28+
"time"
29+
30+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
31+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
32+
33+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
34+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
35+
)
36+
37+
func ResourceBigtableLogicalView() *schema.Resource {
38+
return &schema.Resource{
39+
Create: resourceBigtableLogicalViewCreate,
40+
Read: resourceBigtableLogicalViewRead,
41+
Update: resourceBigtableLogicalViewUpdate,
42+
Delete: resourceBigtableLogicalViewDelete,
43+
44+
Importer: &schema.ResourceImporter{
45+
State: resourceBigtableLogicalViewImport,
46+
},
47+
48+
Timeouts: &schema.ResourceTimeout{
49+
Create: schema.DefaultTimeout(120 * time.Minute),
50+
Update: schema.DefaultTimeout(20 * time.Minute),
51+
Delete: schema.DefaultTimeout(20 * time.Minute),
52+
},
53+
54+
CustomizeDiff: customdiff.All(
55+
tpgresource.DefaultProviderProject,
56+
),
57+
58+
Schema: map[string]*schema.Schema{
59+
"logical_view_id": {
60+
Type: schema.TypeString,
61+
Required: true,
62+
ForceNew: true,
63+
Description: `The unique name of the logical view in the form '[_a-zA-Z0-9][-_.a-zA-Z0-9]*'.`,
64+
},
65+
"query": {
66+
Type: schema.TypeString,
67+
Required: true,
68+
Description: `The logical view's select query.`,
69+
},
70+
"instance": {
71+
Type: schema.TypeString,
72+
Optional: true,
73+
ForceNew: true,
74+
DiffSuppressFunc: tpgresource.CompareResourceNames,
75+
Description: `The name of the instance to create the logical view within.`,
76+
},
77+
"name": {
78+
Type: schema.TypeString,
79+
Computed: true,
80+
Description: `The unique name of the requested logical view. Values are of the form 'projects/<project>/instances/<instance>/logicalViews/<logicalViewId>'.`,
81+
},
82+
"project": {
83+
Type: schema.TypeString,
84+
Optional: true,
85+
Computed: true,
86+
ForceNew: true,
87+
},
88+
},
89+
UseJSONNumber: true,
90+
}
91+
}
92+
93+
func resourceBigtableLogicalViewCreate(d *schema.ResourceData, meta interface{}) error {
94+
config := meta.(*transport_tpg.Config)
95+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
96+
if err != nil {
97+
return err
98+
}
99+
100+
obj := make(map[string]interface{})
101+
queryProp, err := expandBigtableLogicalViewQuery(d.Get("query"), d, config)
102+
if err != nil {
103+
return err
104+
} else if v, ok := d.GetOkExists("query"); !tpgresource.IsEmptyValue(reflect.ValueOf(queryProp)) && (ok || !reflect.DeepEqual(v, queryProp)) {
105+
obj["query"] = queryProp
106+
}
107+
108+
url, err := tpgresource.ReplaceVars(d, config, "{{BigtableBasePath}}projects/{{project}}/instances/{{instance}}/logicalViews?logicalViewId={{logical_view_id}}")
109+
if err != nil {
110+
return err
111+
}
112+
113+
log.Printf("[DEBUG] Creating new LogicalView: %#v", obj)
114+
billingProject := ""
115+
116+
project, err := tpgresource.GetProject(d, config)
117+
if err != nil {
118+
return fmt.Errorf("Error fetching project for LogicalView: %s", err)
119+
}
120+
billingProject = project
121+
122+
// err == nil indicates that the billing_project value was found
123+
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
124+
billingProject = bp
125+
}
126+
127+
headers := make(http.Header)
128+
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
129+
Config: config,
130+
Method: "POST",
131+
Project: billingProject,
132+
RawURL: url,
133+
UserAgent: userAgent,
134+
Body: obj,
135+
Timeout: d.Timeout(schema.TimeoutCreate),
136+
Headers: headers,
137+
})
138+
if err != nil {
139+
return fmt.Errorf("Error creating LogicalView: %s", err)
140+
}
141+
142+
// Store the ID now
143+
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/instances/{{instance}}/logicalViews/{{logical_view_id}}")
144+
if err != nil {
145+
return fmt.Errorf("Error constructing id: %s", err)
146+
}
147+
d.SetId(id)
148+
149+
log.Printf("[DEBUG] Finished creating LogicalView %q: %#v", d.Id(), res)
150+
151+
return resourceBigtableLogicalViewRead(d, meta)
152+
}
153+
154+
func resourceBigtableLogicalViewRead(d *schema.ResourceData, meta interface{}) error {
155+
config := meta.(*transport_tpg.Config)
156+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
157+
if err != nil {
158+
return err
159+
}
160+
161+
url, err := tpgresource.ReplaceVars(d, config, "{{BigtableBasePath}}projects/{{project}}/instances/{{instance}}/logicalViews/{{logical_view_id}}")
162+
if err != nil {
163+
return err
164+
}
165+
166+
billingProject := ""
167+
168+
project, err := tpgresource.GetProject(d, config)
169+
if err != nil {
170+
return fmt.Errorf("Error fetching project for LogicalView: %s", err)
171+
}
172+
billingProject = project
173+
174+
// err == nil indicates that the billing_project value was found
175+
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
176+
billingProject = bp
177+
}
178+
179+
headers := make(http.Header)
180+
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
181+
Config: config,
182+
Method: "GET",
183+
Project: billingProject,
184+
RawURL: url,
185+
UserAgent: userAgent,
186+
Headers: headers,
187+
})
188+
if err != nil {
189+
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("BigtableLogicalView %q", d.Id()))
190+
}
191+
192+
if err := d.Set("project", project); err != nil {
193+
return fmt.Errorf("Error reading LogicalView: %s", err)
194+
}
195+
196+
if err := d.Set("name", flattenBigtableLogicalViewName(res["name"], d, config)); err != nil {
197+
return fmt.Errorf("Error reading LogicalView: %s", err)
198+
}
199+
if err := d.Set("query", flattenBigtableLogicalViewQuery(res["query"], d, config)); err != nil {
200+
return fmt.Errorf("Error reading LogicalView: %s", err)
201+
}
202+
203+
return nil
204+
}
205+
206+
func resourceBigtableLogicalViewUpdate(d *schema.ResourceData, meta interface{}) error {
207+
config := meta.(*transport_tpg.Config)
208+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
209+
if err != nil {
210+
return err
211+
}
212+
213+
billingProject := ""
214+
215+
project, err := tpgresource.GetProject(d, config)
216+
if err != nil {
217+
return fmt.Errorf("Error fetching project for LogicalView: %s", err)
218+
}
219+
billingProject = project
220+
221+
obj := make(map[string]interface{})
222+
queryProp, err := expandBigtableLogicalViewQuery(d.Get("query"), d, config)
223+
if err != nil {
224+
return err
225+
} else if v, ok := d.GetOkExists("query"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, queryProp)) {
226+
obj["query"] = queryProp
227+
}
228+
229+
url, err := tpgresource.ReplaceVars(d, config, "{{BigtableBasePath}}projects/{{project}}/instances/{{instance}}/logicalViews/{{logical_view_id}}")
230+
if err != nil {
231+
return err
232+
}
233+
234+
log.Printf("[DEBUG] Updating LogicalView %q: %#v", d.Id(), obj)
235+
headers := make(http.Header)
236+
updateMask := []string{}
237+
238+
if d.HasChange("query") {
239+
updateMask = append(updateMask, "query")
240+
}
241+
// updateMask is a URL parameter but not present in the schema, so ReplaceVars
242+
// won't set it
243+
url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
244+
if err != nil {
245+
return err
246+
}
247+
248+
// err == nil indicates that the billing_project value was found
249+
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
250+
billingProject = bp
251+
}
252+
253+
// if updateMask is empty we are not updating anything so skip the post
254+
if len(updateMask) > 0 {
255+
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
256+
Config: config,
257+
Method: "PATCH",
258+
Project: billingProject,
259+
RawURL: url,
260+
UserAgent: userAgent,
261+
Body: obj,
262+
Timeout: d.Timeout(schema.TimeoutUpdate),
263+
Headers: headers,
264+
})
265+
266+
if err != nil {
267+
return fmt.Errorf("Error updating LogicalView %q: %s", d.Id(), err)
268+
} else {
269+
log.Printf("[DEBUG] Finished updating LogicalView %q: %#v", d.Id(), res)
270+
}
271+
272+
}
273+
274+
return resourceBigtableLogicalViewRead(d, meta)
275+
}
276+
277+
func resourceBigtableLogicalViewDelete(d *schema.ResourceData, meta interface{}) error {
278+
config := meta.(*transport_tpg.Config)
279+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
280+
if err != nil {
281+
return err
282+
}
283+
284+
billingProject := ""
285+
286+
project, err := tpgresource.GetProject(d, config)
287+
if err != nil {
288+
return fmt.Errorf("Error fetching project for LogicalView: %s", err)
289+
}
290+
billingProject = project
291+
292+
url, err := tpgresource.ReplaceVars(d, config, "{{BigtableBasePath}}projects/{{project}}/instances/{{instance}}/logicalViews/{{logical_view_id}}")
293+
if err != nil {
294+
return err
295+
}
296+
297+
var obj map[string]interface{}
298+
299+
// err == nil indicates that the billing_project value was found
300+
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
301+
billingProject = bp
302+
}
303+
304+
headers := make(http.Header)
305+
306+
log.Printf("[DEBUG] Deleting LogicalView %q", d.Id())
307+
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
308+
Config: config,
309+
Method: "DELETE",
310+
Project: billingProject,
311+
RawURL: url,
312+
UserAgent: userAgent,
313+
Body: obj,
314+
Timeout: d.Timeout(schema.TimeoutDelete),
315+
Headers: headers,
316+
})
317+
if err != nil {
318+
return transport_tpg.HandleNotFoundError(err, d, "LogicalView")
319+
}
320+
321+
log.Printf("[DEBUG] Finished deleting LogicalView %q: %#v", d.Id(), res)
322+
return nil
323+
}
324+
325+
func resourceBigtableLogicalViewImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
326+
config := meta.(*transport_tpg.Config)
327+
if err := tpgresource.ParseImportId([]string{
328+
"^projects/(?P<project>[^/]+)/instances/(?P<instance>[^/]+)/logicalViews/(?P<logical_view_id>[^/]+)$",
329+
"^(?P<project>[^/]+)/(?P<instance>[^/]+)/(?P<logical_view_id>[^/]+)$",
330+
"^(?P<instance>[^/]+)/(?P<logical_view_id>[^/]+)$",
331+
}, d, config); err != nil {
332+
return nil, err
333+
}
334+
335+
// Replace import id for the resource id
336+
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/instances/{{instance}}/logicalViews/{{logical_view_id}}")
337+
if err != nil {
338+
return nil, fmt.Errorf("Error constructing id: %s", err)
339+
}
340+
d.SetId(id)
341+
342+
return []*schema.ResourceData{d}, nil
343+
}
344+
345+
func flattenBigtableLogicalViewName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
346+
return v
347+
}
348+
349+
func flattenBigtableLogicalViewQuery(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
350+
return v
351+
}
352+
353+
func expandBigtableLogicalViewQuery(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
354+
return v, nil
355+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
resource: 'google_bigtable_logical_view'
2+
generation_type: 'mmv1'
3+
source_file: 'products/bigtable/LogicalView.yaml'
4+
api_service_name: 'bigtableadmin.googleapis.com'
5+
api_version: 'v2'
6+
api_resource_type_kind: 'LogicalView'
7+
fields:
8+
- field: 'instance'
9+
provider_only: true
10+
- field: 'logical_view_id'
11+
provider_only: true
12+
- field: 'name'
13+
- field: 'query'

0 commit comments

Comments
 (0)