def _validate_vm_cluster(self, si, all_items_in_vc, auto_att, dc_name, attributes, key):
     accepted_types = (vim.ClusterComputeResource, vim.HostSystem)
     cluster = self._validate_attribute(si, attributes, accepted_types, key, dc_name)
     if not cluster:
         cluster = self._get_default(all_items_in_vc, accepted_types, key)
         c_name = self.get_full_name(dc_name, cluster)
         # removing the upper folder
         c_name = c_name.replace('host/', '')
     else:
         c_name = attributes[key]
     auto_att.append(AutoLoadAttribute('', key, c_name))
     return cluster
 def _validate_vm_storage(self, si, all_items_in_vc, auto_att, dc_name,
                          attributes, key):
     accepted_types = (vim.Datastore, vim.StoragePod)
     datastore = self._validate_attribute(si, attributes, accepted_types,
                                          key, dc_name)
     if not datastore:
         datastore = self._get_default(all_items_in_vc, accepted_types, key)
         d_name = self.get_full_name(dc_name, datastore)
         # removing the upper folder
         d_name = d_name.replace('datastore/', '')
     else:
         d_name = attributes[key]
     auto_att.append(AutoLoadAttribute('', key, d_name))
    def _validate_vm_resource_pool(self, si, all_items_in_vc, auto_att,
                                   dc_name, attributes, key):
        cluster = self._validate_vm_cluster(si, all_items_in_vc, auto_att,
                                            dc_name, attributes, VM_CLUSTER)

        if key not in attributes or not attributes[key]:
            return
        pool_name = attributes[key]
        pool = self._find_resource_pool_by_path(pool_name, cluster)
        if pool:
            auto_att.append(AutoLoadAttribute('', key, pool_name))
            return

        raise ValueError(
            'The given resource pool not found: {0}'.format(pool_name))
    def _validate_default_dvswitch(self, si, all_items_in_vc, auto_att,
                                   dc_name, attributes, key):

        dvs_path = attributes[key]

        # optional pararm
        if (not attributes[key]):
            return

        path = "{}/{}".format(dc_name, dvs_path)
        dv = self.pv_service.find_dvs_by_path(si, path)

        auto_att.append(AutoLoadAttribute('', DEFAULT_DVSWITCH, dvs_path))

        return dv
    def _validate_saved_sandbox_storage(self, si, all_items_in_vc, auto_att,
                                        dc_name, attributes, key):
        accepted_types = (vim.Datastore, vim.StoragePod)

        # saved sandbox storage isnt mandatory so if not configured exit without validation
        if key not in attributes or not attributes[key]:
            return

        datastore = self._validate_attribute(si, attributes, accepted_types,
                                             key, dc_name)
        if not datastore:
            datastore = self._get_default(all_items_in_vc, accepted_types, key)
            d_name = self.get_full_name(dc_name, datastore)
            # removing the upper folder
            d_name = d_name.replace('datastore/', '')
        else:
            d_name = attributes[key]
        auto_att.append(AutoLoadAttribute('', key, d_name))
    def _validate_vm_resource_pool(self, si, all_items_in_vc, auto_att,
                                   dc_name, attributes, key):
        if key not in attributes or not attributes[key]:
            return

        cluster = self._validate_vm_cluster(si, all_items_in_vc, auto_att,
                                            dc_name, attributes, VM_CLUSTER)

        pool_name = attributes[key]

        rp_params = ResourcePoolParams()
        rp_params.cluster_name = cluster.name
        rp_params.resource_pool = pool_name
        rp_params.si = si

        pool = self.pv_service.get_resource_pool(self.dc, rp_params)
        if pool:
            auto_att.append(AutoLoadAttribute('', key, pool_name))
            return

        raise ValueError(
            'The given resource pool not found: {0}'.format(pool_name))
