All Projects → chrisanthropic → Terraform Import Github Organization

chrisanthropic / Terraform Import Github Organization

Licence: mit
Script to fully automate Terraform import of Github Org (teams, users, and repos)

Programming Languages

shell
77523 projects

Projects that are alternatives of or similar to Terraform Import Github Organization

Terraform Aws Spotgpu
Fully automated provisioning of AWS EC2 Spot Instances for Deep Learning workloads using Terraform.
Stars: ✭ 127 (+33.68%)
Mutual labels:  automation, terraform
Electriceye
Continuously monitor your AWS services for configurations that can lead to degradation of confidentiality, integrity or availability. All results will be sent to Security Hub for further aggregation and analysis.
Stars: ✭ 255 (+168.42%)
Mutual labels:  automation, terraform
Tensor
Tensor - Comprehensive web-based automation framework and Centralized infrastructure management platform
Stars: ✭ 136 (+43.16%)
Mutual labels:  automation, terraform
Terraform
Terraform automation for Cloud
Stars: ✭ 121 (+27.37%)
Mutual labels:  automation, terraform
Ebs bckup
Stars: ✭ 32 (-66.32%)
Mutual labels:  automation, terraform
Terrahub
Terraform Automation and Orchestration Tool (Open Source)
Stars: ✭ 148 (+55.79%)
Mutual labels:  automation, terraform
Terrible
An Ansible playbook that apply the principle of the Infrastructure as Code on a QEMU/KVM environment.
Stars: ✭ 161 (+69.47%)
Mutual labels:  automation, terraform
Provisioning
Kubernetes cluster provisioning using Terraform.
Stars: ✭ 277 (+191.58%)
Mutual labels:  automation, terraform
Pre Commit Terraform
pre-commit git hooks to take care of Terraform configurations
Stars: ✭ 902 (+849.47%)
Mutual labels:  automation, terraform
Guide
Kubernetes clusters for the hobbyist.
Stars: ✭ 5,150 (+5321.05%)
Mutual labels:  automation, terraform
Terraform Provider Aiven
Terraform Aiven provider
Stars: ✭ 68 (-28.42%)
Mutual labels:  automation, terraform
Infra Personal
Terraform for setting up my personal infrastructure
Stars: ✭ 45 (-52.63%)
Mutual labels:  automation, terraform
Terraform
Terraform - Beginners | Intermediate | Advanced
Stars: ✭ 77 (-18.95%)
Mutual labels:  automation, terraform
Pci Gke Blueprint
PCI on GKE Blueprint: PCI Deployable Architecture on Google Cloud and GKE
Stars: ✭ 91 (-4.21%)
Mutual labels:  terraform
Raspberry Pi Dramble
Raspberry Pi Kubernetes cluster that runs HA/HP Drupal 8
Stars: ✭ 1,317 (+1286.32%)
Mutual labels:  automation
Ck Tensorflow
Collective Knowledge components for TensorFlow (code, data sets, models, packages, workflows):
Stars: ✭ 90 (-5.26%)
Mutual labels:  automation
Patrowlhears
PatrowlHears - Vulnerability Intelligence Center / Exploits
Stars: ✭ 89 (-6.32%)
Mutual labels:  automation
Fabric Home Assistant
📜 Deploy Home-Assistant easily with Fabric
Stars: ✭ 94 (-1.05%)
Mutual labels:  automation
Coteafs Appium
📱 Wrapper Appium Framework in Java which supports Automation of Mobile and Tablet apps.
Stars: ✭ 93 (-2.11%)
Mutual labels:  automation
Pyppeteer
Headless chrome/chromium automation library (unofficial port of puppeteer)
Stars: ✭ 1,286 (+1253.68%)
Mutual labels:  automation

WHAT

It's a simple bash script to simply import a Github Organization into Terraform. It uses the Github API and Terraform CLI to import the following resources:

  • all public repos (includes pagination support for Orgs with 100+ repos)
  • all private repos (includes pagination support for Orgs with 100+ repos)
  • all team repos (includes pagination support for Orgs with 100+ repos)
  • all teams
  • all team memberships
  • all organization users

After importing the resources the script also writes a basic terraform.tf config for each resource, making this a fully automated process.

WHY

I like managing things with Terraform. Sometimes those things were created before Terraform supported them. Manually importing hundreds of repos/teams/users/etc is a tedious process.

HOW

