Esempio n. 1
0
def network_name_to_id(host_vars, user, password, tenant, auth_url):
    """ Accept one name of network or list of names of networks and return the same
    structure, but names replaced by ids of the network(s). """
    auth = identity.Password(auth_url=auth_url,
                             username=user,
                             password=password,
                             tenant_name=tenant)
    sess = session.Session(auth=auth)
    token = auth.get_token(sess)
    endpoint = auth.get_endpoint(sess,
                                 service_name='neutron',
                                 service_type='network')
    neutron = NeutronClient('2.0', endpoint_url=endpoint, token=token)
    result_as_list = isinstance(host_vars, list)
    if not result_as_list:
        host_vars = [host_vars]
    result = []
    for net in host_vars:
        networks = neutron.list_networks(name=net, fields='name')["networks"]
        if networks:
            result += [networks[0]['id']]
        else:
            raise errors.AnsibleFilterError(
                'There is no network of name {0} accessible for tenant {1}'.
                format(net, tenant))
    if result_as_list:
        return result
    else:
        return result[0]
    def get_stats(self):
        """Retrieves stats from neutron"""
        keystone = self.get_keystone()

        data = { self.prefix: {} }

        tenants = {}
        tenant_list = keystone.tenants.list()
        for tenant in tenant_list:
            tenants[tenant.id] = tenant.name
            data[self.prefix]["tenant-%s" % tenant.name] = {
                    'quotas': {},
                    'networks': { 'count': 0, },
                    'subnets': { 'count': 0,  },
                    'routers': { 'count': 0,  },
                    'ports': { 'count': 0,  },
                    'floatingips': { 'count': 0, },
            }

        neutron_endpoint = keystone.service_catalog.url_for(service_type='network')

        client = NeutronClient('2.0', endpoint_url=neutron_endpoint, token=keystone.auth_token)

        # Get network count
        network_list = client.list_networks()['networks']
        for network in network_list:
            try:
                tenant = tenants[network['tenant_id']]
            except KeyError:
                continue
            data[self.prefix]["tenant-%s" % tenant]['networks']['count'] += 1
            for subnet in network['subnets']:
                data[self.prefix]["tenant-%s" % tenant]['subnets']['count'] += 1

        # Get floating ips
        floatingip_list = client.list_floatingips()['floatingips']
        for floatingip in floatingip_list:
            try:
                tenant = tenants[floatingip['tenant_id']]
            except KeyError:
                continue
            data[self.prefix]["tenant-%s" % tenant]['floatingips']['count'] += 1

        # Get network quotas
        quotas = client.list_quotas()['quotas']
        for quota in quotas:
            try:
                data_tenant = data[self.prefix]["tenant-%s" % tenants[quota['tenant_id']]]
            except KeyError:
                continue
            for item in ('floatingip', 'ikepolicy', 'ipsec_site_connection',
                  'ipsecpolicy', 'network', 'port', 'router',
                  'security_group', 'security_group_rule', 'subnet'):
                data_tenant['quotas'][item] = quota[item]

        return data
Esempio n. 3
0
def _get_client(tenant_name):

    user = settings.KEYSTONE_USER
    password = settings.KEYSTONE_PASSWORD
    auth_url = settings.OPENSTACK_KEYSTONE_URL

    client = Client("2.0",
                    username=user,
                    password=password,
                    auth_url=auth_url,
                    tenant_name=tenant_name,
                    service_type="network")

    client.format = 'json'
    return client
def _get_client(tenant_name):

    user = settings.KEYSTONE_USER
    password = settings.KEYSTONE_PASSWORD
    auth_url = settings.OPENSTACK_KEYSTONE_URL

    client = Client("2.0",
                    username=user,
                    password=password,
                    auth_url=auth_url,
                    tenant_name=tenant_name,
                    service_type="network")

    client.format = 'json'
    return client
Esempio n. 5
0
def neutron_client(os_creds, session=None):
    """
    Instantiates and returns a client for communications with OpenStack's
    Neutron server
    :param os_creds: the credentials for connecting to the OpenStack remote API
    :param session: the keystone session object (optional)
    :return: the client object
    """
    if not session:
        session = keystone_utils.keystone_session(os_creds)
    return Client(api_version=os_creds.network_api_version,
                  session=session,
                  region_name=os_creds.region_name)
def network_id_to_name(host_vars, user, password, tenant, auth_url):
    """ Accept one id of network or list of ids of networks and return the same
    structure, but ids replaced by name of the network(s). """
    auth = identity.Password(auth_url=auth_url, username=user,
                         password=password, tenant_name=tenant)
    sess = session.Session(auth=auth)
    token = auth.get_token(sess)
    endpoint = auth.get_endpoint(sess, service_name='neutron', service_type='network')
    neutron = NeutronClient('2.0', endpoint_url=endpoint, token=token)
    result_as_list = isinstance(host_vars, list)
    if not result_as_list:
        host_vars = [host_vars]
    result = []
    for net in host_vars:
        networks = neutron.list_networks(id=net, fields='name')["networks"]
        if networks:
            result += [networks[0]['name']]
        else:
            raise errors.AnsibleFilterError('There is no network of id {0} accessible for tenant {1}'.format(net, tenant))
    if result_as_list:
        return result
    else:
        return result[0]
    def get_stats(self):
        """Retrieves stats from neutron"""
        keystone = self.get_keystone()

        data = {self.prefix: {}}

        tenants = {}
        tenant_list = keystone.tenants.list()
        for tenant in tenant_list:
            tenants[tenant.id] = tenant.name
            data[self.prefix]["tenant-%s" % tenant.name] = {
                'quotas': {},
                'networks': {
                    'count': 0,
                },
                'subnets': {
                    'count': 0,
                },
                'routers': {
                    'count': 0,
                },
                'ports': {
                    'count': 0,
                },
                'floatingips': {
                    'count': 0,
                },
            }

        neutron_endpoint = keystone.service_catalog.url_for(
            service_type='network')

        client = NeutronClient('2.0',
                               endpoint_url=neutron_endpoint,
                               token=keystone.auth_token)

        # Get network count
        network_list = client.list_networks()['networks']
        for network in network_list:
            try:
                tenant = tenants[network['tenant_id']]
            except KeyError:
                continue
            data[self.prefix]["tenant-%s" % tenant]['networks']['count'] += 1
            for subnet in network['subnets']:
                data[self.prefix]["tenant-%s" %
                                  tenant]['subnets']['count'] += 1

        # Get floating ips
        floatingip_list = client.list_floatingips()['floatingips']
        for floatingip in floatingip_list:
            try:
                tenant = tenants[floatingip['tenant_id']]
            except KeyError:
                continue
            data[self.prefix]["tenant-%s" %
                              tenant]['floatingips']['count'] += 1

        # Get network quotas
        quotas = client.list_quotas()['quotas']
        for quota in quotas:
            try:
                data_tenant = data[self.prefix]["tenant-%s" %
                                                tenants[quota['tenant_id']]]
            except KeyError:
                continue
            for item in ('floatingip', 'ikepolicy', 'ipsec_site_connection',
                         'ipsecpolicy', 'network', 'port', 'router',
                         'security_group', 'security_group_rule', 'subnet'):
                data_tenant['quotas'][item] = quota[item]

        return data