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
|
|
|
|
from openstack.identity import identity_service
|
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-09-23 10:44:16 -04:00
|
|
|
from senlinclient.common import sdk
|
2015-11-02 04:41:30 -05:00
|
|
|
from senlinclient.v1 import models
|
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):
|
2015-09-23 10:44:16 -04:00
|
|
|
|
2015-09-30 04:33:06 -04:00
|
|
|
def __init__(self, preferences=None, user_agent=None, **kwargs):
|
2015-11-26 21:08:20 -05:00
|
|
|
self.conn = sdk.create_connection(preferences, user_agent, **kwargs)
|
|
|
|
# TODO(Qiming): delete this
|
|
|
|
self.session = self.conn.session
|
2015-01-16 15:54:44 +08:00
|
|
|
|
2015-11-02 04:41:30 -05:00
|
|
|
######################################################################
|
|
|
|
# The following operations are interfaces exposed to other software
|
|
|
|
# which invokes senlinclient today.
|
|
|
|
# These methods form a temporary translation layer. This layer will be
|
|
|
|
# useless when OpenStackSDK has adopted all senlin resources.
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def get_build_info(self, **kwargs):
|
2015-11-26 21:08:20 -05:00
|
|
|
return self.conn.cluster.get_build_info(**kwargs)
|
|
|
|
# return self.get(models.BuildInfo)
|
2015-11-02 04:41:30 -05:00
|
|
|
|
|
|
|
def profile_types(self, **kwargs):
|
|
|
|
return self.list(models.ProfileType, paginated=False)
|
|
|
|
|
2015-12-05 09:40:28 -05:00
|
|
|
def get_profile_type(self, value):
|
|
|
|
return self.get(models.ProfileType, dict(name=value))
|
2015-11-02 04:41:30 -05:00
|
|
|
|
|
|
|
def profiles(self, **queries):
|
|
|
|
return self.list(models.Profile, **queries)
|
|
|
|
|
|
|
|
def create_profile(self, **attrs):
|
|
|
|
return self.create(models.Profile, attrs)
|
|
|
|
|
|
|
|
def get_profile(self, value):
|
|
|
|
return self.get(models.Profile, dict(id=value))
|
|
|
|
|
|
|
|
def update_profile(self, value, **attrs):
|
2015-12-02 11:39:23 +09:00
|
|
|
attrs['id'] = value
|
2015-11-02 04:41:30 -05:00
|
|
|
return self.update(models.Profile, attrs)
|
|
|
|
|
|
|
|
def delete_profile(self, value, ignore_missing=True):
|
|
|
|
return self.delete(models.Profile,
|
|
|
|
dict(id=value, ignore_missing=ignore_missing))
|
|
|
|
|
|
|
|
def policy_types(self, **kwargs):
|
|
|
|
return self.list(models.PolicyType, paginated=False)
|
|
|
|
|
2015-12-05 09:40:28 -05:00
|
|
|
def get_policy_type(self, value):
|
|
|
|
return self.get(models.PolicyType, dict(name=value))
|
2015-11-02 04:41:30 -05:00
|
|
|
|
|
|
|
def policies(self, **queries):
|
|
|
|
return self.list(models.Policy, **queries)
|
|
|
|
|
|
|
|
def create_policy(self, **attrs):
|
|
|
|
return self.create(models.Policy, attrs)
|
|
|
|
|
|
|
|
def get_policy(self, value):
|
|
|
|
return self.get(models.Policy, dict(id=value))
|
|
|
|
|
|
|
|
def update_policy(self, value, **attrs):
|
|
|
|
attrs['id'] = value
|
|
|
|
return self.update(models.Policy, attrs)
|
|
|
|
|
|
|
|
def delete_policy(self, value, ignore_missing=True):
|
|
|
|
return self.delete(models.Policy,
|
|
|
|
dict(id=value, ignore_missing=ignore_missing))
|
|
|
|
|
|
|
|
def clusters(self, **queries):
|
2015-11-26 21:08:20 -05:00
|
|
|
return self.conn.cluster.clusters(**queries)
|
2015-11-02 04:41:30 -05:00
|
|
|
|
|
|
|
def create_cluster(self, **attrs):
|
2015-11-26 21:08:20 -05:00
|
|
|
return self.conn.cluster.create_cluster(**attrs)
|
2015-11-02 04:41:30 -05:00
|
|
|
|
|
|
|
def get_cluster(self, value):
|
2015-11-26 21:08:20 -05:00
|
|
|
return self.conn.cluster.get_cluster(value)
|
2015-11-02 04:41:30 -05:00
|
|
|
|
|
|
|
def update_cluster(self, value, **attrs):
|
2015-11-26 21:08:20 -05:00
|
|
|
return self.conn.cluster.update_cluster(value, **attrs)
|
2015-11-02 04:41:30 -05:00
|
|
|
|
|
|
|
def delete_cluster(self, value, ignore_missing=True):
|
2015-11-26 21:08:20 -05:00
|
|
|
return self.conn.cluster.delete_cluster(value,
|
|
|
|
ignore_missing=ignore_missing)
|
2015-11-02 04:41:30 -05:00
|
|
|
|
|
|
|
def cluster_add_nodes(self, value, nodes):
|
|
|
|
params = {
|
|
|
|
'id': value,
|
|
|
|
'action': 'add_nodes',
|
|
|
|
'action_args': {
|
|
|
|
'nodes': nodes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self.action(models.Cluster, params)
|
|
|
|
|
|
|
|
def cluster_del_nodes(self, value, nodes):
|
|
|
|
params = {
|
|
|
|
'id': value,
|
|
|
|
'action': 'del_nodes',
|
|
|
|
'action_args': {
|
|
|
|
'nodes': nodes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self.action(models.Cluster, params)
|
|
|
|
|
|
|
|
def cluster_resize(self, value, **kwargs):
|
|
|
|
params = {
|
|
|
|
'id': value,
|
|
|
|
'action': 'resize',
|
|
|
|
'action_args': kwargs,
|
|
|
|
}
|
|
|
|
return self.action(models.Cluster, params)
|
|
|
|
|
|
|
|
def cluster_scale_out(self, value, count):
|
|
|
|
params = {
|
|
|
|
'id': value,
|
|
|
|
'action': 'scale_out',
|
|
|
|
'action_args': {
|
|
|
|
'count': count
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self.action(models.Cluster, params)
|
|
|
|
|
|
|
|
def cluster_scale_in(self, value, count):
|
|
|
|
params = {
|
|
|
|
'id': value,
|
|
|
|
'action': 'scale_in',
|
|
|
|
'action_args': {
|
|
|
|
'count': count
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self.action(models.Cluster, params)
|
|
|
|
|
|
|
|
def cluster_policies(self, value, **queries):
|
|
|
|
return self.list(models.ClusterPolicy, path_args={'cluster_id': value},
|
|
|
|
**queries)
|
|
|
|
|
|
|
|
def get_cluster_policy(self, value):
|
|
|
|
return self.get(models.ClusterPolicy, value)
|
|
|
|
|
|
|
|
def cluster_attach_policy(self, value, **kwargs):
|
|
|
|
params = {
|
|
|
|
'id': value,
|
|
|
|
'action': 'policy_attach',
|
|
|
|
'action_args': kwargs
|
|
|
|
}
|
|
|
|
return self.action(models.Cluster, params)
|
|
|
|
|
|
|
|
def cluster_detach_policy(self, value, policy):
|
|
|
|
params = {
|
|
|
|
'id': value,
|
|
|
|
'action': 'policy_detach',
|
|
|
|
'action_args': {
|
|
|
|
'policy_id': policy,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self.action(models.Cluster, params)
|
|
|
|
|
|
|
|
def cluster_update_policy(self, value, **attrs):
|
|
|
|
params = {
|
|
|
|
'id': value,
|
|
|
|
'action': 'policy_update',
|
|
|
|
'action_args': attrs
|
|
|
|
}
|
|
|
|
return self.action(models.Cluster, params)
|
|
|
|
|
|
|
|
def cluster_enable_policy(self, value, policy):
|
|
|
|
params = {
|
|
|
|
'id': value,
|
|
|
|
'action': 'policy_enable',
|
|
|
|
'action_args': {
|
|
|
|
'policy_id': policy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self.action(models.Cluster, params)
|
|
|
|
|
|
|
|
def cluster_disable_policy(self, value, policy):
|
|
|
|
params = {
|
|
|
|
'id': value,
|
|
|
|
'action': 'policy_disable',
|
|
|
|
'action_args': {
|
|
|
|
'policy_id': policy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self.action(models.Cluster, params)
|
|
|
|
|
|
|
|
def nodes(self, **queries):
|
|
|
|
return self.list(models.Node, **queries)
|
|
|
|
|
|
|
|
def create_node(self, **attrs):
|
|
|
|
return self.create(models.Node, attrs)
|
|
|
|
|
|
|
|
def get_node(self, value, show_details=False):
|
|
|
|
return self.get_with_args(models.Node,
|
|
|
|
dict(id=value, show_details=show_details))
|
|
|
|
|
|
|
|
def update_node(self, value, **attrs):
|
|
|
|
attrs['id'] = value
|
|
|
|
return self.update(models.Node, attrs)
|
|
|
|
|
|
|
|
def delete_node(self, value, ignore_missing=True):
|
|
|
|
return self.delete(models.Node,
|
|
|
|
dict(id=value, ignore_missing=ignore_missing))
|
|
|
|
|
|
|
|
def node_join(self, value, cluster):
|
|
|
|
params = {
|
|
|
|
'id': value,
|
|
|
|
'action': 'join',
|
|
|
|
'action_args': {
|
|
|
|
'cluster_id': cluster,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self.action(models.Node, params)
|
|
|
|
|
|
|
|
def node_leave(self, value):
|
|
|
|
params = {
|
|
|
|
'id': value,
|
|
|
|
'action': 'leave',
|
|
|
|
}
|
|
|
|
return self.action(models.Node, params)
|
|
|
|
|
2015-12-21 03:05:35 -05:00
|
|
|
def receivers(self, **queries):
|
|
|
|
return self.list(models.Receiver, **queries)
|
|
|
|
|
|
|
|
def create_receiver(self, **attrs):
|
|
|
|
return self.create(models.Receiver, attrs)
|
|
|
|
|
|
|
|
def get_receiver(self, value):
|
|
|
|
return self.get(models.Receiver, dict(id=value))
|
|
|
|
|
|
|
|
def delete_receiver(self, value, ignore_missing=True):
|
|
|
|
return self.delete(models.Receiver,
|
|
|
|
dict(id=value, ignore_missing=ignore_missing))
|
|
|
|
|
2015-11-02 04:41:30 -05:00
|
|
|
def events(self, **queries):
|
|
|
|
return self.list(models.Event, **queries)
|
|
|
|
|
|
|
|
def get_event(self, value):
|
|
|
|
return self.get(models.Event, dict(id=value))
|
|
|
|
|
|
|
|
def actions(self, **queries):
|
|
|
|
return self.list(models.Action, **queries)
|
|
|
|
|
|
|
|
def get_action(self, value):
|
|
|
|
return self.get(models.Action, dict(id=value))
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# The operations below should go away when Senlin resources are all
|
|
|
|
# adopted into OpenStack SDK.
|
|
|
|
######################################################################
|
|
|
|
|
2015-01-16 15:54:44 +08:00
|
|
|
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
|
|
|
|
|
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 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)
|