def _validate_name_servers(name_servers, max_name_servers=None): if not name_servers: raise exceptions.BaseVMagineException( "At least one name server is required") if max_name_servers and len(name_servers) > max_name_servers: raise exceptions.BaseVMagineException( "At most two name servers can be specified") for name_server in name_servers: if not utils.is_valid_hostname(name_server): Worker._validate_single_ip_address(name_server, "Invalid name server: %s")
def add_user_to_local_group(self, username, groupname): lmi = Win32_LOCALGROUP_MEMBERS_INFO_3() lmi.lgrmi3_domainandname = six.text_type(username) ret_val = netapi32.NetLocalGroupAddMembers(0, six.text_type(groupname), 3, ctypes.addressof(lmi), 1) if ret_val == self._NERR_GroupNotFound: raise exceptions.GroupNotFoundException() elif ret_val == self._ERROR_ACCESS_DENIED: raise exceptions.AccessDeniedException() elif ret_val == self._ERROR_NO_SUCH_MEMBER: raise exceptions.UserNotFoundException() elif ret_val == self._ERROR_MEMBER_IN_ALIAS: # The user is already a member of the group pass elif ret_val == self._ERROR_INVALID_MEMBER: raise exceptions.BaseVMagineException('Invalid user') elif ret_val != 0: raise exceptions.BaseVMagineException('Unknown error')
def validate_controller_config(self, mgmt_ext_dhcp, mgmt_ext_ip, mgmt_ext_gateway, mgmt_ext_name_servers, use_proxy, proxy_url, proxy_username, proxy_password): LOG.debug("validate_controller_config called") try: if not mgmt_ext_dhcp: LOG.debug("mgmt_ext_ip: %s", mgmt_ext_ip) try: ip_address = mgmt_ext_ip.split("/")[0] ip = self._validate_single_ip_address(ip_address, "") ip_net = netaddr.IPNetwork(mgmt_ext_ip) net_bits = ip_net.netmask.netmask_bits() if (ip == ip_net.network or "%s/%d" % (ip, net_bits) != mgmt_ext_ip): raise exceptions.InvalidIPAddressException() except Exception: raise exceptions.InvalidIPAddressException( "Invalid IP address: %s. The address needs to be " "in CIDR notation, e.g. 192.168.0.1/24" % mgmt_ext_ip) LOG.debug("mgmt_ext_gateway: %s", mgmt_ext_gateway) self._validate_single_ip_address( mgmt_ext_gateway, "Invalid gateway IP address: %s") LOG.debug("mgmt_ext_name_servers: %s", mgmt_ext_name_servers) self._validate_name_servers(mgmt_ext_name_servers, 2) if use_proxy: LOG.debug("proxy_url: %s", proxy_url) if not validators.url(proxy_url): raise exceptions.InvalidUrlException( "Invalid proxy URL: %s" % proxy_url) if proxy_password and not proxy_username: raise exceptions.BaseVMagineException( "A proxy username must be specified if a password " "is provided") return True except Exception as ex: LOG.exception(ex) self._error_callback(ex)
def validate_controller_config(self, mgmt_ext_dhcp, mgmt_ext_ip, mgmt_ext_gateway, mgmt_ext_name_servers, use_proxy, proxy_url, proxy_username, proxy_password): try: LOG.debug("validate_controller_config called") if not mgmt_ext_dhcp: LOG.debug("mgmt_ext_ip: %s", mgmt_ext_ip) try: ip = netaddr.IPNetwork(mgmt_ext_ip) if ip.ip == ip.network or ip.size == 1: raise exceptions.InvalidIPAddressException() except Exception: raise exceptions.InvalidIPAddressException( "Invalid IP address: %s. The address needs to be " "in CIDR notation, e.g. 192.168.0.1/24" % mgmt_ext_ip) LOG.debug("mgmt_ext_gateway: %s", mgmt_ext_gateway) try: ip = netaddr.IPAddress(mgmt_ext_gateway) if ip.is_netmask(): raise exceptions.InvalidIPAddressException() except Exception: raise exceptions.InvalidIPAddressException( "Invalid gateway IP address: %s" % mgmt_ext_gateway) LOG.debug("mgmt_ext_name_servers: %s", mgmt_ext_name_servers) if not mgmt_ext_name_servers: raise exceptions.BaseVMagineException( "At least one name server is required") if len(mgmt_ext_name_servers) > 2: raise exceptions.BaseVMagineException( "At most two name servers can be specified") for name_server in mgmt_ext_name_servers: if not validators.domain(name_server): try: ip = netaddr.IPAddress(name_server) if ip.is_netmask(): raise exceptions.InvalidIPAddressException() except Exception: raise exceptions.InvalidIPAddressException( "Invalid name server: %s" % name_server) if use_proxy: LOG.debug("proxy_url: %s", proxy_url) if not validators.url(proxy_url): raise exceptions.InvalidUrlException( "Invalid proxy URL: %s" % proxy_url) if proxy_password and not proxy_username: raise exceptions.BaseVMagineException( "A proxy username must be specified if a password " "is provided") return True except Exception as ex: LOG.exception(ex) self._error_callback(ex)