Skip to content
This repository has been archived by the owner on Jan 2, 2019. It is now read-only.

Commit

Permalink
add cloudwatch logs support
Browse files Browse the repository at this point in the history
  • Loading branch information
earthmant authored and earthmant committed Dec 10, 2017
1 parent 4686991 commit 4f2982c
Show file tree
Hide file tree
Showing 9 changed files with 898 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cloudify_awssdk/ecs/resources/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from cloudify_awssdk.common.constants import EXTERNAL_RESOURCE_ID

from cloudify.exceptions import NonRecoverableError

RESOURCE_TYPE = 'ECS Service'
CLUSTER = 'cluster'
CLUSTER_TYPE = 'cloudify.nodes.aws.ECS.Cluster'
Expand Down
52 changes: 52 additions & 0 deletions cloudify_boto3/cloudwatchlogs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# #######
# Copyright (c) 2017 GigaSpaces Technologies Ltd. All rights reserved
#
# 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.
"""
Cloudwatch Logs
~~~
AWS Cloudwatch Logs base interface
"""
# Cloudify AWS
from cloudify_boto3.common import AWSResourceBase
from cloudify_boto3.common.connection import Boto3Connection

# pylint: disable=R0903


class AWSCloudwatchLogsBase(AWSResourceBase):
"""
AWS Cloudwatch Logs interface
"""
def __init__(self, ctx_node, resource_id=None, client=None, logger=None):
AWSResourceBase.__init__(
self, client or Boto3Connection(ctx_node).client('logs'),
resource_id=resource_id, logger=logger)

@property
def properties(self):
"""Gets the properties of an external resource"""
raise NotImplementedError()

@property
def status(self):
"""Gets the status of an external resource"""
raise NotImplementedError()

def create(self, params):
"""Creates a resource"""
raise NotImplementedError()

def delete(self, params=None):
"""Deletes a resource"""
raise NotImplementedError()
1 change: 1 addition & 0 deletions cloudify_boto3/cloudwatchlogs/resources/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# DO NOT REMOVE
118 changes: 118 additions & 0 deletions cloudify_boto3/cloudwatchlogs/resources/log_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# #######
# Copyright (c) 2017 GigaSpaces Technologies Ltd. All rights reserved
#
# 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.
"""
CloudwatchLogs.log_group
~~~~~~~~~~~~~~
AWS Cloudwatch Logs Log Group interface
"""
# Cloudify
from cloudify_boto3.common import decorators, utils
from cloudify_boto3.cloudwatchlogs import AWSCloudwatchLogsBase
# Boto
from botocore.exceptions import ClientError

RESOURCE_TYPE = 'Cloudwatch Logs Log Group'
RESOURCE_NAME = 'logGroupName'
RESOURCES = 'logGroups'
SEARCH_KEY = 'logGroupNamePrefix'


class CloudwatchLogGroup(AWSCloudwatchLogsBase):
"""
AWS AWS Cloudwatch Log Group interface
"""
def __init__(self, ctx_node, resource_id=None, client=None, logger=None):
AWSCloudwatchLogsBase.__init__(
self, ctx_node, resource_id, client, logger)
self.type_name = RESOURCE_TYPE

@property
def properties(self):
"""Gets the properties of an external resource"""
params = {SEARCH_KEY: self.resource_id}
try:
resources = \
self.client.describe_alarms(**params)
except ClientError:
pass
else:
return resources.get(RESOURCES, [None])[0]

@property
def status(self):
"""Gets the status of an external resource"""
props = self.properties
if not props:
return None
return None

def create(self, params):
"""
Create a new AWS Cloudwatch Log Group.
"""
self.logger.debug('Creating %s with parameters: %s'
% (self.type_name, params))
res = self.client.create_log_group(**params)
self.logger.debug('Response: %s' % res)
return res

def delete(self, params=None):
"""
Deletes an existing AWS Cloudwatch Log Group.
"""
self.logger.debug('Deleting %s with parameters: %s'
% (self.type_name, params))
res = self.client.delete_log_group(**params)
self.logger.debug('Response: %s' % res)
return res


@decorators.aws_resource(resource_type=RESOURCE_TYPE)
def prepare(ctx, resource_config, **_):
"""Prepares an AWS Cloudwatch Log Group"""
# Save the parameters
ctx.instance.runtime_properties['resource_config'] = resource_config


@decorators.aws_resource(CloudwatchLogGroup, RESOURCE_TYPE)
def create(ctx, iface, resource_config, **_):
"""Creates an AWS Cloudwatch Log Group"""
# Create a copy of the resource config for clean manipulation.
params = \
dict() if not resource_config else resource_config.copy()
resource_id = \
iface.resource_id or \
utils.get_resource_id(
ctx.node,
ctx.instance,
params.get(RESOURCE_NAME),
use_instance_id=True)
params[RESOURCE_NAME] = resource_id
utils.update_resource_id(ctx.instance, resource_id)

# Actually create the resource
iface.create(params)


