#!/bin/bash

pipelines=$(aws datapipeline list-pipelines | jq -r '.pipelineIdList[].id')
count=$(echo "$pipelines" | wc -l)
resourceRoleArr=()
serviceRoleArr=()

counter=0
for i in $pipelines; do
  echo "Checking pipeline $i"
  pipeline_objects=$(aws datapipeline describe-objects --pipeline-id "$i" --object-id "Default" 2>/dev/null)
  if [ $? == "0" ]; then
    serviceRole=$(echo "$pipeline_objects" | jq -r '.pipelineObjects[0].fields[] | select(.key=="role").stringValue')
    echo "adding $serviceRole to Service Roles array"
    serviceRoleArr[$counter]=$serviceRole
    resourceRole=$(echo "$pipeline_objects" | jq -r '.pipelineObjects[0].fields[] | select(.key=="resourceRole").stringValue')
    echo "adding $resourceRole to Resource Roles array"
    resourceRoleArr[$counter]=$resourceRole
    let counter=counter+1
  else
    echo "$i did not return objects; skipping"
  fi
done

echo Finding unique roles and sorting...

uniqueServiceRoleArr=($(echo "${serviceRoleArr[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
uniqueResourceRoleArr=($(echo "${resourceRoleArr[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))

echo
echo
echo "Printing Service Roles:"
echo ${uniqueServiceRoleArr[*]}
echo
echo "Printing Resource Roles:"
echo ${uniqueResourceRoleArr[*]}