示例#7
0
    def get_inventory(self, context):
        """ Will locate vm in vcenter and fill its uuid """

        logger = get_logger_with_thread_id(context)
        logger.info("Start Autoload process")

        session = self.cs_helper.get_session(
            context.connectivity.server_address,
            context.connectivity.admin_auth_token, self.DOMAIN)

        vcenter_vblade = context.resource.attributes[
            "{}.vFirewall vCenter VM".format(self.SHELL_NAME)].replace(
                "\\", "/")
        vcenter_name = context.resource.attributes["{}.vCenter Name".format(
            self.SHELL_NAME)]

        logger.info("Start AutoLoading VM_Path: {0} on vCenter: {1}".format(
            vcenter_vblade, vcenter_name))

        vcenter_api_res = session.GetResourceDetails(vcenter_name)
        vcenter_resource = self.model_parser.convert_to_vcenter_model(
            vcenter_api_res)

        si = None

        try:
            logger.info("Connecting to vCenter ({0})".format(
                vcenter_api_res.Address))
            si = self._get_connection_to_vcenter(self.pv_service, session,
                                                 vcenter_resource,
                                                 vcenter_api_res.Address)

            logger.info("Loading VMs UUID")
            vm_loader = VMLoader(self.pv_service)

            vfw_uuid = vm_loader.load_vm_uuid_by_name(si, vcenter_resource,
                                                      vcenter_vblade)
            logger.info("PanOS vFirewall VM UUID: {0}".format(vfw_uuid))
            logger.info("Loading the IP of the PanOS vFirewall VM")
            vfw_ip = self._try_get_ip(self.pv_service, si, vfw_uuid,
                                      vcenter_resource, logger)
            if vfw_ip:
                session.UpdateResourceAddress(context.resource.name, vfw_ip)
            else:
                raise Exception(
                    "Determination of PanOS vFirewall IP address failed."
                    "Please, verify that VM is up and running")

            vm = self.pv_service.get_vm_by_uuid(si, vfw_uuid)

            phys_interfaces = []

            for device in vm.config.hardware.device:
                if isinstance(device, vim.vm.device.VirtualEthernetCard):
                    phys_interfaces.append(device)

            resources = []
            attributes = []
            for port_number, phys_interface in enumerate(phys_interfaces):
                if port_number == 0:  # First interface (port number 0) should be Management
                    continue

                network_adapter_number = phys_interface.deviceInfo.label.lower(
                ).strip("network adapter ")
                unique_id = hash(phys_interface.macAddress)

                relative_address = "P{}".format(port_number)

                resources.append(
                    AutoLoadResource(model="{}.{}".format(
                        self.SHELL_NAME, self.PORT_MODEL),
                                     name="Port {}".format(port_number),
                                     relative_address=relative_address,
                                     unique_identifier=unique_id))

                attributes.append(
                    AutoLoadAttribute(
                        attribute_name="{}.{}.MAC Address".format(
                            self.SHELL_NAME, self.PORT_MODEL),
                        attribute_value=phys_interface.macAddress,
                        relative_address=relative_address))

                attributes.append(
                    AutoLoadAttribute(
                        attribute_name="{}.{}.Requested vNIC Name".format(
                            self.SHELL_NAME, self.PORT_MODEL),
                        attribute_value=network_adapter_number,
                        relative_address=relative_address))

                attributes.append(
                    AutoLoadAttribute(
                        attribute_name="{}.{}.Logical Name".format(
                            self.SHELL_NAME, self.PORT_MODEL),
                        attribute_value="Interface {}".format(port_number),
                        relative_address=relative_address))

            attributes.append(
                AutoLoadAttribute("", "VmDetails",
                                  self._get_vm_details(vfw_uuid,
                                                       vcenter_name)))

            autoload_details = AutoLoadDetails(resources=resources,
                                               attributes=attributes)
        except Exception:
            logger.exception("Get inventory command failed")
            raise
        finally:
            if si:
                self.pv_service.disconnect(si)

        return autoload_details
示例#8
0
 def _get_auto_load_response(self, uuid, vcenter_name, resource):
     vm_details = self._get_vm_details(uuid, vcenter_name, resource)
     # return vm_details
     autoload_atts = [AutoLoadAttribute('', 'VmDetails', vm_details)]
     return AutoLoadDetails([], autoload_atts)
 def _validate_shutdown_method(self, si, all_items_in_vc, auto_att, dc_name,
                               attributes, key):
     method = attributes[key]
     if self._is_in_array(key, method, SHUTDOWN_METHODS):
         auto_att.append(AutoLoadAttribute('', key, method))
示例#10
0
 def _validate_datacenter(self, si, all_item_in_vc, auto_att, attributes):
     dc = self._validate_attribute(si, attributes, vim.Datacenter, DEFAULT_DATACENTER)
     if not dc:
         dc = self._get_default(all_item_in_vc, vim.Datacenter, DEFAULT_DATACENTER)
     auto_att.append(AutoLoadAttribute('', DEFAULT_DATACENTER, dc.name))
     return dc