Skip to content

Instantly share code, notes, and snippets.

@ksatirli
Created September 3, 2020 14:20
Show Gist options
  • Save ksatirli/bd6bd7a2d393bd470554ecc941d03034 to your computer and use it in GitHub Desktop.
Save ksatirli/bd6bd7a2d393bd470554ecc941d03034 to your computer and use it in GitHub Desktop.
rename Terraform Cloud Organization
#!/bin/sh
# This script sends a requests to the Terraform Cloud API and updates the
# organization name from "${OLD_TFC_ORG_NAME}" to "${NEW_TFC_ORG_NAME}".
#
# This script can be useful when you need to rename a TFC Organization without
# wanting to (or being able to) start from scratch.
#
# Use this script with care, as this request is carried out against the API, only.
# Check your (local) Terraform Resources for references to `OLD_TFC_ORG_NAME` and
# rename the values and / or use `terraform state mv` to update your Terraform state.
export OLD_TFC_ORG_NAME="startup"
export NEW_TFC_ORG_NAME="bigcorp"
curl \
--show-error \
--request "PATCH" \
--header "Authorization: Bearer ${TFC_TOKEN}" \
--header "Content-Type: application/vnd.api+json" \
--data "{ \"data\": { \"type\": \"organizations\", \"attributes\": { \"name\": \"${NEW_TFC_ORG_NAME}\" } } }" \
"https://app.terraform.io/api/v2/organizations/${OLD_TFC_ORG_NAME}"
@ksatirli
Copy link
Author

When the command completes successfully, the API returns HTTP Status 200, and a JSON response similar to this:

{
    "data": {
        "id": "bigcorp",
        "type": "organizations",
        "attributes": {
            "external-id": "org-AbCdEFgHijK1L2Mn",
            "created-at": "2020-09-01T00:00:00.000Z",
            "email": "[email protected]",
            "session-timeout": 10080,
            "session-remember": 10080,
            "collaborator-auth-policy": "two_factor_mandatory",
            "plan-expired": false,
            "plan-expires-at": null,
            "plan-is-trial": false,
            "plan-is-enterprise": false,
            "cost-estimation-enabled": true,
            "name": "bigcorp",
            "permissions": {
                "can-update": true,
                "can-destroy": true,
                "can-access-via-teams": true,
                "can-create-team": true,
                "can-create-workspace": true,
                "can-manage-users": true,
                "can-manage-subscription": true,
                "can-manage-sso": false,
                "can-update-oauth": true,
                "can-update-sentinel": true,
                "can-update-ssh-keys": true,
                "can-update-api-token": true,
                "can-traverse": true,
                "can-start-trial": false,
                "can-update-agent-pools": false
            },
            "fair-run-queuing-enabled": true,
            "saml-enabled": false,
            "owners-team-saml-role-id": null,
            "two-factor-conformant": true
        },
        "relationships": {
            "oauth-tokens": {
                "links": {
                    "related": "/api/v2/organizations/bigcorp/oauth-tokens"
                }
            },
            "authentication-token": {
                "links": {
                    "related": "/api/v2/organizations/bigcorp/authentication-token"
                }
            },
            "entitlement-set": {
                "data": {
                    "id": "org-AbCdEFgHijK1L2Mn",
                    "type": "entitlement-sets"
                },
                "links": {
                    "related": "/api/v2/organizations/bigcorp/entitlement-set"
                }
            },
            "subscription": {
                "links": {
                    "related": "/api/v2/organizations/bigcorp/subscription"
                }
            },
            "setup-status": {
                "links": {
                    "related": "/api/v2/organizations/bigcorp/setup-status"
                }
            }
        },
        "links": {
            "self": "/api/v2/organizations/bigcorp"
        }
    }
}

Take note that in the example response, the value for the bigcorp TFC Organization would reflect the value you used for NEW_TFC_ORG_NAME (line 14).

Similarly, the values for data.attributes.external-id (org-AbCdEFgHijK1L2Mn) and data.attributes.email ([email protected] in the example response) would reflect _your_ situation, with a different organization ID and an email address linked to the token you specified as part of TFC_TOKEN`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment