Skip to content
Snippets Groups Projects
Commit 69d9ab01 authored by Tomohiro Ono's avatar Tomohiro Ono Committed by Yas Naoi
Browse files

Issue #3292812 by onotm, yas, kumikoono: Refactor the cleanup script for OpenStack resources

parent 700a6d7b
No related branches found
No related tags found
No related merge requests found
......@@ -11,10 +11,15 @@ readonly TOKEN_PLACEHOLDER='%TOKEN%'
readonly DEFAULT_DRY_RUN_OPTION='--dry-run'
readonly DEFAULT_USERNAME='admin'
readonly DEFAULT_DOMAIN_ID='default'
readonly DEFAULT_COMPUTE_API_ENDPOINT_SUFFIX='/compute'
readonly DEFAULT_COMPUTE_API_VERSION='v2.1'
readonly DEFAULT_IDENTITY_API_ENDPOINT_SUFFIX='/identity'
readonly DEFAULT_IDENTITY_API_VERSION='v3'
readonly DEFAULT_IMAGE_API_ENDPOINT_SUFFIX='/image'
readonly DEFAULT_IMAGE_API_VERSION='v2'
readonly DEFAULT_NETWORK_API_ENDPOINT_SUFFIX=':9696'
readonly DEFAULT_NETWORK_API_VERSION='v2.0'
readonly DEFAULT_VOLUME_API_ENDPOINT_SUFFIX='/volume'
readonly DEFAULT_VOLUME_API_VERSION='v3'
......@@ -26,24 +31,28 @@ function err() {
echo " [Error] $*" >&2
}
function error_if_command_not_found() {
function install_if_command_not_found() {
local cmd="${1}"
if ! (command -v "${cmd}" &>/dev/null); then
err "Command not found: ${cmd}"
exit 1
info "Command not found: ${cmd}"
apt-get update && apt-get -y install "${cmd}"
if ! (command -v "${cmd}" &>/dev/null); then
err "Failed to install ${cmd}"
exit 1
fi
fi
}
function usage() {
cat <<EOF
Usage: $0 -n name_pattern --identity-api-endpoint identity_api_endpoint --project-id project_id [OPTION]
Usage: $0 -n name_pattern --project-id project_id [OPTION]
Example:
OPENSTACK_PASSWORD=password \\
$0 \\
-n bdd-.*-random -t instance \\
--identity-api-endpoint https://example.com/identity \\
--compute-api-endpoint https://example.com/compute \\
--api-endpoint-base https://example.com \\
--project-id 177eb30821644194a9bb2befab82d4cd
OPTION:
......@@ -61,7 +70,12 @@ OPTION:
--domain-id: Domain ID. [default: ${DEFAULT_DOMAIN_ID}]
--project-id: Project ID. Required.
--identity-api-endpoint: Identity API endpoint. Required.
--api-endpoint-base: API endpoint base, e.g. \`https://example.com'
At least one of --api-endpoint-base or
--identity-api-endpoint is required
--identity-api-endpoint: Identity API endpoint.
At least one of --api-endpoint-base or
--identity-api-endpoint is required
--identity-api-version: Identity API version. [default: ${DEFAULT_IDENTITY_API_VERSION}]
--compute-api-endpoint: Compute API endpoint.
--compute-api-version: Compute API version. [default: ${DEFAULT_COMPUTE_API_VERSION}]
......@@ -87,6 +101,7 @@ function main() {
--domain-id) shift; domain_id="$1"; shift;;
--project-id) shift; project_id="$1"; shift;;
--api-endpoint-base) shift; api_endpoint_base="$1"; shift;;
--identity-api-endpoint) shift; identity_api_endpoint="$1"; shift;;
--identity-api-version) shift; identity_api_version="$1"; shift;;
--compute-api-endpoint) shift; compute_api_endpoint="$1"; shift;;
......@@ -104,15 +119,20 @@ function main() {
echo 'Missing name pattern.'
usage
fi
if [[ -z "${identity_api_endpoint}" ]]; then
echo 'Missing identity API endpoint.'
if [[ -z "${project_id}" ]]; then
echo 'Missing project ID.'
usage
fi
if [[ -z "${identity_api_endpoint}" ]] && [[ -z "${api_endpoint_base}" ]]; then
err 'At least one of --api-endpoint-base or --identity-api-endpoint' \
'must be passed.'
usage
fi
}
function setup() {
error_if_command_not_found 'curl'
error_if_command_not_found 'jq'
install_if_command_not_found 'curl'
install_if_command_not_found 'jq'
if [[ ! -v OPENSTACK_PASSWORD ]]; then
err 'OPENSTACK_PASSWORD variable is not set.'
......@@ -125,13 +145,23 @@ function setup() {
# Add one for authentication
readonly TOTAL=$(( 1+${#targets[@]} ))
dry_run_option="${dry_run_option:-${DEFAULT_DRY_RUN_OPTION}}"
dry_run_delete="${dry_run_delete:-${DEFAULT_DRY_RUN_OPTION}}"
username="${username:-${DEFAULT_USERNAME}}"
domain_id="${domain_id:-${DEFAULT_DOMAIN_ID}}"
compute_api_endpoint="${compute_api_endpoint:-${api_endpoint_base}${DEFAULT_COMPUTE_API_ENDPOINT_SUFFIX}}"
compute_api_version="${compute_api_version:-${DEFAULT_COMPUTE_API_VERSION}}"
identity_api_endpoint="${identity_api_endpoint:-${api_endpoint_base}${DEFAULT_IDENTITY_API_ENDPOINT_SUFFIX}}"
identity_api_version="${identity_api_version:-${DEFAULT_IDENTITY_API_VERSION}}"
image_api_endpoint="${image_api_endpoint:-${api_endpoint_base}${DEFAULT_IMAGE_API_ENDPOINT_SUFFIX}}"
image_api_version="${image_api_version:-${DEFAULT_IMAGE_API_VERSION}}"
network_api_endpoint="${network_api_endpoint:-${api_endpoint_base}${DEFAULT_NETWORK_API_ENDPOINT_SUFFIX}}"
network_api_version="${network_api_version:-${DEFAULT_NETWORK_API_VERSION}}"
volume_api_endpoint="${volume_api_endpoint:-${api_endpoint_base}${DEFAULT_VOLUME_API_ENDPOINT_SUFFIX}}"
volume_api_version="${volume_api_version:-${DEFAULT_VOLUME_API_VERSION}}"
}
......@@ -251,7 +281,10 @@ function request_api() {
local curl_command="curl -X ${method} -sL -H 'X-Auth-Token: ${TOKEN_PLACEHOLDER}' ${url}"
info "Executing \"${curl_command}\"."
curl_command="$(echo "${curl_command}" | sed -e "s/${TOKEN_PLACEHOLDER}/${token}/")"
curl_command="$(
echo "${curl_command}" \
| sed -e "s/${TOKEN_PLACEHOLDER}/${token}/"
)"
if ! raw_response="$(eval "${curl_command}")"; then
err "Failed to execute curl."
return 1
......@@ -268,6 +301,7 @@ function request_api() {
return 1
fi
echo "${response}"
return 0
}
function list_resources() {
......@@ -277,9 +311,15 @@ function list_resources() {
response="$(request_api 'GET' "${api_url}")" || return 1
attr="$(topic_attribute_of "${resource_type}")" || return 1
if [[ "${resource_type}" == 'keypair' ]]; then
results="$(echo "${response}" | jq -r ".${attr}[].keypair.name | select(.|test(\"^${val_reg_exp}\$\"))")" || return 1
results="$(
echo "${response}" \
| jq -r ".${attr}[].keypair.name | select(.|test(\"^${val_reg_exp}\$\"))"
)" || return 1
else
results="$(echo "${response}" | jq -r ".${attr}[] | select(.name|test(\"^${val_reg_exp}\$\")) | .id")" || return 1
results="$(
echo "${response}" \
| jq -r ".${attr}[] | select(.name|test(\"^${val_reg_exp}\$\")) | .id"
)" || return 1
fi
echo "${results}"
}
......@@ -304,13 +344,24 @@ function delete() {
if [[ "${dry_run_delete}" == '--no-dry-run' ]]; then
method='DELETE'
else
info 'Make a GET request instead of DELETE since "--dry-run" option was passed.'
info 'Make a GET request instead of DELETE since "--dry-run" option was' \
'passed.'
method='GET'
fi
for resource_id in ${resource_ids[@]}; do
response="$(request_api "${method}" "${api_url}/${resource_id}")"
request_api "${method}" "${api_url}/${resource_id}"
request_status="${?}"
if [[ "${request_status}" -ne '0' ]] ; then
return "${request_status}"
fi
done
if [[ "${dry_run_delete}" == '--no-dry-run' ]]; then
return 0
else
return 1
fi
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment