-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbudgets.sh
74 lines (55 loc) · 2.2 KB
/
budgets.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
#!/bin/bash
# usage: bash budgets.sh <csv_file> [EXECUTE]
# example: bash budgets.sh budgets.csv [EXECUTE]
lines=()
# read file
while IFS= read -r x
do
lines+=("$x")
done < $1
declare -A headers
#echo "${!headers[@]}" # ALL KEYS
#echo "${headers[@]}" # ALL VALUES
#echo "${headers[TagValue]}" # SPECIFIC KEY
#echo "${#headers[@]}" # COUNT OF KEY/VALUE PAIRS
# split into lines
for line_no in "${!lines[@]}"
do
line="${lines[line_no]}"
if [[ line_no -eq 0 ]] # HEADER ROW
then
IFS="," read -a headersArr <<< $line
for header_no in "${!headersArr[@]}"
do
headers["${headersArr[header_no]}"]="$header_no"
done
else # DATA ROW
IFS="," read -a dataArr <<< $line
subscriptionId="${dataArr[${headers[SubscriptionId]}]}"
tagName="${dataArr[${headers[TagName]}]}"
tagValue="${dataArr[${headers[TagValue]}]}"
budgetName="${dataArr[${headers[BudgetName]}]}"
amount="${dataArr[${headers[Amount]}]}"
startDate="$(date --date=${dataArr[${headers[StartDate]}]} '+%F')"
endDate="$(date --date=${dataArr[${headers[EndDate]}]} '+%F')"
timeGrain="${dataArr[${headers[TimeGrain]}]}"
echo "## SID:$subscriptionId |$tagName:$tagValue| BUDGET:$budgetName AMOUNT:$amount START:$startDate END:$endDate TIMEGRAIN:$timeGrain"
groupsCLIQuery="az group list --subscription $subscriptionId --query \"[?tags.$tagName == '$tagValue'].{ResourceGroup:name}\" -o tsv | sort | uniq"
#echo "$groupsCLIQuery"
groupsResults="$(eval $groupsCLIQuery)"
#echo "$groupsResults"
IFS=$'\n' read -a groupsArr -d '' <<< "$groupsResults"
#echo "${groupsArr[@]}"
for group in "${groupsArr[@]}"
do
budgetCLIQuery="az consumption budget create --subscription $subscriptionId --resource-group $group --budget-name $budgetName --category cost --amount $amount --start-date $startDate --end-date $endDate --time-grain $timeGrain"
echo "$budgetCLIQuery"
if [ ! -z "$2" ] && ([ $2 == "EXECUTE" ] || [ $2 == "execute" ])
then
eval $budgetCLIQuery
fi
done
echo
echo
fi
done