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

Import host_port_group resources #795

Closed
Closed
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
1 change: 1 addition & 0 deletions vsphere/host_port_group_structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
22 changes: 22 additions & 0 deletions vsphere/resource_vsphere_host_port_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func resourceVSphereHostPortGroup() *schema.Resource {
Read: resourceVSphereHostPortGroupRead,
Update: resourceVSphereHostPortGroupUpdate,
Delete: resourceVSphereHostPortGroupDelete,
Importer: &schema.ResourceImporter{
State: resourceVSphereHostPortGroupImport,
},

Schema: s,
}
}
Expand Down Expand Up @@ -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
}
17 changes: 17 additions & 0 deletions website/docs/r/host_port_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.