From 975985ec420fff36b9f1361edcc9ce6242fbfc05 Mon Sep 17 00:00:00 2001 From: MagicMicky Date: Sun, 23 Jun 2019 13:44:58 +0200 Subject: [PATCH 1/2] Allow importing for vsphere_host_port_group resources --- vsphere/host_port_group_structure.go | 1 + vsphere/resource_vsphere_host_port_group.go | 22 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/vsphere/host_port_group_structure.go b/vsphere/host_port_group_structure.go index be10c0778..7d4a71cac 100644 --- a/vsphere/host_port_group_structure.go +++ b/vsphere/host_port_group_structure.go @@ -58,6 +58,7 @@ func expandHostPortGroupSpec(d *schema.ResourceData) *types.HostPortGroupSpec { // the passed in ResourceData. func flattenHostPortGroupSpec(d *schema.ResourceData, obj *types.HostPortGroupSpec) error { d.Set("vlan_id", obj.VlanId) + d.Set("virtual_switch_name", obj.VswitchName) if err := flattenHostNetworkPolicy(d, &obj.Policy); err != nil { return err } diff --git a/vsphere/resource_vsphere_host_port_group.go b/vsphere/resource_vsphere_host_port_group.go index ff41e4c9b..7046da9d8 100644 --- a/vsphere/resource_vsphere_host_port_group.go +++ b/vsphere/resource_vsphere_host_port_group.go @@ -47,6 +47,10 @@ func resourceVSphereHostPortGroup() *schema.Resource { Read: resourceVSphereHostPortGroupRead, Update: resourceVSphereHostPortGroupUpdate, Delete: resourceVSphereHostPortGroupDelete, + Importer: &schema.ResourceImporter{ + State: resourceVSphereHostPortGroupImport, + }, + Schema: s, } } @@ -146,3 +150,21 @@ func resourceVSphereHostPortGroupDelete(d *schema.ResourceData, meta interface{} return nil } + +func resourceVSphereHostPortGroupImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + hostID, portGroupName, err := splitHostPortGroupID(d.Id()) + if err != nil { + return []*schema.ResourceData{}, err + } + + err = d.Set("host_system_id", hostID) + if err != nil { + return []*schema.ResourceData{}, err + } + + err = d.Set("name", portGroupName) + if err != nil { + return []*schema.ResourceData{}, err + } + return []*schema.ResourceData{d}, nil +} From 93cb8ada29008a0fcff0ae608686bf7689acb0ef Mon Sep 17 00:00:00 2001 From: MagicMicky Date: Sun, 23 Jun 2019 13:53:27 +0200 Subject: [PATCH 2/2] Add docs for import host port groups --- website/docs/r/host_port_group.html.markdown | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/website/docs/r/host_port_group.html.markdown b/website/docs/r/host_port_group.html.markdown index 7a97bc381..05a8cdfc0 100644 --- a/website/docs/r/host_port_group.html.markdown +++ b/website/docs/r/host_port_group.html.markdown @@ -129,3 +129,20 @@ The following attributes are exported: explaining the effective policy for this port group. * `key` - The key for this port group as returned from the vSphere API. * `ports` - A list of ports that currently exist and are used on this port group. + +## Importing + +An existing port group can be [imported][docs-import] into this resource by its ID. +The convention of the id is a prefix, the host system [managed objectID][docs-about-morefs], and the port group +name. An example would be `tf-HostPortGroup:host-10:PGTerraformTest`. +Import can the be done via the following command: + +[docs-import]: https://www.terraform.io/docs/import/index.html +[docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider + +``` +terraform import vsphere_host_port_group.pg tf-HostPortGroup:host-10:PGTerraformTest +``` + +The above would import the port group named `PGTerraformTest` that is located in the `host-10` +vSphere host.