示例#1
0
    def set_dhcp_service(self, ip_range, default_lease_time, max_lease_time):
        """Set DHCP to vApp network.

        :param str ip_range: IP range in StartAddress-EndAddress format.
        :param str default_lease_time: default lease time.
        :param str max_lease_time: max lease time
        :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: Set DHCP service failed as given
            network's connection is Direct
        """
        self._get_resource()
        if self.resource.Configuration.FenceMode == 'bridged':
            raise InvalidParameterException(
                "Set DHCP service failed as given network's connection is "
                "Direct")
        ip_ranges = ip_range.split('-')
        if not hasattr(self.resource.Configuration, 'Features'):
            index = self.resource.Configuration.index(
                self.resource.Configuration.GuestVlanAllowed)
            self.resource.Configuration.insert(index, E.Features())
        if not hasattr(self.resource.Configuration.Features, 'DhcpService'):
            self.resource.Configuration.Features.append(E.DhcpService())
        dhcp = self.resource.Configuration.Features.DhcpService
        if hasattr(dhcp, 'IsEnabled'):
            dhcp.IsEnabled = E.IsEnabled(True)
        else:
            dhcp.append(E.IsEnabled(True))
        if hasattr(dhcp, 'DefaultLeaseTime'):
            dhcp.DefaultLeaseTime = E.DefaultLeaseTime(default_lease_time)
        else:
            dhcp.append(E.DefaultLeaseTime(default_lease_time))
        if hasattr(dhcp, 'MaxLeaseTime'):
            dhcp.MaxLeaseTime = E.MaxLeaseTime(max_lease_time)
        else:
            dhcp.append(E.MaxLeaseTime(max_lease_time))
        if hasattr(dhcp, 'IpRange'):
            dhcp.IpRange.StartAddress = E.StartAddress(ip_ranges[0])
            dhcp.IpRange.EndAddress = E.EndAddress(ip_ranges[1])
        else:
            dhcp.append(
                E.IpRange(E.StartAddress(ip_ranges[0]),
                          E.EndAddress(ip_ranges[1])))
        return self.client.put_linked_resource(self.resource,
                                               RelationType.EDIT,
                                               EntityType.vApp_Network.value,
                                               self.resource)
示例#2
0
文件: vdc.py 项目: joybwang/pyvcloud
    def create_isolated_vdc_network(self,
                                    network_name,
                                    gateway_ip,
                                    netmask,
                                    description=None,
                                    primary_dns_ip=None,
                                    secondary_dns_ip=None,
                                    dns_suffix=None,
                                    ip_range_start=None,
                                    ip_range_end=None,
                                    is_dhcp_enabled=None,
                                    default_lease_time=None,
                                    max_lease_time=None,
                                    dhcp_ip_range_start=None,
                                    dhcp_ip_range_end=None,
                                    is_shared=None):
        """Create a new isolated OrgVdc network in this vdc.

        :param network_name: (str): Name of the new network.
        :param gateway_ip: (str): IP address of the gateway of the new network.
        :param netmask: (str): Network mask.
        :param description: (str): Description of the new network.
        :param primary_dns_ip: (str): IP address of primary DNS server.
        :param secondary_dns_ip: (str): IP address of secondary DNS Server.
        :param dns_suffix: (str): DNS suffix.
        :param ip_range_start: (str): Start address of the IP ranges used for
            static pool allocation in the network.
        :param ip_range_end: (str): End address of the IP ranges used for
            static pool allocation in the network.
        :param is_dhcp_enabled: (bool): Is DHCP service enabled on the new
            network.
        :param default_lease_time: (int): Default lease in seconds for DHCP
            addresses.
        :param max_lease_time: (int): Max lease in seconds for DHCP addresses.
        :param dhcp_ip_range_start: (str): Start address of the IP range
            used for DHCP addresses.
        :param dhcp_ip_range_end: (str): End address of the IP range used for
            DHCP addresses.
        :param is_shared: (bool): True, if the network is shared with other
            vdc(s) in the organization, else False.

        :return: A :class:`lxml.objectify.StringElement` object representing
            a sparsely populated OrgVdcNetwork element.
        """
        if self.resource is None:
            self.resource = self.client.get_resource(self.href)

        request_payload = E.OrgVdcNetwork(name=network_name)
        if description is not None:
            request_payload.append(E.Description(description))

        vdc_network_configuration = E.Configuration()
        ip_scope = E.IpScope()
        ip_scope.append(E.IsInherited('false'))
        ip_scope.append(E.Gateway(gateway_ip))
        ip_scope.append(E.Netmask(netmask))
        if primary_dns_ip is not None:
            ip_scope.append(E.Dns1(primary_dns_ip))
        if secondary_dns_ip is not None:
            ip_scope.append(E.Dns2(secondary_dns_ip))
        if dns_suffix is not None:
            ip_scope.append(E.DnsSuffix(dns_suffix))
        if ip_range_start is not None and ip_range_end is not None:
            ip_range = E.IpRange()
            ip_range.append(E.StartAddress(ip_range_start))
            ip_range.append(E.EndAddress(ip_range_end))
            ip_scope.append(E.IpRanges(ip_range))
        vdc_network_configuration.append(E.IpScopes(ip_scope))
        vdc_network_configuration.append(E.FenceMode(FenceMode.ISOLATED.value))
        request_payload.append(vdc_network_configuration)

        dhcp_service = E.DhcpService()
        if is_dhcp_enabled is not None:
            dhcp_service.append(E.IsEnabled(is_dhcp_enabled))
        if default_lease_time is not None:
            dhcp_service.append(E.DefaultLeaseTime(str(default_lease_time)))
        if max_lease_time is not None:
            dhcp_service.append(E.MaxLeaseTime(str(max_lease_time)))
        if dhcp_ip_range_start is not None and dhcp_ip_range_end is not None:
            dhcp_ip_range = E.IpRange()
            dhcp_ip_range.append(E.StartAddress(dhcp_ip_range_start))
            dhcp_ip_range.append(E.EndAddress(dhcp_ip_range_end))
            dhcp_service.append(dhcp_ip_range)
        request_payload.append(E.ServiceConfig(dhcp_service))

        if is_shared is not None:
            request_payload.append(E.IsShared(is_shared))

        return self.client.post_linked_resource(
            self.resource, RelationType.ADD, EntityType.ORG_VDC_NETWORK.value,
            request_payload)