#
#    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 heat.common import exception
from heat.common.i18n import _
from heat.engine import attributes
from heat.engine import constraints
from heat.engine import properties
from heat.engine import resource
from heat.engine import support


class Node(resource.Resource):
    """A resource that creates a Senlin Node.

    Node is an object that belongs to at most one Cluster, it can be created
    based on a profile.
    """

    support_status = support.SupportStatus(version='6.0.0')

    default_client_name = 'senlin'

    PROPERTIES = (
        NAME, METADATA, PROFILE,
    ) = (
        'name', 'metadata', 'profile',
    )

    _NODE_STATUS = (
        INIT, ACTIVE, CREATING,
    ) = (
        'INIT', 'ACTIVE', 'CREATING',
    )

    _ACTION_STATUS = (
        ACTION_SUCCEEDED, ACTION_FAILED,
    ) = (
        'SUCCEEDED', 'FAILED',
    )

    ATTRIBUTES = (
        ATTR_DETAILS, ATTR_CLUSTER,
    ) = (
        'details', 'cluster_id'
    )

    properties_schema = {
        NAME: properties.Schema(
            properties.Schema.STRING,
            _('Name of the senlin node. By default, physical resource name '
              'is used.'),
            update_allowed=True,
        ),
        METADATA: properties.Schema(
            properties.Schema.MAP,
            _('Metadata key-values defined for node.'),
            update_allowed=True,
        ),
        PROFILE: properties.Schema(
            properties.Schema.STRING,
            _('Name or ID of senlin profile to create this node.'),
            required=True,
            update_allowed=True,
            constraints=[
                constraints.CustomConstraint('senlin.profile')
            ]
        ),
    }

    attributes_schema = {
        ATTR_DETAILS: attributes.Schema(
            _("The details of physical object."),
            type=attributes.Schema.MAP
        ),
        ATTR_CLUSTER: attributes.Schema(
            _("The cluster ID this node belongs to."),
            type=attributes.Schema.STRING
        ),
    }

    def handle_create(self):
        params = {
            'name': (self.properties[self.NAME] or
                     self.physical_resource_name()),
            'metadata': self.properties[self.METADATA],
            'profile_id': self.properties[self.PROFILE],
        }

        node = self.client().create_node(**params)
        action_id = node.location.split('/')[-1]
        self.resource_id_set(node.id)
        return action_id

    def check_create_complete(self, action_id):
        return self.client_plugin().check_action_status(action_id)

    def handle_delete(self):
        if self.resource_id is not None:
            with self.client_plugin().ignore_not_found:
                self.client().delete_node(self.resource_id)
        return self.resource_id

    def check_delete_complete(self, res_id):
        if not res_id:
            return True

        try:
            self.client().get_node(self.resource_id)
        except Exception as ex:
            self.client_plugin().ignore_not_found(ex)
            return True
        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):
        updaters = dict()
        UPDATE_PROPS = [self.NAME, self.METADATA, self.PROFILE]
        if any(p in prop_diff for p in UPDATE_PROPS):
            params = dict((k, v) for k, v in prop_diff.items()
                          if k in UPDATE_PROPS)
            if self.PROFILE in params:
                params['profile_id'] = params.pop(self.PROFILE)
            updaters['profile_update'] = {
                'params': params,
                'finished': False,
            }

        return updaters

    def check_update_complete(self, updaters):
        def check_action(updater, set_key):
            action = self.client().get_action(updater['action'])
            if action.status == self.ACTION_SUCCEEDED:
                updater[set_key] = True
            elif action.status == self.ACTION_FAILED:
                raise exception.ResourceInError(
                    status_reason=action.status_reason,
                    resource_status=action.status,
                )

        if not updaters:
            return True
        profile_update = updaters.get('profile_update')
        if profile_update and not profile_update['finished']:
            if 'action' not in profile_update:
                resp = self.client().update_node(
                    self.resource_id, **profile_update['params'])
                profile_update['action'] = resp.location.split('/')[-1]
                return False
            else:
                check_action(profile_update, 'finished')
                if not profile_update['finished']:
                    return False

        return True

    def _resolve_attribute(self, name):
        node = self.client().get_node(self.resource_id,
                                      args={'show_details': True})
        return getattr(node, name, None)


def resource_mapping():
    return {
        'OS::Senlin::Node': Node
    }

@@ -572,42 +572,8 @@
 e.%0A%0A
-from heat.common import exception%0A
 from
@@ -1277,120 +1277,8 @@
  )%0A%0A
-    _ACTION_STATUS = (%0A        ACTION_SUCCEEDED, ACTION_FAILED,%0A    ) = (%0A        'SUCCEEDED', 'FAILED',%0A    )%0A%0A
     
@@ -3700,254 +3700,46 @@
     
-updaters = dict()%0A        UPDATE_PROPS = %5Bself.NAME, self.METADATA, self.PROFILE%5D%0A        if any(p in prop_diff for p in UPDATE_PROPS):%0A            params = dict((k, v) for k, v in prop_diff.items()%0A                          if k in UPDATE_PROPS)
+action_id = None%0A        if prop_diff:
 %0A   
@@ -3767,21 +3767,24 @@
 ILE in p
-arams
+rop_diff
 :%0A      
@@ -3794,21 +3794,24 @@
        p
-arams
+rop_diff
 %5B'profil
@@ -3820,21 +3820,24 @@
 id'%5D = p
-arams
+rop_diff
 .pop(sel
@@ -3863,793 +3863,215 @@
     
-updaters%5B'profile_update'%5D = %7B%0A                'params': params,%0A                'finished': False,%0A            %7D%0A%0A        return updaters%0A%0A    def check_update_complete(self, updaters):%0A        def check_action(updater, set_key):%0A            action = self.client().get_action(updater%5B'action'%5D)%0A            if action.status == self.ACTION_SUCCEEDED:%0A                updater%5Bset_key%5D = True%0A            elif action.status == self.ACTION_FAILED:%0A                raise exception.ResourceInError(%0A                    status_reason=action.status_reason,%0A                    resource_status=action.status,%0A                )%0A%0A        if not updaters:%0A            return True%0A        profile_update = updaters.get('profile_update')%0A        if profile_update and not profile_update%5B'finished'%5D
+node = self.client().update_node(%0A                self.resource_id, **prop_diff)%0A            action_id = node.location.split('/')%5B-1%5D%0A%0A        return action_id%0A%0A    def check_update_complete(self, action_id)
 :%0A  
@@ -4080,443 +4080,120 @@
     
-    
 if 
-'
 action
-' not in profile_update:%0A                resp = self.client().update_node(%0A                    self.resource_id, **profile_update%5B'params'%5D)%0A                profile_update%5B'action'%5D = resp.location.split('/')%5B-1%5D%0A                return False%0A            else:%0A                check_action(profile_update, 'finished')%0A                if not profile_update%5B'finished'%5D:%0A                    return False%0A%0A        return True
+_id is None:%0A            return True%0A        return self.client_plugin().check_action_status(action_id)
 %0A%0A  

Refactor senlin node update logic