Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

virtual machine - add annotation argument #111

Merged
merged 1 commit into from
Jul 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions vsphere/resource_vsphere_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type virtualMachine struct {
vcpu int32
memoryMb int64
memoryAllocation memoryAllocation
annotation string
template string
networkInterfaces []networkInterface
hardDisks []hardDisk
Expand Down Expand Up @@ -155,6 +156,11 @@ func resourceVSphereVirtualMachine() *schema.Resource {
ForceNew: true,
},

"annotation": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},

"datacenter": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -504,6 +510,11 @@ func resourceVSphereVirtualMachineUpdate(d *schema.ResourceData, meta interface{
rebootRequired = true
}

if d.HasChange("annotation") {
configSpec.Annotation = d.Get("annotation").(string)
hasChanges = true
}

client := meta.(*govmomi.Client)
dc, err := getDatacenter(client, d.Get("datacenter").(string))
if err != nil {
Expand Down Expand Up @@ -696,6 +707,12 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{
vm.timeZone = v.(string)
}

if v, ok := d.GetOk("annotation"); ok {
vm.annotation = v.(string)
} else {
vm.annotation = ""
}

if v, ok := d.GetOk("linked_clone"); ok {
vm.linkedClone = v.(bool)
}
Expand Down Expand Up @@ -1136,6 +1153,7 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{})
d.Set("cpu", mvm.Summary.Config.NumCpu)
d.Set("datastore", rootDatastore)
d.Set("uuid", mvm.Summary.Config.Uuid)
d.Set("annotation", mvm.Summary.Config.Annotation)

return nil
}
Expand Down Expand Up @@ -1776,6 +1794,7 @@ func (vm *virtualMachine) setupVirtualMachine(c *govmomi.Client) error {
Flags: &types.VirtualMachineFlagInfo{
DiskUuidEnabled: &vm.enableDiskUUID,
},
Annotation: vm.annotation,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this fail if vm.annotation is ""?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this doesn`t fail with empty string in vm.annotaton. So everything works fine as with an empty argument and absolutely without it. In both of these situations vm.annotation takes a value "". vsphere/resource_vsphere_virtual_machine.go (L710-L714)

}
if vm.template == "" {
configSpec.GuestId = "otherLinux64Guest"
Expand Down
36 changes: 36 additions & 0 deletions vsphere/resource_vsphere_virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,42 @@ func createAndAttachDisk(t *testing.T, vmName string, size int, datastore string
}
}

const testAccCheckVSphereVirtualMachineConfig_annotation = `
resource "vsphere_virtual_machine" "car" {
name = "terraform-test-annotation"
annotation = "bar"
`

func TestAccVSphereVirtualMachine_annotation(t *testing.T) {

var vm virtualMachine
data := setupTemplateFuncDHCPData()
config := testAccCheckVSphereVirtualMachineConfig_annotation + data.parseDHCPTemplateConfigWithTemplate(testAccCheckVSphereTemplate_dhcp)
vmName := "vsphere_virtual_machine.car"
res := "terraform-test-annotation"

test_exists, test_name, test_cpu, test_uuid, test_mem, test_num_disk, test_num_of_nic, test_nic_label :=
TestFuncData{vm: vm, label: data.label, vmName: vmName, vmResource: res}.testCheckFuncBasic()

log.Printf("[DEBUG] template= %s", testAccCheckVSphereVirtualMachineConfig_annotation+testAccCheckVSphereTemplate_dhcp)
log.Printf("[DEBUG] config= %s", config)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: config,
Check: resource.ComposeTestCheckFunc(
test_exists, test_name, test_cpu, test_uuid, test_mem, test_num_disk, test_num_of_nic, test_nic_label,
resource.TestCheckResourceAttr(vmName, "annotation", "bar"),
),
},
},
})
}

func vmCleanup(dc *object.Datacenter, ds *object.Datastore, vmName string) error {
client := testAccProvider.Meta().(*govmomi.Client)
fileManager := object.NewFileManager(client.Client)
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/virtual_machine.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ The following arguments are supported:
* `enable_disk_uuid` - (Optional) This option causes the vm to mount disks by uuid on the guest OS.
* `custom_configuration_parameters` - (Optional) Map of values that is set as virtual machine custom configurations.
* `skip_customization` - (Optional) skip virtual machine customization (useful if OS is not in the guest OS support matrix of VMware like "other3xLinux64Guest").
* `annotation` - (Optional) Edit the annotation notes field

The `network_interface` block supports:

Expand Down