Skip to content

Commit

Permalink
Add key_revocation_action_field to google_compute_instance and re…
Browse files Browse the repository at this point in the history
…lated resources (hashicorp#11920) (hashicorp#19952)

[upstream:24a8c2f1cbc42d4d29703b9ad928019ad0119b79]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Oct 21, 2024
1 parent 25b309d commit d2503f7
Show file tree
Hide file tree
Showing 14 changed files with 287 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/11920.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: added `key_revocation_action_type` to `google_compute_instance` and related resources
```
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,13 @@ func dataSourceGoogleComputeInstanceRead(d *schema.ResourceData, meta interface{
if err := d.Set("name", instance.Name); err != nil {
return fmt.Errorf("Error setting name: %s", err)
}
if err := d.Set("key_revocation_action_type", instance.KeyRevocationActionType); err != nil {
return fmt.Errorf("Error setting key_revocation_action_type: %s", err)
}
if err := d.Set("creation_timestamp", instance.CreationTimestamp); err != nil {
return fmt.Errorf("Error setting creation_timestamp: %s", err)
}

d.SetId(fmt.Sprintf("projects/%s/zones/%s/instances/%s", project, tpgresource.GetResourceNameFromSelfLink(instance.Zone), instance.Name))
return nil
}
12 changes: 12 additions & 0 deletions google/services/compute/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,14 @@ be from 0 to 999,999,999 inclusive.`,
},
},
},

"key_revocation_action_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"STOP", "NONE", ""}, false),
Description: `Action to be taken when a customer's encryption key is revoked. Supports "STOP" and "NONE", with "NONE" being the default.`,
},
},
CustomizeDiff: customdiff.All(
tpgresource.DefaultProviderProject,
Expand Down Expand Up @@ -1365,6 +1373,7 @@ func expandComputeInstance(project string, d *schema.ResourceData, config *trans
DisplayDevice: expandDisplayDevice(d),
ResourcePolicies: tpgresource.ConvertStringArr(d.Get("resource_policies").([]interface{})),
ReservationAffinity: reservationAffinity,
KeyRevocationActionType: d.Get("key_revocation_action_type").(string),
}, nil
}

Expand Down Expand Up @@ -1740,6 +1749,9 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
if err := d.Set("reservation_affinity", flattenReservationAffinity(instance.ReservationAffinity)); err != nil {
return fmt.Errorf("Error setting reservation_affinity: %s", err)
}
if err := d.Set("key_revocation_action_type", instance.KeyRevocationActionType); err != nil {
return fmt.Errorf("Error setting key_revocation_action_type: %s", err)
}

d.SetId(fmt.Sprintf("projects/%s/zones/%s/instances/%s", project, zone, instance.Name))

Expand Down
12 changes: 12 additions & 0 deletions google/services/compute/resource_compute_instance_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,14 @@ be from 0 to 999,999,999 inclusive.`,
},
},
},

"key_revocation_action_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"NONE", "STOP", ""}, false),
Description: `Action to be taken when a customer's encryption key is revoked. Supports "STOP" and "NONE", with "NONE" being the default.`,
},
},
UseJSONNumber: true,
}
Expand Down Expand Up @@ -1381,6 +1389,7 @@ func resourceComputeInstanceTemplateCreate(d *schema.ResourceData, meta interfac
AdvancedMachineFeatures: expandAdvancedMachineFeatures(d),
ResourcePolicies: resourcePolicies,
ReservationAffinity: reservationAffinity,
KeyRevocationActionType: d.Get("key_revocation_action_type").(string),
}