@decorators.aws_resource(CloudwatchLogGroup, RESOURCE_TYPE,
ignore_properties=True)
def delete(iface, resource_config, **_):
"""Deletes an AWS Cloudwatch Log Group"""
# Create a copy of the resource config for clean manipulation.
params = \
dict() if not resource_config else resource_config.copy()
if RESOURCE_NAME not in params.keys():
params.update({RESOURCE_NAME: iface.resource_id})
iface.delete(params)
153 changes: 153 additions & 0 deletions cloudify_boto3/cloudwatchlogs/resources/log_stream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# #######
# Copyright (c) 2017 GigaSpaces Technologies Ltd. All rights reserved
#
# 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.
"""
CloudwatchLogs.log_stream
~~~~~~~~~~~~~~
AWS Cloudwatch Logs Log Stream interface
"""
# Cloudify
from cloudify_boto3.common import decorators, utils, constants
from cloudify_boto3.cloudwatchlogs import AWSCloudwatchLogsBase
# Boto
from botocore.exceptions import ClientError

RESOURCE_TYPE = 'Cloudwatch Logs Log Stream'
RESOURCE_NAME = 'logStreamName'
RESOURCES = 'logStreams'
SEARCH_KEY = 'logStreamNamePrefix'
PARENT_RESOURCE_NAME = 'logGroupName'


class CloudwatchLogStream(AWSCloudwatchLogsBase):
"""
AWS Cloudwatch Log Stream interface
"""
def __init__(self, ctx_node, resource_id=None, client=None, logger=None):
AWSCloudwatchLogsBase.__init__(
self, ctx_node, resource_id, client, logger)
self.type_name = RESOURCE_TYPE
self.parent_resource_name = None

@property
def properties(self):
"""Gets the properties of an external resource"""
if not self.parent_resource_name:
return None
params = {SEARCH_KEY: self.resource_id}
try:
resources = \
self.client.describe_log_streams(**params)
except ClientError:
pass
else:
return resources.get(RESOURCES, [None])[0]

@property
def status(self):
"""Gets the status of an external resource"""
props = self.properties
if not props:
return None
return None

def create(self, params):
"""
Create a new AWS Cloudwatch Log Stream.
"""
self.logger.debug('Creating %s with parameters: %s'
% (self.type_name, params))
res = self.client.create_log_stream(**params)
self.logger.debug('Response: %s' % res)
return res

def delete(self, params=None):
"""
Deletes an existing AWS Cloudwatch Log Stream.
"""
self.logger.debug('Deleting %s with parameters: %s'
% (self.type_name, params))
res = self.client.delete_log_stream(**params)
self.logger.debug('Response: %s' % res)
return res


@decorators.aws_resource(resource_type=RESOURCE_TYPE)
def prepare(ctx, resource_config, **_):
"""Prepares an AWS Cloudwatch Log Stream"""
# Save the parameters
ctx.instance.runtime_properties['resource_config'] = resource_config


@decorators.aws_resource(CloudwatchLogStream, RESOURCE_TYPE)
def create(ctx, iface, resource_config, **_):
"""Creates an AWS Cloudwatch Log Stream"""

# Create a copy of the resource config for clean manipulation.
params = \
dict() if not resource_config else resource_config.copy()

parent_name = \
params.get(PARENT_RESOURCE_NAME)
if not parent_name:
parent_node = \
utils.find_rel_by_type(
ctx.instance,
'cloudify.relationships.contained_in')
parent_name = \
parent_node.target.instance.runtime_properties.get(
constants.EXTERNAL_RESOURCE_ID)
setattr(iface, 'parent_resource_name', parent_name)
params[PARENT_RESOURCE_NAME] = parent_name
ctx.instance.runtime_properties[PARENT_RESOURCE_NAME] = \
parent_name

resource_id = \
iface.resource_id or \
utils.get_resource_id(
ctx.node,
ctx.instance,
params.get(RESOURCE_NAME),
use_instance_id=True)
params[RESOURCE_NAME] = resource_id
utils.update_resource_id(ctx.instance, resource_id)

# Actually create the resource
iface.create(params)


@decorators.aws_resource(CloudwatchLogStream, RESOURCE_TYPE,
ignore_properties=True)
def delete(iface, ctx, resource_config, **_):
"""Deletes an AWS Cloudwatch Log Stream"""
# Create a copy of the resource config for clean manipulation.
params = \
dict() if not resource_config else resource_config.copy()
parent_name = params.get(PARENT_RESOURCE_NAME)
if not parent_name:
parent_node = \
utils.find_rel_by_type(
ctx.instance,
'cloudify.relationships.contained_in')
parent_name = \
parent_node.target.instance.runtime_properties.get(
constants.EXTERNAL_RESOURCE_ID)
setattr(iface, 'parent_resource_name', parent_name)
params[PARENT_RESOURCE_NAME] = parent_name
ctx.instance.runtime_properties[PARENT_RESOURCE_NAME] = \
parent_name

if RESOURCE_NAME not in params.keys():
params.update({RESOURCE_NAME: iface.resource_id})
iface.delete(params)
Loading

0 comments on commit 4f2982c

Please sign in to comment.