2015-01-16 15:54:44 +08:00
|
|
|
# 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.
|
|
|
|
|
|
|
|
import inspect
|
|
|
|
import json
|
|
|
|
from openstack.identity import identity_service
|
|
|
|
from openstack import transport as trans
|
2015-05-15 03:32:10 -04:00
|
|
|
|
2015-01-23 16:03:51 +08:00
|
|
|
from senlinclient.common import exc as client_exc
|
2015-01-16 15:54:44 +08:00
|
|
|
|
2015-01-25 19:50:02 +08:00
|
|
|
|
2015-01-16 15:54:44 +08:00
|
|
|
class Client(object):
|
|
|
|
def __init__(self, session):
|
|
|
|
self.session = session
|
|
|
|
self.auth = session.authenticator
|
|
|
|
|
2015-01-16 17:41:35 +08:00
|
|
|
def get_options(self, options):
|
2015-01-20 21:45:14 +08:00
|
|
|
return json.loads(options)
|
2015-01-16 15:54:44 +08:00
|
|
|
|
|
|
|
def transport(self, opts):
|
2015-03-21 07:54:57 +08:00
|
|
|
'''Create a transport given some options.'''
|
|
|
|
|
2015-01-16 15:54:44 +08:00
|
|
|
argument = opts.argument
|
|
|
|
xport = trans.Transport(verify=opts.verify)
|
|
|
|
return xport.get(argument).text
|
|
|
|
|
|
|
|
def session(self, cls_name):
|
|
|
|
if cls_name is None:
|
|
|
|
raise Exception("A cls name argument must be specified")
|
|
|
|
|
|
|
|
filtration = identity_service.IdentityService()
|
|
|
|
return self.session.get(cls_name, service=filtration).text
|
|
|
|
|
|
|
|
def authenticate(self, options):
|
|
|
|
xport = trans.Transport(verify=options.verify)
|
|
|
|
print(self.auth.authorize(xport))
|
|
|
|
return xport
|
|
|
|
|
2015-07-25 10:49:22 -04:00
|
|
|
def list(self, cls, path_args=None, **options):
|
2015-01-20 21:45:14 +08:00
|
|
|
try:
|
2015-07-25 10:49:22 -04:00
|
|
|
return cls.list(self.session, path_args=path_args, params=options)
|
2015-02-01 16:30:04 +08:00
|
|
|
except Exception as ex:
|
2015-01-25 19:27:46 +08:00
|
|
|
client_exc.parse_exception(ex)
|
2015-01-20 21:45:14 +08:00
|
|
|
|
2015-04-17 01:10:03 -04:00
|
|
|
def create(self, cls, params, extra_attrs=False):
|
2015-01-20 21:45:14 +08:00
|
|
|
obj = cls.new(**params)
|
2015-01-26 13:33:55 +08:00
|
|
|
try:
|
2015-04-17 01:10:03 -04:00
|
|
|
return obj.create(self.session, extra_attrs=extra_attrs)
|
2015-02-01 16:30:04 +08:00
|
|
|
except Exception as ex:
|
2015-01-26 13:33:55 +08:00
|
|
|
client_exc.parse_exception(ex)
|
2015-01-16 15:54:44 +08:00
|
|
|
|
2015-04-21 09:53:31 -04:00
|
|
|
def get_with_args(self, cls, options=None):
|
|
|
|
if options is None:
|
|
|
|
options = {}
|
|
|
|
try:
|
|
|
|
obj = cls.new(**options)
|
|
|
|
return obj.get_with_args(self.session, options)
|
|
|
|
except Exception as ex:
|
|
|
|
client_exc.parse_exception(ex)
|
|
|
|
|
2015-01-16 17:41:35 +08:00
|
|
|
def get(self, cls, options=None):
|
2015-02-01 16:30:04 +08:00
|
|
|
if options is None:
|
|
|
|
options = {}
|
2015-01-16 15:54:44 +08:00
|
|
|
try:
|
2015-01-20 23:34:45 +08:00
|
|
|
obj = cls.new(**options)
|
2015-01-25 19:27:46 +08:00
|
|
|
return obj.get(self.session)
|
2015-02-01 16:30:04 +08:00
|
|
|
except Exception as ex:
|
2015-01-25 19:27:46 +08:00
|
|
|
client_exc.parse_exception(ex)
|
2015-01-16 15:54:44 +08:00
|
|
|
|
|
|
|
def find(self, cls, options):
|
|
|
|
return cls.find(self.session, options)
|
|
|
|
|
|
|
|
def update(self, cls, options):
|
2015-01-25 19:27:46 +08:00
|
|
|
obj = cls.new(**options)
|
|
|
|
try:
|
2015-03-03 18:00:53 +08:00
|
|
|
obj.update(self.session)
|
2015-02-01 16:30:04 +08:00
|
|
|
except Exception as ex:
|
2015-01-25 19:27:46 +08:00
|
|
|
client_exc.parse_exception(ex)
|
2015-01-16 15:54:44 +08:00
|
|
|
|
|
|
|
def delete(self, cls, options):
|
2015-01-20 21:45:14 +08:00
|
|
|
obj = cls.new(**options)
|
2015-01-25 19:27:46 +08:00
|
|
|
try:
|
|
|
|
obj.delete(self.session)
|
2015-02-01 16:30:04 +08:00
|
|
|
except Exception as ex:
|
2015-01-25 19:27:46 +08:00
|
|
|
client_exc.parse_exception(ex)
|
2015-01-16 15:54:44 +08:00
|
|
|
|
|
|
|
def head(self, cls, options):
|
|
|
|
kwargs = self.get_options(options)
|
|
|
|
obj = cls.new(**kwargs)
|
|
|
|
obj.head(self.session)
|
|
|
|
return obj
|
|
|
|
|
|
|
|
def action(self, cls, options):
|
|
|
|
def filter_args(method, params):
|
|
|
|
expected_args = inspect.getargspec(method).args
|
2015-08-18 10:21:26 +09:00
|
|
|
accepted_args = ([a for a in expected_args and params
|
|
|
|
if a != 'self'])
|
2015-02-06 20:29:36 +08:00
|
|
|
filtered_args = dict((d, params[d]) for d in accepted_args)
|
2015-01-16 15:54:44 +08:00
|
|
|
return filtered_args
|
|
|
|
|
|
|
|
def invoke_method(target, method_name, params):
|
|
|
|
action = getattr(target, method_name)
|
|
|
|
filtered_args = filter_args(action, params)
|
|
|
|
reply = action(**filtered_args)
|
|
|
|
return reply
|
|
|
|
|
2015-02-06 20:29:36 +08:00
|
|
|
action = options.pop('action')
|
|
|
|
if 'action_args' in options:
|
|
|
|
args = options.pop('action_args')
|
2015-01-16 15:54:44 +08:00
|
|
|
else:
|
|
|
|
args = {}
|
|
|
|
|
|
|
|
args.update(session=self.session)
|
2015-02-06 20:29:36 +08:00
|
|
|
obj = cls.new(**options)
|
|
|
|
try:
|
|
|
|
return invoke_method(obj, action, args)
|
|
|
|
except Exception as ex:
|
|
|
|
client_exc.parse_exception(ex)
|