forked from hashicorp/terraform-provider-google-beta
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresource_workbench_instance.go
2333 lines (2078 loc) · 85.7 KB
/
resource_workbench_instance.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: MMv1 ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file in
// .github/CONTRIBUTING.md.
//
// ----------------------------------------------------------------------------
package workbench
import (
"fmt"
"log"
"net/http"
"reflect"
"sort"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/verify"
)
var WorkbenchInstanceProvidedLabels = []string{
"consumer-project-id",
"consumer-project-number",
"notebooks-product",
"resource-name",
}
func WorkbenchInstanceLabelsDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
// Suppress diffs for the labels
for _, label := range WorkbenchInstanceProvidedLabels {
if strings.Contains(k, label) && new == "" {
return true
}
}
// Let diff be determined by labels (above)
if strings.Contains(k, "labels.%") {
return true
}
// For other keys, don't suppress diff.
return false
}
var WorkbenchInstanceProvidedMetadata = []string{
"agent-health-check-interval-seconds",
"agent-health-check-path",
"container",
"cos-update-strategy",
"custom-container-image",
"custom-container-payload",
"data-disk-uri",
"dataproc-allow-custom-clusters",
"dataproc-cluster-name",
"dataproc-configs",
"dataproc-default-subnet",
"dataproc-locations-list",
"dataproc-machine-types-list",
"dataproc-notebooks-url",
"dataproc-region",
"dataproc-service-account",
"disable-check-xsrf",
"framework",
"generate-diagnostics-bucket",
"generate-diagnostics-file",
"generate-diagnostics-options",
"google-logging-enabled",
"image-url",
"install-monitoring-agent",
"install-nvidia-driver",
"installed-extensions",
"last_updated_diagnostics",
"notebooks-api",
"notebooks-api-version",
"notebooks-examples-location",
"notebooks-location",
"proxy-backend-id",
"proxy-byoid-url",
"proxy-mode",
"proxy-status",
"proxy-url",
"proxy-user-mail",
"report-container-health",
"report-event-url",
"report-notebook-metrics",
"report-system-health",
"report-system-status",
"resource-url",
"restriction",
"serial-port-logging-enable",
"service-account-mode",
"shutdown-script",
"title",
"use-collaborative",
"user-data",
"version",
"disable-swap-binaries",
"enable-guest-attributes",
"enable-oslogin",
"proxy-registration-url",
}
func WorkbenchInstanceMetadataDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
// Suppress diffs for the Metadata
for _, metadata := range WorkbenchInstanceProvidedMetadata {
if strings.Contains(k, metadata) && new == "" {
return true
}
}
// Let diff be determined by metadata
if strings.Contains(k, "gce_setup.0.metadata.%") {
return true
}
// For other keys, don't suppress diff.
return false
}
var WorkbenchInstanceProvidedTags = []string{
"deeplearning-vm",
"notebook-instance",
}
func WorkbenchInstanceTagsDiffSuppress(_, _, _ string, d *schema.ResourceData) bool {
old, new := d.GetChange("gce_setup.0.tags")
oldValue := old.([]interface{})
newValue := new.([]interface{})
oldValueList := []string{}
newValueList := []string{}
for _, item := range oldValue {
oldValueList = append(oldValueList, item.(string))
}
for _, item := range newValue {
newValueList = append(newValueList, item.(string))
}
newValueList = append(newValueList, WorkbenchInstanceProvidedTags...)
sort.Strings(oldValueList)
sort.Strings(newValueList)
if reflect.DeepEqual(oldValueList, newValueList) {
return true
}
return false
}
func WorkbenchInstanceAcceleratorDiffSuppress(_, _, _ string, d *schema.ResourceData) bool {
old, new := d.GetChange("gce_setup.0.accelerator_configs")
oldInterface := old.([]interface{})
newInterface := new.([]interface{})
if len(oldInterface) == 0 && len(newInterface) == 1 && newInterface[0] == nil {
return true
}
return false
}
// waitForWorkbenchInstanceActive waits for an workbench instance to become "ACTIVE"
func waitForWorkbenchInstanceActive(d *schema.ResourceData, config *transport_tpg.Config, timeout time.Duration) error {
return retry.Retry(timeout, func() *retry.RetryError {
if err := resourceWorkbenchInstanceRead(d, config); err != nil {
return retry.NonRetryableError(err)
}
name := d.Get("name").(string)
state := d.Get("state").(string)
if state == "ACTIVE" {
log.Printf("[DEBUG] Workbench Instance %q has state %q.", name, state)
return nil
} else {
return retry.RetryableError(fmt.Errorf("Workbench Instance %q has state %q. Waiting for ACTIVE state", name, state))
}
})
}
func modifyWorkbenchInstanceState(config *transport_tpg.Config, d *schema.ResourceData, project string, billingProject string, userAgent string, state string) (map[string]interface{}, error) {
url, err := tpgresource.ReplaceVars(d, config, "{{WorkbenchBasePath}}projects/{{project}}/locations/{{location}}/instances/{{name}}:"+state)
if err != nil {
return nil, err
}
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "POST",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
})
if err != nil {
return nil, fmt.Errorf("Unable to %q google_workbench_instance %q: %s", state, d.Id(), err)
}
return res, nil
}
func WorkbenchInstanceKmsDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
if strings.HasPrefix(old, new) {
return true
}
return false
}
func waitForWorkbenchOperation(config *transport_tpg.Config, d *schema.ResourceData, project string, billingProject string, userAgent string, response map[string]interface{}) error {
var opRes map[string]interface{}
err := WorkbenchOperationWaitTimeWithResponse(
config, response, &opRes, project, "Modifying Workbench Instance state", userAgent,
d.Timeout(schema.TimeoutUpdate))
if err != nil {
return err
}
return nil
}
func resizeWorkbenchInstanceDisk(config *transport_tpg.Config, d *schema.ResourceData, project string, userAgent string, isBoot bool) error {
diskObj := make(map[string]interface{})
var sizeString string
var diskKey string
if isBoot {
sizeString = "gce_setup.0.boot_disk.0.disk_size_gb"
diskKey = "bootDisk"
} else {
sizeString = "gce_setup.0.data_disks.0.disk_size_gb"
diskKey = "dataDisk"
}
disk := make(map[string]interface{})
disk["diskSizeGb"] = d.Get(sizeString)
diskObj[diskKey] = disk
resizeUrl, err := tpgresource.ReplaceVars(d, config, "{{WorkbenchBasePath}}projects/{{project}}/locations/{{location}}/instances/{{name}}:resizeDisk")
if err != nil {
return err
}
dRes, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "POST",
RawURL: resizeUrl,
UserAgent: userAgent,
Body: diskObj,
Timeout: d.Timeout(schema.TimeoutUpdate),
})
if err != nil {
return fmt.Errorf("Error resizing disk: %s", err)
}
var opRes map[string]interface{}
err = WorkbenchOperationWaitTimeWithResponse(
config, dRes, &opRes, project, "Resizing disk", userAgent,
d.Timeout(schema.TimeoutUpdate))
if err != nil {
return fmt.Errorf("Error resizing disk: %s", err)
}
return nil
}
// mergeMaps takes two maps and returns a new map with the merged results.
// If a key exists in oldMap but not in newMap, it is added to the new map with an empty value.
func mergeMaps(oldMap, newMap map[string]interface{}) map[string]string {
modifiedMap := make(map[string]string)
// Add all key-values from newMap to modifiedMap
for k, v := range newMap {
modifiedMap[k] = v.(string)
}
// Add any keys from oldMap that are not in newMap with an empty value
for k := range oldMap {
if _, ok := newMap[k]; !ok {
modifiedMap[k] = ""
}
}
return modifiedMap
}
func ResourceWorkbenchInstance() *schema.Resource {
return &schema.Resource{
Create: resourceWorkbenchInstanceCreate,
Read: resourceWorkbenchInstanceRead,
Update: resourceWorkbenchInstanceUpdate,
Delete: resourceWorkbenchInstanceDelete,
Importer: &schema.ResourceImporter{
State: resourceWorkbenchInstanceImport,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
Update: schema.DefaultTimeout(20 * time.Minute),
Delete: schema.DefaultTimeout(20 * time.Minute),
},
CustomizeDiff: customdiff.All(
tpgresource.SetLabelsDiff,
tpgresource.DefaultProviderProject,
),
Schema: map[string]*schema.Schema{
"location": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Part of 'parent'. See documentation of 'projectsId'.`,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The name of this workbench instance. Format: 'projects/{project_id}/locations/{location}/instances/{instance_id}'`,
},
"disable_proxy_access": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `Optional. If true, the workbench instance will not register with the proxy.`,
},
"enable_third_party_identity": {
Type: schema.TypeBool,
Optional: true,
Description: `Flag that specifies that a notebook can be accessed with third party
identity provider.`,
},
"gce_setup": {
Type: schema.TypeList,
Computed: true,
Optional: true,
Description: `The definition of how to configure a VM instance outside of Resources and Identity.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"accelerator_configs": {
Type: schema.TypeList,
Optional: true,
DiffSuppressFunc: WorkbenchInstanceAcceleratorDiffSuppress,
Description: `The hardware accelerators used on this instance. If you use accelerators, make sure that your configuration has
[enough vCPUs and memory to support the 'machine_type' you have selected](https://cloud.google.com/compute/docs/gpus/#gpus-list).
Currently supports only one accelerator configuration.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"core_count": {
Type: schema.TypeString,
Optional: true,
Description: `Optional. Count of cores of this accelerator.`,
},
"type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: verify.ValidateEnum([]string{"NVIDIA_TESLA_P100", "NVIDIA_TESLA_V100", "NVIDIA_TESLA_P4", "NVIDIA_TESLA_T4", "NVIDIA_TESLA_A100", "NVIDIA_A100_80GB", "NVIDIA_L4", "NVIDIA_TESLA_T4_VWS", "NVIDIA_TESLA_P100_VWS", "NVIDIA_TESLA_P4_VWS", ""}),
Description: `Optional. Type of this accelerator. Possible values: ["NVIDIA_TESLA_P100", "NVIDIA_TESLA_V100", "NVIDIA_TESLA_P4", "NVIDIA_TESLA_T4", "NVIDIA_TESLA_A100", "NVIDIA_A100_80GB", "NVIDIA_L4", "NVIDIA_TESLA_T4_VWS", "NVIDIA_TESLA_P100_VWS", "NVIDIA_TESLA_P4_VWS"]`,
},
},
},
},
"boot_disk": {
Type: schema.TypeList,
Computed: true,
Optional: true,
Description: `The definition of a boot disk.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"disk_encryption": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
ValidateFunc: verify.ValidateEnum([]string{"GMEK", "CMEK", ""}),
Description: `Optional. Input only. Disk encryption method used on the boot and
data disks, defaults to GMEK. Possible values: ["GMEK", "CMEK"]`,
},
"disk_size_gb": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: `Optional. The size of the boot disk in GB attached to this instance,
up to a maximum of 64000 GB (64 TB). If not specified, this defaults to the
recommended value of 150GB.`,
},
"disk_type": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
ValidateFunc: verify.ValidateEnum([]string{"PD_STANDARD", "PD_SSD", "PD_BALANCED", "PD_EXTREME", ""}),
Description: `Optional. Indicates the type of the disk. Possible values: ["PD_STANDARD", "PD_SSD", "PD_BALANCED", "PD_EXTREME"]`,
},
"kms_key": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
DiffSuppressFunc: WorkbenchInstanceKmsDiffSuppress,
Description: `'Optional. The KMS key used to encrypt the disks, only
applicable if disk_encryption is CMEK. Format: 'projects/{project_id}/locations/{location}/keyRings/{key_ring_id}/cryptoKeys/{key_id}'
Learn more about using your own encryption keys.'`,
},
},
},
},
"container_image": {
Type: schema.TypeList,
Optional: true,
Description: `Use a container image to start the workbench instance.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
Description: `The path to the container image repository.
For example: gcr.io/{project_id}/{imageName}`,
},
"tag": {
Type: schema.TypeString,
Optional: true,
Description: `The tag of the container image. If not specified, this defaults to the latest tag.`,
},
},
},
ConflictsWith: []string{"gce_setup.0.vm_image"},
},
"data_disks": {
Type: schema.TypeList,
Computed: true,
Optional: true,
Description: `Data disks attached to the VM instance. Currently supports only one data disk.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"disk_encryption": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
ValidateFunc: verify.ValidateEnum([]string{"GMEK", "CMEK", ""}),
Description: `Optional. Input only. Disk encryption method used on the boot
and data disks, defaults to GMEK. Possible values: ["GMEK", "CMEK"]`,
},
"disk_size_gb": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: `Optional. The size of the disk in GB attached to this VM instance,
up to a maximum of 64000 GB (64 TB). If not specified, this defaults to
100.`,
},
"disk_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: verify.ValidateEnum([]string{"PD_STANDARD", "PD_SSD", "PD_BALANCED", "PD_EXTREME", ""}),
Description: `Optional. Input only. Indicates the type of the disk. Possible values: ["PD_STANDARD", "PD_SSD", "PD_BALANCED", "PD_EXTREME"]`,
},
"kms_key": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
DiffSuppressFunc: WorkbenchInstanceKmsDiffSuppress,
Description: `'Optional. The KMS key used to encrypt the disks,
only applicable if disk_encryption is CMEK. Format: 'projects/{project_id}/locations/{location}/keyRings/{key_ring_id}/cryptoKeys/{key_id}'
Learn more about using your own encryption keys.'`,
},
},
},
},
"disable_public_ip": {
Type: schema.TypeBool,
Computed: true,
Optional: true,
ForceNew: true,
Description: `Optional. If true, no external IP will be assigned to this VM instance.`,
},
"enable_ip_forwarding": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `Optional. Flag to enable ip forwarding or not, default false/off.
https://cloud.google.com/vpc/docs/using-routes#canipforward`,
},
"machine_type": {
Type: schema.TypeString,
Computed: true,
Optional: true,
DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName,
Description: `Optional. The machine type of the VM instance. https://cloud.google.com/compute/docs/machine-resource`,
},
"metadata": {
Type: schema.TypeMap,
Computed: true,
Optional: true,
DiffSuppressFunc: WorkbenchInstanceMetadataDiffSuppress,
Description: `Optional. Custom metadata to apply to this instance.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"network_interfaces": {
Type: schema.TypeList,
Computed: true,
Optional: true,
ForceNew: true,
Description: `The network interfaces for the VM. Supports only one interface.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"access_configs": {
Type: schema.TypeList,
Computed: true,
Optional: true,
ForceNew: true,
Description: `Optional. An array of configurations for this interface. Currently, only one access
config, ONE_TO_ONE_NAT, is supported. If no accessConfigs specified, the
instance will have an external internet access through an ephemeral
external IP address.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"external_ip": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `An external IP address associated with this instance. Specify an unused
static external IP address available to the project or leave this field
undefined to use an IP from a shared ephemeral IP address pool. If you
specify a static external IP address, it must live in the same region as
the zone of the instance.`,
},
},
},
},
"network": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
DiffSuppressFunc: tpgresource.CompareSelfLinkRelativePaths,
Description: `Optional. The name of the VPC that this VM instance is in.`,
},
"nic_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: verify.ValidateEnum([]string{"VIRTIO_NET", "GVNIC", ""}),
Description: `Optional. The type of vNIC to be used on this interface. This
may be gVNIC or VirtioNet. Possible values: ["VIRTIO_NET", "GVNIC"]`,
},
"subnet": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
DiffSuppressFunc: tpgresource.CompareSelfLinkRelativePaths,
Description: `Optional. The name of the subnet that this VM instance is in.`,
},
},
},
},
"service_accounts": {
Type: schema.TypeList,
Computed: true,
Optional: true,
ForceNew: true,
Description: `The service account that serves as an identity for the VM instance. Currently supports only one service account.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"email": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
Description: `Optional. Email address of the service account.`,
},
"scopes": {
Type: schema.TypeList,
Computed: true,
Description: `Output only. The list of scopes to be made available for this
service account. Set by the CLH to https://www.googleapis.com/auth/cloud-platform`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"shielded_instance_config": {
Type: schema.TypeList,
Computed: true,
Optional: true,
Description: `A set of Shielded Instance options. See [Images using supported Shielded
VM features](https://cloud.google.com/compute/docs/instances/modifying-shielded-vm).
Not all combinations are valid.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enable_integrity_monitoring": {
Type: schema.TypeBool,
Optional: true,
Description: `Optional. Defines whether the VM instance has integrity monitoring
enabled. Enables monitoring and attestation of the boot integrity of the VM
instance. The attestation is performed against the integrity policy baseline.
This baseline is initially derived from the implicitly trusted boot image
when the VM instance is created. Enabled by default.`,
},
"enable_secure_boot": {
Type: schema.TypeBool,
Optional: true,
Description: `Optional. Defines whether the VM instance has Secure Boot enabled.
Secure Boot helps ensure that the system only runs authentic software by verifying
the digital signature of all boot components, and halting the boot process
if signature verification fails. Disabled by default.`,
},
"enable_vtpm": {
Type: schema.TypeBool,
Optional: true,
Description: `Optional. Defines whether the VM instance has the vTPM enabled.
Enabled by default.`,
},
},
},
},
"tags": {
Type: schema.TypeList,
Computed: true,
Optional: true,
ForceNew: true,
DiffSuppressFunc: WorkbenchInstanceTagsDiffSuppress,
Description: `Optional. The Compute Engine tags to add to instance (see [Tagging
instances](https://cloud.google.com/compute/docs/label-or-tag-resources#tags)).`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"vm_image": {
Type: schema.TypeList,
Computed: true,
Optional: true,
ForceNew: true,
Description: `Definition of a custom Compute Engine virtual machine image for starting
a workbench instance with the environment installed directly on the VM.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"family": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `Optional. Use this VM image family to find the image; the newest
image in this family will be used.`,
},
"name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `Optional. Use VM image name to find the image.`,
},
"project": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The name of the Google Cloud project that this VM image belongs to.
Format: {project_id}`,
},
},
},
ConflictsWith: []string{"gce_setup.0.container_image"},
},
},
},
},
"instance_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `Required. User-defined unique ID of this instance.`,
},
"instance_owners": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `'Optional. Input only. The owner of this instance after creation. Format:
'[email protected]' Currently supports one owner only. If not specified, all of
the service account users of your VM instance''s service account can use the instance.
If specified, sets the access mode to 'Single user'. For more details, see
https://cloud.google.com/vertex-ai/docs/workbench/instances/manage-access-jupyterlab'`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"labels": {
Type: schema.TypeMap,
Optional: true,
DiffSuppressFunc: WorkbenchInstanceLabelsDiffSuppress,
Description: `Optional. Labels to apply to this instance. These can be later modified
by the UpdateInstance method.
**Note**: This field is non-authoritative, and will only manage the labels present in your configuration.
Please refer to the field 'effective_labels' for all of the labels present on the resource.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Description: `An RFC3339 timestamp in UTC time. This in the format of yyyy-MM-ddTHH:mm:ss.SSSZ.
The milliseconds portion (".SSS") is optional.`,
},
"creator": {
Type: schema.TypeString,
Computed: true,
Description: `Output only. Email address of entity that sent original CreateInstance request.`,
},
"effective_labels": {
Type: schema.TypeMap,
Computed: true,
Description: `All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"health_info": {
Type: schema.TypeList,
Computed: true,
Description: `'Output only. Additional information about instance health. Example:
healthInfo": { "docker_proxy_agent_status": "1", "docker_status": "1", "jupyterlab_api_status":
"-1", "jupyterlab_status": "-1", "updated": "2020-10-18 09:40:03.573409" }'`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{},
},
},
"health_state": {
Type: schema.TypeString,
Computed: true,
Description: `Output only. Instance health_state.`,
},
"proxy_uri": {
Type: schema.TypeString,
Computed: true,
Description: `Output only. The proxy endpoint that is used to access the Jupyter notebook.`,
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: `Output only. The state of this instance.`,
},
"terraform_labels": {
Type: schema.TypeMap,
Computed: true,
Description: `The combination of labels configured directly on the resource
and default labels configured on the provider.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"update_time": {
Type: schema.TypeString,
Computed: true,
Description: `An RFC3339 timestamp in UTC time. This in the format of yyyy-MM-ddTHH:mm:ss.SSSZ.
The milliseconds portion (".SSS") is optional.`,
},
"upgrade_history": {
Type: schema.TypeList,
Computed: true,
Description: `Output only. The upgrade history of this instance.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"action": {
Type: schema.TypeString,
Optional: true,
Description: `Optional. Action. Rolloback or Upgrade.`,
},
"container_image": {
Type: schema.TypeString,
Optional: true,
Description: `Optional. The container image before this instance upgrade.`,
},
"create_time": {
Type: schema.TypeString,
Optional: true,
Description: `An RFC3339 timestamp in UTC time. This in the format of yyyy-MM-ddTHH:mm:ss.SSSZ.
The milliseconds portion (".SSS") is optional.`,
},
"framework": {
Type: schema.TypeString,
Optional: true,
Description: `Optional. The framework of this workbench instance.`,
},
"snapshot": {
Type: schema.TypeString,
Optional: true,
Description: `Optional. The snapshot of the boot disk of this workbench instance before upgrade.`,
},
"target_version": {
Type: schema.TypeString,
Optional: true,
Description: `Optional. Target VM Version, like m63.`,
},
"version": {
Type: schema.TypeString,
Optional: true,
Description: `Optional. The version of the workbench instance before this upgrade.`,
},
"vm_image": {
Type: schema.TypeString,
Optional: true,
Description: `Optional. The VM image before this instance upgrade.`,
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: `Output only. The state of this instance upgrade history entry.`,
},
},
},
},
"desired_state": {
Type: schema.TypeString,
Optional: true,
Description: `Desired state of the Workbench Instance. Set this field to 'ACTIVE' to start the Instance, and 'STOPPED' to stop the Instance.`,
Default: "ACTIVE",
},
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
UseJSONNumber: true,
}
}
func resourceWorkbenchInstanceCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}
obj := make(map[string]interface{})
gceSetupProp, err := expandWorkbenchInstanceGceSetup(d.Get("gce_setup"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("gce_setup"); !tpgresource.IsEmptyValue(reflect.ValueOf(gceSetupProp)) && (ok || !reflect.DeepEqual(v, gceSetupProp)) {
obj["gceSetup"] = gceSetupProp
}
instanceOwnersProp, err := expandWorkbenchInstanceInstanceOwners(d.Get("instance_owners"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("instance_owners"); !tpgresource.IsEmptyValue(reflect.ValueOf(instanceOwnersProp)) && (ok || !reflect.DeepEqual(v, instanceOwnersProp)) {
obj["instanceOwners"] = instanceOwnersProp
}
disableProxyAccessProp, err := expandWorkbenchInstanceDisableProxyAccess(d.Get("disable_proxy_access"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("disable_proxy_access"); !tpgresource.IsEmptyValue(reflect.ValueOf(disableProxyAccessProp)) && (ok || !reflect.DeepEqual(v, disableProxyAccessProp)) {
obj["disableProxyAccess"] = disableProxyAccessProp
}
enableThirdPartyIdentityProp, err := expandWorkbenchInstanceEnableThirdPartyIdentity(d.Get("enable_third_party_identity"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("enable_third_party_identity"); !tpgresource.IsEmptyValue(reflect.ValueOf(enableThirdPartyIdentityProp)) && (ok || !reflect.DeepEqual(v, enableThirdPartyIdentityProp)) {
obj["enableThirdPartyIdentity"] = enableThirdPartyIdentityProp
}
labelsProp, err := expandWorkbenchInstanceEffectiveLabels(d.Get("effective_labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("effective_labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}
url, err := tpgresource.ReplaceVars(d, config, "{{WorkbenchBasePath}}projects/{{project}}/locations/{{location}}/instances?instanceId={{name}}")
if err != nil {
return err
}
log.Printf("[DEBUG] Creating new Instance: %#v", obj)
billingProject := ""
project, err := tpgresource.GetProject(d, config)
if err != nil {
return fmt.Errorf("Error fetching project for Instance: %s", err)
}
billingProject = project
// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}
headers := make(http.Header)
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "POST",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutCreate),
Headers: headers,
})
if err != nil {
return fmt.Errorf("Error creating Instance: %s", err)
}
// Store the ID now
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/instances/{{name}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)
// Use the resource in the operation response to populate
// identity fields and d.Id() before read
var opRes map[string]interface{}
err = WorkbenchOperationWaitTimeWithResponse(
config, res, &opRes, project, "Creating Instance", userAgent,
d.Timeout(schema.TimeoutCreate))
if err != nil {
// The resource didn't actually create
d.SetId("")
return fmt.Errorf("Error waiting to create Instance: %s", err)
}
// This may have caused the ID to update - update it if so.
id, err = tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/instances/{{name}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)
if err := waitForWorkbenchInstanceActive(d, config, d.Timeout(schema.TimeoutCreate)-time.Minute); err != nil {
return fmt.Errorf("Workbench instance %q did not reach ACTIVE state: %q", d.Get("name").(string), err)
}
if p, ok := d.GetOk("desired_state"); ok && p.(string) == "STOPPED" {
dRes, err := modifyWorkbenchInstanceState(config, d, project, billingProject, userAgent, "stop")
if err != nil {
return err
}
if err := waitForWorkbenchOperation(config, d, project, billingProject, userAgent, dRes); err != nil {
return fmt.Errorf("Error stopping Workbench Instance: %s", err)
}
}
log.Printf("[DEBUG] Finished creating Instance %q: %#v", d.Id(), res)
return resourceWorkbenchInstanceRead(d, meta)
}
func resourceWorkbenchInstanceRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}
url, err := tpgresource.ReplaceVars(d, config, "{{WorkbenchBasePath}}projects/{{project}}/locations/{{location}}/instances/{{name}}")
if err != nil {
return err
}
billingProject := ""
project, err := tpgresource.GetProject(d, config)
if err != nil {
return fmt.Errorf("Error fetching project for Instance: %s", err)
}
billingProject = project
// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}
headers := make(http.Header)
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Headers: headers,
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("WorkbenchInstance %q", d.Id()))
}
// Explicitly set virtual fields to default values if unset