if _, ok := d.GetOk("effective_labels"); ok {
Expand Down Expand Up @@ -1777,6 +1786,9 @@ func resourceComputeInstanceTemplateRead(d *schema.ResourceData, meta interface{
if err = d.Set("instance_description", instanceTemplate.Properties.Description); err != nil {
return fmt.Errorf("Error setting instance_description: %s", err)
}
if err = d.Set("key_revocation_action_type", instanceTemplate.Properties.KeyRevocationActionType); err != nil {
return fmt.Errorf("Error setting key_revocation_action_type: %s", err)
}
if err = d.Set("project", project); err != nil {
return fmt.Errorf("Error setting project: %s", err)
}
Expand Down
77 changes: 77 additions & 0 deletions google/services/compute/resource_compute_instance_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,56 @@ func TestAccComputeInstanceTemplate_resourceManagerTags(t *testing.T) {
})
}

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

var instanceTemplate compute.InstanceTemplate
context_1 := map[string]interface{}{
"instance_name": fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10)),
"key_revocation_action_type": `"NONE"`,
}
context_2 := map[string]interface{}{
"instance_name": context_1["instance_name"].(string),
"key_revocation_action_type": `"STOP"`,
}
context_3 := map[string]interface{}{
"instance_name": context_1["instance_name"].(string),
"key_revocation_action_type": `""`,
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeInstanceTemplateDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeInstanceTemplate_keyRevocationActionType(context_1),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceTemplateExists(
t, "google_compute_instance_template.foobar", &instanceTemplate),
resource.TestCheckResourceAttr("google_compute_instance_template.foobar", "key_revocation_action_type", "NONE"),
),
},
{
Config: testAccComputeInstanceTemplate_keyRevocationActionType(context_2),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceTemplateExists(
t, "google_compute_instance_template.foobar", &instanceTemplate),
resource.TestCheckResourceAttr("google_compute_instance_template.foobar", "key_revocation_action_type", "STOP"),
),
},
{
Config: testAccComputeInstanceTemplate_keyRevocationActionType(context_3),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceTemplateExists(
t, "google_compute_instance_template.foobar", &instanceTemplate),
resource.TestCheckResourceAttr("google_compute_instance_template.foobar", "key_revocation_action_type", ""),
),
},
},
})
}

func TestUnitComputeInstanceTemplate_IpCidrRangeDiffSuppress(t *testing.T) {
cases := map[string]struct {
Old, New string
Expand Down Expand Up @@ -4008,3 +4058,30 @@ resource "google_compute_instance_template" "foobar" {
}
`, context)
}

func testAccComputeInstanceTemplate_keyRevocationActionType(context map[string]interface{}) string {
return acctest.Nprintf(`
data "google_compute_image" "my_image" {
family = "debian-11"
project = "debian-cloud"
}
resource "google_compute_instance_template" "foobar" {
name = "%{instance_name}"
machine_type = "e2-medium"
disk {
source_image = data.google_compute_image.my_image.self_link
auto_delete = true
disk_size_gb = 10
boot = true
}
network_interface {
network = "default"
}
key_revocation_action_type = %{key_revocation_action_type}
}
`, context)
}
77 changes: 77 additions & 0 deletions google/services/compute/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3337,6 +3337,56 @@ func TestAccComputeInstance_proactiveAttributionLabel(t *testing.T) {
})
}

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

var instance compute.Instance
context_1 := map[string]interface{}{
"instance_name": fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10)),
"key_revocation_action_type": `"NONE"`,
}
context_2 := map[string]interface{}{
"instance_name": context_1["instance_name"].(string),
"key_revocation_action_type": `"STOP"`,
}
context_3 := map[string]interface{}{
"instance_name": context_1["instance_name"].(string),
"key_revocation_action_type": `""`,
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeInstance_keyRevocationActionType(context_1),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(
t, "google_compute_instance.foobar", &instance),
resource.TestCheckResourceAttr("google_compute_instance.foobar", "key_revocation_action_type", "NONE"),
),
},
{
Config: testAccComputeInstance_keyRevocationActionType(context_2),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(
t, "google_compute_instance.foobar", &instance),
resource.TestCheckResourceAttr("google_compute_instance.foobar", "key_revocation_action_type", "STOP"),
),
},
{
Config: testAccComputeInstance_keyRevocationActionType(context_3),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(
t, "google_compute_instance.foobar", &instance),
resource.TestCheckResourceAttr("google_compute_instance.foobar", "key_revocation_action_type", ""),
),
},
},
})
}

func testAccCheckComputeInstanceUpdateMachineType(t *testing.T, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -9076,3 +9126,30 @@ resource "google_compute_instance" "foobar" {
}
`, diskName, instanceName, machineType, zone, bootDiskInterface, allowStoppingForUpdate)
}

