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