| 1 |  |  | import uuid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | from pyVmomi import vim | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | from vmwarelib import inventory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | from vmwarelib.actions import BaseAction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 9 |  |  | class VMAddNic(BaseAction): | 
            
                                                        
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 11 |  |  |     def run(self, vm_id, network_id, ip, subnet, gateway=None, domain=None): | 
            
                                                        
            
                                    
            
            
                | 12 |  |  |         # convert ids to stubs | 
            
                                                        
            
                                    
            
            
                | 13 |  |  |         virtualmachine = inventory.get_virtualmachine(self.si_content, vm_id) | 
            
                                                        
            
                                    
            
            
                | 14 |  |  |         network = inventory.get_network(self.si_content, network_id) | 
            
                                                        
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 16 |  |  |         # add new vnic | 
            
                                                        
            
                                    
            
            
                | 17 |  |  |         configspec = vim.vm.ConfigSpec() | 
            
                                                        
            
                                    
            
            
                | 18 |  |  |         nic = vim.vm.device.VirtualDeviceSpec() | 
            
                                                        
            
                                    
            
            
                | 19 |  |  |         nic.operation = vim.vm.device.VirtualDeviceSpec.Operation.add  # or edit if a device exists | 
            
                                                        
            
                                    
            
            
                | 20 |  |  |         nic.device = vim.vm.device.VirtualVmxnet3() | 
            
                                                        
            
                                    
            
            
                | 21 |  |  |         nic.device.wakeOnLanEnabled = True | 
            
                                                        
            
                                    
            
            
                | 22 |  |  |         nic.device.addressType = 'assigned' | 
            
                                                        
            
                                    
            
            
                | 23 |  |  |         # docs recommend using unique negative integers as temporary keys. | 
            
                                                        
            
                                    
            
            
                | 24 |  |  |         # See https://github.com/vmware/pyvmomi/blob/master/docs/vim/vm/device/VirtualDevice.rst | 
            
                                                        
            
                                    
            
            
                | 25 |  |  |         nic.device.key = -1 | 
            
                                                        
            
                                    
            
            
                | 26 |  |  |         nic.device.deviceInfo = vim.Description() | 
            
                                                        
            
                                    
            
            
                | 27 |  |  |         nic.device.deviceInfo.label = 'Network Adapter-%s' % (ip) | 
            
                                                        
            
                                    
            
            
                | 28 |  |  |         nic.device.deviceInfo.summary = 'summary' | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |         nic.device.backing = vim.vm.device.VirtualEthernetCard.DistributedVirtualPortBackingInfo() | 
            
                                                        
            
                                    
            
            
                | 30 |  |  |         nic.device.backing.port = vim.dvs.PortConnection() | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |         nic.device.backing.port.switchUuid = network.config.distributedVirtualSwitch.uuid | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |         nic.device.backing.port.portgroupKey = network.config.key | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |         nic.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo() | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |         nic.device.connectable.startConnected = True | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |         nic.device.connectable.allowGuestControl = True | 
            
                                                        
            
                                    
            
            
                | 36 |  |  |         configspec.deviceChange = [nic] | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |         add_vnic_task = virtualmachine.ReconfigVM_Task(configspec) | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |         successfully_added_vnic = self._wait_for_task(add_vnic_task) | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |         # customize VM | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |         cust_item = vim.CustomizationSpecItem() | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |         cust_specinfo = vim.CustomizationSpecInfo() | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |         cust_specinfo.name = 'assignip-' + str(uuid.uuid4()) | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |         cust_specinfo.type = 'Linux' | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |         cust_item.info = cust_specinfo | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |         # fixed ip | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |         cust_spec = vim.vm.customization.Specification() | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |         cust_item.spec = cust_spec | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |         ip_adapter_mapping = vim.vm.customization.AdapterMapping() | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |         ip_adapter_mapping.adapter = vim.vm.customization.IPSettings() | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |         ip_adapter_mapping.adapter.ip = vim.vm.customization.FixedIp() | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |         ip_adapter_mapping.adapter.ip.ipAddress = ip | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |         ip_adapter_mapping.adapter.subnetMask = subnet | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |         ip_adapter_mapping.adapter.gateway = gateway | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |         ip_adapter_mapping.adapter.dnsDomain = domain | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |         cust_spec.nicSettingMap = [ip_adapter_mapping] | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |         cust_spec.identity = vim.vm.customization.LinuxPrep() | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |         cust_spec.identity.hostName = vim.vm.customization.PrefixNameGenerator() | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |         cust_spec.identity.hostName.base = 'st2' | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         cust_spec.identity.domain = 'demo.net' | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |         cust_spec.globalIPSettings = vim.vm.customization.GlobalIPSettings() | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |         try: | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |             self.si_content.customizationSpecManager.CreateCustomizationSpec(cust_item) | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |         except: | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |             self.logger.exception('Failed to create customization spec.') | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |             raise | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |         return {'state': successfully_added_vnic} | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |  |