Skip to content

Commit ed9ef83

Browse files
BigqueryReservation Assignment (#5748) (#11215)
Co-authored-by: Stephen Lewis <[email protected]> Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Stephen Lewis <[email protected]>
1 parent d646bad commit ed9ef83

11 files changed

+594
-63
lines changed

.changelog/5748.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
`google_bigqueryreservation_assignment`
3+
```

google/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
11481148
map[string]*schema.Resource{
11491149
"google_app_engine_application": resourceAppEngineApplication(),
11501150
"google_bigquery_table": resourceBigQueryTable(),
1151+
"google_bigquery_reservation_assignment": resourceBigqueryReservationAssignment(),
11511152
"google_bigtable_gc_policy": resourceBigtableGCPolicy(),
11521153
"google_bigtable_instance": resourceBigtableInstance(),
11531154
"google_bigtable_table": resourceBigtableTable(),

google/provider_dcl_client_creation.go

+24
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"time"
2121

2222
assuredworkloads "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/assuredworkloads"
23+
bigqueryreservation "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/bigqueryreservation"
2324
cloudbuild "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/cloudbuild"
2425
cloudresourcemanager "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/cloudresourcemanager"
2526
compute "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/compute"
@@ -57,6 +58,29 @@ func NewDCLAssuredWorkloadsClient(config *Config, userAgent, billingProject stri
5758
return assuredworkloads.NewClient(dclConfig)
5859
}
5960

61+
func NewDCLBigqueryReservationClient(config *Config, userAgent, billingProject string, timeout time.Duration) *bigqueryreservation.Client {
62+
configOptions := []dcl.ConfigOption{
63+
dcl.WithHTTPClient(config.client),
64+
dcl.WithUserAgent(userAgent),
65+
dcl.WithLogger(dclLogger{}),
66+
dcl.WithBasePath(config.BigqueryReservationBasePath),
67+
}
68+
69+
if timeout != 0 {
70+
configOptions = append(configOptions, dcl.WithTimeout(timeout))
71+
}
72+
73+
if config.UserProjectOverride {
74+
configOptions = append(configOptions, dcl.WithUserProjectOverride())
75+
if billingProject != "" {
76+
configOptions = append(configOptions, dcl.WithBillingProject(billingProject))
77+
}
78+
}
79+
80+
dclConfig := dcl.NewConfig(configOptions...)
81+
return bigqueryreservation.NewClient(dclConfig)
82+
}
83+
6084
func NewDCLCloudbuildClient(config *Config, userAgent, billingProject string, timeout time.Duration) *cloudbuild.Client {
6185
configOptions := []dcl.ConfigOption{
6286
dcl.WithHTTPClient(config.client),

google/resource_assured_workloads_workload.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,6 @@ func resourceAssuredWorkloadsWorkloadCreate(d *schema.ResourceData, meta interfa
239239
return fmt.Errorf("Error creating Workload: %s", err)
240240
}
241241

242-
log.Printf("[DEBUG] Finished creating Workload %q: %#v", d.Id(), res)
243-
244242
if err = d.Set("name", res.Name); err != nil {
245243
return fmt.Errorf("error setting name in state: %s", err)
246244
}
@@ -251,6 +249,8 @@ func resourceAssuredWorkloadsWorkloadCreate(d *schema.ResourceData, meta interfa
251249
}
252250
d.SetId(id)
253251

252+
log.Printf("[DEBUG] Finished creating Workload %q: %#v", d.Id(), res)
253+
254254
return resourceAssuredWorkloadsWorkloadRead(d, meta)
255255
}
256256

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
// ----------------------------------------------------------------------------
2+
//
3+
// *** AUTO GENERATED CODE *** Type: DCL ***
4+
//
5+
// ----------------------------------------------------------------------------
6+
//
7+
// This file is managed by Magic Modules (https://github.com/GoogleCloudPlatform/magic-modules)
8+
// and is based on the DCL (https://github.com/GoogleCloudPlatform/declarative-resource-client-library).
9+
// Changes will need to be made to the DCL or Magic Modules instead of here.
10+
//
11+
// We are not currently able to accept contributions to this file. If changes
12+
// are required, please file an issue at https://github.com/hashicorp/terraform-provider-google/issues/new/choose
13+
//
14+
// ----------------------------------------------------------------------------
15+
16+
package google
17+
18+
import (
19+
"context"
20+
"fmt"
21+
"log"
22+
"time"
23+
24+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
25+
26+
dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
27+
bigqueryreservation "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/bigqueryreservation"
28+
)
29+
30+
func resourceBigqueryReservationAssignment() *schema.Resource {
31+
return &schema.Resource{
32+
Create: resourceBigqueryReservationAssignmentCreate,
33+
Read: resourceBigqueryReservationAssignmentRead,
34+
Delete: resourceBigqueryReservationAssignmentDelete,
35+
36+
Importer: &schema.ResourceImporter{
37+
State: resourceBigqueryReservationAssignmentImport,
38+
},
39+
40+
Timeouts: &schema.ResourceTimeout{
41+
Create: schema.DefaultTimeout(20 * time.Minute),
42+
Delete: schema.DefaultTimeout(20 * time.Minute),
43+
},
44+
45+
Schema: map[string]*schema.Schema{
46+
"assignee": {
47+
Type: schema.TypeString,
48+
Required: true,
49+
ForceNew: true,
50+
DiffSuppressFunc: compareSelfLinkOrResourceName,
51+
Description: "The resource which will use the reservation. E.g. projects/myproject, folders/123, organizations/456.",
52+
},
53+
54+
"job_type": {
55+
Type: schema.TypeString,
56+
Required: true,
57+
ForceNew: true,
58+
Description: "Types of job, which could be specified when using the reservation. Possible values: JOB_TYPE_UNSPECIFIED, PIPELINE, QUERY",
59+
},
60+
61+
"reservation": {
62+
Type: schema.TypeString,
63+
Required: true,
64+
ForceNew: true,
65+
DiffSuppressFunc: compareSelfLinkOrResourceName,
66+
Description: "The reservation for the resource",
67+
},
68+
69+
"location": {
70+
Type: schema.TypeString,
71+
Computed: true,
72+
Optional: true,
73+
ForceNew: true,
74+
Description: "The location for the resource",
75+
},
76+
77+
"project": {
78+
Type: schema.TypeString,
79+
Computed: true,
80+
Optional: true,
81+
ForceNew: true,
82+
DiffSuppressFunc: compareSelfLinkOrResourceName,
83+
Description: "The project for the resource",
84+
},
85+
86+
"name": {
87+
Type: schema.TypeString,
88+
Computed: true,
89+
Description: "Output only. The resource name of the assignment.",
90+
},
91+
92+
"state": {
93+
Type: schema.TypeString,
94+
Computed: true,
95+
Description: "Assignment will remain in PENDING state if no active capacity commitment is present. It will become ACTIVE when some capacity commitment becomes active. Possible values: STATE_UNSPECIFIED, PENDING, ACTIVE",
96+
},
97+
},
98+
}
99+
}
100+
101+
func resourceBigqueryReservationAssignmentCreate(d *schema.ResourceData, meta interface{}) error {
102+
config := meta.(*Config)
103+
project, err := getProject(d, config)
104+
if err != nil {
105+
return err
106+
}
107+
108+
obj := &bigqueryreservation.Assignment{
109+
Assignee: dcl.String(d.Get("assignee").(string)),
110+
JobType: bigqueryreservation.AssignmentJobTypeEnumRef(d.Get("job_type").(string)),
111+
Reservation: dcl.String(d.Get("reservation").(string)),
112+
Location: dcl.StringOrNil(d.Get("location").(string)),
113+
Project: dcl.String(project),
114+
}
115+
116+
id, err := obj.ID()
117+
if err != nil {
118+
return fmt.Errorf("error constructing id: %s", err)
119+
}
120+
d.SetId(id)
121+
createDirective := CreateDirective
122+
userAgent, err := generateUserAgentString(d, config.userAgent)
123+
if err != nil {
124+
return err
125+
}
126+
billingProject := project
127+
// err == nil indicates that the billing_project value was found
128+
if bp, err := getBillingProject(d, config); err == nil {
129+
billingProject = bp
130+
}
131+
client := NewDCLBigqueryReservationClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutCreate))
132+
if bp, err := replaceVars(d, config, client.Config.BasePath); err != nil {
133+
d.SetId("")
134+
return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err)
135+
} else {
136+
client.Config.BasePath = bp
137+
}
138+
res, err := client.ApplyAssignment(context.Background(), obj, createDirective...)
139+
140+
if _, ok := err.(dcl.DiffAfterApplyError); ok {
141+
log.Printf("[DEBUG] Diff after apply returned from the DCL: %s", err)
142+
} else if err != nil {
143+
// The resource didn't actually create
144+
d.SetId("")
145+
return fmt.Errorf("Error creating Assignment: %s", err)
146+
}
147+
148+
id, err = obj.ID()
149+
if err != nil {
150+
return fmt.Errorf("error constructing id: %s", err)
151+
}
152+
d.SetId(id)
153+
154+
log.Printf("[DEBUG] Finished creating Assignment %q: %#v", d.Id(), res)
155+
156+
return resourceBigqueryReservationAssignmentRead(d, meta)
157+
}
158+
159+
func resourceBigqueryReservationAssignmentRead(d *schema.ResourceData, meta interface{}) error {
160+
config := meta.(*Config)
161+
project, err := getProject(d, config)
162+
if err != nil {
163+
return err
164+
}
165+
166+
obj := &bigqueryreservation.Assignment{
167+
Assignee: dcl.String(d.Get("assignee").(string)),
168+
JobType: bigqueryreservation.AssignmentJobTypeEnumRef(d.Get("job_type").(string)),
169+
Reservation: dcl.String(d.Get("reservation").(string)),
170+
Location: dcl.StringOrNil(d.Get("location").(string)),
171+
Project: dcl.String(project),
172+
Name: dcl.StringOrNil(d.Get("name").(string)),
173+
}
174+
175+
userAgent, err := generateUserAgentString(d, config.userAgent)
176+
if err != nil {
177+
return err
178+
}
179+
billingProject := project
180+
// err == nil indicates that the billing_project value was found
181+
if bp, err := getBillingProject(d, config); err == nil {
182+
billingProject = bp
183+
}
184+
client := NewDCLBigqueryReservationClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutRead))
185+
if bp, err := replaceVars(d, config, client.Config.BasePath); err != nil {
186+
d.SetId("")
187+
return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err)
188+
} else {
189+
client.Config.BasePath = bp
190+
}
191+
res, err := client.GetAssignment(context.Background(), obj)
192+
if err != nil {
193+
resourceName := fmt.Sprintf("BigqueryReservationAssignment %q", d.Id())
194+
return handleNotFoundDCLError(err, d, resourceName)
195+
}
196+
197+
if err = d.Set("assignee", res.Assignee); err != nil {
198+
return fmt.Errorf("error setting assignee in state: %s", err)
199+
}
200+
if err = d.Set("job_type", res.JobType); err != nil {
201+
return fmt.Errorf("error setting job_type in state: %s", err)
202+
}
203+
if err = d.Set("reservation", res.Reservation); err != nil {
204+
return fmt.Errorf("error setting reservation in state: %s", err)
205+
}
206+
if err = d.Set("location", res.Location); err != nil {
207+
return fmt.Errorf("error setting location in state: %s", err)
208+
}
209+
if err = d.Set("project", res.Project); err != nil {
210+
return fmt.Errorf("error setting project in state: %s", err)
211+
}
212+
if err = d.Set("name", res.Name); err != nil {
213+
return fmt.Errorf("error setting name in state: %s", err)
214+
}
215+
if err = d.Set("state", res.State); err != nil {
216+
return fmt.Errorf("error setting state in state: %s", err)
217+
}
218+
219+
return nil
220+
}
221+
222+
func resourceBigqueryReservationAssignmentDelete(d *schema.ResourceData, meta interface{}) error {
223+
config := meta.(*Config)
224+
project, err := getProject(d, config)
225+
if err != nil {
226+
return err
227+
}
228+
229+
obj := &bigqueryreservation.Assignment{
230+
Assignee: dcl.String(d.Get("assignee").(string)),
231+
JobType: bigqueryreservation.AssignmentJobTypeEnumRef(d.Get("job_type").(string)),
232+
Reservation: dcl.String(d.Get("reservation").(string)),
233+
Location: dcl.StringOrNil(d.Get("location").(string)),
234+
Project: dcl.String(project),
235+
Name: dcl.StringOrNil(d.Get("name").(string)),
236+
}
237+
238+
log.Printf("[DEBUG] Deleting Assignment %q", d.Id())
239+
userAgent, err := generateUserAgentString(d, config.userAgent)
240+
if err != nil {
241+
return err
242+
}
243+
billingProject := project
244+
// err == nil indicates that the billing_project value was found
245+
if bp, err := getBillingProject(d, config); err == nil {
246+
billingProject = bp
247+
}
248+
client := NewDCLBigqueryReservationClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutDelete))
249+
if bp, err := replaceVars(d, config, client.Config.BasePath); err != nil {
250+
d.SetId("")
251+
return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err)
252+
} else {
253+
client.Config.BasePath = bp
254+
}
255+
if err := client.DeleteAssignment(context.Background(), obj); err != nil {
256+
return fmt.Errorf("Error deleting Assignment: %s", err)
257+
}
258+
259+
log.Printf("[DEBUG] Finished deleting Assignment %q", d.Id())
260+
return nil
261+
}
262+
263+
func resourceBigqueryReservationAssignmentImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
264+
config := meta.(*Config)
265+
if err := parseImportId([]string{
266+
"projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/reservations/(?P<reservation>[^/]+)/assignments/(?P<name>[^/]+)",
267+
"(?P<project>[^/]+)/(?P<location>[^/]+)/(?P<reservation>[^/]+)/(?P<name>[^/]+)",
268+
"(?P<location>[^/]+)/(?P<reservation>[^/]+)/(?P<name>[^/]+)",
269+
}, d, config); err != nil {
270+
return nil, err
271+
}
272+
273+
// Replace import id for the resource id
274+
id, err := replaceVarsForId(d, config, "projects/{{project}}/locations/{{location}}/reservations/{{reservation}}/assignments/{{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+
}

0 commit comments

Comments
 (0)