Skip to content

Instantly share code, notes, and snippets.

@guyk-bp
Last active June 26, 2024 13:36
Show Gist options
  • Save guyk-bp/7b1e2c0af00c946d6be4a58bc2e0d48e to your computer and use it in GitHub Desktop.
Save guyk-bp/7b1e2c0af00c946d6be4a58bc2e0d48e to your computer and use it in GitHub Desktop.
Update Postman Env by BigPanda

Update Postman Environment based on BigPanda printout (like from dev)

Instructions:

  1. Create an environment in Postman (you need to login to an account) (e.g. guyk-dev)
  2. Follow this guide to create an API key
  3. Download the script
  4. Edit it, and update the Postman API key
  5. chmod +x updatePostmanEnv.sh
  6. Now you can use it: e.g. kubectl logs $(kubectl get pods | grep init | awk '{print $1}') -n guyk | ~/updatePostman.sh guyk-dev

Important notice

With this script only the Initial Values of the variables would be updated, not the Current Values. To achieve this - add to your collection this script in the pre-req script:

pm.environment.variables.forEach(variable => {
  pm.environment.set(variable.key, variable.value);
});
#!/bin/bash
#
# Usage:
# kubectl logs guyk-initializer-workflow-template-24893853 -n guyk | ~/updatePostman.sh guyk-dev
#
# Hardcoded values
POSTMAN_API_KEY="FILL_HERE"
POSTMAN_API_URL="https://api.getpostman.com/environments/$ENV_ID"
# Variables to hold extracted values
org_name=""
org_id=""
username=""
password=""
api_token=""
rest_api_app_key=""
user_api_key=""
user_api_key_id=""
login_url=""
base_uri=""
# Function to get environment ID from name
get_env_id_from_name() {
local env_name="$1"
local env_id=$(curl --silent --location --request GET "$POSTMAN_API_URL" \
--header "X-Api-Key: $POSTMAN_API_KEY" | jq -r --arg ENV_NAME "$env_name" '.environments[] | select(.name == $ENV_NAME) | .id')
echo "$env_id"
}
# Function to update Postman environment
update_postman_env() {
local env_id="$1"
local env_url="https://api.getpostman.com/environments/$env_id"
# Fetch the current environment
current_env=$(curl --silent --location --request GET "$env_url" \
--header "X-Api-Key: $POSTMAN_API_KEY")
# Extract the current values array
current_values=$(echo "$current_env" | jq '.environment.values')
# Create new values for the fields to be updated
new_values=$(jq -n \
--arg org_name "$org_name" \
--arg org_id "$org_id" \
--arg username "$username" \
--arg password "$password" \
--arg api_token "$api_token" \
--arg rest_api_app_key "$rest_api_app_key" \
--arg user_api_key "$user_api_key" \
--arg user_api_key_id "$user_api_key_id" \
--arg base_uri "$base_uri" \
'[
{ "key": "org_name", "value": $org_name, "enabled": true },
{ "key": "org_id", "value": $org_id, "enabled": true },
{ "key": "username", "value": $username, "enabled": true },
{ "key": "password", "value": $password, "enabled": true },
{ "key": "api_token", "value": $api_token, "enabled": true },
{ "key": "rest_api_app_key", "value": $rest_api_app_key, "enabled": true },
{ "key": "user_api_key", "value": $user_api_key, "enabled": true },
{ "key": "user_api_key_id", "value": $user_api_key_id, "enabled": true },
{ "key": "base_uri", "value": $base_uri, "enabled": true}
]')
# Merge the new values with the current values
updated_values=$(echo "$new_values" "$current_values" | jq -s 'add | unique_by(.key)')
# Prepare the JSON data
json_data=$(jq -n \
--argjson values "$updated_values" \
'{
environment: {
values: $values
}
}')
# Print the JSON data
echo "JSON data to be sent:"
echo "$json_data" | jq -r '.environment.values[] | "\(.key): \(.value)"'
# Run the curl command and pretty print the response
response=$(curl --silent --location --request PUT "$env_url" \
--header "X-Api-Key: $POSTMAN_API_KEY" \
--header "Content-Type: application/json" \
--data-raw "$json_data")
echo "$response" | jq '.'
}
# Flag to start processing after seeing the line "Info: Primary Organization Details"
start_processing=false
# Read input line by line
while IFS= read -r line; do
if [ "$line" == "Info: Primary Organization Details" ]; then
start_processing=true
continue
fi
if [ "$start_processing" = true ]; then
case "$line" in
"Org Name:"*) org_name="${line#Org Name: }" ;;
"Org ID:"*) org_id="${line#Org ID: }" ;;
"Username:"*) username="${line#Username: }" ;;
"Password:"*) password="${line#Password: }" ;;
"API Token:"*) api_token="${line#API Token: }" ;;
"Rest API App Key:"*) rest_api_app_key="${line#Rest API App Key: }" ;;
"User API Key:"*) user_api_key="${line#User API Key: }" ;;
"User API Key ID:"*) user_api_key_id="${line#User API Key ID: }" ;;
"Login URL:"*)
base_uri=$(echo "${line#Login URL: }" | sed -E 's|https?://([^/]+).*|\1|')
base_uri=$(echo "$base_uri" | sed 's/^\([^.]*\)\./\1-api./')
;;
esac
fi
done
# Check if environment name is provided as an argument
if [ -z "$1" ]; then
echo "Environment name is required as an argument."
exit 1
fi
# Get the environment ID from the name
ENV_NAME="$1"
ENV_ID=$(get_env_id_from_name "$ENV_NAME")
if [ -z "$ENV_ID" ]; then
echo "Environment with name '$ENV_NAME' not found."
exit 1
fi
# Update the Postman environment with the extracted values
update_postman_env "$ENV_ID"
echo "Don't forget to open Postman, go to your environment and select 'Reset All' to override your current values with the initialized ones that changed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment