Skip to content

Commit

Permalink
Add moid in vsphere_virtual_machine data source
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Este-Gracias <sestegra@gmail.com>
  • Loading branch information
sestegra authored and appilon committed Apr 13, 2023
1 parent 150830a commit 9b4ea83
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
13 changes: 10 additions & 3 deletions vsphere/data_source_vsphere_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,16 @@ func dataSourceVSphereVirtualMachine() *schema.Resource {
// include the number of cpus, memory, firmware, disks, etc.
structure.MergeSchema(s, schemaVirtualMachineConfigSpec())

// make name/uuid Optional/AtLeastOneOf since UUID lookup is now supported
// make name/uuid/moid Optional/AtLeastOneOf since UUID lookup is now supported
s["name"].Required = false
s["name"].Optional = true
s["name"].AtLeastOneOf = []string{"name", "uuid"}
s["name"].AtLeastOneOf = []string{"name", "uuid", "moid"}
s["uuid"].Required = false
s["uuid"].Optional = true
s["uuid"].AtLeastOneOf = []string{"name", "uuid"}
s["uuid"].AtLeastOneOf = []string{"name", "uuid", "moid"}
s["moid"].Required = false
s["moid"].Optional = true
s["moid"].AtLeastOneOf = []string{"name", "uuid", "moid"}

// Now that the schema has been composed and merged, we can attach our reader and
// return the resource back to our host process.
Expand All @@ -171,13 +174,17 @@ func dataSourceVSphereVirtualMachine() *schema.Resource {
func dataSourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client).vimClient
uuid := d.Get("uuid").(string)
moid := d.Get("moid").(string)
name := d.Get("name").(string)
var vm *object.VirtualMachine
var err error

if uuid != "" {
log.Printf("[DEBUG] Looking for VM or template by UUID %q", uuid)
vm, err = virtualmachine.FromUUID(client, uuid)
} else if moid != "" {
log.Printf("[DEBUG] Looking for VM or template by MOID %q", moid)
vm, err = virtualmachine.FromMOID(client, moid)
} else {
log.Printf("[DEBUG] Looking for VM or template by name/path %q", name)
var dc *object.Datacenter
Expand Down
5 changes: 0 additions & 5 deletions vsphere/resource_vsphere_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,6 @@ func resourceVSphereVirtualMachine() *schema.Resource {
Computed: true,
Description: "A flag internal to Terraform that indicates that this resource was either imported or came from a earlier major version of this resource. Reset after the first post-import or post-upgrade apply.",
},
"moid": {
Type: schema.TypeString,
Computed: true,
Description: "The machine object ID from VMware vSphere.",
},
"power_state": {
Type: schema.TypeString,
Computed: true,
Expand Down
5 changes: 5 additions & 0 deletions vsphere/virtual_machine_config_structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ func schemaVirtualMachineConfigSpec() map[string]*schema.Schema {
Computed: true,
Description: "The UUID of the virtual machine. Also exposed as the ID of the resource.",
},
"moid": {
Type: schema.TypeString,
Computed: true,
Description: "The machine object ID from VMware vSphere.",
},
"storage_policy_id": {
Type: schema.TypeString,
Optional: true,
Expand Down

0 comments on commit 9b4ea83

Please sign in to comment.