Skip to content

Commit

Permalink
Option to specify region, option to create user based on region, few …
Browse files Browse the repository at this point in the history
…other changes
  • Loading branch information
wmariuss committed Aug 6, 2019
1 parent 3ba9505 commit 9a7d3f5
Show file tree
Hide file tree
Showing 16 changed files with 239 additions and 169 deletions.
1 change: 1 addition & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
disable=missing-docstring
18 changes: 13 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
# Changelog

## v0.1.0
## 0.3.0

* Create/Remove multiple s3 buckets and dynamodb tables
- Fix unspecified location constraint
- Now you can specify region in config file
- Fix checking for existing bucket
- User can be created based on region specified in config file
- Apply PEP8

## v0.2.0

* Add tags for s3 bucket(s) and dynamodb table(s) created
* Optional add/remove IAM user
* Generate/remove access keys for the user created
- Add tags for s3 bucket(s) and dynamodb table(s) created
- Optional add/remove IAM user
- Generate/remove access keys for the user created

## v0.1.0

- Create/Remove multiple s3 buckets and dynamodb tables
4 changes: 3 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ pex = "==1.6.5"
setuptools = "*"
trequire = {editable = true,path = "."}
tox = "*"
pep8 = "*"
mypy = "*"

[packages]
boto3 = "==1.9.122"
cerberus = "==1.2"
pyyaml = "==5.1"
click = "==7.0"
termcolor = "==1.1.0"
urllib3 = ">=1.24.2"
botocore = "==1.12.127"

[requires]
python_version = "3.6"
259 changes: 156 additions & 103 deletions Pipfile.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Manage backend resources for terraform states (`.tfstate`). `trequire` means ter
## Requirements

* `Python >= 3.6`
* `aws credentials` file
* `aws credentials` file

## Install

Expand Down Expand Up @@ -52,7 +52,7 @@ requirements: # Required

## Tests

Very soon.
Soon.

## Authors

Expand Down
8 changes: 0 additions & 8 deletions examples/add_config.yaml

This file was deleted.

9 changes: 9 additions & 0 deletions examples/dev-add-backend.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
requirements:
profile: default
region: us-east-2
add:
buckets:
- dev-terraform-states
dynamodb:
- dev-terraform-lock-states
user: terraform
9 changes: 9 additions & 0 deletions examples/dev-remove-backend.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
requirements:
profile: default
region: us-east-2
remove:
buckets:
- dev-terraform-states
dynamodb:
- dev-terraform-lock-states
user: terraform
14 changes: 0 additions & 14 deletions examples/example_config.yaml

This file was deleted.

9 changes: 9 additions & 0 deletions examples/prod-add-backend.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
requirements:
profile: default
region: us-east-2
add:
buckets:
- prod-terraform-states
dynamodb:
- prod-terraform-lock-states
user: terraform-aws-api
8 changes: 0 additions & 8 deletions examples/remove_config.yaml

This file was deleted.

