Skip to content

Commit d5efe30

Browse files
authored
Merge pull request #267 from NetApp/add_create_cifs_share
Added volume_cifs_share_create
2 parents 6407c9b + 395f459 commit d5efe30

2 files changed

Lines changed: 134 additions & 1 deletion

File tree

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/bin/bash
2+
#
3+
################################################################################
4+
# This script is used to create a cifs share in a volume in a FSx for ONTAP
5+
# file system. It will assume that the path is /cifs_share_name and will give
6+
# it full control permissions for everyone.
7+
#
8+
# It is dependent on the 'wf_utils' file that is included in this repo. That
9+
# file contains the 'get_token' function that is used to obtain a valid
10+
# access token that is needed to run the Workload Factory APIs. The file needs
11+
# to either be in the command search path or in the current directory.
12+
################################################################################
13+
14+
################################################################################
15+
# This function just prints the usage of this script and exits the program.
16+
################################################################################
17+
usage() {
18+
cat >&2 <<EOF
19+
This script is used to create a cifs share in a volume in a FSx for ONTAP
20+
file system. It will assume that the path is /cifs_share_name and will give
21+
it full control permissions for everyone.
22+
23+
Usage: $(basename $0) -t refresh_token -a blueXP_account_ID -c credentials_ID -r aws_region -f filesystem_ID -v volume_ID -s cifs_share_name
24+
25+
Where: refresh_token - Is a refresh token used to obtain an access token needed
26+
to run the Workload Factory APIs. You can obtain a refresh
27+
token by going to https://services.cloud.netapp.com/refresh-token
28+
blueXP_account_ID - is the BlueXP account ID. Run 'list_bluexp_accts' to get a
29+
list of accounts you have access to
30+
credentials_ID - is the Workload Factory credentials ID for the AWS account. Run
31+
'list_credentials' to get a list of credentials you have access to
32+
aws_region - is the AWS region where the FSx file systems are located
33+
filesystem_id - is the AWS file system ID of the FSx file system where the volume resides
34+
volume_ID - is the AWS volume ID of the volume where you want to create the cifs share.
35+
cifs_share_name - is the name of the share to create. Only alphanumeric characters, underscores and dashes are allowed.
36+
37+
Instead of passing parameters on the command line, you can set the
38+
following environment variables:
39+
40+
export REFRESH_TOKEN=<refresh_token>
41+
export BLUEXP_ACCOUNT_ID=<blueXP_account_ID>
42+
export CREDENTIALS_ID=<credentials_ID>
43+
export AWS_REGION=<aws_region>
44+
EOF
45+
exit 1
46+
}
47+
48+
################################################################################
49+
# Main logic starts here.
50+
################################################################################
51+
tmpout=$(mktemp /tmp/create_share-out.XXXXXX)
52+
tmperr=$(mktemp /tmp/create_share-err.XXXXXX)
53+
trap 'rm -f $tmpout $tmperr' exit
54+
#
55+
# Source the wf_utils file.
56+
wf_utils=$(command -v wf_utils)
57+
if [ -z "$wf_utils" ]; then
58+
if [ ! -x "./wf_utils" ]; then
59+
cat >&2 <<EOF
60+
Error: The 'wf_utils' script was not found in the current directory or in the command search path.
61+
It is required to run this script. You can download it from:
62+
https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples
63+
EOF
64+
exit 1
65+
else
66+
wf_utils=./wf_utils
67+
fi
68+
fi
69+
. "$wf_utils"
70+
#
71+
# Process command line options.
72+
while getopts "ht:a:c:r:f:v:s:" opt; do
73+
case $opt in
74+
t) REFRESH_TOKEN="$OPTARG" ;;
75+
a) BLUEXP_ACCOUNT_ID="$OPTARG" ;;
76+
c) CREDENTIALS_ID="$OPTARG" ;;
77+
r) AWS_REGION="$OPTARG" ;;
78+
f) FILESYSTEM_ID="$OPTARG" ;;
79+
v) VOLUME_ID="$OPTARG" ;;
80+
s) CIFS_SHARE_NAME="$OPTARG" ;;
81+
*) usage ;;
82+
esac
83+
done
84+
#
85+
# Declare an array of required options and the error message to display if they are not set.
86+
declare -A required_options
87+
required_options["REFRESH_TOKEN"]='Error: A BlueXP refresh token is required to run this script. It can be obtain from this web page:
88+
https://services.cloud.netapp.com/refresh-token\n\n'
89+
required_options["BLUEXP_ACCOUNT_ID"]='Error: A BlueXP account ID is required to run this script.
90+
You can get the list of accounts you have access to by running the "list_bluexp_accts" script
91+
found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples\n\n'
92+
required_options["CREDENTIALS_ID"]='Error: A Worload Factory Credential ID is required to run this script.
93+
You can get a list of credentials by running the "list_credentials" script
94+
found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples\n\n'
95+
required_options["AWS_REGION"]='Error: The AWS region where the file system is located is required.\n\n'
96+
required_options["FILESYSTEM_ID"]='Error: The ID of the FSxN file system is required.\n\n'
97+
required_options["VOLUME_ID"]='Error: An AWS Volume ID is required to run this script.
98+
You can get the list of file systems you have access to by running the "list_volumes" script
99+
found in this GitHub repository: https://github.com/NetApp/FSx-ONTAP-samples-scripts/tree/main/Management-Utilities/Workload-Factory-API-Samples\n\n'
100+
required_options["CIFS_SHARE_NAME"]='Error: The name of the cifs share is required.\n\n'
101+
102+
check_required_options
103+
#
104+
# Check if the required commands are available.
105+
for cmd in jq curl; do
106+
if ! command -v $cmd &> /dev/null; then
107+
echo "Error: The required command '$cmd' was not found. Please install it." >&2
108+
exit 1
109+
fi
110+
done
111+
112+
token=$(get_token)
113+
if [ -z "$token" ]; then
114+
echo "Error: Failed to obtain an access token. Exiting." >&2
115+
exit 1
116+
fi
117+
#
118+
# Get the existing shares.
119+
run_curl GET "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/fsx/v2/credentials/${CREDENTIALS_ID}/regions/${AWS_REGION}/file-systems/${FILESYSTEM_ID}/volumes/${VOLUME_ID}?include=cifsShares" $tmpout $tmperr
120+
cifsShares=$(jq -r '.cifsShares' $tmpout)
121+
#
122+
# Create a new share definition.
123+
# Check that the CIFS_SHARE_NAME only contains allowed characters (alphanumeric, underscores and dashes).
124+
if [[ ! "$CIFS_SHARE_NAME" =~ ^[a-zA-Z0-9_-]+$ ]]; then
125+
echo "Error: The CIFS share name can only contain alphanumeric characters, underscores and dashes." >&2
126+
exit 1
127+
fi
128+
newShare='{"name":"'$CIFS_SHARE_NAME'","path":"'/$CIFS_SHARE_NAME'","acls":[{"userOrGroup":"Everyone","permission":"full_control","type":"windows"}]}'
129+
#
130+
# Add the new share to the existing ones.
131+
cifsShares=$(echo "$cifsShares" | jq -r '. += ['$newShare']')
132+
cifsShares="{\"cifsShares\": $cifsShares}"
133+
run_curl PATCH "$token" "https://api.workloads.netapp.com/accounts/${BLUEXP_ACCOUNT_ID}/fsx/v2/credentials/${CREDENTIALS_ID}/regions/${AWS_REGION}/file-systems/${FILESYSTEM_ID}/volumes/${VOLUME_ID}" $tmpout $tmperr "$cifsShares"

Management-Utilities/Workload-Factory-API-Samples/wf_utils

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ run_curl () {
129129
-H "Authorization: Bearer $token" -o $output > $errorOutput
130130
exitCode=$?
131131
;;
132-
POST|PUT)
132+
POST|PUT|PATCH)
133133
curl -X "$method" -sw "%{http_code},%{errormsg}" "$url" \
134134
-H "Accept: $accept" "${extraHeaders[@]}" \
135135
-H "Content-Type:application/json" \

0 commit comments

Comments
 (0)