| 1 |  |  | #!/usr/bin/env python | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | # Licensed to the StackStorm, Inc ('StackStorm') under one or more | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | # contributor license agreements.  See the NOTICE file distributed with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | # this work for additional information regarding copyright ownership. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | # The ASF licenses this file to You under the Apache License, Version 2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | # (the "License"); you may not use this file except in compliance with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | # the License.  You may obtain a copy of the License at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | #     http://www.apache.org/licenses/LICENSE-2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | # Unless required by applicable law or agreed to in writing, software | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | # distributed under the License is distributed on an "AS IS" BASIS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | # See the License for the specific language governing permissions and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | # limitations under the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | from pyVmomi import vim | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | def get_managed_entity(content, vimtype, moid=None, name=None): | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     if not name and not moid: | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         return | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     container = content.viewManager.CreateContainerView( | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         content.rootFolder, [vimtype], True) | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     count = 0 | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     for entity in container.view: | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         # Find matches in the results | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         if moid and entity._moId == moid: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |             results = entity | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |             count += 1 | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         elif name and entity.name == name: | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |             results = entity | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |             count += 1 | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         # check to see if multiple matches were found | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         if count >= 2: | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |             raise Exception("Multiple Managed Objects found,\ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |                             Check Names or IDs provided are unique") | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         elif count == 1: | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |             # Single Match found | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |             return results | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     #if this area is reached no object has been found | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     #if a name was passed error | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     if name: | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         raise Exception("Inventory Error: Unable to Find Object (%s): %s" | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |                         % (vimtype, name)) | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     #if a moid was passed error | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     elif moid: | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         raise Exception("Inventory Error: Unable to Find Object (%s): %s" | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                         % (vimtype, moid)) | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     #catch all error | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     else: | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         raise Exception("Inventory Error: No Name or moid provided (%s)" | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                         % vimtype) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | def get_managed_entities(content, vimtype): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     container = content.viewManager.CreateContainerView( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         content.rootFolder, [vimtype], True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     return container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | def get_datacenter(content, moid=None, name=None): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     return get_managed_entity(content, vim.Datacenter, moid=moid, name=name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | def get_cluster(content, moid=None, name=None): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     return get_managed_entity(content, vim.ClusterComputeResource, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                               moid=moid, name=name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | def get_folder(content, moid=None, name=None): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     return get_managed_entity(content, vim.Folder, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                               moid=moid, name=name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | def get_resource_pool(content, moid=None, name=None): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     return get_managed_entity(content, vim.ResourcePool, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                               moid=moid, name=name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | def get_datastore_cluster(content, moid=None, name=None): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     return get_managed_entity(content, vim.StoragePod, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                               moid=moid, name=name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | def get_datastore(content, moid=None, name=None): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     return get_managed_entity(content, vim.Datastore, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                               moid=moid, name=name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | def get_network(content, moid=None, name=None): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     return get_managed_entity(content, vim.Network, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                               moid=moid, name=name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | def get_virtualmachine(content, moid=None, name=None): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     return get_managed_entity(content, vim.VirtualMachine, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                               moid=moid, name=name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | def get_virtualmachines(content): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     return get_managed_entities(content, vim.VirtualMachine) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | def get_task(content, moid=None): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     return get_managed_entity(content, vim.Task, | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 109 |  |  |                               moid=moid, name=None) | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |  | 
            
                        
Prefixing a member variable
_is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class: