Add clients module to Tacker

We are invoking individual openstack clients everytime
we need it. But we need a generic openstack clients module
for code resuability. This patch does that by creating new clients.py
file in tacker/common.

Change-Id: Ibe06ff2eafa00b2825d5f412620cb4d051626164
Closes-Bug: #1518244
This commit is contained in:
Bharath Thiruveedula 2015-11-23 11:31:06 +05:30
parent 255bd8c06e
commit 263a93e18a
3 changed files with 74 additions and 40 deletions

69
tacker/common/clients.py Normal file
View File

@ -0,0 +1,69 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, 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.
from heatclient import client as heatclient
from keystoneclient.v2_0 import client as ks_client
from oslo_config import cfg
from oslo_log import log as logging
LOG = logging.getLogger(__name__)
CONF = cfg.CONF
OPTS = [
cfg.StrOpt('heat_uri',
default='http://localhost:8004/v1',
help=_("Heat service URI to create VNF resources"
"specified in the VNFD templates")),
]
CONF.register_opts(OPTS, group='servicevm_heat')
class OpenstackClients(object):
def __init__(self):
super(OpenstackClients, self).__init__()
self.keystone_client = None
self.heat_client = None
self.nova_client = None
self.auth_url = CONF.keystone_authtoken.auth_uri + '/v2.0'
self.auth_username = CONF.keystone_authtoken.username
self.auth_password = CONF.keystone_authtoken.password
self.auth_tenant_name = CONF.keystone_authtoken.project_name
def _keystone_client(self):
return ks_client.Client(
tenant_name=self.auth_tenant_name,
username=self.auth_username,
password=self.auth_password,
auth_url=self.auth_url)
def _heat_client(self):
tenant_id = self.auth_token['tenant_id']
token = self.auth_token['id']
endpoint = CONF.servicevm_heat.heat_uri + '/' + tenant_id
return heatclient.Client('1', endpoint=endpoint, token=token)
@property
def auth_token(self):
return self.keystone.service_catalog.get_token()
@property
def keystone(self):
if not self.keystone_client:
self.keystone_client = self._keystone_client()
return self.keystone_client
@property
def heat(self):
if not self.heat_client:
self.heat_client = self._heat_client()
return self.heat_client

View File

@ -20,12 +20,11 @@ import sys
import time
import yaml
from heatclient import client as heat_client
from heatclient import exc as heatException
from keystoneclient.v2_0 import client as ks_client
from oslo_config import cfg
from toscaparser.utils import yamlparser
from tacker.common import clients
from tacker.common import log
from tacker.extensions import vnfm
from tacker.openstack.common import jsonutils
@ -36,10 +35,6 @@ from tacker.vm.drivers import abstract_driver
LOG = logging.getLogger(__name__)
CONF = cfg.CONF
OPTS = [
cfg.StrOpt('heat_uri',
default='http://localhost:8004/v1',
help=_("Heat server address to create services "
"specified in the service chain.")),
cfg.IntOpt('stack_retries',
default=10,
help=_("Number of attempts to retry for stack deletion")),
@ -454,25 +449,7 @@ class DeviceHeat(abstract_driver.DeviceAbstractDriver):
class HeatClient:
def __init__(self, context, password=None):
# context, password are unused
auth_url = CONF.keystone_authtoken.auth_uri + '/v2.0'
authtoken = CONF.keystone_authtoken
kc = ks_client.Client(
tenant_name=authtoken.project_name,
username=authtoken.username,
password=authtoken.password,
auth_url=auth_url)
token = kc.service_catalog.get_token()
api_version = "1"
endpoint = "%s/%s" % (cfg.CONF.servicevm_heat.heat_uri,
token['tenant_id'])
kwargs = {
'token': token['id'],
'tenant_name': authtoken.project_name,
'username': authtoken.username,
}
self.client = heat_client.Client(api_version, endpoint, **kwargs)
self.stacks = self.client.stacks
self.stacks = clients.OpenstackClients().heat.stacks
def create(self, fields):
fields = fields.copy()

View File

@ -24,10 +24,10 @@ import six
import threading
import time
from keystoneclient.v2_0 import client as ks_client
from oslo_config import cfg
from oslo_utils import timeutils
from tacker.common import clients
from tacker.common import driver_manager
from tacker import context as t_context
from tacker.openstack.common import jsonutils
@ -228,14 +228,8 @@ class ActionRespawn(ActionPolicy):
LOG.debug(_('new_device %s'), new_device)
# keystone v2.0 specific
auth_url = CONF.keystone_authtoken.auth_uri + '/v2.0'
authtoken = CONF.keystone_authtoken
kc = ks_client.Client(
tenant_name=authtoken.project_name,
username=authtoken.username,
password=authtoken.password,
auth_url=auth_url)
token = kc.service_catalog.get_token()
token = clients.OpenstackClients().auth_token
context = t_context.get_admin_context()
context.tenant_name = authtoken.project_name
@ -284,14 +278,8 @@ class ActionRespawnHeat(ActionPolicy):
time.sleep(10)
# keystone v2.0 specific
auth_url = CONF.keystone_authtoken.auth_uri + '/v2.0'
authtoken = CONF.keystone_authtoken
kc = ks_client.Client(
tenant_name=authtoken.project_name,
username=authtoken.username,
password=authtoken.password,
auth_url=auth_url)
token = kc.service_catalog.get_token()
token = clients.OpenstackClients().auth_token
context = t_context.get_admin_context()
context.tenant_name = authtoken.project_name