Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Jc2k committed May 25, 2015
1 parent a614912 commit 884e92b
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 31 deletions.
4 changes: 2 additions & 2 deletions touchdown/aws/account.py
Expand Up @@ -37,11 +37,11 @@ class Account(BaseAccount):
root = argument.Resource(Workspace)


class Describe(Plan):
class Null(Plan):

resource = Account
default = True
name = "describe"
name = "null"
_session = None

@property
Expand Down
31 changes: 17 additions & 14 deletions touchdown/aws/common.py
Expand Up @@ -172,23 +172,10 @@ def run(self):
)


class SimpleDescribe(object):

name = "describe"

describe_filters = None
describe_notfound_exception = None

signature = (
Present('name'),
)
class SimplePlan(object):

_client = None

def __init__(self, runner, resource):
super(SimpleDescribe, self).__init__(runner, resource)
self.object = {}

@property
def session(self):
return self.parent.session
Expand All @@ -200,6 +187,22 @@ def client(self):
self._client = session.create_client(self.service_name)
return self._client


class SimpleDescribe(SimplePlan):

name = "describe"

describe_filters = None
describe_notfound_exception = None

signature = (
Present('name'),
)

def __init__(self, runner, resource):
super(SimpleDescribe, self).__init__(runner, resource)
self.object = {}

def get_describe_filters(self):
return {
self.key: self.resource.name
Expand Down
4 changes: 3 additions & 1 deletion touchdown/aws/logs/tail.py
Expand Up @@ -19,13 +19,15 @@

from touchdown.core import plan
from touchdown.core.datetime import parse_datetime_as_seconds
from touchdown.aws import common
from touchdown.aws.logs import LogGroup


class Plan(plan.Plan):
class Plan(common.SimplePlan, plan.Plan):

name = "tail"
resource = LogGroup
service_name = "logs"

def tail(self, start, end, follow):
kwargs = {
Expand Down
5 changes: 4 additions & 1 deletion touchdown/core/plan.py
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

import six
from . import errors
from . import errors, resource


class PlanType(type):
Expand Down Expand Up @@ -76,6 +76,9 @@ def get_actions(self):
class NullPlan(Plan):
""" A plan that doesn't do anything """

resource = resource.Resource
name = "null"


class ArgumentAssertion(object):

Expand Down
6 changes: 6 additions & 0 deletions touchdown/core/resource.py
Expand Up @@ -77,6 +77,11 @@ def __init__(self):
self.fields = {}
self.field_order = []

def get_plan(self, plan):
for cls in self.mro:
if hasattr(cls, "meta") and plan in cls.meta.plans:
return cls.meta.plans[plan]

def iter_fields_in_order(self):
for name in self.field_order:
yield self.fields[name]
Expand Down Expand Up @@ -124,6 +129,7 @@ def __new__(meta_cls, class_name, bases, new_attrs):

# Actually build a class
cls = type.__new__(meta_cls, class_name, bases, new_attrs)
cls.meta.mro = cls.mro()

# Allow fields to contribute to the class...
for field in cls.meta.iter_fields_in_order():
Expand Down
7 changes: 3 additions & 4 deletions touchdown/goals/apply.py
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from touchdown.core import plan
from touchdown.core.goals import Goal, register
from touchdown.goals.action import ActionGoalMixin

Expand All @@ -23,12 +22,12 @@ class Apply(ActionGoalMixin, Goal):

def get_plan_class(self, resource):
if "destroy" in resource.policies:
return resource.meta.plans["destroy"]
return resource.meta.get_plan("destroy")

if "never-create" in resource.policies:
return resource.meta.plans["describe"]
return resource.meta.get_plan("describe")

return resource.meta.plans.get("apply", resource.meta.plans.get("describe", plan.NullPlan))
return resource.meta.get_plan("apply") or resource.meta.get_plan("describe") or resource.meta.get_plan("null")


register(Apply)
5 changes: 2 additions & 3 deletions touchdown/goals/destroy.py
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from touchdown.core import plan
from touchdown.core.goals import Goal, register
from touchdown.goals.action import ActionGoalMixin

Expand All @@ -24,8 +23,8 @@ class Destroy(ActionGoalMixin, Goal):

def get_plan_class(self, resource):
if "never-destroy" not in resource.policies:
return resource.meta.plans.get("destroy", resource.meta.plans.get("describe", plan.NullPlan))
return resource.meta.plans.get("describe", plan.NullPlan)
return resource.meta.get_plan("destroy") or resource.meta.get_plan("describe") or resource.meta.get_plan("null")
return resource.meta.get_plan("describe") or resource.meta.get_plan("null")


register(Destroy)
15 changes: 9 additions & 6 deletions touchdown/goals/tail.py
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from touchdown.core import plan
from touchdown.core.goals import Goal, register


Expand All @@ -21,19 +20,23 @@ class Tail(Goal):
name = "tail"

def get_plan_class(self, resource):
return resource.meta.plans.get("tail", plan.NullPlan)
plan_class = resource.meta.get_plan("tail")
if not plan_class:
plan_class = resource.meta.get_plan("null")
return plan_class

def execute(self):
tailers = {}

def _(e, r):
plan = self.get_plan(r)
if not plan.name:
return
tailers[plan.name] = plan
p = self.get_plan(r)
if p.name == "tail":
tailers[p.resource.name] = p

for progress in self.Map(self.get_plan_order(), _, self.ui.echo):
self.ui.echo("\r[{: >6.2%}] Building plan...".format(progress), nl=False)
self.ui.echo("")

tailers["application.log"].tail("5m", None, True)

register(Tail)

0 comments on commit 884e92b

Please sign in to comment.