-
Notifications
You must be signed in to change notification settings - Fork 2
/
list_fsxn_volumes
executable file
·165 lines (157 loc) · 7.48 KB
/
list_fsxn_volumes
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/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 FSx volumes that a user has access to.
# It will list:
# o Region
# o File System ID
# o File System Name - optional
# o Volume ID
# o Volume Name
# o Volume Status
#
################################################################################
################################################################################
# This function outputs the usage information and exists.
################################################################################
usage () {
cat 1>&2 <<EOF
Usage $(basename $0) [-r region] [-a] [-o] [-f fileSystemName] [-i fileSystemId] [-n] [-s svmID]
Where: -r region allows you to specify the region you want the list from.
-a means all regions.
-n means include the file system name.
-f fileSystemName means to only include volumes that are under the named file system.
-i fileSystemId means to only include volumes that are under the file system with the specified file system ID.
-s svmID - Only show volumes that are under the specified SVM ID.
-o means to exclude svm root volumes. Only works in conjunction with the -s option.
EOF
exit 1
}
################################################################################
# Main logic starts here.
################################################################################
tmpout=/tmp/list_aws_vol-out.$$
tmpout2=/tmp/list_aws_vol-out0.$$
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, this script requires both the 'aws' and 'jq' commands to run." 1>&2
exit 1
fi
#
# Process command line arguments.
allRegions=false
region=$(aws configure list | egrep '^.*egion ' | awk '{print $2}')
includeFsName=false
excludeRoot=false
filter=""
fsid=""
fileSystemName=""
while getopts "hr:af:i:nos:" option; do
case "$option" in
r) region="$OPTARG"
;;
a) allRegions=true
;;
f) fileSystemName="$OPTARG"
;;
i) fsid="$OPTARG"
;;
n) includeFsName=true
;;
o) excludeRoot=true
er="and (.OntapConfiguration.StorageVirtualMachineRoot | not)"
;;
s) svmID="$OPTARG"
;;
*) usage
;;
esac
done
if [ ! -z "$fileSystenName" -a ! -z "$fsid" ]; then
echo "Error, you can't provide both -f and -n options." 1>&2
exit 1
fi
if [ ! -z "$fileSystemName" ]; then
fsid=$(aws fsx describe-file-systems --region $region --output=json 2> /dev/null | jq -r ".FileSystems[] | if((.Tags[] | select(.Key == \"Name\") .Value) == \"${fileSystemName}\") then .FileSystemId else empty end" 2> /dev/null)
if [ -z "$fsid" ]; then
echo "Error, failed to find the file system with the file system name of '$fileSystemName'." 1>&2
exit 1
fi
filter='--filters [{"Name":"file-system-id","Values":["'$fsid'"]}]'
fi
if [ ! -z "$fsid" -a -z "$fileSystemName" ]; then
fileSystemName=$(aws fsx describe-file-systems --region $region --output=json 2> /dev/null | jq -r ".FileSystems[] | if(.FileSystemId == \"$fsid\") then (.Tags[] | select(.Key == \"Name\") .Value) else empty end" 2> /dev/null)
if [ -z "$fileSystemName" ]; then
echo "Error, failed to find the file system with the file system ID of '$fsid'." 1>&2
exit 1
fi
filter='--filters [{"Name":"file-system-id","Values":["'$fsid'"]}]'
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
#
# Loop on all the regions.
for region in ${regions[*]}; do
#
# Check that the fsx service is supported in thie region
if [ ! -z "$(getent hosts fsx.$region.amazonaws.com)" ]; then
if [ -z "$svmID" ]; then
if [ "$excludeRoot" != "true" ]; then
aws fsx describe-volumes $filter --region=$region --output=json | jq -r '.Volumes[] | .FileSystemId + "," + .Name + "," + .VolumeId + "," + .Lifecycle' | sort > $tmpout
else
aws fsx describe-volumes $filter --region=$region --output=json | jq -r '.Volumes[] | if(.OntapConfiguration.StorageVirtualMachineRoot | not) then .FileSystemId + "," + .Name + "," + .VolumeId + "," + .Lifecycle else empty end' | sort > $tmpout
fi
else
if [ "$excludeRoot" != "true" ]; then
aws fsx describe-volumes $filter --region=$region --output=json | jq -r '.Volumes[] | if(.OntapConfiguration.StorageVirtualMachineId == "'$svmID'") then .FileSystemId + "," + .Name + "," + .VolumeId + "," + .Lifecycle else empty end' | sort > $tmpout
else
aws fsx describe-volumes $filter --region=$region --output=json | jq -r '.Volumes[] | if(.OntapConfiguration.StorageVirtualMachineId == "'$svmID'" and (.OntapConfiguration.StorageVirtualMachineRoot | not)) then .FileSystemId + "," + .Name + "," + .VolumeId + "," + .Lifecycle else empty end' | sort > $tmpout
fi
fi
if [ $includeFsName == "true" ]; then
aws fsx describe-file-systems --region=$region --output=json | jq -r '.FileSystems[] | .FileSystemId + "," + (.Tags[] | select(.Key == "Name") .Value)' | fgrep "$fileSystemName" > $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 %21s%-"maxNameLen"s %24s %10s %s\n"}; {if(first) {printf "\n"; printf formatStr, "Region", "FileSystem ID", "(Name)", "Volume ID", "State", "Volume Name"; first=0}; name="("fss[$1]")"; printf formatStr, region, $1, name, $3, $4, $2}' < $tmpout
else
awk -F, -v region=$region 'BEGIN {first=1; formatStr="%12s %21s %24s %10s %s\n"}; {if(first) {printf "\n"; printf formatStr, "Region", "FileSystem ID", "Volume ID", "State", "Volume Name"; first=0}; printf formatStr, region, $1, $3, $4, $2}' < $tmpout
fi
else
if [ $allRegions != "true" ]; then
printf "The fsx service is currently not supported in the $region region.\n"
fi
fi
done