-
Notifications
You must be signed in to change notification settings - Fork 2
/
list_fsxn_svms
executable file
·125 lines (118 loc) · 5.49 KB
/
list_fsxn_svms
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
#!/bin/bash
################################################################################
# THIS SOFTWARE IS PROVIDED BY NETAPP "AS IS" AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL NETAPP BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR'
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
################################################################################
#
################################################################################
# This script will list all the AWS FSxN storage virtual machines (SVMs) that
# the user has access to. It will list:
# o Region
# o File system ID
# o File System Name - optional
# o SVM ID
# o SVM Name
################################################################################
################################################################################
# This function outputs the usage information and exists.
################################################################################
usage () {
cat 1>&2 <<EOF
Usage $(basename $0) [-r region] [-a] [-n] [-i fileSystemID] [-f fileSystemName]
Where: -a means all regions
-n means to include file systems name
-i means to only include SVMs that reside under the FSxN file system with the fileSystemID.
-f means to only include SVMs that reside under the FSxN file system with the file system name.
EOF
exit 1
}
################################################################################
# Main logic starts here.
################################################################################
tmpout=/tmp/list_aws_svms-out.$$
tmpout2=/tmp/list_aws_svms-out2.$$
trap 'rm -f $tmpout $tmpout2' exit
#
# Check that the required utilities are installed.
if which aws jq > /dev/null 2>&1; then
:
else
echo "Error, both the 'aws' and 'jq' commands are required to run this script." 1>&2
exit 1
fi
#
# Process command line arguments.
allRegions=false
includeFsName=false
region=$(aws configure list | egrep '^.*egion ' | awk '{print $2}')
while getopts "hanr:i:f:" option; do
case "$option" in
r) region="$OPTARG"
;;
a) allRegions=true
;;
n) includeFsName=true
;;
i) fileSystemID="$OPTARG"
;;
f) fileSystemName="$OPTARG"
;;
*) usage
;;
esac
done
if [ ! -z "$fileSystemID" -a ! -z "$fileSystemName" ]; then
echo "Error, you can't specify both a file system ID and a file system name." 1>&2
exit 1
fi
if [ "$allRegions" = "true" ]; then
#
# Generate a list of all the valid regions the user can search. That is the
# intersection of all the regions they have enabled, and the regions that
# support FSxN
allEndabledRegions=$(aws ec2 describe-regions --query "Regions[].RegionName" --output=json | jq -r '.[]')
allFsxnRegions=$(curl -s https://api.regional-table.region-services.aws.a2z.com/index.json | jq -r '.prices[] | select(.attributes."aws:serviceName" == "Amazon FSx for NetApp ONTAP") .attributes."aws:region"')
for reg in $allEndabledRegions; do
for fsxnReg in $allFsxnRegions; do
if [ $reg == $fsxnReg ]; then
regions+=($reg)
fi
done
done
if [ -z "$regions" ]; then
echo "Error, failed to get the list of regions that support FSxN" 1>&2
exit 1
fi
else
regions=($region)
fi
if [ ! -z "$fileSystemName" ]; then
fileSystemID=$(aws fsx describe-file-systems --output=json --region=$region 2> /dev/null | jq -r '.FileSystems[] | if((.Tags[] | select(.Key == "Name") .Value) == "'"${fileSystemName}"'") then .FileSystemId else empty end' 2> /dev/null)
if [ -z "$fileSystemID" ]; then
echo "Error, failed to find a file system with the name '$fileSystemName'. Maybe in a different region?" 1>&2
exit 1
fi
fi
#
# Loop on all the regions.
for region in ${regions[*]}; do
if [ -z "$fileSystemID" ]; then
aws fsx describe-storage-virtual-machines --region=$region | jq -r '.StorageVirtualMachines[] | .FileSystemId + "," + .StorageVirtualMachineId + "," + .Name' | sort > $tmpout
else
aws fsx describe-storage-virtual-machines --region=$region | jq -r '.StorageVirtualMachines[] | if(.FileSystemId == "'$fileSystemID'") then .FileSystemId + "," + .StorageVirtualMachineId + "," + .Name else empty end' | sort > $tmpout
fi
if [ $includeFsName == "true" ]; then
aws fsx describe-file-systems --region=$region | jq -r '.FileSystems[] | .FileSystemId + "," + (.Tags[] | select(.Key == "Name") .Value)' > $tmpout2
awk -F, -v region=$region 'BEGIN {first=1; maxNameLen=0; while(getline < "'$tmpout2'") {fss[$1]=$2; if(length($2) > maxNameLen) {maxNameLen=length($2)}}; maxNameLen +=2; formatStr="%12s %20s%-"maxNameLen"s %23s %s\n"}; {if(first) {printf "\n"; printf formatStr, "Region", "FileSystem ID", "(Name)", "SVM ID", "SVM Name"; first=0}; name="("fss[$1]")"; printf formatStr, region, $1, name, $2, $3}' < $tmpout
else
awk -F, -v region=$region 'BEGIN {first=1; formatStr="%12s %23s %23s %s\n"}; {if(first) {printf "\n"; printf formatStr, "Region", "FileSystem ID", "SVM ID", "SVM Name"; first=0}; printf formatStr, region, $1, $2, $3}' < $tmpout
fi
done