Пример #1
0
    def _update_ip_translation_nat_rule(nat_rule,
                                        vapp_scoped_vm_id=None,
                                        vm_nic_id=None,
                                        mapping_mode=None,
                                        ext_ip_address=None):
        """Update IP translation NAT rule of vApp network.

        :param str vapp_scoped_vm_id: vapp network scoped vm id.
        :param str vm_nic_id: vapp network scoped vm nic id.
        :param str mapping_mode: NAT rule mapping mode (automatic/manual) if
            nat_type is ipTranslation.
        :param str external_ip_address: external ip address if mapping mode is
            manual.
        """
        one_to_one_rule = nat_rule.OneToOneVmRule
        if vapp_scoped_vm_id is not None:
            one_to_one_rule.VAppScopedVmId = E.VAppScopedVmId(
                vapp_scoped_vm_id)
        if vm_nic_id is not None:
            one_to_one_rule.VmNicId = E.VmNicId(vm_nic_id)
        if mapping_mode is not None:
            one_to_one_rule.MappingMode = E.MappingMode(mapping_mode)
        if ext_ip_address is not None:
            ext_ip_node = E.ExternalIpAddress(ext_ip_address)
            if one_to_one_rule.MappingMode == 'manual' and hasattr(
                    one_to_one_rule, 'ExternalIpAddress'):
                one_to_one_rule.ExternalIpAddress = ext_ip_node
            elif one_to_one_rule.MappingMode == 'manual':
                one_to_one_rule.MappingMode.addnext(ext_ip_node)
Пример #2
0
    def add_nat_rule(self,
                     nat_type,
                     vapp_scoped_vm_id,
                     vm_nic_id,
                     mapping_mode='automatic',
                     external_ip_address=None,
                     external_port=-1,
                     internal_port=-1,
                     protocol='TCP'):
        """Add NAT rule to vApp network.

        :param str nat_type: NAT type (portForwarding/ipTranslation).
        :param str vapp_scoped_vm_id: vapp network scoped vm id.
        :param str vm_nic_id: vapp network scoped vm nic id.
        :param str mapping_mode: NAT rule mapping mode (automatic/manual) if
            nat_type is ipTranslation.
        :param str external_ip_address: external ip address if mapping mode is
            manual.
        :param int external_port: external port of NAT rule if nat_type is
            portForwarding.
        :param int internal_port: internal port of NAT rule if nat_type is
            portForwarding.
        :param str protocol: protocol of NAT rule if nat_type is
            portForwarding.
        :return: an object containing EntityType.TASK XML data which represents
            the asynchronous task that is updating the vApp network.
        :rtype: lxml.objectify.ObjectifiedElement
        :raises: InvalidParameterException: Enable NAT service failed as
            given network's connection is not routed
        """
        self._get_resource()
        fence_mode = self.resource.Configuration.FenceMode
        if fence_mode != 'natRouted':
            raise InvalidParameterException(
                "Enable NAT service failed as given network's connection "
                "is not routed")
        features = self.resource.Configuration.Features
        if not hasattr(features, 'NatService'):
            VappNat._makeNatServiceAttr(features)
        nat_service = features.NatService
        if nat_service.NatType != nat_type:
            VappNat._delete_all_nat_rule(nat_service)
            nat_service.NatType = E.NatType(nat_type)
        nat_rule = E.NatRule()
        if nat_type == 'ipTranslation':
            one_to_vm_rule = E.OneToOneVmRule()
            one_to_vm_rule.append(E.MappingMode(mapping_mode))
            if mapping_mode == 'manual':
                one_to_vm_rule.append(E.ExternalIpAddress(external_ip_address))
            one_to_vm_rule.append(E.VAppScopedVmId(vapp_scoped_vm_id))
            one_to_vm_rule.append(E.VmNicId(vm_nic_id))
            nat_rule.append(one_to_vm_rule)
        elif nat_type == 'portForwarding':
            vm_rule = E.VmRule()
            vm_rule.append(E.ExternalPort(external_port))
            vm_rule.append(E.VAppScopedVmId(vapp_scoped_vm_id))
            vm_rule.append(E.VmNicId(vm_nic_id))
            vm_rule.append(E.InternalPort(internal_port))
            vm_rule.append(E.Protocol(protocol))
            nat_rule.append(vm_rule)
        nat_service.append(nat_rule)
        return self.client.put_linked_resource(self.resource,
                                               RelationType.EDIT,
                                               EntityType.vApp_Network.value,
                                               self.resource)