#!/bin/bash

# Adding and connecting an EFS Storage Backend
## Pre requisite 
EFS_ID=fs-47cd610e

echo "managing EFS drive $EFS_ID"

VPC_ID=$(aws --profile canonical --region us-east-1 ec2 describe-vpcs \
    | jq --raw-output '.[][].VpcId')
# vpc-b4ce2bd1

echo "Will deploy in VPC $VPC_ID"

SUBNET_IDS=$(aws --profile canonical --region us-east-1 ec2 describe-subnets \
    | jq --raw-output '.[][].SubnetId')
# subnet-26300e52
# subnet-418dea7b
# subnet-0cc0984a
# subnet-645b204c

echo "Subnets to target are $SUBNET_IDS"

SG_ID=$(aws --profile canonical --region us-east-1 ec2 describe-instances \
    | jq --raw-output '.[][].Instances[] | select( .Tags[].Value | contains ("juju-k8s-machine-0")) | .SecurityGroups[0].GroupId')
# sg-bc4101c0

echo "Security Group will be $SG_ID"

for subnet in ${SUBNET_IDS}
do 
    aws --profile canonical --region us-east-1 efs create-mount-target \
        --file-system-id ${EFS_ID} \
        --subnet-id ${subnet} \
        --security-groups ${SG_ID}
done
# {
#     "LifeCycleState": "creating",
#     "IpAddress": "172.31.24.140",
#     "OwnerId": "131768076974",
#     "NetworkInterfaceId": "eni-8c0bb86c",
#     "MountTargetId": "fsmt-d900b590",
#     "SubnetId": "subnet-26300e52",
#     "FileSystemId": "fs-69de7c20"
# }
# {
#     "OwnerId": "131768076974",
#     "NetworkInterfaceId": "eni-265845ce",
#     "SubnetId": "subnet-418dea7b",
#     "FileSystemId": "fs-69de7c20",
#     "LifeCycleState": "creating",
#     "MountTargetId": "fsmt-da00b593",
#     "IpAddress": "172.31.63.172"
# }
# {
#     "IpAddress": "172.31.6.243",
#     "NetworkInterfaceId": "eni-8d930649",
#     "SubnetId": "subnet-0cc0984a",
#     "FileSystemId": "fs-69de7c20",
#     "OwnerId": "131768076974",
#     "MountTargetId": "fsmt-dc00b595",
#     "LifeCycleState": "creating"
# }
# {
#     "SubnetId": "subnet-645b204c",
#     "FileSystemId": "fs-69de7c20",
#     "NetworkInterfaceId": "eni-17f676e5",
#     "OwnerId": "131768076974",
#     "IpAddress": "172.31.44.34",
#     "LifeCycleState": "creating",
#     "MountTargetId": "fsmt-df00b596"
# }


