Merge "Restore: initialize heat client using current login tenant by default"

This commit is contained in:
Jenkins 2016-11-10 03:10:43 +00:00 committed by Gerrit Code Review
commit 52d819a59c
5 changed files with 105 additions and 15 deletions

View File

@ -248,11 +248,15 @@ class RestoresController(wsgi.Controller):
" a restore.")
raise exception.InvalidInput(reason=msg)
# restore_auth and restore_target are optional
# Heat client can be initialized using current login tenant when the
# restore_target and restore_auth is not provided.
restore_auth = restore.get("restore_auth")
if not isinstance(restore_auth, dict):
msg = _("restore_auth must be a dict when creating"
" a restore.")
raise exception.InvalidInput(reason=msg)
if restore_auth is not None:
if not isinstance(restore_auth, dict):
msg = _("restore_auth must be a dict when creating"
" a restore.")
raise exception.InvalidInput(reason=msg)
restore_properties = {
'project_id': context.project_id,

View File

@ -81,7 +81,7 @@ class RequestContext(context.RequestContext):
if s.get('type') in
('identity', 'compute', 'object-store',
'image', 'volume', 'volumev2', 'network',
'volumev3')]
'volumev3', 'orchestration')]
else:
# if list is empty or none
self.service_catalog = []

View File

@ -12,24 +12,60 @@
from heatclient import client as hc
from keystoneclient.v2_0 import client as kc
from oslo_config import cfg
from oslo_log import log as logging
from karbor.i18n import _LE, _LI
from karbor.services.protection import utils
LOG = logging.getLogger(__name__)
SERVICE = 'heat'
heat_client_opts = [
cfg.StrOpt('region_id',
default='RegionOne',
help='The region id which the service belongs to.'),
cfg.StrOpt(SERVICE + '_endpoint',
help='URL of the heat endpoint.'),
cfg.StrOpt(SERVICE + '_catalog_info',
default='orchestration:heat:publicURL',
help='Info to match when looking for heat in the service '
'catalog. Format is: separated values of the form: '
'<service_type>:<service_name>:<endpoint_type> - '
'Only used if cinder_endpoint is unset'),
cfg.StrOpt(SERVICE + '_ca_cert_file',
default=None,
help='Location of the CA certificate file '
'to use for client requests in SSL connections.'),
cfg.BoolOpt(SERVICE + '_auth_insecure',
default=False,
help='Bypass verification of server certificate when '
'making SSL connection to Cinder.'),
]
cfg.CONF.register_opts(heat_client_opts, group=SERVICE + '_client')
KEYSTONECLIENT_VERSION = (3, 0)
HEATCLIENT_VERSION = '1'
def create(context, conf, **kwargs):
cfg.CONF.register_opts(heat_client_opts, group=SERVICE + '_client')
auth_url = kwargs.get("auth_url", None)
if auth_url is not None:
return create_heat_client_with_auth_url(context, conf, **kwargs)
else:
return create_heat_client_with_tenant(context, conf)
def create_heat_client_with_auth_url(context, conf, **kwargs):
auth_url = kwargs["auth_url"]
username = kwargs["username"]
password = kwargs["password"]
tenant_name = context.project_name
LOG.info(_LI('Creating heat client with url %s.'), auth_url)
cacert = conf.heat_client.heat_ca_cert_file
insecure = conf.heat_client.heat_auth_insecure
LOG.info(_LI('Creating heat client with auth url %s.'), auth_url)
try:
keystone = kc.Client(version=KEYSTONECLIENT_VERSION,
username=username,
@ -44,8 +80,31 @@ def create(context, conf, **kwargs):
if service['name'] == 'heat':
heat_endpoint = service['endpoints'][0]['publicURL']
heat = hc.Client(HEATCLIENT_VERSION, endpoint=heat_endpoint,
token=auth_token)
token=auth_token, cacert=cacert, insecure=insecure)
return heat
except Exception:
LOG.error(_LE('Creating heat client with url %s.'), auth_url)
raise
def create_heat_client_with_tenant(context, conf):
cacert = conf.heat_client.heat_ca_cert_file
insecure = conf.heat_client.heat_auth_insecure
conf.register_opts(heat_client_opts, group=SERVICE + '_client')
try:
url = utils.get_url(SERVICE, context, conf,
append_project_fmt='%(url)s/%(project)s')
except Exception:
LOG.error(_LE("Get heat service endpoint url failed."))
raise
try:
LOG.info(_LI('Creating heat client with url %s.'), url)
heat = hc.Client(HEATCLIENT_VERSION, endpoint=url,
token=context.auth_token, cacert=cacert,
insecure=insecure)
return heat
except Exception:
LOG.error(_LE('Creating heat client with endpoint fails.'))
raise

View File

@ -93,16 +93,24 @@ class SyncStackStatusTask(task.Task):
def get_flow(context, workflow_engine, operation_type, checkpoint, provider,
restore, restore_auth):
target = restore['restore_target']
auth_type = restore_auth["type"]
if auth_type == "password":
username = restore_auth["username"]
password = restore_auth["password"]
target = restore.get('restore_target', None)
# TODO(luobin): create a heat_client
kwargs = {"auth_url": target,
"username": username,
"password": password}
# Initialize a heat client using current login tenant when the
# restore_target and restore_auth is not provided.
if target is not None:
username = None
password = None
if restore_auth is not None:
auth_type = restore_auth.get("type", None)
if auth_type == "password":
username = restore_auth["username"]
password = restore_auth["password"]
kwargs = {"auth_url": target,
"username": username,
"password": password}
else:
kwargs = {}
heat_client = ClientFactory.create_client("heat",
context=context,
**kwargs)

View File

@ -70,6 +70,25 @@ class RestoresTest(karbor_base.KarborBaseTest):
after_num = len(restores)
self.assertEqual(1, after_num - before_num)
def test_restore_create_without_target_and_auth(self):
volume = self.store(objects.Volume())
volume.create(1)
plan = self.store(objects.Plan())
plan.create(self.provider_id, [volume, ])
checkpoint = self.store(objects.Checkpoint())
checkpoint.create(self.provider_id, plan.id)
restores = self.karbor_client.restores.list()
before_num = len(restores)
restore = self.store(objects.Restore())
restore.create(self.provider_id, checkpoint.id,
None, self.parameters, None)
restores = self.karbor_client.restores.list()
after_num = len(restores)
self.assertEqual(1, after_num - before_num)
def test_restore_get(self):
volume = self.store(objects.Volume())
volume.create(1)