def nova_api_version(version): try: from novaclient import client, exceptions client.get_client_class(version) return version except exceptions.UnsupportedVersion as ex: raise Invalid("Invalid option for `nova_api_version`: %s" % ex)
def nova_api_version(version): try: from novaclient import client, exceptions client.get_client_class(version) return version except exceptions.UnsupportedVersion as err: raise ValueError( "Unsupported Nova API version: {0}".format(err))
def nova_api_version(version): try: from novaclient import client, exceptions client.get_client_class(version) return version except exceptions.UnsupportedVersion as ex: raise Invalid( "Invalid option for `nova_api_version`: %s" % ex)
def get_volume(instance_id): try: _client_class = novaclient.get_client_class(2) nova = _client_class( config.auth['username'], config.auth['password'], config.auth['default_tenant'], config.auth['end_point'], region_name=config.main['region'], insecure=config.auth['insecure']) instance = nova.servers.get(instance_id) # Assume first volume is the root disk volume_id = getattr(instance, 'os-extended-volumes:volumes_attached')[0]['id'] if not volume_id: return None cinder = cinderclient.Client( config.auth['username'], config.auth['password'], config.auth['default_tenant'], config.auth['end_point'], region=config.main['region'], insecure=config.auth['insecure']) return cinder.volumes.get(volume_id) except Exception: return
def make_client(instance): """Returns a compute service client.""" # Defer client imports until we actually need them from novaclient import client as nova_client from novaclient import extension try: from novaclient.v2.contrib import list_extensions except ImportError: from novaclient.v1_1.contrib import list_extensions compute_client = nova_client.get_client_class( instance._api_version[API_NAME], ) LOG.debug('Instantiating compute client: %s', compute_client) # Set client http_log_debug to True if verbosity level is high enough http_log_debug = utils.get_effective_log_level() <= logging.DEBUG extensions = [extension.Extension('list_extensions', list_extensions)] client = compute_client( session=instance.session, extensions=extensions, http_log_debug=http_log_debug, timings=instance.timing, region_name=instance._region_name, ) return client
def _make_nova_client(cfg): bypass_url = None if cfg.nova_admin_tenant_id: bypass_url = '%s/%s' % (cfg.nova_url, cfg.nova_admin_tenant_id) novaclient_cls = nova_client.get_client_class(NOVA_API_VERSION) params = dict( username=cfg.nova_admin_username, api_key=cfg.nova_admin_password, tenant_id=cfg.nova_admin_tenant_id, auth_url=cfg.nova_admin_auth_url, bypass_url=bypass_url, ) try: params['cacert'] = cfg.nova_ca_certificates_file except NoSuchOptError: pass try: params['insecure'] = cfg.nova_api_insecure except NoSuchOptError: pass try: params['region_name'] = cfg.nova_region_name except NoSuchOptError: pass return novaclient_cls(**params)
def __init__(self): auth = ks_auth.load_from_conf_options(cfg.CONF, 'nova') endpoint_override = None if not auth: if cfg.CONF.nova_admin_tenant_id: endpoint_override = "%s/%s" % (cfg.CONF.nova_url, cfg.CONF.nova_admin_tenant_id) auth = n_nova.DefaultAuthPlugin( auth_url=cfg.CONF.nova_admin_auth_url, username=cfg.CONF.nova_admin_username, password=cfg.CONF.nova_admin_password, tenant_id=cfg.CONF.nova_admin_tenant_id, tenant_name=cfg.CONF.nova_admin_tenant_name, endpoint_override=endpoint_override) session = ks_session.Session.load_from_conf_options(cfg.CONF, 'nova', auth=auth) novaclient_cls = nclient.get_client_class(n_nova.NOVA_API_VERSION) self.nclient = novaclient_cls(session=session, region_name=cfg.CONF.nova.region_name)
def __init__(self): auth = ks_auth.load_from_conf_options(cfg.CONF, 'nova') endpoint_override = None if not auth: if cfg.CONF.nova_admin_tenant_id: endpoint_override = "%s/%s" % (cfg.CONF.nova_url, cfg.CONF.nova_admin_tenant_id) auth = n_nova.DefaultAuthPlugin( auth_url=cfg.CONF.nova_admin_auth_url, username=cfg.CONF.nova_admin_username, password=cfg.CONF.nova_admin_password, tenant_id=cfg.CONF.nova_admin_tenant_id, tenant_name=cfg.CONF.nova_admin_tenant_name, endpoint_override=endpoint_override) session = ks_session.Session.load_from_conf_options( cfg.CONF, 'nova', auth=auth) novaclient_cls = nclient.get_client_class(n_nova.NOVA_API_VERSION) self.nclient = novaclient_cls( session=session, region_name=cfg.CONF.nova.region_name)
def __init__(self): # TODO(arosen): we need to cache the endpoints and figure out # how to deal with different regions here.... if cfg.CONF.nova_admin_tenant_id: bypass_url = "%s/%s" % (cfg.CONF.nova_url, cfg.CONF.nova_admin_tenant_id) else: bypass_url = None # NOTE(andreykurilin): novaclient.v1_1 was renamed to v2 and there is # no way to import the contrib module directly without referencing v2, # which would only work for novaclient >= 2.21.0. novaclient_cls = nova_client.get_client_class(NOVA_API_VERSION) server_external_events = importutils.import_module( novaclient_cls.__module__.replace( ".client", ".contrib.server_external_events")) self.nclient = novaclient_cls( username=cfg.CONF.nova_admin_username, api_key=cfg.CONF.nova_admin_password, project_id=cfg.CONF.nova_admin_tenant_name, tenant_id=cfg.CONF.nova_admin_tenant_id, auth_url=cfg.CONF.nova_admin_auth_url, cacert=cfg.CONF.nova_ca_certificates_file, insecure=cfg.CONF.nova_api_insecure, bypass_url=bypass_url, region_name=cfg.CONF.nova_region_name, extensions=[server_external_events]) self.pending_events = [] self._waiting_to_send = False
def get_nova_conn(self): novaclient_cls = nova_client.get_client_class(NOVA_API_VERSION) server_external_events = importutils.import_module( novaclient_cls.__module__.replace( ".client", ".contrib.server_external_events")) auth_token = bottle.request.get_header('X-Auth-Token') conn = novaclient_cls(auth_token=auth_token, bypass_url=self._nova_url, extensions=[server_external_events]) return conn
def gen_nova_client(self): """ instantiate nova compute client """ print "\t* Generating nova client" client = nClient.get_client_class('2') self.novaclient = client(self.username, self.password, self.tenant_name, self.auth_url, service_type='compute')
def flavor_name(f_id): """Grabs the correct flavor name from Nova given the correct ID.""" _client_class = novaclient.get_client_class(2) if f_id not in cache['flavors']: nova = _client_class( config.auth['username'], config.auth['password'], config.auth['default_tenant'], config.auth['end_point'], region_name=config.main['region'], insecure=config.auth['insecure']) cache['flavors'][f_id] = nova.flavors.get(f_id).name return cache['flavors'][f_id]
def setup(self, api_version='1'): from cinderclient import client (options, args) = self.cinder.parser.parse_known_args(self.base_argv) if options.help: options.command = None self.cinder.do_help(options) sys.exit(2) if options.os_volume_api_version: api_version = options.os_volume_api_version client = client.get_client_class(api_version)( options.os_username, options.os_password, options.os_tenant_name, tenant_id=options.os_tenant_id, auth_url=options.os_auth_url, region_name=options.os_region_name, cacert=options.os_cacert, insecure=options.insecure) return options, args, client
def __init__(self): # FIXME(jamielennox): A notifier is being created for each Controller # and each Notifier is handling it's own auth. That means that we are # authenticating the exact same thing len(controllers) times. This # should be an easy thing to optimize. auth = ks_auth.load_from_conf_options(cfg.CONF, 'nova') endpoint_override = None if not auth: LOG.warning(_LW('Authenticating to nova using nova_admin_* options' ' is deprecated. This should be done using' ' an auth plugin, like password')) if cfg.CONF.nova_admin_tenant_id: endpoint_override = "%s/%s" % (cfg.CONF.nova_url, cfg.CONF.nova_admin_tenant_id) auth = DefaultAuthPlugin( auth_url=cfg.CONF.nova_admin_auth_url, username=cfg.CONF.nova_admin_username, password=cfg.CONF.nova_admin_password, tenant_id=cfg.CONF.nova_admin_tenant_id, tenant_name=cfg.CONF.nova_admin_tenant_name, endpoint_override=endpoint_override) session = ks_session.Session.load_from_conf_options(cfg.CONF, 'nova', auth=auth) # NOTE(andreykurilin): novaclient.v1_1 was renamed to v2 and there is # no way to import the contrib module directly without referencing v2, # which would only work for novaclient >= 2.21.0. novaclient_cls = nova_client.get_client_class(NOVA_API_VERSION) server_external_events = importutils.import_module( novaclient_cls.__module__.replace( ".client", ".contrib.server_external_events")) self.nclient = novaclient_cls( session=session, region_name=cfg.CONF.nova.region_name, extensions=[server_external_events]) self.batch_notifier = batch_notifier.BatchNotifier( cfg.CONF.send_events_interval, self.send_events)
def __init__(self): # FIXME(jamielennox): A notifier is being created for each Controller # and each Notifier is handling it's own auth. That means that we are # authenticating the exact same thing len(controllers) times. This # should be an easy thing to optimize. auth = ks_auth.load_from_conf_options(cfg.CONF, 'nova') endpoint_override = None if not auth: LOG.warning( _LW('Authenticating to nova using nova_admin_* options' ' is deprecated. This should be done using' ' an auth plugin, like password')) if cfg.CONF.nova_admin_tenant_id: endpoint_override = "%s/%s" % (cfg.CONF.nova_url, cfg.CONF.nova_admin_tenant_id) auth = DefaultAuthPlugin( auth_url=cfg.CONF.nova_admin_auth_url, username=cfg.CONF.nova_admin_username, password=cfg.CONF.nova_admin_password, tenant_id=cfg.CONF.nova_admin_tenant_id, tenant_name=cfg.CONF.nova_admin_tenant_name, endpoint_override=endpoint_override) session = ks_session.Session.load_from_conf_options(cfg.CONF, 'nova', auth=auth) # NOTE(andreykurilin): novaclient.v1_1 was renamed to v2 and there is # no way to import the contrib module directly without referencing v2, # which would only work for novaclient >= 2.21.0. novaclient_cls = nova_client.get_client_class(NOVA_API_VERSION) server_external_events = importutils.import_module( novaclient_cls.__module__.replace( ".client", ".contrib.server_external_events")) self.nclient = novaclient_cls(session=session, region_name=cfg.CONF.nova.region_name, extensions=[server_external_events]) self.batch_notifier = batch_notifier.BatchNotifier( cfg.CONF.send_events_interval, self.send_events)
def make_client(instance): """Returns a compute service client.""" compute_client = nova_client.get_client_class( instance._api_version[API_NAME], ) LOG.debug('Instantiating compute client: %s', compute_client) # Set client http_log_debug to True if verbosity level is high enough http_log_debug = utils.get_effective_log_level() <= logging.DEBUG extensions = [extension.Extension('list_extensions', list_extensions)] client = compute_client( session=instance.session, extensions=extensions, http_log_debug=http_log_debug, timings=instance.timing, region_name=instance._region_name, ) return client
def gen_clients(self): try: kcreds = self.get_keystone_creds() except: print "\t! Environment not set. Did you source credentials file` ?" sys.exit(1) print "\t* Connecting to keystone" self.keystoneclient = ksclient.Client(**kcreds) tokenlen = len(self.keystoneclient.auth_token) print "\t* AuthToken: " + self.keystoneclient.auth_token[0:20] + "..." + \ self.keystoneclient.auth_token[tokenlen-20:tokenlen] ncreds = self.get_nova_creds() client = nClient.get_client_class('2') self.novaclient = client(**ncreds) network_url = self.keystoneclient.service_catalog.url_for(service_type='network') self.neutronclient = neutronclient.Client(endpoint_url=network_url, token=self.keystoneclient.auth_token) return kcreds["tenant_name"]
class NovaAction(base.OpenStackAction): _client_class = novaclient.get_client_class(2) def _get_client(self): ctx = context.ctx() LOG.debug("Nova action security context: %s" % ctx) keystone_endpoint = keystone_utils.get_keystone_endpoint_v2() nova_endpoint = keystone_utils.get_endpoint_for_project('nova') client = self._client_class(username=None, api_key=None, endpoint_type='publicURL', service_type='compute', auth_token=ctx.auth_token, tenant_id=ctx.project_id, region_name=keystone_endpoint.region, auth_url=keystone_endpoint.url) client.client.management_url = keystone_utils.format_url( nova_endpoint.url, {'tenant_id': ctx.project_id}) return client
def flavor_name(f_id): """Grabs the correct flavor name from Nova given the correct ID.""" if f_id not in cache['flavors']: try: _client_class = nova_client.get_client_class(2) nova = _client_class( config.auth['username'], config.auth['password'], config.auth['default_tenant'], config.auth['end_point'], region_name=config.main['region'], insecure=config.auth['insecure']) except: nova = nova_client.Client('2', username=config.auth['username'], api_key=config.auth['password'], password=config.auth['password'], project_id=config.auth['default_tenant'], auth_url=config.auth['end_point'], region_name=config.main['region'], insecure=config.auth['insecure']) cache['flavors'][f_id] = nova.flavors.get(f_id).name return cache['flavors'][f_id]
def get_volume(instance_id): try: try: _client_class = nova_client.get_client_class(2) nova = _client_class( config.auth['username'], config.auth['password'], config.auth['default_tenant'], config.auth['end_point'], region_name=config.main['region'], insecure=config.auth['insecure']) except: nova = nova_client.Client('2', username=config.auth['username'], api_key=config.auth['password'], password=config.auth['password'], project_id=config.auth['default_tenant'], auth_url=config.auth['end_point'], region_name=config.main['region'], insecure=config.auth['insecure']) instance = nova.servers.get(instance_id) # Assume first volume is the root disk volume_id = getattr(instance, 'os-extended-volumes:volumes_attached')[0]['id'] if not volume_id: return None cinder = cinderclient.Client( config.auth['username'], config.auth['password'], config.auth['default_tenant'], config.auth['end_point'], region=config.main['region'], insecure=config.auth['insecure']) return cinder.volumes.get(volume_id) except Exception: return
def setup(self, api_version='1.1'): from novaclient import client (options, args) = self.nova.parser.parse_known_args(self.base_argv) if options.help: options.command = None self.nova.do_help(options) sys.exit(2) auth_token = None if options.os_auth_token and options.os_endpoint: auth_token = options.os_auth_token if options.os_compute_api_version: api_version = options.os_compute_api_version client = client.get_client_class(api_version)( options.os_username, options.os_password, options.os_tenant_name, tenant_id=options.os_tenant_id, auth_token=auth_token, auth_url=options.os_auth_url, region_name=options.os_region_name, cacert=options.os_cacert, insecure=options.insecure, timeout=options.timeout) return options, args, client
settings = {} _environment = "default" identity = None # Value to plug into the user-agent headers USER_AGENT = "pyrax/%s" % version.version # Do we output HTTP traffic for debugging? _http_debug = False # Regions and services available from the service catalog regions = tuple() services = tuple() _client_classes = { "compute": _cs_client.get_client_class(_cs_max_version), "cdn": CloudCDNClient, "object_store": StorageClient, "database": CloudDatabaseClient, "load_balancer": CloudLoadBalancerClient, "volume": CloudBlockStorageClient, "dns": CloudDNSClient, "compute:network": CloudNetworkClient, "monitor": CloudMonitorClient, "autoscale": AutoScaleClient, "image": ImageClient, "queues": QueueClient, } def _id_type(ityp):
# See the License for the specific language governing permissions and # limitations under the License. import mock from novaclient import client as base_client from novaclient import exceptions as nova_exceptions import requests import six from six.moves.urllib import parse as urlparse from heat.tests import fakes NOVACLIENT_VERSION = "2" Client = base_client.get_client_class(NOVACLIENT_VERSION) def fake_exception(status_code=404, message=None, details=None): resp = mock.Mock() resp.status_code = status_code resp.headers = None body = {'error': {'message': message, 'details': details}} return nova_exceptions.from_response(resp, body, None) class FakeClient(fakes.FakeClient, Client): def __init__(self, *args, **kwargs): super(FakeClient, self).__init__('username', 'password', 'project_id', 'auth_url')
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import mock from novaclient import client as base_client from novaclient import exceptions as nova_exceptions import requests import six from six.moves.urllib import parse as urlparse from heat.tests import fakes NOVACLIENT_VERSION = "2" Client = base_client.get_client_class(NOVACLIENT_VERSION) def fake_exception(status_code=404, message=None, details=None): resp = mock.Mock() resp.status_code = status_code resp.headers = None body = {'error': {'message': message, 'details': details}} return nova_exceptions.from_response(resp, body, None) class FakeClient(fakes.FakeClient, Client): def __init__(self, *args, **kwargs): super(FakeClient, self).__init__('username', 'password', 'project_id', 'auth_url') self.client = FakeHTTPClient(**kwargs)
def connect_to_cloudservers(region=None, context=None, verify_ssl=None, **kwargs): """Creates a client for working with cloud servers.""" context = context or identity _cs_auth_plugin.discover_auth_systems() id_type = get_setting("identity_type") if id_type != "keystone": auth_plugin = _cs_auth_plugin.load_plugin(id_type) else: auth_plugin = None region = _safe_region(region, context=context) mgt_url = _get_service_endpoint(context, "compute", region) cloudservers = None if not mgt_url: # Service is not available return if verify_ssl is None: insecure = not get_setting("verify_ssl") else: insecure = not verify_ssl try: extensions = nc.discover_extensions(_cs_max_version) except AttributeError: extensions = None clt_class = _cs_client.get_client_class(_cs_max_version) cloudservers = clt_class(context.username, context.password, project_id=context.tenant_id, auth_url=context.auth_endpoint, auth_system=id_type, region_name=region, service_type="compute", auth_plugin=auth_plugin, insecure=insecure, extensions=extensions, http_log_debug=_http_debug, **kwargs) agt = cloudservers.client.USER_AGENT cloudservers.client.USER_AGENT = _make_agent_name(agt) cloudservers.client.management_url = mgt_url cloudservers.client.auth_token = context.token cloudservers.exceptions = _cs_exceptions # Add some convenience methods cloudservers.list_images = cloudservers.images.list cloudservers.list_flavors = cloudservers.flavors.list cloudservers.list = cloudservers.servers.list def list_base_images(): """ Returns a list of all base images; excludes any images created by this account. """ return [image for image in cloudservers.images.list() if not hasattr(image, "server")] def list_snapshots(): """ Returns a list of all images created by this account; in other words, it excludes all the base images. """ return [image for image in cloudservers.images.list() if hasattr(image, "server")] def find_images_by_name(expr): """ Returns a list of images whose name contains the specified expression. The value passed is treated as a regular expression, allowing for more specific searches than simple wildcards. The matching is done in a case-insensitive manner. """ return [image for image in cloudservers.images.list() if re.search(expr, image.name, re.I)] cloudservers.list_base_images = list_base_images cloudservers.list_snapshots = list_snapshots cloudservers.find_images_by_name = find_images_by_name cloudservers.identity = identity return cloudservers
def connect_to_cloudservers(region=None, context=None, verify_ssl=None, **kwargs): """Creates a client for working with cloud servers.""" context = context or identity _cs_auth_plugin.discover_auth_systems() id_type = get_setting("identity_type") if id_type != "keystone": auth_plugin = _cs_auth_plugin.load_plugin(id_type) else: auth_plugin = None region = _safe_region(region, context=context) mgt_url = _get_service_endpoint(context, "compute", region) cloudservers = None if not mgt_url: # Service is not available return if verify_ssl is None: insecure = not get_setting("verify_ssl") else: insecure = not verify_ssl try: extensions = nc.discover_extensions(_cs_max_version) except AttributeError: extensions = None clt_class = _cs_client.get_client_class(_cs_max_version) cloudservers = clt_class(context.username, context.password, project_id=context.tenant_id, auth_url=context.auth_endpoint, auth_system=id_type, region_name=region, service_type="compute", auth_plugin=auth_plugin, insecure=insecure, extensions=extensions, http_log_debug=_http_debug, **kwargs) agt = cloudservers.client.USER_AGENT cloudservers.client.USER_AGENT = _make_agent_name(agt) cloudservers.client.management_url = mgt_url cloudservers.client.auth_token = context.token cloudservers.exceptions = _cs_exceptions # Add some convenience methods cloudservers.list_images = cloudservers.images.list cloudservers.list_flavors = cloudservers.flavors.list cloudservers.list = cloudservers.servers.list def list_base_images(): """ Returns a list of all base images; excludes any images created by this account. """ return [ image for image in cloudservers.images.list() if not hasattr(image, "server") ] def list_snapshots(): """ Returns a list of all images created by this account; in other words, it excludes all the base images. """ return [ image for image in cloudservers.images.list() if hasattr(image, "server") ] def find_images_by_name(expr): """ Returns a list of images whose name contains the specified expression. The value passed is treated as a regular expression, allowing for more specific searches than simple wildcards. The matching is done in a case-insensitive manner. """ return [ image for image in cloudservers.images.list() if re.search(expr, image.name, re.I) ] cloudservers.list_base_images = list_base_images cloudservers.list_snapshots = list_snapshots cloudservers.find_images_by_name = find_images_by_name cloudservers.identity = identity return cloudservers