Merge "Add host aggregate / av zones util functions"
authorJose Lausuch <jose.lausuch@ericsson.com>
Tue, 13 Dec 2016 12:21:43 +0000 (12:21 +0000)
committerGerrit Code Review <gerrit@opnfv.org>
Tue, 13 Dec 2016 12:21:43 +0000 (12:21 +0000)
1  2 
functest/utils/openstack_utils.py

@@@ -56,19 -56,8 +56,19 @@@ def get_credentials(service)
      """
      creds = {}
  
 +    keystone_api_version = os.getenv('OS_IDENTITY_API_VERSION')
 +    if (keystone_api_version is None or
 +            keystone_api_version == '2'):
 +        keystone_v3 = False
 +        tenant_env = 'OS_TENANT_NAME'
 +        tenant = 'tenant_name'
 +    else:
 +        keystone_v3 = True
 +        tenant_env = 'OS_PROJECT_NAME'
 +        tenant = 'project_name'
 +
      # Check that the env vars exists:
 -    envvars = ('OS_USERNAME', 'OS_PASSWORD', 'OS_AUTH_URL', 'OS_TENANT_NAME')
 +    envvars = ('OS_USERNAME', 'OS_PASSWORD', 'OS_AUTH_URL', tenant_env)
      for envvar in envvars:
          if os.getenv(envvar) is None:
              raise MissingEnvVar(envvar)
@@@ -80,6 -69,7 +80,6 @@@
          tenant = "project_id"
      else:
          password = "password"
 -        tenant = "tenant_name"
  
      # The most common way to pass these info to the script is to do it through
      # environment variables.
          "username": os.environ.get("OS_USERNAME"),
          password: os.environ.get("OS_PASSWORD"),
          "auth_url": os.environ.get("OS_AUTH_URL"),
 -        tenant: os.environ.get("OS_TENANT_NAME")
 +        tenant: os.environ.get(tenant_env)
      })
 +    if keystone_v3:
 +        if os.getenv('OS_USER_DOMAIN_NAME') is not None:
 +            creds.update({
 +                "user_domain_name": os.getenv('OS_USER_DOMAIN_NAME')
 +            })
 +        if os.getenv('OS_PROJECT_DOMAIN_NAME') is not None:
 +            creds.update({
 +                "project_domain_name": os.getenv('OS_PROJECT_DOMAIN_NAME')
 +            })
 +
      if os.getenv('OS_ENDPOINT_TYPE') is not None:
          creds.update({
              "endpoint_type": os.environ.get("OS_ENDPOINT_TYPE")
@@@ -133,14 -113,7 +133,14 @@@ def source_credentials(rc_file)
  
  def get_credentials_for_rally():
      creds = get_credentials("keystone")
 -    admin_keys = ['username', 'tenant_name', 'password']
 +    keystone_api_version = os.getenv('OS_IDENTITY_API_VERSION')
 +    if (keystone_api_version is None or
 +            keystone_api_version == '2'):
 +        admin_keys = ['username', 'tenant_name', 'password']
 +    else:
 +        admin_keys = ['username', 'password', 'user_domain_name',
 +                      'project_name', 'project_domain_name']
 +
      endpoint_types = [('internalURL', 'internal'),
                        ('publicURL', 'public'), ('adminURL', 'admin')]
      if 'endpoint_type' in creds.keys():
@@@ -245,6 -218,45 +245,45 @@@ def get_flavor_id_by_ram_range(nova_cli
      return id
  
  
+ def get_aggregates(nova_client):
+     try:
+         aggregates = nova_client.aggregates.list()
+         return aggregates
+     except Exception, e:
+         logger.error("Error [get_aggregates(nova_client)]: %s" % e)
+         return None
+ def get_aggregate_id(nova_client, aggregate_name):
+     try:
+         aggregates = get_aggregates(nova_client)
+         _id = [ag.id for ag in aggregates if ag.name == aggregate_name][0]
+         return _id
+     except Exception, e:
+         logger.error("Error [get_aggregate_id(nova_client, %s)]:"
+                      " %s" % (aggregate_name, e))
+         return None
+ def get_availability_zones(nova_client):
+     try:
+         availability_zones = nova_client.availability_zones.list()
+         return availability_zones
+     except Exception, e:
+         logger.error("Error [get_availability_zones(nova_client)]: %s" % e)
+         return None
+ def get_availability_zone_names(nova_client):
+     try:
+         az_names = [az.zoneName for az in get_availability_zones(nova_client)]
+         return az_names
+     except Exception, e:
+         logger.error("Error [get_availability_zone_names(nova_client)]:"
+                      " %s" % e)
+         return None
  def create_flavor(nova_client, flavor_name, ram, disk, vcpus, public=True):
      try:
          flavor = nova_client.flavors.create(
@@@ -308,6 -320,40 +347,40 @@@ def get_hypervisors(nova_client)
          return None
  
  
+ def create_aggregate(nova_client, aggregate_name, av_zone):
+     try:
+         nova_client.aggregates.create(aggregate_name, av_zone)
+         return True
+     except Exception, e:
+         logger.error("Error [create_aggregate(nova_client, %s, %s)]: %s"
+                      % (aggregate_name, av_zone, e))
+         return None
+ def add_host_to_aggregate(nova_client, aggregate_name, compute_host):
+     try:
+         aggregate_id = get_aggregate_id(nova_client, aggregate_name)
+         nova_client.aggregates.add_host(aggregate_id, compute_host)
+         return True
+     except Exception, e:
+         logger.error("Error [add_host_to_aggregate(nova_client, %s, %s)]: %s"
+                      % (aggregate_name, compute_host, e))
+         return None
+ def create_aggregate_with_host(
+         nova_client, aggregate_name, av_zone, compute_host):
+     try:
+         create_aggregate(nova_client, aggregate_name, av_zone)
+         add_host_to_aggregate(nova_client, aggregate_name, compute_host)
+         return True
+     except Exception, e:
+         logger.error("Error [create_aggregate_with_host("
+                      "nova_client, %s, %s, %s)]: %s"
+                      % (aggregate_name, av_zone, compute_host, e))
+         return None
  def create_instance(flavor_name,
                      image_id,
                      network_id,
@@@ -430,6 -476,36 +503,36 @@@ def delete_floating_ip(nova_client, flo
          return False
  
  
+ def remove_host_from_aggregate(nova_client, aggregate_name, compute_host):
+     try:
+         aggregate_id = get_aggregate_id(nova_client, aggregate_name)
+         nova_client.aggregates.remove_host(aggregate_id, compute_host)
+         return True
+     except Exception, e:
+         logger.error("Error [remove_host_from_aggregate(nova_client, %s, %s)]:"
+                      " %s" % (aggregate_name, compute_host, e))
+         return False
+ def remove_hosts_from_aggregate(nova_client, aggregate_name):
+     aggregate_id = get_aggregate_id(nova_client, aggregate_name)
+     hosts = nova_client.aggregates.get(aggregate_id).hosts
+     assert(
+         all(remove_host_from_aggregate(nova_client, aggregate_name, host)
+             for host in hosts))
+ def delete_aggregate(nova_client, aggregate_name):
+     try:
+         remove_hosts_from_aggregate(nova_client, aggregate_name)
+         nova_client.aggregates.delete(aggregate_name)
+         return True
+     except Exception, e:
+         logger.error("Error [delete_aggregate(nova_client, %s)]: %s"
+                      % (aggregate_name, e))
+         return False
  # *********************************************
  #   NEUTRON
  # *********************************************