dvs.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. Copyright (c) 2017 VMware, Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package simulator
  14. import (
  15. "github.com/vmware/govmomi/vim25/methods"
  16. "github.com/vmware/govmomi/vim25/mo"
  17. "github.com/vmware/govmomi/vim25/soap"
  18. "github.com/vmware/govmomi/vim25/types"
  19. )
  20. type DistributedVirtualSwitch struct {
  21. mo.DistributedVirtualSwitch
  22. }
  23. func (s *DistributedVirtualSwitch) AddDVPortgroupTask(c *types.AddDVPortgroup_Task) soap.HasFault {
  24. task := CreateTask(s, "addDVPortgroup", func(t *Task) (types.AnyType, types.BaseMethodFault) {
  25. f := Map.getEntityParent(s, "Folder").(*Folder)
  26. portgroups := s.Portgroup
  27. portgroupNames := s.Summary.PortgroupName
  28. for _, spec := range c.Spec {
  29. pg := &DistributedVirtualPortgroup{}
  30. pg.Name = spec.Name
  31. pg.Entity().Name = pg.Name
  32. if obj := Map.FindByName(pg.Name, f.ChildEntity); obj != nil {
  33. return nil, &types.DuplicateName{
  34. Name: pg.Name,
  35. Object: obj.Reference(),
  36. }
  37. }
  38. f.putChild(pg)
  39. pg.Key = pg.Self.Value
  40. pg.Config = types.DVPortgroupConfigInfo{
  41. Key: pg.Key,
  42. Name: pg.Name,
  43. NumPorts: spec.NumPorts,
  44. DistributedVirtualSwitch: &s.Self,
  45. DefaultPortConfig: spec.DefaultPortConfig,
  46. Description: spec.Description,
  47. Type: spec.Type,
  48. Policy: spec.Policy,
  49. PortNameFormat: spec.PortNameFormat,
  50. Scope: spec.Scope,
  51. VendorSpecificConfig: spec.VendorSpecificConfig,
  52. ConfigVersion: spec.ConfigVersion,
  53. AutoExpand: spec.AutoExpand,
  54. VmVnicNetworkResourcePoolKey: spec.VmVnicNetworkResourcePoolKey,
  55. }
  56. if pg.Config.DefaultPortConfig == nil {
  57. pg.Config.DefaultPortConfig = &types.VMwareDVSPortSetting{
  58. Vlan: new(types.VmwareDistributedVirtualSwitchVlanIdSpec),
  59. }
  60. }
  61. pg.PortKeys = []string{}
  62. portgroups = append(portgroups, pg.Self)
  63. portgroupNames = append(portgroupNames, pg.Name)
  64. for _, h := range s.Summary.HostMember {
  65. pg.Host = append(pg.Host, h)
  66. host := Map.Get(h).(*HostSystem)
  67. Map.AppendReference(host, &host.Network, pg.Reference())
  68. parent := Map.Get(*host.HostSystem.Parent)
  69. computeNetworks := append(hostParent(&host.HostSystem).Network, pg.Reference())
  70. Map.Update(parent, []types.PropertyChange{
  71. {Name: "network", Val: computeNetworks},
  72. })
  73. }
  74. }
  75. Map.Update(s, []types.PropertyChange{
  76. {Name: "portgroup", Val: portgroups},
  77. {Name: "summary.portgroupName", Val: portgroupNames},
  78. })
  79. return nil, nil
  80. })
  81. return &methods.AddDVPortgroup_TaskBody{
  82. Res: &types.AddDVPortgroup_TaskResponse{
  83. Returnval: task.Run(),
  84. },
  85. }
  86. }
  87. func (s *DistributedVirtualSwitch) ReconfigureDvsTask(req *types.ReconfigureDvs_Task) soap.HasFault {
  88. task := CreateTask(s, "reconfigureDvs", func(t *Task) (types.AnyType, types.BaseMethodFault) {
  89. spec := req.Spec.GetDVSConfigSpec()
  90. members := s.Summary.HostMember
  91. for _, member := range spec.Host {
  92. h := Map.Get(member.Host)
  93. if h == nil {
  94. return nil, &types.ManagedObjectNotFound{Obj: member.Host}
  95. }
  96. host := h.(*HostSystem)
  97. switch types.ConfigSpecOperation(member.Operation) {
  98. case types.ConfigSpecOperationAdd:
  99. if FindReference(s.Summary.HostMember, member.Host) != nil {
  100. return nil, &types.AlreadyExists{Name: host.Name}
  101. }
  102. hostNetworks := append(host.Network, s.Portgroup...)
  103. Map.Update(host, []types.PropertyChange{
  104. {Name: "network", Val: hostNetworks},
  105. })
  106. members = append(members, member.Host)
  107. parent := Map.Get(*host.HostSystem.Parent)
  108. var pgs []types.ManagedObjectReference
  109. for _, ref := range s.Portgroup {
  110. pg := Map.Get(ref).(*DistributedVirtualPortgroup)
  111. pgs = append(pgs, ref)
  112. pgHosts := append(pg.Host, member.Host)
  113. Map.Update(pg, []types.PropertyChange{
  114. {Name: "host", Val: pgHosts},
  115. })
  116. }
  117. Map.Update(parent, []types.PropertyChange{
  118. {Name: "network", Val: pgs},
  119. })
  120. case types.ConfigSpecOperationRemove:
  121. for _, ref := range host.Vm {
  122. vm := Map.Get(ref).(*VirtualMachine)
  123. if pg := FindReference(vm.Network, s.Portgroup...); pg != nil {
  124. return nil, &types.ResourceInUse{
  125. Type: pg.Type,
  126. Name: pg.Value,
  127. }
  128. }
  129. }
  130. RemoveReference(&members, member.Host)
  131. case types.ConfigSpecOperationEdit:
  132. return nil, &types.NotSupported{}
  133. }
  134. }
  135. Map.Update(s, []types.PropertyChange{
  136. {Name: "summary.hostMember", Val: members},
  137. })
  138. return nil, nil
  139. })
  140. return &methods.ReconfigureDvs_TaskBody{
  141. Res: &types.ReconfigureDvs_TaskResponse{
  142. Returnval: task.Run(),
  143. },
  144. }
  145. }
  146. func (s *DistributedVirtualSwitch) FetchDVPorts(req *types.FetchDVPorts) soap.HasFault {
  147. body := &methods.FetchDVPortsBody{}
  148. body.Res = &types.FetchDVPortsResponse{
  149. Returnval: s.dvPortgroups(req.Criteria),
  150. }
  151. return body
  152. }
  153. func (s *DistributedVirtualSwitch) DestroyTask(req *types.Destroy_Task) soap.HasFault {
  154. task := CreateTask(s, "destroy", func(t *Task) (types.AnyType, types.BaseMethodFault) {
  155. f := Map.getEntityParent(s, "Folder").(*Folder)
  156. f.removeChild(s.Reference())
  157. return nil, nil
  158. })
  159. return &methods.Destroy_TaskBody{
  160. Res: &types.Destroy_TaskResponse{
  161. Returnval: task.Run(),
  162. },
  163. }
  164. }
  165. func (s *DistributedVirtualSwitch) dvPortgroups(_ *types.DistributedVirtualSwitchPortCriteria) []types.DistributedVirtualPort {
  166. // TODO(agui): Filter is not implemented yet
  167. var res []types.DistributedVirtualPort
  168. for _, ref := range s.Portgroup {
  169. pg := Map.Get(ref).(*DistributedVirtualPortgroup)
  170. res = append(res, types.DistributedVirtualPort{
  171. DvsUuid: s.Uuid,
  172. Key: pg.Key,
  173. Config: types.DVPortConfigInfo{
  174. Setting: pg.Config.DefaultPortConfig,
  175. },
  176. })
  177. for _, key := range pg.PortKeys {
  178. res = append(res, types.DistributedVirtualPort{
  179. DvsUuid: s.Uuid,
  180. Key: key,
  181. Config: types.DVPortConfigInfo{
  182. Setting: pg.Config.DefaultPortConfig,
  183. },
  184. })
  185. }
  186. }
  187. return res
  188. }