Provide unified _show_resource() for senlin resources

Adds the base class for senlin resources, and override
the method _show_resource() in the base class, all
senlin resources can use it.

Change-Id: If900aa5ff60e1c79ce5e3c1edaa9d15f177751b1
Blueprint: refactor-show-resource
This commit is contained in:
huangtianhua 2017-01-16 12:16:46 +08:00
parent 1fb9d72333
commit 10ddb8962a
6 changed files with 60 additions and 61 deletions

View File

@ -19,12 +19,12 @@ from heat.common.i18n import _
from heat.engine import attributes from heat.engine import attributes
from heat.engine import constraints from heat.engine import constraints
from heat.engine import properties from heat.engine import properties
from heat.engine import resource from heat.engine.resources.openstack.senlin import res_base
from heat.engine import support from heat.engine import support
from heat.engine import translation from heat.engine import translation
class Cluster(resource.Resource): class Cluster(res_base.BaseSenlinResource):
"""A resource that creates a Senlin Cluster. """A resource that creates a Senlin Cluster.
Cluster resource in senlin can create and manage objects of Cluster resource in senlin can create and manage objects of
@ -32,9 +32,7 @@ class Cluster(resource.Resource):
The collection of these objects is referred to as a cluster. The collection of these objects is referred to as a cluster.
""" """
support_status = support.SupportStatus(version='6.0.0') entity = 'cluster'
default_client_name = 'senlin'
PROPERTIES = ( PROPERTIES = (
NAME, PROFILE, DESIRED_CAPACITY, MIN_SIZE, MAX_SIZE, NAME, PROFILE, DESIRED_CAPACITY, MIN_SIZE, MAX_SIZE,
@ -370,18 +368,8 @@ class Cluster(resource.Resource):
} }
raise exception.StackValidationFailed(message=msg) raise exception.StackValidationFailed(message=msg)
def _resolve_attribute(self, name):
if self.resource_id is None:
return
if name == self.ATTR_POLICIES:
return self.client().cluster_policies(self.resource_id)
cluster = self.client().get_cluster(self.resource_id)
return getattr(cluster, name, None)
def _show_resource(self): def _show_resource(self):
cluster = self.client().get_cluster(self.resource_id) cluster_dict = super(Cluster, self)._show_resource()
cluster_dict = cluster.to_dict()
cluster_dict[self.ATTR_POLICIES] = self.client().cluster_policies( cluster_dict[self.ATTR_POLICIES] = self.client().cluster_policies(
self.resource_id) self.resource_id)
return cluster_dict return cluster_dict

View File

