forked from GoogleCloudPlatform/pbmm-on-gcp-onboarding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeids.sh
executable file
·137 lines (122 loc) · 3.98 KB
/
writeids.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/bash
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This Script will perform the below actions
# 1) set 3 types of variables inside all tfvar content
# 2) unset variables above (to be able to obfuscate code changes and git commit without posting org/billing/folder ids
set -e
modify()
{
array=$( find environments/** -type f -name "*.auto.tfvars" )
for i in "${array[@]}"
do
echo "$i pass - fill:${FILL}"
# OSX required empty arg after -i
if [[ $FILL == true ]]
then
# OSX requires ''
#sed -i '' "s/${BILLING_ID_SEARCH}/${BILLING_ID}/g" $i
sed -i "s/${BILLING_ID_SEARCH}/${BILLING_ID}/g" $i
sed -i "s/${ORGANIZATION_ID_SEARCH}/${ORGANIZATION_ID}/g" $i
sed -i "s/${FOLDER_ID_SEARCH}/${FOLDER_ID}/g" $i
else
sed -i "s/${BILLING_ID}/${BILLING_ID_SEARCH}/g" $i
sed -i "s/${ORGANIZATION_ID}/${ORGANIZATION_ID_SEARCH}/g" $i
sed -i "s/${FOLDER_ID}/${FOLDER_ID_SEARCH}/g" $i
fi
done
}
usage()
{
echo "usage: options:<c|o|b|f>"
echo "example: ./writeids.sh -c fill|unfill -o org_id -b billing_id -f folder_id"
echo "example: ./writeids.sh -c unfill -b 1111-2222-3333 -o 4444-5555-9999 -f 012345678901"
echo "example: ./writeids.sh -c fill -b 1111-2222-3333 -o 4444-5555-9999 -f 012345678901"
echo "example: project only (org/billing derived): ./writeids.sh -c fill -p michael-proj-id -f 012345678901"
echo "example: no project (project/org/billing derived): ./writeids.sh -c fill -f 012345678901"
}
unsetids()
{
FILL=false
echo "reverting IDs: billing: ${BILLING_ID} organization: ${ORGANIZATION_ID} folder: ${FOLDER_ID} to placeholders"
modify
}
setids()
{
FILL=true
echo "replacing IDs: billing: ${BILLING_ID} organization: ${ORGANIZATION_ID} folder: ${FOLDER_ID} from placeholders"
modify
}
no_args="true"
PROJECT_ID=
COMMAND=fill
BILLING_ID_SEARCH=REPLACE_WITH_BILLING_ID
ORGANIZATION_ID_SEARCH=REPLACE_ORGANIZATION_ID
#DOMAIN_NAME
FOLDER_ID_SEARCH=REPLACE_FOLDER_ID
BILLING_ID=
ORGANIZATION_ID=
FOLDER_ID=
FILL=true
#FILL=false
while getopts "p:c:o:b:f:v:" flag;
do
case "${flag}" in
c) COMMAND=${OPTARG};;
p) PROJECT_ID=${OPTARG};;
o) ORGANIZATION_ID=${OPTARG};;
b) BILLING_ID=${OPTARG};;
f) FOLDER_ID=${OPTARG};;
*) usage
exit 1
;;
esac
no_args="false"
done
# get current project
if [[ -z "$PROJECT_ID" ]]
then
PROJECT_ID=$(gcloud config list --format 'value(core.project)')
if [[ -z "$PROJECT_ID" ]]
then
echo "Run the following before starting the script so we can derive the org/billing ids from the project:"
echo "gcloud config set project <project_id>"
exit 1
fi
else
echo "project passed as: $PROJECT_ID"
fi
# get org and billing id based on project if required
if [[ -z "$ORGANIZATION_ID" ]]
then
ORGANIZATION_ID=$(gcloud projects get-ancestors $PROJECT_ID --format='get(id)' | tail -1)
echo "Derived organization_id: $ORGANIZATION_ID"
fi
if [[ -z "$BILLING_ID" ]]
then
BILLING_ID=$(gcloud alpha billing projects describe $PROJECT_ID '--format=value(billingAccountName)' | sed 's/.*\///')
echo "Derived billing_id: $BILLING_ID"
fi
# Exit script and print usage if no arguments are passed.
if [[ $no_args == true ]]; then
usage
exit 1
fi
if [[ $COMMAND == "unfill" ]]
then
unsetids
else
setids
fi