Skip to content

Instantly share code, notes, and snippets.

@aerostitch
Last active May 17, 2018 06:13
Show Gist options
  • Save aerostitch/40828c5c2a4bd8430fefb4795a9059fe to your computer and use it in GitHub Desktop.
Save aerostitch/40828c5c2a4bd8430fefb4795a9059fe to your computer and use it in GitHub Desktop.
Import github resources in terraform
export ORG=VEVO
# generate import commands for users in the org
# /!\ Note the parge_arg, this one looks for the page number 2 of the list
export page_arg="page=2"
curl -s -H "Authorization: token ${GITHUB_AUTH_TOKEN}" https://api.github.com/orgs/VEVO/members?${page_arg} | jq -r '.[]| "terraform import github_membership.\(.login) VEVO:\(.login)"'
# generate import commands for teams in the org
# /!\ Note the parge_arg, this one looks for the page number 2 of the list
export page_arg="page=2"
curl -s -H "Authorization: token ${GITHUB_AUTH_TOKEN}" https://api.github.com/orgs/${ORG}/teams?${page_arg} | jq -r '.[]| "terraform import github_team.\(.slug) \(.id)"'
# generate import commands for team memberships
curl -s -H "Authorization: token ${GITHUB_AUTH_TOKEN}" https://api.github.com/orgs/${ORG}/teams | jq -r '.[] | "\(.id) \(.slug)"' | while read -r teamId teamSlug ; do
# Be careful, you only get the 1st page here!
curl -s -H "Authorization: token ${GITHUB_AUTH_TOKEN}" https://api.github.com/teams/${teamId}/members | jq -r '.[] | "terraform import github_team_membership.\(.login)-'${teamSlug}' '${teamId}':\(.login)"';
done
curl -s -H "Authorization: token ${GITHUB_AUTH_TOKEN}" https://api.github.com/orgs/${ORG}/teams | jq -r '.[] | "\(.id) \(.slug)"' | while read -r teamId teamSlug ; do
# get 3 pages all the time
for i in $(seq 1 3); do
page_arg="page=${i}"
curl -s -H "Authorization: token ${GITHUB_AUTH_TOKEN}" https://api.github.com/teams/${teamId}/members?${page_arg} | jq -r '.[] | "terraform import github_team_membership.\(.login)-'${teamSlug}' '${teamId}':\(.login)"';
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment