github.com/vmware/govmomi@v0.51.0/eam/simulator/eam_object.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  	"time"
     9  
    10  	"github.com/vmware/govmomi/eam/methods"
    11  	"github.com/vmware/govmomi/eam/mo"
    12  	"github.com/vmware/govmomi/eam/types"
    13  	"github.com/vmware/govmomi/simulator"
    14  	"github.com/vmware/govmomi/vim25/soap"
    15  	vim "github.com/vmware/govmomi/vim25/types"
    16  )
    17  
    18  // EamObject contains the fields and functions common to all objects.
    19  type EamObject mo.EamObject
    20  
    21  func (m *EamObject) Reference() vim.ManagedObjectReference {
    22  	return m.Self
    23  }
    24  
    25  func (m *EamObject) AddIssue(
    26  	ctx *simulator.Context,
    27  	req *types.AddIssue) soap.HasFault {
    28  
    29  	// Get the typed issue to ensure the correct type of issue is stored and
    30  	// returned to the caller.
    31  	issue := issueType(req.Issue)
    32  
    33  	// Get the base issue in order to assign an issue key and timestamp.
    34  	baseIssue := issue.GetIssue()
    35  	baseIssue.Key = nextAvailableIssueKey()
    36  	baseIssue.Time = time.Now().UTC()
    37  
    38  	// Store and return the typed issue.
    39  	m.Issue = append(m.Issue, issue)
    40  
    41  	return &methods.AddIssueBody{
    42  		Res: &types.AddIssueResponse{
    43  			Returnval: issue,
    44  		},
    45  	}
    46  }
    47  
    48  func (m *EamObject) QueryIssue(
    49  	ctx *simulator.Context,
    50  	req *types.QueryIssue) soap.HasFault {
    51  
    52  	var issues []types.BaseIssue
    53  
    54  	if len(req.IssueKey) == 0 {
    55  		// If no keys were specified then return all issues.
    56  		issues = m.Issue
    57  	} else {
    58  		// Get only the issues for the specified keys.
    59  		for _, issueKey := range req.IssueKey {
    60  			for _, issue := range m.Issue {
    61  				if issue.GetIssue().Key == issueKey {
    62  					issues = append(issues, issue)
    63  				}
    64  			}
    65  		}
    66  	}
    67  
    68  	return &methods.QueryIssueBody{
    69  		Res: &types.QueryIssueResponse{
    70  			Returnval: issues,
    71  		},
    72  	}
    73  }
    74  
    75  func (m *EamObject) Resolve(
    76  	ctx *simulator.Context,
    77  	req *types.Resolve) soap.HasFault {
    78  
    79  	// notFoundKeys is a list of issue keys that were sent but
    80  	// not found for the given object.
    81  	notFoundKeys := []int32{}
    82  
    83  	// issueExists is a helper function that returns true
    84  	issueExists := func(issueKey int32) bool {
    85  		for _, k := range req.IssueKey {
    86  			if k == issueKey {
    87  				return true
    88  			}
    89  		}
    90  		return false
    91  	}
    92  
    93  	// Iterate over the object's issues, and if a key matches, then remove
    94  	// the issue from the list of the object's issues. If a key does not match
    95  	// then record the key as notFound.
    96  	for i := 0; i < len(m.Issue); i++ {
    97  		issueKey := m.Issue[i].GetIssue().Key
    98  
    99  		if ok := issueExists(issueKey); ok {
   100  			// Update the object's issue list so that it no longer includes
   101  			// the current issue.
   102  			m.Issue = append(m.Issue[:i], m.Issue[i+1:]...)
   103  			i--
   104  
   105  			// Ensure the key is removed from the global key space.
   106  			freeIssueKey(issueKey)
   107  		} else {
   108  			notFoundKeys = append(notFoundKeys, issueKey)
   109  		}
   110  	}
   111  
   112  	return &methods.ResolveBody{
   113  		Res: &types.ResolveResponse{
   114  			Returnval: notFoundKeys,
   115  		},
   116  	}
   117  }
   118  
   119  func (m *EamObject) ResolveAll(
   120  	ctx *simulator.Context,
   121  	req *types.ResolveAll) soap.HasFault {
   122  
   123  	// Iterate over the issues and ensure each one of their keys are removed
   124  	// from the global key space.
   125  	for _, issue := range m.Issue {
   126  		freeIssueKey(issue.GetIssue().Key)
   127  	}
   128  
   129  	// Reset the object's issues.
   130  	m.Issue = m.Issue[:0]
   131  
   132  	return &methods.ResolveAllBody{Res: &types.ResolveAllResponse{}}
   133  }