Skip to content

Commit

Permalink
feat: use gcp project name for bucket
Browse files Browse the repository at this point in the history
The bucket for terraform state is now comprised of the gcp project name to ensure uniqueness of the bucket name

BREAKING CHANGE: There is no longer a project parameter
  • Loading branch information
aps831 committed May 9, 2023
1 parent e7af1de commit 920ca1b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 37 deletions.
8 changes: 3 additions & 5 deletions README.tpl.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,27 @@ region = AWS region (eg eu-west-2)
To initialise a GCP project and bucket use:

```bash
curl -L https://raw.githubusercontent.com/aps831/terraform-bucket/${TAG}/gcp-init.sh | bash -s -- --account ${account} --project ${project} --gcpproject ${gcpproject} --region ${region}
curl -L https://raw.githubusercontent.com/aps831/terraform-bucket/${TAG}/gcp-init.sh | bash -s -- --account ${account} --gcpproject ${gcpproject} --region ${region}
```

where

```text
account = GCP account email address
project = project name (eg git repo plus random word)
gcpproject = GCP project name
region = GCP region (eg europe-west2)
```

Note that `gcpproject` and the storage bucket with name `${project}-terraform-state` must be globally unique.
Note that `gcpproject` and the storage bucket with name `${gcpproject}-terraform-state` must be globally unique.

To cleanup a GCP project and bucket use:

```bash
curl -L https://raw.githubusercontent.com/aps831/terraform-bucket/${TAG}/gcp-cleanup.sh | bash -s -- --project ${project} --gcpproject ${gcpproject}
curl -L https://raw.githubusercontent.com/aps831/terraform-bucket/${TAG}/gcp-cleanup.sh | bash -s -- --gcpproject ${gcpproject}
```

where

```text
project = project name (eg git repo plus random word)
gcpproject = GCP project name
```
20 changes: 5 additions & 15 deletions gcp-cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
set -e

function bucket_name() {
local project=$1
echo "${project}-terraform-state"
local gcpproject=$1
echo "${gcpproject}-terraform-state"
}

function delete_project_and_bucket() {

local project=$1
local gcpproject=$2
local gcpproject=$1

bucket_name="$(bucket_name "${project}")"
bucket_name="$(bucket_name "${gcpproject}")"

gsutil rm -r gs://"${bucket_name}"
gcloud projects delete "${gcpproject}" --quiet
Expand All @@ -22,17 +21,12 @@ function delete_project_and_bucket() {
## Script
##

project=""
gcpproject=""
while [[ $# -gt 0 ]]; do
case "$1" in
--help)
usage
;;
--project)
project="$2"
shift
;;
--gcpproject)
gcpproject="$2"
shift
Expand All @@ -44,12 +38,8 @@ while [[ $# -gt 0 ]]; do
shift
done

if [ "${project}" == "" ]; then
usage "Project name must be provided"
fi

if [ "${gcpproject}" == "" ]; then
usage "GCP project name must be provided"
fi

delete_project_and_bucket "${project}" "${gcpproject}"
delete_project_and_bucket "${gcpproject}"
23 changes: 6 additions & 17 deletions gcp-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Create GCP project and terraform state bucket
Options:
--help display this usage message and exit
--account GCP account email
--project Project name
--gcpproject GCP project name
--region GCP region
EOF
Expand Down Expand Up @@ -50,18 +49,17 @@ EOF
}

function bucket_name() {
local project=$1
echo "${project}-terraform-state"
local gcpproject=$1
echo "${gcpproject}-terraform-state"
}

function create_project_and_bucket() {

local account=$1
local project=$2
local gcpproject=$3
local region=$4
local gcpproject=$2
local region=$3

bucket_name="$(bucket_name "${project}")"
bucket_name="$(bucket_name "${gcpproject}")"

# Project
gcloud projects create "${gcpproject}" --enable-cloud-apis
Expand Down Expand Up @@ -108,7 +106,6 @@ function create_project_and_bucket() {
##

account=""
project=""
gcpproject=""
region=""
while [[ $# -gt 0 ]]; do
Expand All @@ -120,10 +117,6 @@ while [[ $# -gt 0 ]]; do
account="$2"
shift
;;
--project)
project="$2"
shift
;;
--gcpproject)
gcpproject="$2"
shift
Expand All @@ -143,10 +136,6 @@ if [ "${account}" == "" ]; then
usage "Account email must be provided"
fi

if [ "${project}" == "" ]; then
usage "Project name must be provided"
fi

if [ "${gcpproject}" == "" ]; then
usage "GCP project name must be provided"
fi
Expand All @@ -155,4 +144,4 @@ if [ "${region}" == "" ]; then
usage "Region must be provided"
fi

create_project_and_bucket "${account}" "${project}" "${gcpproject}" "${region}"
create_project_and_bucket "${account}" "${gcpproject}" "${region}"

0 comments on commit 920ca1b

Please sign in to comment.