Skip to content

Commit

Permalink
Add healthcheck to swag manager and implement on all backend types
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Bengtson committed Jan 17, 2018
1 parent 470cbe7 commit 363423c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion swag_client/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
__summary__ = ("Cloud multi-account metadata management tool.")
__uri__ = "https://github.com/Netflix-Skunkworks/swag-client"

__version__ = "0.3.3"
__version__ = "0.3.4"

__author__ = "The swag developers"
__email__ = "oss@netflix.com"
Expand Down
5 changes: 5 additions & 0 deletions swag_client/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def __init__(self, *args, **kwargs):

def configure(self, *args, **kwargs):
"""Configures a SWAG manager. Overrides existing configuration."""

self.version = kwargs['schema_version']
self.namespace = kwargs['namespace']
self.backend = get(kwargs['type'])(*args, **kwargs)
Expand Down Expand Up @@ -84,6 +85,10 @@ def get_all(self, search_filter=None):

return items

def health_check(self):
"""Fetch all data from backend."""
return self.backend.health_check()

def get_service_enabled(self, name, accounts_list=None, search_filter=None, region=None):
"""Get a list of accounts where a service has been enabled."""
if not accounts_list:
Expand Down
13 changes: 13 additions & 0 deletions swag_client/backends/dynamodb.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

import boto3
from botocore.exceptions import ClientError
from dogpile.cache import make_region

from swag_client.backend import SWAGManager
Expand Down Expand Up @@ -67,3 +68,15 @@ def get_all(self):
))

return self.table.scan()['Items']

def health_check(self):
"""Gets a single item to determine if Dynamo is functioning."""
logger.debug('Health Check on Table: {namespace}'.format(
namespace=self.namespace
))

try:
self.table.scan(Limit=1)
return True
except ClientError as e:
logger.debug('Error encountered with Database. Assume unhealthy')
9 changes: 9 additions & 0 deletions swag_client/backends/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,12 @@ def get_all(self):
))

return load_file(self.data_file)


def health_check(self):
"""Checks to make sure the file is there."""
logger.debug('Health Check on file for: {namespace}'.format(
namespace=self.namespace
))

return os.path.isfile(self.data_file)
12 changes: 12 additions & 0 deletions swag_client/backends/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,15 @@ def get_all(self):
))

return load_file(self.client, self.bucket_name, self.data_file)

def health_check(self):
"""Uses head object to make sure the file exists in S3."""
logger.debug('Health Check on S3 file for: {namespace}'.format(
namespace=self.namespace
))

try:
self.client.head_object(Bucket=self.bucket_name, Key=self.data_file)
return True
except ClientError as e:
logger.debug('Error encountered with S3. Assume unhealthy')

0 comments on commit 363423c

Please sign in to comment.