@ -17,21 +17,19 @@ from heat.common.i18n import _
from heat.engine import attributes from heat.engine import attributes
from heat.engine import constraints from heat.engine import constraints
from heat.engine import properties from heat.engine import properties
from heat.engine import resource from heat.engine.resources.openstack.senlin import res_base
from heat.engine import support from heat.engine import support
from heat.engine import translation from heat.engine import translation
class Node(resource.Resource): class Node(res_base.BaseSenlinResource):
"""A resource that creates a Senlin Node. """A resource that creates a Senlin Node.
Node is an object that belongs to at most one Cluster, it can be created Node is an object that belongs to at most one Cluster, it can be created
based on a profile. based on a profile.
""" """
support_status = support.SupportStatus(version='6.0.0') entity = 'node'
default_client_name = 'senlin'
PROPERTIES = ( PROPERTIES = (
NAME, METADATA, PROFILE, CLUSTER NAME, METADATA, PROFILE, CLUSTER
@ -145,10 +143,6 @@ class Node(resource.Resource):
return True return True
return False return False
def _show_resource(self):
node = self.client().get_node(self.resource_id)
return node.to_dict()
def handle_update(self, json_snippet, tmpl_diff, prop_diff): def handle_update(self, json_snippet, tmpl_diff, prop_diff):
actions = [] actions = []
if prop_diff: if prop_diff:

View File

@ -18,21 +18,18 @@ from heat.common import exception
from heat.common.i18n import _ from heat.common.i18n import _
from heat.engine import constraints from heat.engine import constraints
from heat.engine import properties from heat.engine import properties
from heat.engine import resource from heat.engine.resources.openstack.senlin import res_base
from heat.engine import support
from heat.engine import translation from heat.engine import translation
class Policy(resource.Resource): class Policy(res_base.BaseSenlinResource):
"""A resource that creates a Senlin Policy. """A resource that creates a Senlin Policy.
A policy is a set of rules that can be checked and/or enforced when A policy is a set of rules that can be checked and/or enforced when
an action is performed on a Cluster. an action is performed on a Cluster.
""" """
support_status = support.SupportStatus(version='6.0.0') entity = 'policy'
default_client_name = 'senlin'
PROPERTIES = ( PROPERTIES = (
NAME, TYPE, POLICY_PROPS, BINDINGS, NAME, TYPE, POLICY_PROPS, BINDINGS,
@ -209,10 +206,6 @@ class Policy(resource.Resource):
ret = self.check_action_done(actions['add']) ret = self.check_action_done(actions['add'])
return ret return ret
def _show_resource(self):
policy = self.client().get_policy(self.resource_id)
return policy.to_dict()
def resource_mapping(): def resource_mapping():
return { return {

View File

@ -16,20 +16,17 @@
from heat.common.i18n import _ from heat.common.i18n import _
from heat.engine import constraints from heat.engine import constraints
from heat.engine import properties from heat.engine import properties
from heat.engine import resource from heat.engine.resources.openstack.senlin import res_base
from heat.engine import support
class Profile(resource.Resource): class Profile(res_base.BaseSenlinResource):
"""A resource that creates a Senlin Profile. """A resource that creates a Senlin Profile.
Profile resource in senlin is a template describing how to create nodes in Profile resource in senlin is a template describing how to create nodes in
cluster. cluster.
""" """
support_status = support.SupportStatus(version='6.0.0') entity = 'profile'
default_client_name = 'senlin'
PROPERTIES = ( PROPERTIES = (
NAME, TYPE, METADATA, PROFILE_PROPERTIES, NAME, TYPE, METADATA, PROFILE_PROPERTIES,
@ -86,10 +83,6 @@ class Profile(resource.Resource):
profile_obj = self.client().get_profile(self.resource_id) profile_obj = self.client().get_profile(self.resource_id)
self.client().update_profile(profile_obj, **prop_diff) self.client().update_profile(profile_obj, **prop_diff)
def _show_resource(self):
profile = self.client().get_profile(self.resource_id)
return profile.to_dict()
def resource_mapping(): def resource_mapping():
return { return {

View File

@ -15,20 +15,17 @@ from heat.common.i18n import _
from heat.engine import attributes from heat.engine import attributes
from heat.engine import constraints from heat.engine import constraints
from heat.engine import properties from heat.engine import properties
from heat.engine import resource from heat.engine.resources.openstack.senlin import res_base
from heat.engine import support
class Receiver(resource.Resource): class Receiver(res_base.BaseSenlinResource):
"""A resource that creates Senlin Receiver. """A resource that creates Senlin Receiver.
Receiver is an abstract resource created at the senlin engine Receiver is an abstract resource created at the senlin engine
that can be used to hook the engine to some external event/alarm sources. that can be used to hook the engine to some external event/alarm sources.
""" """
support_status = support.SupportStatus(version='6.0.0') entity = 'receiver'
default_client_name = 'senlin'
PROPERTIES = ( PROPERTIES = (
CLUSTER, ACTION, NAME, TYPE, PARAMS, CLUSTER, ACTION, NAME, TYPE, PARAMS,
@ -116,16 +113,6 @@ class Receiver(resource.Resource):
with self.client_plugin().ignore_not_found: with self.client_plugin().ignore_not_found:
self.client().delete_receiver(self.resource_id) self.client().delete_receiver(self.resource_id)
def _show_resource(self):
recv = self.client().get_receiver(self.resource_id)
return recv.to_dict()
def _resolve_attribute(self, name):
if self.resource_id is None:
return
recv = self.client().get_receiver(self.resource_id)
return getattr(recv, name, None)
def resource_mapping(): def resource_mapping():
return { return {

View File

@ -0,0 +1,44 @@
# Copyright 2015 IBM Corp.
#
# 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 oslo_log import log as logging
from heat.common.i18n import _LW
from heat.engine import resource
from heat.engine import support
LOG = logging.getLogger(__name__)
class BaseSenlinResource(resource.Resource):
"""A base class for Senlin resources."""
support_status = support.SupportStatus(version='6.0.0')
default_client_name = 'senlin'
def _show_resource(self):
method_name = 'get_' + self.entity
try:
client_method = getattr(self.client(), method_name)
res_info = client_method(self.resource_id)
return res_info.to_dict()
except AttributeError as ex:
LOG.warning(_LW("No method to get the resource: %s"), ex)
def _resolve_attribute(self, name):
if self.resource_id is None:
return
res_info = self._show_resource()
return res_info.get(name)