github.com/vmware/govmomi@v0.51.0/eam/simulator/agency.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package simulator 6 7 import ( 8 "fmt" 9 "math/rand" 10 "time" 11 12 "github.com/google/uuid" 13 14 "github.com/vmware/govmomi/eam/internal" 15 "github.com/vmware/govmomi/eam/methods" 16 "github.com/vmware/govmomi/eam/mo" 17 "github.com/vmware/govmomi/eam/types" 18 "github.com/vmware/govmomi/simulator" 19 "github.com/vmware/govmomi/vim25" 20 "github.com/vmware/govmomi/vim25/soap" 21 vim "github.com/vmware/govmomi/vim25/types" 22 ) 23 24 // Agency handles the deployment of a single type of agent virtual 25 // machine and any associated VIB bundle, on a set of compute resources. 26 type Agency struct { 27 EamObject 28 mo.Agency 29 } 30 31 // NewAgency returns a new Agency as if CreateAgency were called on the 32 // EsxAgentManager object. 33 func NewAgency( 34 ctx *simulator.Context, 35 baseAgencyConfig types.BaseAgencyConfigInfo, 36 initialGoalState string) (*Agency, vim.BaseMethodFault) { 37 38 agencyConfig := baseAgencyConfig.GetAgencyConfigInfo() 39 if agencyConfig.AgentName == "" { 40 agencyConfig.AgentName = agencyConfig.AgencyName 41 } 42 43 // Define a new Agency object. 44 agency := &Agency{ 45 EamObject: EamObject{ 46 Self: vim.ManagedObjectReference{ 47 Type: internal.Agency, 48 Value: uuid.New().String(), 49 }, 50 }, 51 Agency: mo.Agency{ 52 Config: agencyConfig, 53 Runtime: types.EamObjectRuntimeInfo{ 54 GoalState: initialGoalState, 55 }, 56 }, 57 } 58 59 // Register the agency with the registry in order for the agency to 60 // start receiving API calls from clients. 61 ctx.Map.Put(agency) 62 63 // Define a random numbrer generator to help select resources for the 64 // agent VMs. 65 rng := rand.New(rand.NewSource(time.Now().UnixNano())) 66 67 // Alias the registry that contains the vim25 objects. 68 vimMap := ctx.For(vim25.Path).Map 69 70 // Create the agents. 71 for i, agentConfig := range agencyConfig.AgentConfig { 72 73 // vmName follows the defined pattern for naming agent VMs 74 vmName := fmt.Sprintf("%s (%d)", agencyConfig.AgentName, i+1) 75 76 // vmPlacement contains MoRefs to the resources required to create and 77 // place the VM inside of the inventory. 78 vmPlacement, err := getAgentVMPlacementOptions( 79 ctx, 80 vimMap, 81 rng, 82 i, 83 agencyConfig) 84 if err != nil { 85 return nil, &vim.MethodFault{ 86 FaultCause: &vim.LocalizedMethodFault{ 87 LocalizedMessage: err.Error(), 88 }, 89 } 90 } 91 92 if _, fault := NewAgent( 93 ctx, 94 agency.Self, 95 agentConfig, 96 vmName, 97 vmPlacement); fault != nil { 98 99 return nil, fault 100 } 101 } 102 103 return agency, nil 104 } 105 106 func (m *Agency) AgencyQueryRuntime( 107 ctx *simulator.Context, 108 req *types.AgencyQueryRuntime) soap.HasFault { 109 110 // Copy the agency's issues into its runtime object upon return. 111 m.Runtime.Issue = make([]types.BaseIssue, len(m.Issue)) 112 i := 0 113 for _, issue := range m.Issue { 114 m.Runtime.Issue[i] = issue 115 i++ 116 } 117 118 return &methods.AgencyQueryRuntimeBody{ 119 Res: &types.AgencyQueryRuntimeResponse{ 120 Returnval: &m.Runtime, 121 }, 122 } 123 } 124 125 func (m *Agency) DestroyAgency( 126 ctx *simulator.Context, 127 req *types.DestroyAgency) soap.HasFault { 128 129 // Remove any agents associated with this agency. 130 agentObjs := ctx.Map.AllReference(internal.Agent) 131 for _, obj := range agentObjs { 132 agent := obj.(*Agent) 133 if *agent.Runtime.Agency == m.Self { 134 ctx.Map.Remove(ctx, agent.Self) 135 } 136 } 137 138 ctx.Map.Remove(ctx, m.Self) 139 return &methods.DestroyAgencyBody{ 140 Res: &types.DestroyAgencyResponse{}, 141 } 142 } 143 144 func (m *Agency) Agency_Disable( 145 ctx *simulator.Context, 146 req *types.Agency_Disable) soap.HasFault { 147 148 m.Runtime.GoalState = string(types.EamObjectRuntimeInfoGoalStateDisabled) 149 150 return &methods.Agency_DisableBody{ 151 Res: &types.Agency_DisableResponse{}, 152 } 153 } 154 155 func (m *Agency) Agency_Enable( 156 ctx *simulator.Context, 157 req *types.Agency_Enable) soap.HasFault { 158 159 m.Runtime.GoalState = string(types.EamObjectRuntimeInfoGoalStateEnabled) 160 161 return &methods.Agency_EnableBody{ 162 Res: &types.Agency_EnableResponse{}, 163 } 164 } 165 166 func (m *Agency) QueryAgent( 167 ctx *simulator.Context, 168 req *types.QueryAgent) soap.HasFault { 169 170 objs := ctx.Map.AllReference(internal.Agent) 171 moRefs := make([]vim.ManagedObjectReference, len(objs)) 172 i := 0 173 for _, ref := range objs { 174 moRefs[i] = ref.Reference() 175 i++ 176 } 177 return &methods.QueryAgentBody{ 178 Res: &types.QueryAgentResponse{ 179 Returnval: moRefs, 180 }, 181 } 182 } 183 184 func (m *Agency) QueryConfig( 185 ctx *simulator.Context, 186 req *types.QueryConfig) soap.HasFault { 187 188 return &methods.QueryConfigBody{ 189 Res: &types.QueryConfigResponse{ 190 Returnval: m.Config, 191 }, 192 } 193 } 194 195 func (m *Agency) RegisterAgentVm( 196 ctx *simulator.Context, 197 req *types.RegisterAgentVm) soap.HasFault { 198 199 return &methods.RegisterAgentVmBody{ 200 Res: &types.RegisterAgentVmResponse{ 201 Returnval: vim.ManagedObjectReference{}, 202 }, 203 } 204 } 205 206 func (m *Agency) Uninstall( 207 ctx *simulator.Context, 208 req *types.Uninstall) soap.HasFault { 209 210 m.Runtime.GoalState = string(types.EamObjectRuntimeInfoGoalStateUninstalled) 211 212 return &methods.UninstallBody{ 213 Res: &types.UninstallResponse{}, 214 } 215 } 216 217 func (m *Agency) UnregisterAgentVm( 218 ctx *simulator.Context, 219 req *types.UnregisterAgentVm) soap.HasFault { 220 221 return &methods.UnregisterAgentVmBody{ 222 Res: &types.UnregisterAgentVmResponse{}, 223 } 224 } 225 226 func (m *Agency) Update( 227 ctx *simulator.Context, 228 req *types.Update) soap.HasFault { 229 230 m.Config = req.Config 231 232 return &methods.UpdateBody{ 233 Res: &types.UpdateResponse{}, 234 } 235 }