def main(): argument_spec = openstack_argument_spec() argument_spec.update(dict( name = dict(required=True), image_id = dict(default=None), image_name = dict(default=None), image_exclude = dict(default='(deprecated)'), flavor_id = dict(default=1), flavor_ram = dict(default=None, type='int'), flavor_include = dict(default=None), key_name = dict(default=None), security_groups = dict(default='default'), nics = dict(default=None, type='list'), meta = dict(default=None, type='dict'), wait = dict(default='yes', choices=['yes', 'no']), wait_for = dict(default=180), state = dict(default='present', choices=['absent', 'present']), user_data = dict(default=None), config_drive = dict(default=False, type='bool'), auto_floating_ip = dict(default=False, type='bool'), floating_ips = dict(default=None, type='list'), floating_ip_pools = dict(default=None, type='list'), scheduler_hints = dict(default=None, type='dict'), )) module = AnsibleModule( argument_spec=argument_spec, mutually_exclusive=[ ['auto_floating_ip','floating_ips'], ['auto_floating_ip','floating_ip_pools'], ['floating_ips','floating_ip_pools'], ['image_id','image_name'], ['flavor_id','flavor_ram'], ], ) if not HAS_NOVACLIENT: module.fail_json(msg='python-novaclient is required for this module') nova = nova_client.Client(module.params['login_username'], module.params['login_password'], module.params['login_tenant_name'], module.params['auth_url'], region_name=module.params['region_name'], service_type='compute') try: nova.authenticate() except exceptions.Unauthorized as e: module.fail_json(msg = "Invalid OpenStack Nova credentials.: %s" % e.message) except exceptions.AuthorizationFailure as e: module.fail_json(msg = "Unable to authorize user: %s" % e.message) if module.params['state'] == 'present': if not module.params['image_id'] and not module.params['image_name']: module.fail_json( msg = "Parameter 'image_id' or `image_name` is required if state == 'present'") else: _get_server_state(module, nova) _create_server(module, nova) if module.params['state'] == 'absent': _get_server_state(module, nova) _delete_server(module, nova)
def test_auth_call(): auth_plugin.discover_auth_systems() plugin = auth_plugin.DeprecatedAuthPlugin("notexists") cs = client.Client("username", "password", "project_id", utils.AUTH_URL_V2, auth_system="notexists", auth_plugin=plugin) self.assertRaises(exceptions.AuthSystemNotFound, cs.client.authenticate)
def addInterface(neutron, router_id, network_name): credentials = get_nova_credentials() nova_client = nvclient.Client(**credentials) subnets = neutron.list_subnets(name=network_name) subnet_id = subnets['subnets'][0]['id'] print(subnet_id) router_interface = {'subnet_id': subnet_id} neutron.add_interface_router(router_id, router_interface)
def create_instance(self): nc = novaclient.Client(self.user, self.pwd, self.tenant, auth_url=self.url) for f in nc.flavors.list(): print f # nc.servers.create('cloudscale', self.image_name, ) for server in nc.servers.list(): if server._info['name'] == self.instance_name: return server
def get_nova_client(cls): if cls.nova_client is None: cls.nova_client = nova_client.Client(username=OPENSTACK_USERNAME, api_key=OPENSTACK_PASSWORD, project_id=PROJECT_ID, auth_url=AUTH_URL, connection_pool=True) return cls.nova_client
def get_novaclient(self): """Get a nova client for the tenant.""" # TODO: We ought to be able to derive this from the keystone client, # but it's proving trickier than I expected --isd return novaclient.Client(self.cluster_account.cluster_user_name, self.cluster_account.cluster_password, self.name, self.cluster_account.cluster.auth_url)
def init_clients(): cc = cinderclient.Client('2', USER, PASSWORD, TENANT, AUTH_URL) nc = novaclient.Client(USER, PASSWORD, TENANT, AUTH_URL, service_type="compute") return cc, nc
def __init__(self, config, configuration_ini, cloud_prefix='SRC', results_path='.'): self.configuration_ini = configuration_ini self.results_path = results_path logging_config.dictConfig(conf.logging_configuration) self.log = logging.getLogger(__name__) self.filtering_utils = utils.FilteringUtils( self.configuration_ini['migrate']['filter_path']) self.migration_utils = utils.MigrationUtils(config) self.src_vms_from_config = \ self.migration_utils.get_all_vms_from_config() self.config = config self.cloud_prefix = cloud_prefix.lower() self.username = self.configuration_ini[self.cloud_prefix]['user'] self.password = self.configuration_ini[self.cloud_prefix]['password'] self.tenant = self.configuration_ini[self.cloud_prefix]['tenant'] self.auth_url = self.configuration_ini[self.cloud_prefix]['auth_url'] self.keystoneclient = keystone.Client(auth_url=self.auth_url, username=self.username, password=self.password, tenant_name=self.tenant) self.keystoneclient.authenticate() self.novaclient = nova.Client(username=self.username, api_key=self.password, project_id=self.tenant, auth_url=self.auth_url) self.token = self.keystoneclient.auth_token self.image_endpoint = \ self.keystoneclient.service_catalog.get_endpoints( service_type='image', endpoint_type='publicURL')['image'][0]['publicURL'] self.glanceclient = glance(endpoint=self.image_endpoint, token=self.token) self.neutronclient = neutron.Client(username=self.username, password=self.password, tenant_name=self.tenant, auth_url=self.auth_url) self.cinderclient = cinder.Client(self.username, self.password, self.tenant, self.auth_url) self.openstack_release = self._get_openstack_release() self.server_groups_supported = self.openstack_release in [ 'icehouse', 'juno' ] self.swift_connection = SwiftConnection(self.auth_url, self.username, self.password, self.tenant)
def test_auth_call(): plugin = auth_plugin.DeprecatedAuthPlugin("fakewithauthurl") cs = client.Client("username", "password", "project_id", auth_system="fakewithauthurl", auth_plugin=plugin) cs.client.authenticate() self.assertEqual("http://faked/v2.0", cs.client.auth_url)
def get_nova_bm_client(username, password, tenant_name, auth_url, cacert=None): LOG.debug('Creating nova client.') baremetal_extension = Extension('baremetal', baremetal) return novav11client.Client(username, password, tenant_name, auth_url, extensions=[baremetal_extension], cacert=cacert)
def openstack_clients(self): creds = self._client_creds() return (nvclient.Client(**creds), neclient.Client(username=creds['username'], password=creds['api_key'], tenant_name=creds['project_id'], region_name=creds['region_name'], auth_url=creds['auth_url']), cinderclient.Client(**creds))
def __init__(self, config, logger): OpenstackConfig.__init__(self, config, logger) self.instance_name = 'cloudscale-lb' self.nc = novaclient.Client(self.user, self.pwd, self.tenant, auth_url=self.url) showcase_servers = self.nc.servers.findall(name="cloudscale-sc") if len(showcase_servers) == 1: self.logger.log("Only one instance") server_id = showcase_servers[0].id self.load_balancer_public_ip = self.add_floating_ip(server_id) self.add_security_group(server_id, "ssh") return self.logger.log("Creating load balancer instance:") self.file_path = os.path.dirname(__file__) + "/../../software" ha_proxy_config = open(self.file_path + '/haproxy.cfg', 'r').read() checker = base64.b64encode(open(self.file_path + '/check-running-showcase_instances.py', 'r').read()) openstack_config = base64.b64encode(open(self.config.config_path, 'r').read()) ############################################################################################################# # TODO: remove this when instances can connect to openstack, as servers will be automatically added by script # add showcase servers to config for server in showcase_servers: for address in server.addresses[server.addresses.keys()[0]]: if address['OS-EXT-IPS:type'] == 'fixed': server_ip = address['addr'] break else: server_ip = None self.logger.log("Error: can not get IP address") ha_proxy_config += """ server %s %s:8080 check""" % (server_ip, server_ip) ############################################################################################################# userdata = open(self.file_path + '/install-load-balancer.sh', 'r').read() userdata = ha_proxy_config.join( userdata.split("#####REPLACE_ME_WITH_CONFIG#####") ) userdata = checker.join( userdata.split("###PLACEHOLDER_FOR_checker.py###") ) userdata = openstack_config.join( userdata.split("###PLACEHOLDER_FOR_config.ini###") ) server_id = self.create_instance(userdata=userdata) self.add_security_group(server_id, "ssh") self.add_security_group(server_id, "http") self.load_balancer_public_ip = self.add_floating_ip(server_id) self.logger.log('Done creating load balancer instance')
def assignFloatingIP(instance_name): credentials = get_nova_credentials() nova_client = nvclient.Client(**credentials) nova_client.floating_ip_pools.list() floating_ip = nova_client.floating_ips.create( nova_client.floating_ip_pools.list()[0].name) print("Floating IP is ", floating_ip) instance = nova_client.servers.find(name=instance_name) instance.add_floating_ip(floating_ip)
def clean(username, password, project_id, auth_url, exclude): with client.Client(username=username, api_key=password, project_id=project_id, auth_url=auth_url) as nova: keypairs = nova.keypairs.list() for keypair in keypairs: if keypair.name not in exclude: nova.keypairs.delete(keypair.name)
def get_client(self): return nova_client.Client(self.config.cloud.user, self.config.cloud.password, self.config.cloud.tenant, self.config.cloud.auth_url, cacert=self.config.cloud.cacert, insecure=self.config.cloud.insecure, region_name=self.config.cloud.region)
def test_passing_endpoint_type(self): endpoint_type = uuidutils.generate_uuid(dashed=False) s = session.Session() c = client.Client(session=s, endpoint_type=endpoint_type, direct_use=False) self.assertEqual(endpoint_type, c.client.interface)
def get_nova_client(self): if self._nova_client is None: self._nova_client = novaclient.Client( username=cfg.CONF.nova_admin_username, api_key=cfg.CONF.nova_admin_password, auth_url=cfg.CONF.nova_admin_auth_url, tenant_id=cfg.CONF.nova_admin_tenant_id, region_name=cfg.CONF.nova_region_name, insecure=True) return self._nova_client
def test_auth_manual(self): cs = client.Client("username", "password", "project_id", utils.AUTH_URL) @mock.patch.object(cs.client, 'authenticate') def test_auth_call(m): cs.authenticate() self.assertTrue(m.called) test_auth_call()
def test_passing_interface(self): endpoint_type = uuid.uuid4().hex interface = uuid.uuid4().hex s = session.Session() c = client.Client(session=s, interface=interface, endpoint_type=endpoint_type) self.assertEqual(interface, c.client.interface)
def _get_novaclient(self): if not self.nova_client: # should not cache this forever # novaclient doesn't yet support v3 keystone auth # use keystoneauth.session self.nova_client = nova_client_v2.Client( session=self._get_keystone_session(OPENSTACK_CONFIG), auth_url=self._get_auth_url(OPENSTACK_CONFIG), endpoint_type='internalURL', direct_use=False, region_name=cfg.CONF[OPENSTACK_CONFIG].nova_region_name) return self.nova_client
def nova(self): if self._nova: return self._nova endpoint_type = self._get_client_option('nova', 'endpoint_type') region_name = self._get_client_option('nova', 'region_name') endpoint = self.url_for(service_type='compute', endpoint_type=endpoint_type, region_name=region_name) self._nova = novaclient.Client(auth_token=self.auth_token) self._nova.client.management_url = endpoint return self._nova
def test_authenticate_failure(self): cs = client.Client("username", "password", "project_id", utils.AUTH_URL) auth_response = utils.TestResponse({'status_code': 401}) mock_request = mock.Mock(return_value=(auth_response)) @mock.patch.object(requests, "request", mock_request) def test_auth_call(): self.assertRaises(exceptions.Unauthorized, cs.client.authenticate) test_auth_call()
def _get_novaclient(self): if not self.nova_client: # should not cache this forever # novaclient doesn't yet support v3 keystone auth # use keystoneauth.session self.nova_client = nova_client_v2.Client( session=self._get_keystone_session(), auth_url=self.auth_url, endpoint_type='internalURL', direct_use=False, region_name=cfg.CONF.KEYSTONE_AUTHTOKEN.nova_region_name) return self.nova_client
def openstack_clients(self): creds = self._client_creds() self.logger.info('Creating a Nova/Cinder client with the following ' 'arguments: {0}'.format(creds)) return (nvclient.Client(**creds), neclient.Client(username=creds['username'], password=creds['api_key'], tenant_name=creds['project_id'], region_name=creds['region_name'], auth_url=creds['auth_url']), cinderclient.Client(**creds))
def test_adapter_properties(self): # sample of properties, there are many more user_agent = uuid.uuid4().hex endpoint_override = uuid.uuid4().hex s = session.Session() c = client.Client(session=s, user_agent=user_agent, endpoint_override=endpoint_override) self.assertEqual(user_agent, c.client.user_agent) self.assertEqual(endpoint_override, c.client.endpoint_override)
def novaclient(request, region=None): insecure = getattr(settings, 'OPENSTACK_SSL_NO_VERIFY', False) cacert = getattr(settings, 'OPENSTACK_SSL_CACERT', None) c = nova_client.Client(request.user.username, request.user.token.id, project_id=request.user.tenant_id, auth_url=base.url_for(request, 'compute'), insecure=insecure, cacert=cacert, http_log_debug=settings.DEBUG) c.client.auth_token = request.user.token.id c.client.management_url = base.url_for(request, 'compute', region=region) return c
def test_auth_manual(self): cs = client.Client(username="******", api_key="password", project_id="project_id", auth_url=utils.AUTH_URL, direct_use=False) @mock.patch.object(cs.client, 'authenticate') def test_auth_call(m): cs.authenticate() self.assertTrue(m.called) test_auth_call()
def nova(self): print 'Establishing connection to Nova...' auth_url = 'http://%s:5000/v2.0' % self.main_ip nova_connection = novaclient.Client(self.user, self.key, self.project_name, auth_url, service_type='compute') print 'Connected to Nova' return nova_connection
def get_nova_client(tenant_id=None): global nova_clients client = nova_clients.get(tenant_id) if client is None: if tenant_id is None: tenant_name = settings.OS_TENANT else: tenant_name = None _, session = get_keystone_session(tenant_name, tenant_id) client = nova.Client(session=session, insecure=settings.OS_INSECURE) nova_clients[tenant_id] = client return client
def test_authenticate_with_token_failure(self): cs = client.Client("username", None, "project_id", utils.AUTH_URL_V2) cs.client.auth_token = "FAKE_ID" resp = {"unauthorized": {"message": "Unauthorized", "code": "401"}} auth_response = utils.TestResponse({ "status_code": 401, "text": json.dumps(resp), }) mock_request = mock.Mock(return_value=(auth_response)) with mock.patch.object(requests.Session, "request", mock_request): self.assertRaises(exceptions.Unauthorized, cs.client.authenticate)