github.com/vmware/govmomi@v0.37.2/simulator/host_network_system.go (about)

     1  /*
     2  Copyright (c) 2017 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package simulator
    18  
    19  import (
    20  	"github.com/vmware/govmomi/vim25/methods"
    21  	"github.com/vmware/govmomi/vim25/mo"
    22  	"github.com/vmware/govmomi/vim25/soap"
    23  	"github.com/vmware/govmomi/vim25/types"
    24  )
    25  
    26  type HostNetworkSystem struct {
    27  	mo.HostNetworkSystem
    28  
    29  	Host *mo.HostSystem
    30  
    31  	types.QueryNetworkHintResponse
    32  }
    33  
    34  func NewHostNetworkSystem(host *mo.HostSystem) *HostNetworkSystem {
    35  	return &HostNetworkSystem{
    36  		Host: host,
    37  		HostNetworkSystem: mo.HostNetworkSystem{
    38  			NetworkInfo: &types.HostNetworkInfo{
    39  				Vswitch: []types.HostVirtualSwitch{
    40  					{
    41  						Name:      "vSwitch0",
    42  						Portgroup: []string{"VM Network"},
    43  					},
    44  				},
    45  				Portgroup: host.Config.Network.Portgroup,
    46  			},
    47  		},
    48  	}
    49  }
    50  
    51  func (s *HostNetworkSystem) init(r *Registry) {
    52  	for _, obj := range r.objects {
    53  		if h, ok := obj.(*HostSystem); ok {
    54  			if h.ConfigManager.NetworkSystem.Value == s.Self.Value {
    55  				s.Host = &h.HostSystem
    56  			}
    57  		}
    58  	}
    59  }
    60  
    61  func (s *HostNetworkSystem) folder() *Folder {
    62  	f := Map.getEntityDatacenter(s.Host).NetworkFolder
    63  	return Map.Get(f).(*Folder)
    64  }
    65  
    66  func (s *HostNetworkSystem) AddVirtualSwitch(c *types.AddVirtualSwitch) soap.HasFault {
    67  	r := &methods.AddVirtualSwitchBody{}
    68  
    69  	for _, vswitch := range s.NetworkInfo.Vswitch {
    70  		if vswitch.Name == c.VswitchName {
    71  			r.Fault_ = Fault("", &types.AlreadyExists{Name: c.VswitchName})
    72  			return r
    73  		}
    74  	}
    75  
    76  	s.NetworkInfo.Vswitch = append(s.NetworkInfo.Vswitch, types.HostVirtualSwitch{
    77  		Name: c.VswitchName,
    78  	})
    79  
    80  	r.Res = &types.AddVirtualSwitchResponse{}
    81  
    82  	return r
    83  }
    84  
    85  func (s *HostNetworkSystem) RemoveVirtualSwitch(c *types.RemoveVirtualSwitch) soap.HasFault {
    86  	r := &methods.RemoveVirtualSwitchBody{}
    87  
    88  	vs := s.NetworkInfo.Vswitch
    89  
    90  	for i, v := range vs {
    91  		if v.Name == c.VswitchName {
    92  			s.NetworkInfo.Vswitch = append(vs[:i], vs[i+1:]...)
    93  			r.Res = &types.RemoveVirtualSwitchResponse{}
    94  			return r
    95  		}
    96  	}
    97  
    98  	r.Fault_ = Fault("", &types.NotFound{})
    99  
   100  	return r
   101  }
   102  
   103  func (s *HostNetworkSystem) AddPortGroup(ctx *Context, c *types.AddPortGroup) soap.HasFault {
   104  	var vswitch *types.HostVirtualSwitch
   105  
   106  	r := &methods.AddPortGroupBody{}
   107  
   108  	if c.Portgrp.Name == "" {
   109  		r.Fault_ = Fault("", &types.InvalidArgument{InvalidProperty: "name"})
   110  		return r
   111  	}
   112  
   113  	for i := range s.NetworkInfo.Vswitch {
   114  		if s.NetworkInfo.Vswitch[i].Name == c.Portgrp.VswitchName {
   115  			vswitch = &s.NetworkInfo.Vswitch[i]
   116  			break
   117  		}
   118  	}
   119  
   120  	if vswitch == nil {
   121  		r.Fault_ = Fault("", &types.NotFound{})
   122  		return r
   123  	}
   124  
   125  	network := &mo.Network{}
   126  	network.Name = c.Portgrp.Name
   127  	network.Entity().Name = network.Name
   128  
   129  	folder := s.folder()
   130  
   131  	if obj := ctx.Map.FindByName(c.Portgrp.Name, folder.ChildEntity); obj != nil {
   132  		r.Fault_ = Fault("", &types.DuplicateName{
   133  			Name:   c.Portgrp.Name,
   134  			Object: obj.Reference(),
   135  		})
   136  
   137  		return r
   138  	}
   139  
   140  	folderPutChild(ctx, &folder.Folder, network)
   141  
   142  	vswitch.Portgroup = append(vswitch.Portgroup, c.Portgrp.Name)
   143  
   144  	s.NetworkInfo.Portgroup = append(s.NetworkInfo.Portgroup, types.HostPortGroup{
   145  		Key:  "key-vim.host.PortGroup-" + c.Portgrp.Name,
   146  		Port: nil,
   147  		Spec: c.Portgrp,
   148  	})
   149  
   150  	r.Res = &types.AddPortGroupResponse{}
   151  
   152  	return r
   153  }
   154  
   155  func (s *HostNetworkSystem) RemovePortGroup(ctx *Context, c *types.RemovePortGroup) soap.HasFault {
   156  	var vswitch *types.HostVirtualSwitch
   157  
   158  	r := &methods.RemovePortGroupBody{}
   159  
   160  	for i, v := range s.NetworkInfo.Vswitch {
   161  		for j, pg := range v.Portgroup {
   162  			if pg == c.PgName {
   163  				vswitch = &s.NetworkInfo.Vswitch[i]
   164  				vswitch.Portgroup = append(vswitch.Portgroup[:j], vswitch.Portgroup[j+1:]...)
   165  			}
   166  		}
   167  	}
   168  
   169  	if vswitch == nil {
   170  		r.Fault_ = Fault("", &types.NotFound{})
   171  		return r
   172  	}
   173  
   174  	folder := s.folder()
   175  	e := ctx.Map.FindByName(c.PgName, folder.ChildEntity)
   176  	folderRemoveChild(ctx, &folder.Folder, e.Reference())
   177  
   178  	for i, pg := range s.NetworkInfo.Portgroup {
   179  		if pg.Spec.Name == c.PgName {
   180  			var portgroup = s.NetworkInfo.Portgroup
   181  			s.NetworkInfo.Portgroup = append(portgroup[:i], portgroup[i+1:]...)
   182  		}
   183  	}
   184  
   185  	r.Res = &types.RemovePortGroupResponse{}
   186  
   187  	return r
   188  }
   189  
   190  func (s *HostNetworkSystem) UpdateNetworkConfig(req *types.UpdateNetworkConfig) soap.HasFault {
   191  	s.NetworkConfig = &req.Config
   192  
   193  	return &methods.UpdateNetworkConfigBody{
   194  		Res: &types.UpdateNetworkConfigResponse{
   195  			Returnval: types.HostNetworkConfigResult{},
   196  		},
   197  	}
   198  }
   199  
   200  func (s *HostNetworkSystem) QueryNetworkHint(req *types.QueryNetworkHint) soap.HasFault {
   201  	if s.Host.Runtime.ConnectionState != types.HostSystemConnectionStateConnected {
   202  		return &methods.QueryNetworkHintBody{
   203  			Fault_: Fault("", &types.HostNotConnected{}),
   204  		}
   205  	}
   206  
   207  	return &methods.QueryNetworkHintBody{
   208  		Res: &types.QueryNetworkHintResponse{
   209  			Returnval: s.QueryNetworkHintResponse.Returnval,
   210  		},
   211  	}
   212  }