Create s3 buckets via terraform and bind to app

This commit is contained in:
Ryan Ahearn
2022-09-16 16:26:02 -04:00
parent cb4036b1b0
commit e9815a6f8e
5 changed files with 60 additions and 2 deletions

View File

@@ -2,6 +2,12 @@ import json
import os import os
def find_by_service_name(services, service_name):
for i in range(len(services)):
if services[i]['name'] == service_name:
return services[i]
return None
def extract_cloudfoundry_config(): def extract_cloudfoundry_config():
vcap_services = json.loads(os.environ['VCAP_SERVICES']) vcap_services = json.loads(os.environ['VCAP_SERVICES'])
@@ -10,3 +16,13 @@ def extract_cloudfoundry_config():
'postgresql') 'postgresql')
# Redis config # Redis config
os.environ['REDIS_URL'] = vcap_services['aws-elasticache-redis'][0]['credentials']['uri'] os.environ['REDIS_URL'] = vcap_services['aws-elasticache-redis'][0]['credentials']['uri']
# CSV Upload Bucket Name
csv_bucket_service = find_by_service_name(vcap_services['s3'], f"notifications-api-csv-upload-bucket-{os.environ['DEPLOY_ENV']}")
if csv_bucket_service:
os.environ['CSV_UPLOAD_BUCKET_NAME'] = csv_bucket_service['credentials']['bucket']
# Contact List Bucket Name
contact_bucket_service = find_by_service_name(vcap_services['s3'], f"notifications-api-contact-list-bucket-{os.environ['DEPLOY_ENV']}")
if contact_bucket_service:
os.environ['CONTACT_LIST_BUCKET_NAME'] = contact_bucket_service['credentials']['bucket']

View File

@@ -539,8 +539,8 @@ class Staging(Config):
class Live(Config): class Live(Config):
NOTIFY_ENVIRONMENT = 'live' NOTIFY_ENVIRONMENT = 'live'
# buckets # buckets
CSV_UPLOAD_BUCKET_NAME = 'notifications-prototype-csv-upload' # created in gsa sandbox CSV_UPLOAD_BUCKET_NAME = os.environ.get('CSV_UPLOAD_BUCKET_NAME', 'notifications-prototype-csv-upload') # created in gsa sandbox
CONTACT_LIST_BUCKET_NAME = 'notifications-prototype-contact-list-upload' # created in gsa sandbox CONTACT_LIST_BUCKET_NAME = os.environ.get('CONTACT_LIST_BUCKET_NAME', 'notifications-prototype-contact-list-upload') # created in gsa sandbox
# TODO: verify below buckets only used for letters # TODO: verify below buckets only used for letters
# TEST_LETTERS_BUCKET_NAME = 'production-test-letters' # not created in gsa sandbox # TEST_LETTERS_BUCKET_NAME = 'production-test-letters' # not created in gsa sandbox
# DVLA_RESPONSE_BUCKET_NAME = 'notifications.service.gov.uk-ftp' # not created in gsa sandbox # DVLA_RESPONSE_BUCKET_NAME = 'notifications.service.gov.uk-ftp' # not created in gsa sandbox

View File

@@ -13,12 +13,15 @@ applications:
services: services:
- notifications-api-rds-((env)) - notifications-api-rds-((env))
- notifications-api-redis-((env)) - notifications-api-redis-((env))
- notifications-api-csv-upload-bucket-((env))
- notifications-api-contact-list-bucket-((env))
env: env:
NOTIFY_APP_NAME: api NOTIFY_APP_NAME: api
NOTIFY_LOG_PATH: /home/vcap/logs/app.log NOTIFY_LOG_PATH: /home/vcap/logs/app.log
FLASK_APP: application.py FLASK_APP: application.py
FLASK_ENV: production FLASK_ENV: production
DEPLOY_ENV: ((env))
NOTIFY_ENVIRONMENT: live NOTIFY_ENVIRONMENT: live
API_HOST_NAME: https://notifications-api.app.cloud.gov API_HOST_NAME: https://notifications-api.app.cloud.gov

View File

@@ -31,3 +31,25 @@ module "redis" {
recursive_delete = local.recursive_delete recursive_delete = local.recursive_delete
redis_plan_name = "redis-dev" redis_plan_name = "redis-dev"
} }
module "csv_upload_bucket" {
source = "github.com/18f/terraform-cloudgov//s3"
cf_user = var.cf_user
cf_password = var.cf_password
cf_org_name = local.cf_org_name
cf_space_name = local.cf_space_name
recursive_delete = local.recursive_delete
s3_service_name = "${local.app_name}-csv-upload-bucket-${local.env}"
}
module "contact_list_bucket" {
source = "github.com/18f/terraform-cloudgov//s3"
cf_user = var.cf_user
cf_password = var.cf_password
cf_org_name = local.cf_org_name
cf_space_name = local.cf_space_name
recursive_delete = local.recursive_delete
s3_service_name = "${local.app_name}-contact-list-bucket-${local.env}"
}

View File

@@ -19,13 +19,30 @@ def vcap_services():
'uri': 'redis uri' 'uri': 'redis uri'
} }
}], }],
's3': [
{
'name': 'notifications-api-csv-upload-bucket-test',
'credentials': {
'bucket': 'csv-upload-bucket'
}
},
{
'name': 'notifications-api-contact-list-bucket-test',
'credentials': {
'bucket': 'contact-list-bucket'
}
}
],
'user-provided': [] 'user-provided': []
} }
def test_extract_cloudfoundry_config_populates_other_vars(os_environ, vcap_services): def test_extract_cloudfoundry_config_populates_other_vars(os_environ, vcap_services):
os.environ['DEPLOY_ENV'] = 'test'
os.environ['VCAP_SERVICES'] = json.dumps(vcap_services) os.environ['VCAP_SERVICES'] = json.dumps(vcap_services)
extract_cloudfoundry_config() extract_cloudfoundry_config()
assert os.environ['SQLALCHEMY_DATABASE_URI'] == 'postgresql uri' assert os.environ['SQLALCHEMY_DATABASE_URI'] == 'postgresql uri'
assert os.environ['REDIS_URL'] == 'redis uri' assert os.environ['REDIS_URL'] == 'redis uri'
assert os.environ['CSV_UPLOAD_BUCKET_NAME'] == 'csv-upload-bucket'
assert os.environ['CONTACT_LIST_BUCKET_NAME'] == 'contact-list-bucket'