Skip to content

Commit

Permalink
Add support for Shielded VMs to compute_instance and compute_instance…
Browse files Browse the repository at this point in the history
…_template (hashicorp#3209)
  • Loading branch information
mlauter committed May 13, 2019
1 parent 77a8c8d commit b1e181e
Show file tree
Hide file tree
Showing 9 changed files with 688 additions and 6 deletions.
26 changes: 26 additions & 0 deletions google/compute_instance_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,29 @@ func resourceInstanceTags(d TerraformResourceData) *computeBeta.Tags {

return tags
}

func expandShieldedVmConfigs(d *schema.ResourceData) *computeBeta.ShieldedVmConfig {
if _, ok := d.GetOk("shielded_vm_config"); !ok {
return nil
}

prefix := "shielded_vm_config.0"
return &computeBeta.ShieldedVmConfig{
EnableSecureBoot: d.Get(prefix + ".enable_secure_boot").(bool),
EnableVtpm: d.Get(prefix + ".enable_vtpm").(bool),
EnableIntegrityMonitoring: d.Get(prefix + ".enable_integrity_monitoring").(bool),
ForceSendFields: []string{"EnableSecureBoot", "EnableVtpm", "EnableIntegrityMonitoring"},
}
}

func flattenShieldedVmConfig(shieldedVmConfig *computeBeta.ShieldedVmConfig) []map[string]bool {
if shieldedVmConfig == nil {
return nil
}

return []map[string]bool{{
"enable_secure_boot": shieldedVmConfig.EnableSecureBoot,
"enable_vtpm": shieldedVmConfig.EnableVtpm,
"enable_integrity_monitoring": shieldedVmConfig.EnableIntegrityMonitoring,
}}
}
5 changes: 5 additions & 0 deletions google/data_source_google_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ func dataSourceGoogleComputeInstanceRead(d *schema.ResourceData, meta interface{
return err
}

err = d.Set("shielded_vm_config", flattenShieldedVmConfig(instance.ShieldedVmConfig))
if err != nil {
return err
}

d.Set("attached_disk", ads)
d.Set("cpu_platform", instance.CpuPlatform)
d.Set("min_cpu_platform", instance.MinCpuPlatform)
Expand Down
45 changes: 45 additions & 0 deletions google/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,33 @@ func resourceComputeInstance() *schema.Resource {
},
},

"shielded_vm_config": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enable_secure_boot": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},

"enable_vtpm": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},

"enable_integrity_monitoring": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
},
},

"tags": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -670,6 +697,7 @@ func expandComputeInstance(project string, zone *compute.Zone, d *schema.Resourc
DeletionProtection: d.Get("deletion_protection").(bool),
Hostname: d.Get("hostname").(string),
ForceSendFields: []string{"CanIpForward", "DeletionProtection"},
ShieldedVmConfig: expandShieldedVmConfigs(d),
}, nil
}

Expand Down Expand Up @@ -879,6 +907,7 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
d.Set("scratch_disk", scratchDisks)
d.Set("scheduling", flattenScheduling(instance.Scheduling))
d.Set("guest_accelerator", flattenGuestAccelerators(instance.GuestAccelerators))
d.Set("shielded_vm_config", flattenShieldedVmConfig(instance.ShieldedVmConfig))
d.Set("cpu_platform", instance.CpuPlatform)
d.Set("min_cpu_platform", instance.MinCpuPlatform)
d.Set("deletion_protection", instance.DeletionProtection)
Expand Down Expand Up @@ -1341,6 +1370,22 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
}
}

if d.HasChange("shielded_vm_config") {
shieldedVmConfig := expandShieldedVmConfigs(d)

op, err := config.clientComputeBeta.Instances.UpdateShieldedVmConfig(project, zone, d.Id(), shieldedVmConfig).Do()
if err != nil {
return fmt.Errorf("Error updating shielded vm config: %s", err)
}

opErr := computeSharedOperationWaitTime(config.clientCompute, op, project, int(d.Timeout(schema.TimeoutUpdate).Minutes()), "shielded vm config update")
if opErr != nil {
return opErr
}

d.SetPartial("shielded_vm_config")
}

// We made it, disable partial mode
d.Partial(false)

Expand Down
37 changes: 37 additions & 0 deletions google/resource_compute_instance_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,37 @@ func resourceComputeInstanceTemplate() *schema.Resource {
},
},

"shielded_vm_config": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enable_secure_boot": {
Type: schema.TypeBool,
Optional: true,
Default: false,
ForceNew: true,
},

"enable_vtpm": {
Type: schema.TypeBool,
Optional: true,
Default: false,
ForceNew: true,
},

"enable_integrity_monitoring": {
Type: schema.TypeBool,
Optional: true,
Default: false,
ForceNew: true,
},
},
},
},

"guest_accelerator": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -644,6 +675,7 @@ func resourceComputeInstanceTemplateCreate(d *schema.ResourceData, meta interfac
Scheduling: scheduling,
ServiceAccounts: expandServiceAccounts(d.Get("service_account").([]interface{})),
Tags: resourceInstanceTags(d),
ShieldedVmConfig: expandShieldedVmConfigs(d),
}

if _, ok := d.GetOk("labels"); ok {
Expand Down Expand Up @@ -841,6 +873,11 @@ func resourceComputeInstanceTemplateRead(d *schema.ResourceData, meta interface{
return fmt.Errorf("Error setting guest_accelerator: %s", err)
}
}
if instanceTemplate.Properties.ShieldedVmConfig != nil {
if err = d.Set("shielded_vm_config", flattenShieldedVmConfig(instanceTemplate.Properties.ShieldedVmConfig)); err != nil {
return fmt.Errorf("Error setting shielded_vm_config: %s", err)
}
}
return nil
}

Expand Down
Loading

0 comments on commit b1e181e

Please sign in to comment.