Skip to content

Instantly share code, notes, and snippets.

@xinaxu
Created May 20, 2024 21:30
Show Gist options
  • Save xinaxu/d75bc852a96dfc5815c54a130dc166de to your computer and use it in GitHub Desktop.
Save xinaxu/d75bc852a96dfc5815c54a130dc166de to your computer and use it in GitHub Desktop.
Add a Storage Provider to Spade and associate with a tenant
#!/bin/bash
# Script to add a storage provider (SP) to the Spade database
# Usage: ./add_sp.sh <provider_id> <tenant_id> <org_id> <city_id> <country_id> <continent_id>
# Check if the correct number of arguments are passed
if [ "$#" -ne 6 ]; then
echo "Usage: $0 <provider_id> <tenant_id> <org_id> <city_id> <country_id> <continent_id>"
echo "<provider_id>: The SP f-id represented as a number (e.g., f01234 -> 1234)."
echo "<tenant_id>: The ID of the tenant to associate with the SP."
echo "<org_id>: The organization ID for the SP."
echo "<city_id>: The city ID for the SP."
echo "<country_id>: The country ID for the SP."
echo "<continent_id>: The continent ID for the SP."
exit 1
fi
# Assigning arguments to variables
PROVIDER_ID=$1
TENANT_ID=$2
ORG_ID=$3
CITY_ID=$4
COUNTRY_ID=$5
CONTINENT_ID=$6
# Define database connection variables
DB_HOST="your_db_host"
DB_NAME="your_db_name"
DB_USER="your_db_user"
DB_PASSWORD="your_db_password"
# Function to execute SQL commands
execute_sql() {
SQL_COMMAND=$1
PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -d $DB_NAME -U $DB_USER -t -c "$SQL_COMMAND"
}
# Start script execution
echo "Starting the process to add/associate SP: $PROVIDER_ID"
# Check if the provider already exists
SQL_CHECK_PROVIDER="SELECT COUNT(*) FROM spd.providers WHERE provider_id = $PROVIDER_ID;"
PROVIDER_EXISTS=$(execute_sql "$SQL_CHECK_PROVIDER" | xargs)
if [ "$PROVIDER_EXISTS" -eq 0 ]; then
# Insert storage provider if it doesn't exist
SQL_INSERT_PROVIDER="INSERT INTO spd.providers (provider_id, org_id, city_id, country_id, continent_id) VALUES ($PROVIDER_ID, $ORG_ID, $CITY_ID, $COUNTRY_ID, $CONTINENT_ID);"
execute_sql "$SQL_INSERT_PROVIDER"
echo "Storage provider added successfully with provider_id: $PROVIDER_ID"
else
echo "Storage provider already exists with provider_id: $PROVIDER_ID, skipping insertion."
fi
# Associate SP with tenant
SQL_ASSOCIATE_SP="INSERT INTO spd.tenants_providers (provider_id, tenant_id) VALUES ($PROVIDER_ID, $TENANT_ID);"
execute_sql "$SQL_ASSOCIATE_SP"
echo "Storage provider associated successfully with tenant_id: $TENANT_ID"
echo "SP setup completed successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment