<s> from __future__ import unicode_literals <EOL> import json <EOL> import boto . cloudformation <EOL> from moto . core import BaseBackend <EOL> from . parsing import ResourceMap , OutputMap <EOL> from . utils import generate_stack_id <EOL> from . exceptions import ValidationError <EOL> class FakeStack ( object ) : <EOL> def __init__ ( self , stack_id , name , template , parameters , region_name , notification_arns = None , tags = None ) : <EOL> self . stack_id = stack_id <EOL> self . name = name <EOL> self . template = template <EOL> self . template_dict = json . loads ( self . template ) <EOL> self . parameters = parameters <EOL> self . region_name = region_name <EOL> self . notification_arns = notification_arns if notification_arns else [ ] <EOL> self . tags = tags if tags else { } <EOL> self . status = '<STR_LIT>' <EOL> self . description = self . template_dict . get ( '<STR_LIT>' ) <EOL> self . resource_map = self . _create_resource_map ( ) <EOL> self . output_map = self . _create_output_map ( ) <EOL> def _create_resource_map ( self ) : <EOL> resource_map = ResourceMap ( self . stack_id , self . name , self . parameters , self . tags , self . region_name , self . template_dict ) <EOL> resource_map . create ( ) <EOL> return resource_map <EOL> def _create_output_map ( self ) : <EOL> output_map = OutputMap ( self . resource_map , self . template_dict ) <EOL> output_map . create ( ) <EOL> return output_map <EOL> @ property <EOL> def stack_parameters ( self ) : <EOL> return self . resource_map . resolved_parameters <EOL> @ property <EOL> def stack_resources ( self ) : <EOL> return self . resource_map . values ( ) <EOL> @ property <EOL> def stack_outputs ( self ) : <EOL> return self . output_map . values ( ) <EOL> def update ( self , template ) : <EOL> self . template = template <EOL> self . resource_map . update ( json . loads ( template ) ) <EOL> self . output_map = self . _create_output_map ( ) <EOL> self . status = "<STR_LIT>" <EOL> def delete ( self ) : <EOL> self . resource_map . delete ( ) <EOL> self . status = "<STR_LIT>" <EOL> class CloudFormationBackend ( BaseBackend ) : <EOL> def __init__ ( self ) : <EOL> self . stacks = { } <EOL> self . deleted_stacks = { } <EOL> def create_stack ( self , name , template , parameters , region_name , notification_arns = None , tags = None ) : <EOL> stack_id = generate_stack_id ( name ) <EOL> new_stack = FakeStack ( <EOL> stack_id = stack_id , <EOL> name = name , <EOL> template = template , <EOL> parameters = parameters , <EOL> region_name = region_name , <EOL> notification_arns = notification_arns , <EOL> tags = tags , <EOL> ) <EOL> self . stacks [ stack_id ] = new_stack <EOL> return new_stack <EOL> def describe_stacks ( self , name_or_stack_id ) : <EOL> stacks = self . stacks . values ( ) <EOL> if name_or_stack_id : <EOL> for stack in stacks : <EOL> if stack . name == name_or_stack_id or stack . stack_id == name_or_stack_id : <EOL> return [ stack ] <EOL> if self . deleted_stacks : <EOL> deleted_stacks = self . deleted_stacks . values ( ) <EOL> for stack in deleted_stacks : <EOL> if stack . stack_id == name_or_stack_id : <EOL> return [ stack ] <EOL> raise ValidationError ( name_or_stack_id ) <EOL> else : <EOL> return stacks <EOL> def list_stacks ( self ) : <EOL> return self . stacks . values ( ) <EOL> def get_stack ( self , name_or_stack_id ) : <EOL> if name_or_stack_id in self . stacks : <EOL> return self . stacks . get ( name_or_stack_id ) <EOL> else : <EOL> return [ stack for stack in self . stacks . values ( ) if stack . name == name_or_stack_id ] [ <NUM_LIT:0> ] <EOL> def update_stack ( self , name , template ) : <EOL> stack = self . get_stack ( name ) <EOL> stack . update ( template ) <EOL> return stack <EOL> def list_stack_resources ( self , stack_name_or_id ) : <EOL> stack = self . get_stack ( stack_name_or_id ) <EOL> return stack . stack_resources <EOL> def delete_stack ( self , name_or_stack_id ) : <EOL> if name_or_stack_id in self . stacks : <EOL> stack = self . stacks . pop ( name_or_stack_id , None ) <EOL> stack . delete ( ) <EOL> self . deleted_stacks [ stack . stack_id ] = stack <EOL> return self . stacks . pop ( name_or_stack_id , None ) <EOL> else : <EOL> for stack in list ( self . stacks . values ( ) ) : <EOL> if stack . name == name_or_stack_id : <EOL> self . delete_stack ( stack . stack_id ) <EOL> cloudformation_backends = { } <EOL> for region in boto . cloudformation . regions ( ) : <EOL> cloudformation_backends [ region . name ] = CloudFormationBackend ( ) </s>