func testAccComputeInstance_keyRevocationActionType(context map[string]interface{}) string {
return acctest.Nprintf(`
data "google_compute_image" "my_image" {
family = "debian-11"
project = "debian-cloud"
}
resource "google_compute_instance" "foobar" {
name = "%{instance_name}"
machine_type = "e2-medium"
zone = "us-central1-a"
boot_disk {
initialize_params {
image = data.google_compute_image.my_image.self_link
}
}
network_interface {
network = "default"
}
key_revocation_action_type = %{key_revocation_action_type}
}
`, context)
}
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,14 @@ be from 0 to 999,999,999 inclusive.`,
},
},
},

"key_revocation_action_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"NONE", "STOP", ""}, false),
Description: `Action to be taken when a customer's encryption key is revoked. Supports "STOP" and "NONE", with "NONE" being the default.`,
},
},
UseJSONNumber: true,
}
Expand Down Expand Up @@ -1087,6 +1095,7 @@ func resourceComputeRegionInstanceTemplateCreate(d *schema.ResourceData, meta in
AdvancedMachineFeatures: expandAdvancedMachineFeatures(d),
ResourcePolicies: resourcePolicies,
ReservationAffinity: reservationAffinity,
KeyRevocationActionType: d.Get("key_revocation_action_type").(string),
}

if _, ok := d.GetOk("effective_labels"); ok {
Expand Down Expand Up @@ -1275,6 +1284,9 @@ func resourceComputeRegionInstanceTemplateRead(d *schema.ResourceData, meta inte
if err = d.Set("instance_description", instanceProperties.Description); err != nil {
return fmt.Errorf("Error setting instance_description: %s", err)
}
if err = d.Set("key_revocation_action_type", instanceProperties.KeyRevocationActionType); err != nil {
return fmt.Errorf("Error setting key_revocation_action_type: %s", err)
}
if err = d.Set("project", project); err != nil {
return fmt.Errorf("Error setting project: %s", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,56 @@ func TestAccComputeRegionInstanceTemplate_resourceManagerTags(t *testing.T) {
})
}

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

var instanceTemplate compute.InstanceTemplate
context_1 := map[string]interface{}{
"instance_name": fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10)),
"key_revocation_action_type": `"NONE"`,
}
context_2 := map[string]interface{}{
"instance_name": context_1["instance_name"].(string),
"key_revocation_action_type": `"STOP"`,
}
context_3 := map[string]interface{}{
"instance_name": context_1["instance_name"].(string),
"key_revocation_action_type": `""`,
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeRegionInstanceTemplateDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeRegionInstanceTemplate_keyRevocationActionType(context_1),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeRegionInstanceTemplateExists(
t, "google_compute_region_instance_template.foobar", &instanceTemplate),
resource.TestCheckResourceAttr("google_compute_region_instance_template.foobar", "key_revocation_action_type", "NONE"),
),
},
{
Config: testAccComputeRegionInstanceTemplate_keyRevocationActionType(context_2),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeRegionInstanceTemplateExists(
t, "google_compute_region_instance_template.foobar", &instanceTemplate),
resource.TestCheckResourceAttr("google_compute_region_instance_template.foobar", "key_revocation_action_type", "STOP"),
),
},
{
Config: testAccComputeRegionInstanceTemplate_keyRevocationActionType(context_3),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeRegionInstanceTemplateExists(
t, "google_compute_region_instance_template.foobar", &instanceTemplate),
resource.TestCheckResourceAttr("google_compute_region_instance_template.foobar", "key_revocation_action_type", ""),
),
},
},
})
}

func testAccCheckComputeRegionInstanceTemplateDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
config := acctest.GoogleProviderConfig(t)
Expand Down Expand Up @@ -3546,3 +3596,31 @@ resource "google_compute_region_instance_template" "foobar" {
}
`, context)
}

func testAccComputeRegionInstanceTemplate_keyRevocationActionType(context map[string]interface{}) string {
return acctest.Nprintf(`
data "google_compute_image" "my_image" {
family = "debian-11"
project = "debian-cloud"
}
resource "google_compute_region_instance_template" "foobar" {
name = "%{instance_name}"
machine_type = "e2-medium"
region = "us-central1"
disk {
source_image = data.google_compute_image.my_image.self_link
auto_delete = true
disk_size_gb = 10
boot = true
}
network_interface {
network = "default"
}
key_revocation_action_type = %{key_revocation_action_type}
}
`, context)
}
2 changes: 2 additions & 0 deletions website/docs/d/compute_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ The following arguments are supported:
encoded SHA-256 hash of the [customer-supplied encryption key]
(https://cloud.google.com/compute/docs/disks/customer-supplied-encryption) that protects this resource.

* `key_revocation_action_type` - Action to be taken when a customer's encryption key is revoked.

---

<a name="nested_boot_disk"></a>The `boot_disk` block supports:
Expand Down
Loading

0 comments on commit d2503f7

Please sign in to comment.