class RdpEndpointConfig(TcpEndpointConfig): def __init__(self, *args, **kwargs): super(RdpEndpointConfig, self).__init__(*args, **kwargs) self._connection = None domain = Config.string( label="Active Directory Domain", order=0, validate=lambda self: self.check_credentials(), description="Fully-qualified name of the Active Directory domain for " + "the VM machine accounts.") username = Config.string(label="Active Directory Username", order=1, description="An Administrator within the domain.") password = Config.password(label="Active Directory Password", order=2, description="The Administrator password.") orgunit = Config.string( label="Active Directory Orginational Unit", order=3, description="The orgunit for new machines. The master machine account " + "must be in the same orgunit prior to live-imaging.") template = Config.string(label="Machine Name Template", default="windowsVM######", order=4, validate=lambda self: len(self.template) == 15 or \ Config.error("Template must be 15 characters long."), description="The template machine name for new machines.") host = Config.string( label="Domain Controller", order=5, validate=lambda self: self.check_connection(), description= "The network address (hostname or IP) of the AD server to contact.") def ldap_connection(self): if not (self.domain): return None if self._connection is None: self._connection = LdapConnection(self.domain, self.username, self.password, orgunit=self.orgunit, host=self.host) return self._connection def check_credentials(self): try: self.check_connection(False) except ldap.INVALID_CREDENTIALS: Config.error("Invalid credentials") except Exception as e: Config.error("Unknown exception: %s" % repr(e)) def check_connection(self, hostcheck=True): try: self.try_connection() except ldap.SERVER_DOWN: if hostcheck: Config.error("Could not connect to host") except Exception as e: # If we're not just validating the host, propagate if not hostcheck: raise e def try_connection(self): conn = self.ldap_connection() self._connection = None if conn: # If we have a connection (i.e. the user has # specified a windows domain in their config) # then we ensure that we can connect. conn.open()
class BaseOsEndpointConfig(Config): # Cached client. _client = None def __init__(self, *args, **kwargs): super(BaseOsEndpointConfig, self).__init__(*args, **kwargs) # Last refresh for the cache. self._last_refresh = None # Initialize our list cache. self._list_cache = [] # Initialize our floating IP cache. self._float_cache = {} # Common authentication elements. auth_url = Config.string( label="OpenStack Auth URL", default="http://localhost:5000/v2.0/", order=0, validate=lambda self: self.validate_connection_params(), alternates=["authurl"], description="The OpenStack authentication URL (OS_AUTH_URL).") username = Config.string( label="OpenStack User", default="admin", order=1, alternates=["user"], description="The user for authentication (OS_USERNAME).") password = Config.password( label="OpenStack Password", default="admin", order=2, alternates=["apikey"], description="The api key or password (OS_PASSWORD).") tenant_name = Config.string( "OpenStack Tenant/Project", default="admin", order=3, alternates=["project"], description="The project or tenant (OS_TENANT_NAME).") region_name = Config.string(label="Region Name", order=4, description="The region (OS_REGION_NAME).") list_rate_limit = Config.integer(label="Rate limit", default=120, order=1, validate=lambda self: self.list_rate_limit >= 0 or \ Config.error("Rate limit must be non-negative."), description="Limit list requests to this often.") # Elements common to launching and booting. security_groups = Config.list( label="Security Groups", order=5, description="Security groups for new instances.") availability_zone = Config.string( label="Availability Zone", order=5, description="Availability zone for new instances.") user_data = Config.text( "User Data", order=6, description="Script or cloud-config for new instances.") filter_instances = Config.boolean( "Filter Instances", default=False, order=7, description="Use only instances that match image, flavor, etc.") floating_ips = Config.list( label="Floating IPs", order=7, validate=lambda self: self.validate_floating_ips(), description="Floating IPs to distribute.") def novaclient(self): from novaclient import shell from novaclient.v1_1.client import Client as NovaClient if self._client is None: extensions = shell.OpenStackComputeShell()._discover_extensions( "1.1") self._client = NovaClient(self.username, self.password, self.tenant_name, self.auth_url, region_name=self.region_name, service_type="compute", extensions=extensions) return self._client def validate_connection_params(self, throwerror=True): import novaclient.exceptions try: self.novaclient().authenticate() except Exception, e: if throwerror: # If we got an unathorized exception, propagate. if isinstance(e, novaclient.exceptions.Unauthorized): Config.error(e.message) elif isinstance(e, novaclient.exceptions.EndpointNotFound): Config.error( "Problem connecting to cloud endpoint. Bad Region?") else: Config.error( "Could not connect to OpenStack cloud. Bad URL?") else: return False return True