Skip to content

Commit

Permalink
r/distributed_port_group: Use MOID as resource ID
Browse files Browse the repository at this point in the history
The additions that are in #201 dictate the basis for how we will be
working with vSphere networks in the future - any resource that requires
a network for a backing (ie: virtual ethernet cards, VMkernel nics, etc)
will require a MOID to some sort of network, versus the name itself,
allowing us to circumvent actually searching for the network in the
downstream resource itself. Searching would be circumvented altogether
in the case of DVS portgroups being managed by TF completely as we would
just pass the MOID to the downstream resource.

The current behaviour of the vsphere_distributed_port_group resource is
to use the key attribute in the MO as the ID of the resource, which is
documented in the API as the UUID of the DVS port group, although in
reality it actually seems to be the MOID, which allows #201 to work
without issue right now. However, in order to guarantee that things will
be stable in the long run, we need to be sure we are using the right
values, so this update changes the ID to be the MOID of the object
itself. key is now being exported as a computed attribute of the same
name.
  • Loading branch information
vancluever committed Oct 12, 2017
1 parent 6f8ba38 commit 71ff4dd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
32 changes: 19 additions & 13 deletions vsphere/resource_vsphere_distributed_port_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ import (
func resourceVSphereDistributedPortGroup() *schema.Resource {
s := map[string]*schema.Schema{
"distributed_virtual_switch_uuid": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Description: "The UUID of the DVS to attach this port group to.",
Required: true,
ForceNew: true,
},
"key": {
Type: schema.TypeString,
Description: "The generated UUID of the portgroup.",
Computed: true,
},
// Tagging
vSphereTagAttributeKey: tagsSchema(),
Expand Down Expand Up @@ -69,7 +75,8 @@ func resourceVSphereDistributedPortGroupCreate(d *schema.ResourceData, meta inte
return fmt.Errorf("error fetching portgroup properties after creation: %s", err)
}

d.SetId(props.Key)
d.SetId(pg.Reference().Value)
d.Set("key", props.Key)

// Apply any pending tags now
if tagsClient != nil {
Expand All @@ -85,17 +92,18 @@ func resourceVSphereDistributedPortGroupRead(d *schema.ResourceData, meta interf
if err := validateVirtualCenter(client); err != nil {
return err
}
dvsID := d.Get("distributed_virtual_switch_uuid").(string)
pgID := d.Id()
pg, err := dvPortgroupFromUUID(client, dvsID, pgID)
pg, err := dvPortgroupFromMOID(client, pgID)
if err != nil {
return fmt.Errorf("could not find portgroup %q on DVS %q: %s", pgID, dvsID, err)
return fmt.Errorf("could not find portgroup %q: %s", pgID, err)
}
props, err := dvPortgroupProperties(pg)
if err != nil {
return fmt.Errorf("error fetching portgroup properties: %s", err)
}

d.Set("key", props.Key)

if err := flattenDVPortgroupConfigInfo(d, props.Config); err != nil {
return err
}
Expand All @@ -117,11 +125,10 @@ func resourceVSphereDistributedPortGroupUpdate(d *schema.ResourceData, meta inte
if err != nil {
return err
}
dvsID := d.Get("distributed_virtual_switch_uuid").(string)
pgID := d.Id()
pg, err := dvPortgroupFromUUID(client, dvsID, pgID)
pg, err := dvPortgroupFromMOID(client, pgID)
if err != nil {
return fmt.Errorf("could not find portgroup %q on DVS %q: %s", pgID, dvsID, err)
return fmt.Errorf("could not find portgroup %q: %s", pgID, err)
}
spec := expandDVPortgroupConfigSpec(d)
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout)
Expand Down Expand Up @@ -150,11 +157,10 @@ func resourceVSphereDistributedPortGroupDelete(d *schema.ResourceData, meta inte
if err := validateVirtualCenter(client); err != nil {
return err
}
dvsID := d.Get("distributed_virtual_switch_uuid").(string)
pgID := d.Id()
pg, err := dvPortgroupFromUUID(client, dvsID, pgID)
pg, err := dvPortgroupFromMOID(client, pgID)
if err != nil {
return fmt.Errorf("could not find portgroup %q on DVS %q: %s", pgID, dvsID, err)
return fmt.Errorf("could not find portgroup %q: %s", pgID, err)
}

ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout)
Expand Down
9 changes: 8 additions & 1 deletion website/docs/r/distributed_port_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,14 @@ group can be overridden on the individual port:

The following attributes are exported:

* `id`: The UUID of the created port group.
* `id`: The managed object reference ID of the created port group.
* `key`: The generated UUID of the portgroup.

~> **NOTE:** While `id` and `key` may look the same in state, they are
documented differently in the vSphere API and come from different fields in the
port group object. If you are asked to supply an managed object reference ID to
another resource, be sure to use the `id` field.

* `config_version`: The current version of the port group configuration,
incremented by subsequent updates to the port group.

Expand Down

0 comments on commit 71ff4dd

Please sign in to comment.