How the script works

Public repos

Imports all public repos owned by the organization (includes full pagination support for Orgs with 100+ repos). Also writes a Terraform resource block in a single file (github-public-repos.tf), using the following template and populating it with values pulled via the Github API:

resource "github_repository" "$PUBLIC_REPO_NAME" {
  name        = "$PUBLIC_REPO_NAME"
  private     = false
  description = "$PUBLIC_REPO_DESCRIPTION"
  has_wiki    = "$PUBLIC_REPO_WIKI"
  has_downloads = "$PUBLIC_REPO_DOWNLOADS"
  has_issues  = "$PUBLIC_REPO_ISSUES"
}

Private repos

Imports all private repos owned by the organization (includes full pagination support for Orgs with 100+ repos). Also writes a Terraform resource block in a single file (github-private-repos.tf), using the following template and populating it with values pulled via the Github API:

resource "github_repository" "$PRIVATE_REPO_NAME" {
  name        = "$PRIVATE_REPO_NAME"
  private     = true
  description = "$PRIVATE_REPO_DESCRIPTION"
  has_wiki    = "$PRIVATE_REPO_WIKI"
  has_downloads = "$PRIVATE_REPO_DOWNLOADS"
  has_issues  = "$PRIVATE_REPO_ISSUES"
}

Team repos

Imports all team repos owned by the organization (includes full pagination support for Orgs with 100+ repos). Also writes a Terraform resource block in a unique file per team (github-teams-$TEAM_NAME.tf), using the following template and populating it with values pulled via the Github API:

resource "github_team_repository" "$TEAM_NAME-$TERRAFORM_TEAM_REPO_NAME" {
  team_id    = "$TEAM_ID"
  repository = "$REPO_NAME"
  permission = "admin" or "push" or "pull"
}

Teams

Imports all teams belonging to the organization. Also writes a Terraform resource block in a unique file per team (github-teams-$TEAM_NAME.tf), using the following template and populating it with values pulled via the Github API:

resource "github_team" "$TEAM_NAME" {
  name        = "$TEAM_NAME"
  description = "$TEAM_DESCRIPTION"
  privacy     = "closed" or "secret"
}

Team memberships

Imports the team membership for all teams owned by the organization (what users belong to what teams). Also writes a Terraform resource block in a unique file per team (github-team-memberships-$TEAM_NAME.tf), using the following template and populating it with values pulled via the Github API:

resource "github_team_membership" "$TEAM_NAME-$USER_NAME" {
  username    = "$USER_NAME"
  team_id     = "$TEAM_ID"
  role        = "maintainer" or "member"
}

Organization users

Imports all users belonging to the organization. Also writes a Terraform resource block in a single file (github-users.tf), using the following template and populating it with values pulled via the Github API:

resource "github_membership" "$USER_NAME" {
  username        = "$USER_NAME"
  role            = "member"
}

How to use the script

Requirements

  • An existing Github account with a user that belongs to an organization
  • A github personal access token with the following permissions:
    • repo (all)
    • admin:org (all)
  • Terraform
  • jq

Do it

  • git clone this repo

  • create a basic terraform configuration file, e.g. main.tf with something like:

    provider "github" {
      token        = "TOKENGOESHERE"
      organization = "my_org"
      # optional, if using GitHub Enterprise
      base_url     =  "https://github.mycompany.com/api/v3/"
    }
    
  • run terraform init to e.g. install the GitHub provider

  • configure the variables at the top of the script

    • GITHUB_TOKEN=...
    • ORG=...
    • if you're using GitHub Enterprise, API_URL_PREFIX=... or remember to pass them in via the environment
  • run the scriptm, perhaps passing the necessary environment variables

    GITHUB_TOKEN=12334...4555 ORG=my_org terraform-import-github-org.sh
    
  • run a terraform plan to see that everything was imported and that no changes are required.

    • some manual modifications could be required since not every field supported by Terraform has been implemented by this script.
    • HEADS UP - the script hardcodes user roles to "member".

Using with GitHub Enterprise

This should also work with GitHub Enterprise deployments if you also set (either editing the script or via an environment variable) the API_URL_PREFIX correctly, e.g. https://github.mycompany.com/api/v3.

FAQ

  • Q) Why bash?
    • A) I like bash.
  • Q) Do you plan on extending this?
    • A) Sure, see the TODO section.

TODO

Issues

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].