Skip to content

Commit

Permalink
fix:Add support to enable services on a vmk
Browse files Browse the repository at this point in the history
  • Loading branch information
zxinyu08 committed Mar 15, 2023
1 parent c8fcd69 commit 407ed09
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 1 deletion.
125 changes: 124 additions & 1 deletion vsphere/resource_vsphere_vnic.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,25 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/hostsystem"
"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/structure"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
)

const (
vnicServiceTypeVsan = "vsan"
vnicServiceTypeVmotion = "vmotion"
vnicServiceTypeManagement = "management"
)

var vnicServiceTypeAllowedValues = []string{
vnicServiceTypeVsan,
vnicServiceTypeVmotion,
vnicServiceTypeManagement,
}

func resourceVsphereNic() *schema.Resource {
return &schema.Resource{
Create: resourceVsphereNicCreate,
Expand Down Expand Up @@ -118,6 +131,35 @@ func resourceVsphereNicRead(d *schema.ResourceData, meta interface{}) error {
}
}
}

// get enabled services
host_system, err := hostsystem.FromID(client, hostID)
if err != nil {
return err
}

hostVnicMgr, err := host_system.ConfigManager().VirtualNicManager(ctx)
if err != nil {
return nil
}

hostVnicMgrInfo, err := hostVnicMgr.Info(ctx)
if err != nil {
return nil
}

var services []string
for _, netConfig := range hostVnicMgrInfo.NetConfig {
for _, vnic := range netConfig.SelectedVnic {
if isNicIdContained := strings.Contains(vnic, nicID); isNicIdContained {
services = append(services, netConfig.NicType)
}
}
}
if err := d.Set("services", schema.NewSet(schema.HashString, structure.SliceStringsToInterfaces(services))); err != nil {
return err
}

return nil
}

Expand All @@ -136,7 +178,7 @@ func resourceVsphereNicCreate(d *schema.ResourceData, meta interface{}) error {
func resourceVsphereNicUpdate(d *schema.ResourceData, meta interface{}) error {
for _, k := range []string{
"portgroup", "distributed_switch_port", "distributed_port_group",
"mac", "mtu", "ipv4", "ipv6", "netstack"} {
"mac", "mtu", "ipv4", "ipv6", "netstack", "services"} {
if d.HasChange(k) {
_, err := updateVNic(d, meta)
if err != nil {
Expand Down Expand Up @@ -271,11 +313,22 @@ func BaseVMKernelSchema() map[string]*schema.Schema {
Default: "defaultTcpipStack",
ForceNew: true,
},
"services": {
Type: schema.TypeSet,
Optional: true,
Description: "Enabled services setting for this interface. Current possible values are 'vmotion', 'management' and 'vsan'",
Elem: &schema.Schema{Type: schema.TypeString},
},
}
return sch
}

func updateVNic(d *schema.ResourceData, meta interface{}) (string, error) {
err := precheckEnableServices(d)
if err != nil {
return "", err
}

client := meta.(*Client).vimClient
hostID, nicID := splitHostIDNicID(d)
ctx := context.TODO()
Expand All @@ -295,10 +348,74 @@ func updateVNic(d *schema.ResourceData, meta interface{}) (string, error) {
return "", err
}

err = updateVnicService(d, hostID, nicID, meta)
if err != nil {
return "", err
}

return nicID, nil
}

func updateVnicService(d *schema.ResourceData, hostID string, nicID string, meta interface{}) error {
service_old, service_new := d.GetChange("services")
deleteList := service_old.(*schema.Set).List()
addList := service_new.(*schema.Set).List()

client := meta.(*Client).vimClient
ctx := context.TODO()
host_system, err := hostsystem.FromID(client, hostID)
if err != nil {
return err
}
method, err := host_system.ConfigManager().VirtualNicManager(ctx)
if err != nil {
return nil
}

for _, value := range deleteList {
err = method.DeselectVnic(ctx, value.(string), nicID)
if err != nil {
return err
}
}

for _, value := range addList {
err = method.SelectVnic(ctx, value.(string), nicID)
if err != nil {
return err
}
}

return nil
}

func precheckEnableServices(d *schema.ResourceData) error {
if d.Get("netstack").(string) != "defaultTcpipStack" && len(d.Get("services").(*schema.Set).List()) != 0 {
return fmt.Errorf("Services could be configured only when the netstack is set to defaultTcpipStack.")
}
// will expand scope later, current possible values are vmotion, management and vsan.
data := d.Get("services").(*schema.Set).List()
for _, value := range data {
isValueAllowed := false
for _, allowedValue := range vnicServiceTypeAllowedValues {
if value == allowedValue {
isValueAllowed = true
break
}
}
if !isValueAllowed {
return fmt.Errorf("%s is not an allowed service type.", value)
}
}
return nil
}

func createVNic(d *schema.ResourceData, meta interface{}) (string, error) {
err := precheckEnableServices(d)
if err != nil {
return "", err
}

client := meta.(*Client).vimClient
ctx := context.TODO()

Expand All @@ -319,6 +436,12 @@ func createVNic(d *schema.ResourceData, meta interface{}) (string, error) {
return "", err
}
d.SetId(fmt.Sprintf("%s_%s", hostID, nicID))

err = updateVnicService(d, hostID, nicID, meta)
if err != nil {
return "", err
}

return nicID, nil
}

Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/vnic.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ resource "vsphere_vnic" "v1" {
ipv4 {
dhcp = true
}
enabled_services = ["vsan", "management"]
}
```

Expand All @@ -98,6 +99,7 @@ resource "vsphere_vnic" "v1" {
* `mac` - (Optional) MAC address of the interface.
* `mtu` - (Optional) MTU of the interface.
* `netstack` - (Optional) TCP/IP stack setting for this interface. Possible values are 'defaultTcpipStack', 'vmotion', 'vSphereProvisioning'. Changing this will force the creation of a new interface since it's not possible to change the stack once it gets created. (Default: `defaultTcpipStack`)
* `services` - (Optional) Enabled services setting for this interface. Current possible values are 'vmotion', 'management', and 'vsan'.


### ipv4 options
Expand Down

0 comments on commit 407ed09

Please sign in to comment.