4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='trequire',
version='0.2.0',
version='0.3.0',
author='Marius Stanca',
author_email=['me@marius.xyz'],
url='https://github.com/wmariuss/trequire.git',
Expand Down Expand Up @@ -36,4 +36,4 @@
[console_scripts]
trequire=trequire.main:cli
'''
)
)
45 changes: 25 additions & 20 deletions trequire/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

from trequire.exceptions import GeneralExceptions


class SessionService(object):
def __init__(self, profile):
self.session = boto3.Session(profile_name=profile)
def __init__(self, profile, region):
self.session = boto3.Session(profile_name=profile, region_name=region)

def resource(self, service_name, client=False):
if client:
Expand All @@ -13,20 +14,24 @@ def resource(self, service_name, client=False):


class ManageRequirements(object):
def __init__(self, profile):
session = SessionService(profile)
def __init__(self, profile, region):
session = SessionService(profile, region)

self.s3_res = session.resource('s3')
self.s3_client = session.resource('s3', client=True)
self.dynamodb_res = session.resource('dynamodb')
self.dynamodb_client = session.resource('dynamodb', client=True)
self.iam_client = session.resource('iam', client=True)
self.region = region

def _enable_bucket_versioning(self, name):
versioning = self.s3_res.BucketVersioning(name)
versioning.enable()

def bucket(self, name):
'''
Create s3 bucket
'''
status = None
tag_list = {
'TagSet': [{
Expand All @@ -36,7 +41,8 @@ def bucket(self, name):
}

try:
self.s3_client.create_bucket(Bucket=name)
self.s3_client.create_bucket(Bucket=name,
CreateBucketConfiguration={'LocationConstraint': self.region})
status = '{} bucket created'.format(name)
except Exception as exc:
raise GeneralExceptions(exc)
Expand All @@ -59,23 +65,24 @@ def bucket(self, name):
return status

def dynamodb(self, name):
'''
Create dynamodb table
'''
tag_list = [{
'Key': 'Created by',
'Value': 'trequire tool'
}]
try:
table = self.dynamodb_res.create_table(
TableName=name,
KeySchema=[
{
KeySchema=[{
'AttributeName': 'LockID',
'KeyType': 'HASH'
},
],
AttributeDefinitions=[
{
'AttributeName': 'LockID',
'AttributeType': 'S'
AttributeDefinitions=[{
'AttributeName': 'LockID',
'AttributeType': 'S'
},
],
ProvisionedThroughput={
Expand All @@ -86,7 +93,7 @@ def dynamodb(self, name):

table.meta.client.get_waiter('table_exists').wait(TableName=name)
self.dynamodb_client.tag_resource(ResourceArn=table.table_arn,
Tags=tag_list)
Tags=tag_list)
return '{} table created'.format(name)
except Exception as e:
if 'Cannot create preexisting table' in str(e):
Expand All @@ -105,14 +112,13 @@ def get_buckets(self):

@property
def get_dynamodb_tables(self):
tables_list = []
tables_list = []

for table in self.dynamodb_res.tables.all():
if table.name not in tables_list:
tables_list.append(table.name)

return tables_list

return tables_list

def remove_bucket(self, name):
status = None
Expand All @@ -121,7 +127,7 @@ def remove_bucket(self, name):
status = 'Removed'
except Exception as exc:
raise GeneralExceptions(exc)

return status

def remove_dynamodb_table(self, name):
Expand All @@ -144,7 +150,7 @@ def iam_users(self):
users_list.update({
user['UserName']: user['UserId']
})

return users_list

def add_user(self, name):
Expand Down Expand Up @@ -192,12 +198,11 @@ def remove_user(self, name):

return status


def _get_access_id(self, user):
access_keys = self.iam_client.list_access_keys(UserName=user)
access_id_list = []

for access_info in access_keys['AccessKeyMetadata']:
access_id_list.append(access_info['AccessKeyId'])
return access_id_list

return access_id_list
5 changes: 3 additions & 2 deletions trequire/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class GeneralExceptions(Exception):
pass



class ConfigValidationExceptions(Exception):
pass
pass
6 changes: 3 additions & 3 deletions trequire/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ def run(file):
data = get_data(file)
requirements = data.get('requirements')
profile = requirements.get('profile', 'default')
region = requirements.get('region', 'us-east-1')

list_to_add = requirements.get('add', '')
list_to_remove = requirements.get('remove', '')

manage = ManageRequirements(profile)
manage = ManageRequirements(profile, region)
s3_buckets_list = manage.get_buckets
dynamodb_tables_list = manage.get_dynamodb_tables
users_list = manage.iam_users()
Expand All @@ -49,9 +50,8 @@ def run(file):
# Create s3 bucket(s)
if buckets_list_add:
for bucket in buckets_list_add:
create_bucket = manage.bucket(bucket)
if bucket not in s3_buckets_list:
# create_bucket = manage.bucket(bucket)
create_bucket = manage.bucket(bucket)
if 'created' in create_bucket:
click.echo(colored('[{}] s3 bucket created'.format(bucket), 'green'))
else:
Expand Down
5 changes: 4 additions & 1 deletion trequire/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from cerberus import Validator
from trequire.exceptions import ConfigValidationExceptions


class ConfigValidation(object):
def __init__(self):
pass
Expand All @@ -23,6 +24,8 @@ def data_validation(self, data={}):
schema:
profile:
type: string
region:
type: string
add:
type: dict
schema:
Expand Down Expand Up @@ -51,4 +54,4 @@ def data_validation(self, data={}):
if not status:
raise ConfigValidationExceptions("Invalid syntax: {0}".format(str((v.errors))))
else:
return status
return status

0 comments on commit 9a7d3f5

Please sign in to comment.