github.com/vmware/govmomi@v0.37.1/eam/simulator/agency.go (about)

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