Skip to content

Commit

Permalink
0.0.3 pre release
Browse files Browse the repository at this point in the history
  • Loading branch information
MacHu-GWU committed Jul 9, 2021
1 parent c711bd7 commit c3ee151
Show file tree
Hide file tree
Showing 34 changed files with 1,044 additions and 253 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ omit =
cottonformation/tests/*
cottonformation/code/*
cottonformation/core/env.py
cottonformation/stacks/*

[report]
# Regexes for lines to exclude from consideration
Expand Down
4 changes: 2 additions & 2 deletions cottonformation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
__fingerprint_of_license_file = "e5834e80c86c4898dd9f1462a2349cbf"


from .core import constant, helpers
from .core import constant, helpers, exc
from .core.model import (
# data model
Parameter, Property, Resource, Output, Export,
Rule, Mapping, Condition, Transform,
Pack, Tag,
ResourceGroup, Tag,

# intrinsic function
Ref, Base64, Cidr, FindInMap, GetAtt, GetAZs,
Expand Down
2 changes: 1 addition & 1 deletion cottonformation/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.0.2"
__version__ = "0.0.3"

if __name__ == "__main__": # pragma: no cover
print(__version__)
6 changes: 6 additions & 0 deletions cottonformation/core/exc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-

from .template import (
AWSObjectLogicIdConlictError,
AWSObjectNotExistsError,
)
15 changes: 9 additions & 6 deletions cottonformation/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TypeHint:
intrinsic_int = typing.Union[int, dict, 'IntrinsicFunction']
addable_obj = typing.Union[
'Parameter', 'Resource', 'Output',
'Rule', 'Mapping', 'Condition', 'Pack',
'Rule', 'Mapping', 'Condition', 'ResourceGroup',
]
dependency_obj = typing.Union[
str, 'Resource', 'Parameter', 'Mapping', 'Condition'
Expand All @@ -38,6 +38,8 @@ class _IntrinsicFunctionType:
class _Addable:
"""
A base class for type check for item to be added to a Template.
Includes 'Parameter', 'Resource', 'Output', 'Rule', 'Mapping', 'Condition', 'Pack'.
"""
@property
def gid(self) -> str:
Expand Down Expand Up @@ -864,16 +866,17 @@ class Transform(AwsObject, _ListMember):


@attr.s
class Pack(AwsObject, _DictMember):
CLASS_TYPE = "99-Pack"
class ResourceGroup(AwsObject, _DictMember, _Dependency):
CLASS_TYPE = "99-Resource-Group"

id: str = attr.ib(
default="__never_exists__",
validator=vs.instance_of(str)
)
DependsOn: typing.List[TypeHint.addable_obj] = attr.ib(
DependsOn: typing.Union[TypeHint.dependency_obj, typing.List[TypeHint.dependency_obj]] = attr.ib(
factory=list,
validator=vs.instance_of(list),
validator=vs.optional(vs.instance_of((str, _Dependency, list))),
converter=ensure_list,
)

def add(self, obj: TypeHint.addable_obj):
Expand Down Expand Up @@ -958,5 +961,5 @@ def serialize(obj: typing.Union['AwsObject', dict, typing.Any]) -> typing.Any:
Rule.CLASS_TYPE: "Rules",
Mapping.CLASS_TYPE: "Mappings",
Condition.CLASS_TYPE: "Conditions",
Pack.CLASS_TYPE: "Packs",
ResourceGroup.CLASS_TYPE: "Groups",
}
33 changes: 32 additions & 1 deletion cottonformation/core/stack.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
# -*- coding: utf-8 -*-

import attr
from .template import Template


@attr.s
class Stack:
_stack_data = None
"""
Stack is an abstraction layer representing a CloudFormation stack.
All AWS Resource declaration logics should go to the method of a Stack object.
Declare aws resource directly in python script without putting them into a
method / function is a bad idea. If there's a single line of code breaks,
it breaks the entire scripts. And you have to comment in/out big chunk of
code for debugging. But if you organize AWS Resource declaration logics
in many methods. You can easily control the subset of the resources to create,
the order of those resources to be created.
"""
_stack_data = None # cloudformation stack data cache

@property
def stack_name(self):
"""
CloudFormation stack name.
"""
raise NotImplementedError

def get_stack_data(self, boto_ses):
"""
Get the boto3 cloudformation.describe_stacks response data that represent
the current stack.
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudformation.html#CloudFormation.Client.describe_stacks
"""
cf_client = boto_ses.client("cloudformation")
if self._stack_data is None:
self._stack_data = cf_client.describe_stacks(StackName=self.stack_name) \
Expand All @@ -25,4 +47,13 @@ def get_output_value(self, boto_ses, output_id):
}
return output_key_values[output_id]

def __attrs_post_init__(self):
self.template = Template()
self.post_hook()

def post_hook(self):
"""
A user custom post stack initialization hook function. Will be executed
after object initialization.
"""
pass
Loading

0 comments on commit c3ee151

Please sign in to comment.