Skip to content

Commit

Permalink
Add desired state to workbench instances. (GoogleCloudPlatform#9945)
Browse files Browse the repository at this point in the history
* add labels to runtime update test

* Add desired_state for workbench instances

* fix(tgc): Fixing the if block for existingConverterAsset and add test case (GoogleCloudPlatform#9914)

* Add sweepers for vertex_dataset, vertex_tensorboard and vertex_metadataStore (GoogleCloudPlatform#9911)

* Make acctest use bootstrapped KMS key, instead of making new key that resembles a shared key (GoogleCloudPlatform#9926)

* Refactor the TeamCity config to define all projects, across GA, Beta, and VCR testing (GoogleCloudPlatform#9837)

* Replace contents of .teamcity/ with new refactored config files

* Remove remaining automation for generating service lists automatically

* Remove copyright headers, as they're added during generation process

* Update header comments on .teamcity files

* Make VCR testing compatible with TeamCity agent

* Update GA + Beta service lists to match the currrent provider

* Fix whitespace

* Update default TEST ENV value for VCR builds

* Add chmod commands to handling of creds file

* Update pre- and post-VCR steps to echo out more information about their actions

* Rename `generated` folder to `inputs`

* Update header comments on .teamcity files

* Refactor how VCR recording buid configurations are made

* Fix comment on MM VCR build

* Update VCR post step to fail if no cassettes are produced by the tests before

* Whitespace changes

* Fix how teamcity files are copied to the downstreams

* Change how VCR build configs are named

* remove newlines

* Fix defect in post VCR build steps

* Refactor labelling that indicates automated vs ad hoc builds

* Make VCR builds be triggered to indicate VCR_MODE

* Add first version of TeamCity documentation!

* Add contents section to PERFORMING_TASKS_IN_TEAMCITY.md

* Move details about DSL into technical README, away from process doc

* Make the .teamcity folder only propogate to the TPG repo, and no longer be in the TPGB repo

* Ensure all TeamCity files are being copied to the downstream repo

* Move echo command to after `BRANCH_NAME` ENV declared

* Update VCR steps to look for fixtures files inside beta folder

* Update mmv1/third_party/terraform/.teamcity/components/builds/vcr_build_steps.kt

Co-authored-by: Shuya Ma <[email protected]>

* Update mmv1/third_party/terraform/.teamcity/CONTRIBUTION_GUIDE.md

Co-authored-by: Stephen Lewis (Burrows) <[email protected]>

---------

Co-authored-by: Shuya Ma <[email protected]>
Co-authored-by: Stephen Lewis (Burrows) <[email protected]>

* [#15779] Add google_network_security_firewall_endpoint resource (GoogleCloudPlatform#9813)

* [#15779] Add google_network_security_firewall_endpoint resource

* Fix yaml linting

* Removing unused fields from yaml

* Fixing tests

* Fixes per comments

---------

Co-authored-by: Luca Prete <[email protected]>

* enhancement: Allow overriding the Billing Project for google_cloud_asset_resources_search_all (GoogleCloudPlatform#9935)

* enhancement: Allow overriding the Billing Project for cloud_asset_resources_search_all

* chore: Fixed indentation

* Add firebaseappcheck to service package lists (GoogleCloudPlatform#9937)

* Enable CRONs in TeamCity (GoogleCloudPlatform#9938)

* Set CRONs for 4am UTC

* Enable CRONs

* Update enrolled_teams.yml (GoogleCloudPlatform#9939)

* add NickElliot to vacation reviewers (GoogleCloudPlatform#9942)

* Update membership.go

* Update membership.go

* fix lint issues

* fix conversion compilation

* Remove Runtime example

* Add back kms flatten

* change encryption to CMEK to test for KMS

* Try using a global KMS key

* kms key location to us-central1

* remove service account in full example

* Adding back service account into full test

---------

Co-authored-by: Iris Chen <[email protected]>
Co-authored-by: hao-nan-li <[email protected]>
Co-authored-by: Sarah French <[email protected]>
Co-authored-by: Shuya Ma <[email protected]>
Co-authored-by: Stephen Lewis (Burrows) <[email protected]>
Co-authored-by: Luca Prete <[email protected]>
Co-authored-by: Luca Prete <[email protected]>
Co-authored-by: Koen van Zuijlen <[email protected]>
Co-authored-by: Ryan Oaks <[email protected]>
Co-authored-by: Nick Elliot <[email protected]>
  • Loading branch information
11 people authored and balanaguharsha committed Apr 19, 2024
1 parent 787589a commit 81fa60a
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 129 deletions.
27 changes: 16 additions & 11 deletions mmv1/products/workbench/Instance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ examples:
ignore_read_extra:
- 'gce_setup.0.vm_image'
- !ruby/object:Provider::Terraform::Examples
name: 'workbench_instance_labels'
name: 'workbench_instance_labels_stopped'
primary_resource_id: 'instance'
primary_resource_name: "fmt.Sprintf(\"tf-test-workbench-instance%s\",
context[\"\
Expand All @@ -66,6 +66,8 @@ examples:
network_name: 'wbi-test-default'
test_env_vars:
service_account: :SERVICE_ACCT
ignore_read_extra:
- 'desired_state'
- !ruby/object:Provider::Terraform::Examples
name: 'workbench_instance_full'
primary_resource_id: 'instance'
Expand All @@ -83,15 +85,20 @@ examples:
service_account: :SERVICE_ACCT
ignore_read_extra:
- 'gce_setup.0.vm_image'
- 'gce_setup.0.boot_disk.0.disk_encryption'
- 'gce_setup.0.boot_disk.0.disk_type'
- 'gce_setup.0.boot_disk.0.kms_key'
- 'gce_setup.0.data_disks.0.disk_encryption'
- 'gce_setup.0.data_disks.0.disk_type'
- 'gce_setup.0.data_disks.0.kms_key'
timeouts: !ruby/object:Api::Timeouts
insert_minutes: 10
update_minutes: 20
virtual_fields:
- !ruby/object:Api::Type::Enum
name: desired_state
description: |
Desired state of the Workbench Instance. Set this field to `ACTIVE` to start the Instance, and `STOPPED` to stop the Instance.
values:
- :ACTIVE
- :STOPPED
default_value: :ACTIVE
custom_code: !ruby/object:Provider::Terraform::CustomCode
constants: templates/terraform/constants/workbench_instance.go
post_create: templates/terraform/post_create/workbench_instance.go.erb
Expand Down Expand Up @@ -231,7 +238,6 @@ properties:
- !ruby/object:Api::Type::Enum
name: diskEncryption
default_from_api: true
custom_flatten: templates/terraform/custom_flatten/workbench_instance_boot_disk_encryption_flatten.go.erb
values:
- GMEK
- CMEK
Expand All @@ -242,11 +248,11 @@ properties:
- !ruby/object:Api::Type::String
name: kmsKey
description: |
'Optional. Input only. The KMS key used to encrypt the disks, only
'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.'
immutable: true
custom_flatten: templates/terraform/custom_flatten/workbench_instance_boot_disk_kms_flatten.go.erb
diff_suppress_func: WorkbenchInstanceKmsDiffSuppress
- !ruby/object:Api::Type::Array
name: dataDisks
description: Data disks attached to the VM instance. Currently supports only one data disk.
Expand Down Expand Up @@ -276,7 +282,6 @@ properties:
- !ruby/object:Api::Type::Enum
name: diskEncryption
default_from_api: true
custom_flatten: templates/terraform/custom_flatten/workbench_instance_data_disk_encryption_flatten.go.erb
values:
- GMEK
- CMEK
Expand All @@ -287,11 +292,11 @@ properties:
- !ruby/object:Api::Type::String
name: kmsKey
description: |
'Optional. Input only. The KMS key used to encrypt the disks,
'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.'
immutable: true
custom_flatten: templates/terraform/custom_flatten/workbench_instance_data_disk_kms_flatten.go.erb
diff_suppress_func: WorkbenchInstanceKmsDiffSuppress
description: |
Optional. Data disks attached to the VM instance. Currently supports
only one data disk.
Expand Down
39 changes: 39 additions & 0 deletions mmv1/templates/terraform/constants/workbench_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,42 @@ func waitForWorkbenchInstanceActive(d *schema.ResourceData, config *transport_tp
})
}
<% end -%>

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
}

<% unless compiler == "terraformgoogleconversion-codegen" -%>
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
}
<% end -%>

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ resource "google_workbench_instance" "<%= ctx[:primary_resource_id] %>" {
boot_disk {
disk_size_gb = 310
disk_type = "PD_SSD"
disk_encryption = "GMEK"
disk_encryption = "CMEK"
kms_key = "<%= ctx[:vars]['key_name'] %>"
}

data_disks {
disk_size_gb = 330
disk_type = "PD_SSD"
disk_encryption = "GMEK"
disk_encryption = "CMEK"
kms_key = "<%= ctx[:vars]['key_name'] %>"
}

Expand Down Expand Up @@ -65,4 +65,6 @@ resource "google_workbench_instance" "<%= ctx[:primary_resource_id] %>" {
k = "val"
}

desired_state = "ACTIVE"

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ resource "google_workbench_instance" "<%= ctx[:primary_resource_id] %>" {
k = "val"
}

desired_state = "STOPPED"

}
10 changes: 10 additions & 0 deletions mmv1/templates/terraform/post_create/workbench_instance.go.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
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)
}
}
33 changes: 9 additions & 24 deletions mmv1/templates/terraform/post_update/workbench_instance.go.erb
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
state := d.Get("state").(string)
desired_state := d.Get("desired_state").(string)

if state != "ACTIVE" {
startURL, err := tpgresource.ReplaceVars(d, config, "{{WorkbenchBasePath}}projects/{{project}}/locations/{{location}}/instances/{{name}}:start")
if err != nil {
return err
if state != desired_state {
verb := "start"
if desired_state == "STOPPED" {
verb = "stop"
}

log.Printf("[DEBUG] Starting Workbench Instance: %q", name)

emptyReqBody := make(map[string]interface{})

pRes, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "POST",
Project: billingProject,
RawURL: startURL,
UserAgent: userAgent,
Body: emptyReqBody,
})
pRes, err := modifyWorkbenchInstanceState(config, d, project, billingProject, userAgent, verb)
if err != nil {
return fmt.Errorf("Error Starting Workbench Instance: %s", err)
return err
}

var opResp map[string]interface{}
err = WorkbenchOperationWaitTimeWithResponse(
config, pRes, &opResp, project, "Starting Workbench Instance", userAgent,
d.Timeout(schema.TimeoutUpdate))
if err != nil {
return fmt.Errorf("Error waiting to start Workbench Instance: %s", err)
if err := waitForWorkbenchOperation(config, d, project, billingProject, userAgent, pRes); err != nil {
return fmt.Errorf("Error waiting to modify Workbench Instance state: %s", err)
}

} else {
Expand Down
29 changes: 5 additions & 24 deletions mmv1/templates/terraform/pre_update/workbench_instance.go.erb
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
name := d.Get("name").(string)
if d.HasChange("gce_setup.0.machine_type") || d.HasChange("gce_setup.0.accelerator_configs") {
state := d.Get("state").(string)
if state != "STOPPED" {
stopURL, err := tpgresource.ReplaceVars(d, config, "{{WorkbenchBasePath}}projects/{{project}}/locations/{{location}}/instances/{{name}}:stop")
if err != nil {
return err
}

log.Printf("[DEBUG] Stopping Workbench Instance: %q", name)

emptyReqBody := make(map[string]interface{})

dRes, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "POST",
Project: billingProject,
RawURL: stopURL,
UserAgent: userAgent,
Body: emptyReqBody,
})
if state != "STOPPED" {
dRes, err := modifyWorkbenchInstanceState(config, d, project, billingProject, userAgent, "stop")
if err != nil {
return fmt.Errorf("Error Stopping Workbench Instance: %s", err)
return err
}

var opRes map[string]interface{}
err = WorkbenchOperationWaitTimeWithResponse(
config, dRes, &opRes, project, "Stopping Workbench Instance", userAgent,
d.Timeout(schema.TimeoutUpdate))
if err != nil {
return fmt.Errorf("Error waiting to stop Workbench Instance: %s", err)
if err := waitForWorkbenchOperation(config, d, project, billingProject, userAgent, dRes); err != nil {
return fmt.Errorf("Error stopping Workbench Instance: %s", err)
}

} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,57 @@ resource "google_workbench_instance" "instance" {
}
`, context)
}

func TestAccWorkbenchInstance_updateState(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccWorkbenchInstance_basic(context),
},
{
ResourceName: "google_workbench_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name", "instance_owners", "location", "instance_id", "request_id", "labels", "terraform_labels","desired_state"},
},
{
Config: testAccWorkbenchInstance_updateState(context),
},
{
ResourceName: "google_workbench_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name", "instance_owners", "location", "instance_id", "request_id", "labels", "terraform_labels","desired_state"},
},
{
Config: testAccWorkbenchInstance_basic(context),
},
{
ResourceName: "google_workbench_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name", "instance_owners", "location", "instance_id", "request_id", "labels", "terraform_labels","desired_state"},
},
},
})
}

func testAccWorkbenchInstance_updateState(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_workbench_instance" "instance" {
name = "tf-test-workbench-instance%{random_suffix}"
location = "us-central1-a"

desired_state = "STOPPED"

}
`, context)
}

0 comments on commit 81fa60a

Please sign in to comment.