Skip to content

Instantly share code, notes, and snippets.

@alexbasista
Last active September 7, 2021 13:03
Show Gist options
  • Select an option

  • Save alexbasista/1c76865d4db2fad1c740eaaa0bb06808 to your computer and use it in GitHub Desktop.

Select an option

Save alexbasista/1c76865d4db2fad1c740eaaa0bb06808 to your computer and use it in GitHub Desktop.

Revisions

  1. alexbasista revised this gist Sep 16, 2020. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions tfe_policy_set_parameters.py
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ def _get_ps_id(hostname, header, org, name):
    """
    GET /organizations/:organization_name/policy-sets
    """
    ps_endpoint = '/'.join(['https:/', hostname, 'organizations', org, 'policy-sets'])
    ps_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'organizations', org, 'policy-sets'])
    r = requests.get(url=ps_endpoint, headers=header)
    ps_id = [ i['id'] for i in r.json()['data'] if i['attributes']['name'] == name ]

    @@ -18,7 +18,7 @@ def _get_psp_id(hostname, header, org, policy_set, parameter):
    GET /policy-sets/:policy_set_id/parameters
    """
    ps_id = _get_ps_id(hostname=hostname, header=header, org=org, name=policy_set)
    psp_endpoint = '/'.join(['https:/', hostname, 'policy-sets', ps_id, 'parameters'])
    psp_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'policy-sets', ps_id, 'parameters'])
    r = requests.get(url=psp_endpoint, headers=header)
    psp_id = [ i['id'] for i in r.json()['data'] if i['attributes']['key'] == parameter ]

    @@ -43,7 +43,7 @@ def create(hostname, org, policy_set, key, value, sensitive=False, **kwargs):
    }

    ps_id = _get_ps_id(hostname=hostname, header=header, org=org, name=policy_set)
    psp_endpoint = '/'.join(['https:/', hostname, 'policy-sets', ps_id, 'parameters'])
    psp_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'policy-sets', ps_id, 'parameters'])

    payload = {}
    data = {}
    @@ -79,7 +79,7 @@ def list(hostname, org, policy_set, **kwargs):
    }

    ps_id = _get_ps_id(hostname=hostname, header=header, org=org, name=policy_set)
    psp_endpoint = '/'.join(['https:/', hostname, 'policy-sets', ps_id, 'parameters'])
    psp_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'policy-sets', ps_id, 'parameters'])
    r = requests.get(url=psp_endpoint, headers=header)
    r.raise_for_status()
    return r
    @@ -110,7 +110,7 @@ def update(hostname, org, policy_set, parameter, **kwargs):

    ps_id = _get_ps_id(hostname=hostname, header=header, org=org, name=policy_set)
    psp_id = _get_psp_id(hostname=hostname, header=header, org=org, policy_set=policy_set, parameter=parameter)
    psp_endpoint = '/'.join(['https:/', hostname, 'policy-sets', ps_id, 'parameters', psp_id])
    psp_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'policy-sets', ps_id, 'parameters', psp_id])

    payload = {}
    data = {}
    @@ -152,7 +152,7 @@ def delete(hostname, org, policy_set, parameter, **kwargs):

    ps_id = _get_ps_id(hostname=hostname, header=header, org=org, name=policy_set)
    psp_id = _get_psp_id(hostname=hostname, header=header, org=org, policy_set=policy_set, parameter=parameter)
    psp_endpoint = '/'.join(['https:/', hostname, 'policy-sets', ps_id, 'parameters', psp_id])
    psp_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'policy-sets', ps_id, 'parameters', psp_id])

    r = requests.delete(url=psp_endpoint, headers=header)
    r.raise_for_status()
  2. alexbasista revised this gist Sep 16, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions tfe_policy_set_parameters.py
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ def _get_ps_id(hostname, header, org, name):
    """
    ps_endpoint = '/'.join(['https:/', hostname, 'organizations', org, 'policy-sets'])
    r = requests.get(url=ps_endpoint, headers=header)
    ps_id = [ i['id'] for i in r['data'] if i['attributes']['name'] == name ]
    ps_id = [ i['id'] for i in r.json()['data'] if i['attributes']['name'] == name ]

    return ps_id[0]

    @@ -20,7 +20,7 @@ def _get_psp_id(hostname, header, org, policy_set, parameter):
    ps_id = _get_ps_id(hostname=hostname, header=header, org=org, name=policy_set)
    psp_endpoint = '/'.join(['https:/', hostname, 'policy-sets', ps_id, 'parameters'])
    r = requests.get(url=psp_endpoint, headers=header)
    psp_id = [ i['id'] for i in r['data'] if i['attributes']['key'] == parameter ]
    psp_id = [ i['id'] for i in r.json()['data'] if i['attributes']['key'] == parameter ]

    return psp_id[0]

  3. alexbasista revised this gist Sep 16, 2020. 1 changed file with 159 additions and 0 deletions.
    159 changes: 159 additions & 0 deletions tfe_policy_set_parameters.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,159 @@
    import os
    import sys
    import json
    import requests

    def _get_ps_id(hostname, header, org, name):
    """
    GET /organizations/:organization_name/policy-sets
    """
    ps_endpoint = '/'.join(['https:/', hostname, 'organizations', org, 'policy-sets'])
    r = requests.get(url=ps_endpoint, headers=header)
    ps_id = [ i['id'] for i in r['data'] if i['attributes']['name'] == name ]

    return ps_id[0]

    def _get_psp_id(hostname, header, org, policy_set, parameter):
    """
    GET /policy-sets/:policy_set_id/parameters
    """
    ps_id = _get_ps_id(hostname=hostname, header=header, org=org, name=policy_set)
    psp_endpoint = '/'.join(['https:/', hostname, 'policy-sets', ps_id, 'parameters'])
    r = requests.get(url=psp_endpoint, headers=header)
    psp_id = [ i['id'] for i in r['data'] if i['attributes']['key'] == parameter ]

    return psp_id[0]

    def create(hostname, org, policy_set, key, value, sensitive=False, **kwargs):
    """
    POST /policy-sets/:policy_set_id/parameters
    """
    if kwargs.get('tfe_token'):
    token = kwargs.get('tfe_token')
    kwargs.pop('tfe_token')
    elif os.getenv('TFE_TOKEN'):
    token = os.getenv('TFE_TOKEN')
    else:
    print('ERROR: Missing API token.')
    sys.exit()

    header = {
    'Authorization': 'Bearer ' + token,
    'Content-Type': 'application/vnd.api+json'
    }

    ps_id = _get_ps_id(hostname=hostname, header=header, org=org, name=policy_set)
    psp_endpoint = '/'.join(['https:/', hostname, 'policy-sets', ps_id, 'parameters'])

    payload = {}
    data = {}
    data['type'] = 'vars'
    attributes = {}
    attributes['key'] = key
    attributes['value'] = value
    attributes['category'] = 'policy-set'
    attributes['sensitive'] = sensitive
    data['attributes'] = attributes
    payload['data'] = data

    r = requests.post(url=psp_endpoint, headers=header, data=json.dumps(payload))
    r.raise_for_status()
    return r

    def list(hostname, org, policy_set, **kwargs):
    """
    GET /policy-sets/:policy_set_id/parameters
    """
    if kwargs.get('tfe_token'):
    token = kwargs.get('tfe_token')
    kwargs.pop('tfe_token')
    elif os.getenv('TFE_TOKEN'):
    token = os.getenv('TFE_TOKEN')
    else:
    print('ERROR: Missing API token.')
    sys.exit()

    header = {
    'Authorization': 'Bearer ' + token,
    'Content-Type': 'application/vnd.api+json'
    }

    ps_id = _get_ps_id(hostname=hostname, header=header, org=org, name=policy_set)
    psp_endpoint = '/'.join(['https:/', hostname, 'policy-sets', ps_id, 'parameters'])
    r = requests.get(url=psp_endpoint, headers=header)
    r.raise_for_status()
    return r

    def update(hostname, org, policy_set, parameter, **kwargs):
    """
    PATCH /policy-sets/:policy_set_id/parameters/:parameter_id
    """
    psp_attributes_list = [
    'key',
    'value',
    'sensitive'
    ]

    if kwargs.get('tfe_token'):
    token = kwargs.get('tfe_token')
    kwargs.pop('tfe_token')
    elif os.getenv('TFE_TOKEN'):
    token = os.getenv('TFE_TOKEN')
    else:
    print('ERROR: Missing API token.')
    sys.exit()

    header = {
    'Authorization': 'Bearer ' + token,
    'Content-Type': 'application/vnd.api+json'
    }

    ps_id = _get_ps_id(hostname=hostname, header=header, org=org, name=policy_set)
    psp_id = _get_psp_id(hostname=hostname, header=header, org=org, policy_set=policy_set, parameter=parameter)
    psp_endpoint = '/'.join(['https:/', hostname, 'policy-sets', ps_id, 'parameters', psp_id])

    payload = {}
    data = {}
    data['type'] = 'vars'
    data['id'] = psp_id
    attributes = {}

    for key, value in kwargs.items():
    if key in psp_attributes_list:
    attributes[key] = value
    else:
    print("ERROR: '{}' is an invalid key for Policy Set Parameters API".format(key))

    attributes['category'] = 'policy-set'
    data['attributes'] = attributes
    payload['data'] = data

    r = requests.patch(url=psp_endpoint, headers=header, data=json.dumps(payload))
    r.raise_for_status()
    return r

    def delete(hostname, org, policy_set, parameter, **kwargs):
    """
    DELETE /policy-sets/:policy_set_id/parameters/:parameter_id
    """
    if kwargs.get('tfe_token'):
    token = kwargs.get('tfe_token')
    kwargs.pop('tfe_token')
    elif os.getenv('TFE_TOKEN'):
    token = os.getenv('TFE_TOKEN')
    else:
    print('ERROR: Missing API token.')
    sys.exit()

    header = {
    'Authorization': 'Bearer ' + token,
    'Content-Type': 'application/vnd.api+json'
    }

    ps_id = _get_ps_id(hostname=hostname, header=header, org=org, name=policy_set)
    psp_id = _get_psp_id(hostname=hostname, header=header, org=org, policy_set=policy_set, parameter=parameter)
    psp_endpoint = '/'.join(['https:/', hostname, 'policy-sets', ps_id, 'parameters', psp_id])

    r = requests.delete(url=psp_endpoint, headers=header)
    r.raise_for_status()
    return r
  4. alexbasista revised this gist Sep 10, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion create_tfe_state_version.py
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@ def _unlock_workspace(hostname, header, ws_id):
    POST /workspaces/:workspace_id/actions/unlock
    """
    ws_unlock_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'workspaces', ws_id, 'actions', 'unlock'])
    r = requests.post(url=ws_unlock_endpoint, headers=header, data=json.dumps(payload))
    r = requests.post(url=ws_unlock_endpoint, headers=header)
    return r

    def create_state_version(hostname, org, ws, serial, md5, state, **kwargs):
  5. alexbasista revised this gist Sep 10, 2020. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion create_tfe_state_version.py
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ def _get_workspace_id(hostname, header, org, name):
    r = requests.get(url=ws_endpoint, headers=header)
    return r.json()['data']['id']

    def _lock_workspace(hostname, header, org, ws_id, reason='Locked by Python script.'):
    def _lock_workspace(hostname, header, ws_id, reason='Locked by Python script.'):
    """
    POST /workspaces/:workspace_id/actions/lock
    """
    @@ -21,6 +21,14 @@ def _lock_workspace(hostname, header, org, ws_id, reason='Locked by Python scrip
    r = requests.post(url=ws_lock_endpoint, headers=header, data=json.dumps(payload))
    return r

    def _unlock_workspace(hostname, header, ws_id):
    """
    POST /workspaces/:workspace_id/actions/unlock
    """
    ws_unlock_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'workspaces', ws_id, 'actions', 'unlock'])
    r = requests.post(url=ws_unlock_endpoint, headers=header, data=json.dumps(payload))
    return r

    def create_state_version(hostname, org, ws, serial, md5, state, **kwargs):
    """
    POST /workspaces/:workspace_id/state-versions
  6. alexbasista revised this gist Sep 10, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion create_tfe_state_version.py
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ def _lock_workspace(hostname, header, org, ws_id, reason='Locked by Python scrip
    ws_lock_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'workspaces', ws_id, 'actions', 'lock'])
    payload = { "reason": reason }
    r = requests.post(url=ws_lock_endpoint, headers=header, data=json.dumps(payload))

    return r

    def create_state_version(hostname, org, ws, serial, md5, state, **kwargs):
    """
  7. alexbasista revised this gist Sep 10, 2020. 1 changed file with 14 additions and 2 deletions.
    16 changes: 14 additions & 2 deletions create_tfe_state_version.py
    Original file line number Diff line number Diff line change
    @@ -3,12 +3,24 @@
    import json
    import requests

    def _get_workspace_id(hostname, header, org, name):

    def _get_workspace_id(hostname, header, org, name):
    """
    GET /workspaces/:workspace_id
    """
    ws_endpoint = '/'.join(['https:/', hostname, 'organizations', org, 'workspaces', name])
    r = requests.get(url=ws_endpoint, headers=header)
    return r.json()['data']['id']

    def _lock_workspace(hostname, header, org, ws_id, reason='Locked by Python script.'):
    """
    POST /workspaces/:workspace_id/actions/lock
    """
    ws_lock_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'workspaces', ws_id, 'actions', 'lock'])
    payload = { "reason": reason }
    r = requests.post(url=ws_lock_endpoint, headers=header, data=json.dumps(payload))


    def create_state_version(hostname, org, ws, serial, md5, state, **kwargs):
    """
    POST /workspaces/:workspace_id/state-versions
    @@ -38,7 +50,7 @@ def create_state_version(hostname, org, ws, serial, md5, state, **kwargs):
    attributes['state'] = state

    if kwargs.get('lineage'):
    attributes['lineage'] = kwargs.get('lilneage')
    attributes['lineage'] = kwargs.get('lineage')

    data['attributes'] = attributes
    payload['data'] = data
  8. alexbasista revised this gist Sep 10, 2020. 1 changed file with 48 additions and 0 deletions.
    48 changes: 48 additions & 0 deletions create_tfe_state_version.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    import os
    import sys
    import json
    import requests

    def _get_workspace_id(hostname, header, org, name):

    ws_endpoint = '/'.join(['https:/', hostname, 'organizations', org, 'workspaces', name])
    r = requests.get(url=ws_endpoint, headers=header)
    return r.json()['data']['id']

    def create_state_version(hostname, org, ws, serial, md5, state, **kwargs):
    """
    POST /workspaces/:workspace_id/state-versions
    """
    if kwargs.get('tfe_token'):
    token = kwargs.get('tfe_token')
    kwargs.pop('tfe_token')
    elif os.getenv('TFE_TOKEN'):
    token = os.getenv('TFE_TOKEN')
    else:
    print('ERROR: Missing API token.')
    sys.exit()

    header = {
    'Authorization': 'Bearer ' + token,
    'Content-Type': 'application/vnd.api+json'
    }
    ws_id = _get_workspace_id(hostname=hostname, header=header, org=org, name=ws)
    sv_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'workspaces', ws_id, 'state-versions'])

    payload = {}
    data = {}
    data['type'] = 'state-versions'
    attributes = {}
    attributes['serial'] = serial
    attributes['md5'] = md5
    attributes['state'] = state

    if kwargs.get('lineage'):
    attributes['lineage'] = kwargs.get('lilneage')

    data['attributes'] = attributes
    payload['data'] = data

    r = requests.post(url=sv_endpoint, headers=header, data=json.dumps(payload))
    r.raise_for_status()
    return r
  9. alexbasista revised this gist Sep 9, 2020. 1 changed file with 134 additions and 0 deletions.
    134 changes: 134 additions & 0 deletions create_tfe_org.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,134 @@
    import os
    import sys
    import json
    import requests

    def create_team(hostname, org, name, **kwargs):
    """
    POST /organizations/:organization_name/teams
    """
    if kwargs.get('tfe_token'):
    token = kwargs.get('tfe_token')
    kwargs.pop('tfe_token')
    elif os.getenv('TFE_TOKEN'):
    token = os.getenv('TFE_TOKEN')
    else:
    print('ERROR: Missing API token.')
    sys.exit()

    header = {
    'Authorization': 'Bearer ' + token,
    'Content-Type': 'application/vnd.api+json'
    }
    teams_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'organizations', org, 'teams'])

    payload = {}
    data = {}
    data['type'] = 'teams'
    attributes = {}
    attributes['name'] = name

    if kwargs.get('organization_access'):
    if not isinstance(kwargs.get('organization_access'), dict):
    print("ERROR: 'organization_access' argument must be of type dict.")
    sys.exit()
    else:
    attributes['organization-access'] = kwargs.get('organization_access')

    if kwargs.get('visibility'):
    attributes['visibility'] = kwargs.get('visibility')

    data['attributes'] = attributes
    payload['data'] = data

    r = requests.post(url=teams_endpoint, headers=header, data=json.dumps(payload))
    r.raise_for_status()
    return r

    def create_oauth_client(hostname, org, name, service_provider, http_url, api_url, oauth_token_string, **kwargs):
    """
    POST /organizations/:organization_name/oauth-clients
    """
    if kwargs.get('tfe_token'):
    token = kwargs.get('tfe_token')
    kwargs.pop('tfe_token')
    elif os.getenv('TFE_TOKEN'):
    token = os.getenv('TFE_TOKEN')
    else:
    print('ERROR: Missing API token.')
    sys.exit()

    header = {
    'Authorization': 'Bearer ' + token,
    'Content-Type': 'application/vnd.api+json'
    }
    oc_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'organizations', org, 'oauth-clients'])

    payload = {}
    data = {}
    data['type'] = 'oauth-clients'
    attributes = {}
    attributes['service-provider'] = service_provider
    attributes['name'] = name
    attributes['http-url'] = http_url
    attributes['api-url'] = api_url
    attributes['oauth-token-string'] = oauth_token_string

    if kwargs.get('private_key'):
    attributes['private-key'] = kwargs.get('private_key')

    data['attributes'] = attributes
    payload['data'] = data

    r = requests.post(url=oc_endpoint, headers=header, data=json.dumps(payload))
    r.raise_for_status()
    return r


    def create_org(hostname, name, email, **kwargs):
    """
    POST /organizations
    """
    org_attributes_list = [
    'name',
    'email',
    'session_timeout',
    'session_remember',
    'collaborator_auth_policy',
    'cost_estimation_enabled',
    'owners_team_saml_role_id'
    ]

    if kwargs.get('tfe_token'):
    token = kwargs.get('tfe_token')
    kwargs.pop('tfe_token')
    elif os.getenv('TFE_TOKEN'):
    token = os.getenv('TFE_TOKEN')
    else:
    print('ERROR: Missing API token.')
    sys.exit()

    header = {
    'Authorization': 'Bearer ' + token,
    'Content-Type': 'application/vnd.api+json'
    }
    org_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'organizations'])

    payload = {}
    data = {}
    data['type'] = 'organizations'
    attributes = {}
    attributes['name'] = name
    attributes['email'] = email

    for key, value in kwargs.items():
    if key in org_attributes_list:
    attributes[key] = value
    else:
    print("WARNING: '{}' is an invalid key for Organizations API.".format(key))

    data['attributes'] = attributes
    payload['data'] = data

    r = requests.post(url=org_endpoint, headers=header, data=json.dumps(payload))
    return r
  10. alexbasista revised this gist Sep 9, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion create_tfe_policy_set.py
    Original file line number Diff line number Diff line change
    @@ -39,6 +39,7 @@ def create_policy_set(hostname, org, name, **kwargs):

    if kwargs.get('tfe_token'):
    token = kwargs.get('tfe_token')
    kwargs.pop('tfe_token')
    elif os.getenv('TFE_TOKEN'):
    token = os.getenv('TFE_TOKEN')
    else:
    @@ -49,7 +50,6 @@ def create_policy_set(hostname, org, name, **kwargs):
    'Authorization': 'Bearer ' + token,
    'Content-Type': 'application/vnd.api+json'
    }
    kwargs.pop('tfe_token')
    ps_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'organizations', org, 'policy-sets'])

    payload = {}
  11. alexbasista revised this gist Sep 9, 2020. 1 changed file with 22 additions and 25 deletions.
    47 changes: 22 additions & 25 deletions create_tfe_workspace.py
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,22 @@
    import json
    import requests

    def _does_workspace_exist(hostname, header, org, name):
    """
    GET /organizations/:organization_name/workspaces/:name
    """
    ws_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'organizations', org, 'workspaces', name])

    r = requests.get(url=ws_endpoint, headers=header)

    if r.status_code == 404:
    return False
    elif r.status_code == 200:
    return True
    else:
    print('ERROR: Received unexpected reponse code: {}'.format(r.status_code))
    return True

    def create_workspace(hostname, org, name, **kwargs):
    """
    POST /organizations/:organization_name/workspaces
    @@ -24,15 +40,16 @@ def create_workspace(hostname, org, name, **kwargs):
    'working_directory',
    ]
    ws_vcs_repo_attributes_list = [
    'oauth-token-id',
    'oauth_token_id',
    'branch',
    'default-branch',
    'ingress-submodules',
    'default_branch',
    'ingress_submodules',
    'identifier'
    ]

    if kwargs.get('tfe_token'):
    token = kwargs.get('tfe_token')
    kwargs.pop('tfe_token')
    elif os.getenv('TFE_TOKEN'):
    token = os.getenv('TFE_TOKEN')
    else:
    @@ -66,30 +83,10 @@ def create_workspace(hostname, org, name, **kwargs):
    data['attributes'] = attributes
    payload['data'] = data

    if does_workspace_exist(hostname=hostname, org=org, name=name, token=token):
    if _does_workspace_exist(hostname=hostname, header=header, org=org, name=name):
    print("ERROR: Workspace '{}' already exists".format(name))
    sys.exit()

    r = requests.post(url=ws_endpoint, headers=header, data=json.dumps(payload))
    r.raise_for_status()
    return r

    def does_workspace_exist(hostname, org, name, token):
    """
    GET /organizations/:organization_name/workspaces/:name
    """
    header = {
    'Authorization': 'Bearer ' + token,
    'Content-Type': 'application/vnd.api+json'
    }
    ws_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'organizations', org, 'workspaces', name])

    r = requests.get(url=ws_endpoint, headers=header)

    if r.status_code == 404:
    return False
    elif r.status_code == 200:
    return True
    else:
    print('ERROR: Received unexpected reponse code: {}'.format(r.status_code))
    return True
    return r
  12. alexbasista revised this gist Sep 8, 2020. 1 changed file with 26 additions and 0 deletions.
    26 changes: 26 additions & 0 deletions list_tfe_orgs.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    import os
    import sys
    import json
    import requests

    def list_orgs(hostname, **kwargs):
    """
    GET /organizations
    """
    if kwargs.get('tfe_token'):
    token = kwargs.get('tfe_token')
    elif os.getenv('TFE_TOKEN'):
    token = os.getenv('TFE_TOKEN')
    else:
    print("ERROR: Missing API token.")
    sys.exit()

    header = {
    'Authorization': 'Bearer ' + token,
    'Content-Type': 'application/vnd.api+json'
    }
    org_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'organizations'])

    r = requests.get(url=org_endpoint, headers=header)
    r.raise_for_status()
    return [ i['id'] for i in r.json()['data'] ]
  13. alexbasista renamed this gist Sep 8, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  14. alexbasista revised this gist Sep 8, 2020. 1 changed file with 0 additions and 10 deletions.
    10 changes: 0 additions & 10 deletions create_policy_set.py
    Original file line number Diff line number Diff line change
    @@ -3,16 +3,6 @@
    import json
    import requests

    def _get_orgs(hostname, header):
    """
    GET /organizations
    """
    org_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'organizations'])

    r = requests.get(url=org_endpoint, headers=header)
    r.raise_for_status()
    return [ i['id'] for i in r.json()['data'] ]

    def _get_oauth_token_id(hostname, header, org, oauth_client_name):
    """
    GET /organizations/:organization_name/oauth-clients
  15. alexbasista revised this gist Sep 8, 2020. 1 changed file with 10 additions and 21 deletions.
    31 changes: 10 additions & 21 deletions create_tfe_workspace.py
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,6 @@ def create_workspace(hostname, org, name, **kwargs):
    """
    POST /organizations/:organization_name/workspaces
    """

    ws_attributes_list = [
    'name',
    'agent_pool_id',
    @@ -24,29 +23,26 @@ def create_workspace(hostname, org, name, **kwargs):
    'trigger_prefixes'
    'working_directory',
    ]

    ws_vcs_repo_attributes_list = [
    'oauth_token_id',
    'oauth-token-id',
    'branch',
    'default_branch',
    'ingress_submodules',
    'default-branch',
    'ingress-submodules',
    'identifier'
    ]

    if os.getenv('TFE_TOKEN'):
    if kwargs.get('tfe_token'):
    token = kwargs.get('tfe_token')
    elif os.getenv('TFE_TOKEN'):
    token = os.getenv('TFE_TOKEN')
    else:
    if kwargs.get('tfe_token'):
    token = kwargs.get('tfe_token')
    else:
    print('ERROR: Missing API token')
    sys.exit()
    print("ERROR: Missing API token.")
    sys.exit()

    header = {
    'Authorization': 'Bearer ' + token,
    'Content-Type': 'application/vnd.api+json'
    }

    ws_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'organizations', org, 'workspaces'])

    payload = {}
    @@ -62,37 +58,30 @@ def create_workspace(hostname, org, name, **kwargs):
    elif key in ws_vcs_repo_attributes_list:
    vcs_repo[key] = value
    else:
    print('WARNING: "{}" is an invalid key for Workspaces API'.format(key))
    print("WARNING: '{}' is an invalid key for Workspaces API".format(key))

    if len(vcs_repo) > 0:
    attributes['vcs-repo'] = vcs_repo
    else:
    pass

    data['attributes'] = attributes
    payload['data'] = data

    if does_workspace_exist(hostname=hostname, org=org, name=name, token=token):
    print('ERROR: Workspace "{}" already exists'.format(name))
    print("ERROR: Workspace '{}' already exists".format(name))
    sys.exit()
    else:
    pass

    r = requests.post(url=ws_endpoint, headers=header, data=json.dumps(payload))
    r.raise_for_status()
    return r


    def does_workspace_exist(hostname, org, name, token):
    """
    GET /organizations/:organization_name/workspaces/:name
    """

    header = {
    'Authorization': 'Bearer ' + token,
    'Content-Type': 'application/vnd.api+json'
    }

    ws_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'organizations', org, 'workspaces', name])

    r = requests.get(url=ws_endpoint, headers=header)
  16. alexbasista revised this gist Sep 8, 2020. No changes.
  17. alexbasista revised this gist Sep 8, 2020. 1 changed file with 113 additions and 0 deletions.
    113 changes: 113 additions & 0 deletions create_policy_set.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,113 @@
    import os
    import sys
    import json
    import requests

    def _get_orgs(hostname, header):
    """
    GET /organizations
    """
    org_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'organizations'])

    r = requests.get(url=org_endpoint, headers=header)
    r.raise_for_status()
    return [ i['id'] for i in r.json()['data'] ]

    def _get_oauth_token_id(hostname, header, org, oauth_client_name):
    """
    GET /organizations/:organization_name/oauth-clients
    """
    oc_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'organizations', org, 'oauth-clients'])

    r = requests.get(url=oc_endpoint, headers=header)
    r.raise_for_status()
    oc = [ i for i in r.json()['data'] if i['attributes']['name'] == oauth_client_name ]
    return oc[0]['relationships']['oauth-tokens']['data'][0]['id']

    def create_policy_set(hostname, org, name, **kwargs):
    """
    POST /organizations/:organization_name/policy-sets
    """
    ps_attributes_list = [
    'name',
    'description',
    'global',
    'policies_path'
    ]
    ps_vcs_repo_attributes_list = [
    'branch',
    'identifier',
    'ingress_submodules',
    'oauth_token_id'
    ]
    ps_relationships_list = [
    'workspace_ids'
    ]
    ps_kwargs_non_params = [
    'oauth_client_name'
    ]

    if kwargs.get('tfe_token'):
    token = kwargs.get('tfe_token')
    elif os.getenv('TFE_TOKEN'):
    token = os.getenv('TFE_TOKEN')
    else:
    print('ERROR: Missing API token.')
    sys.exit()

    header = {
    'Authorization': 'Bearer ' + token,
    'Content-Type': 'application/vnd.api+json'
    }
    kwargs.pop('tfe_token')
    ps_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'organizations', org, 'policy-sets'])

    payload = {}
    data = {}
    data['type'] = 'policy-sets'
    attributes = {}
    vcs_repo = {}
    attributes['name'] = name

    for key, value in kwargs.items():
    if key in ps_attributes_list:
    attributes[key] = value
    elif key in ps_vcs_repo_attributes_list:
    vcs_repo[key] = value
    elif key in ps_relationships_list:
    pass
    elif key in ps_kwargs_non_params:
    pass
    else:
    print("WARNING: '{}' is an invalid key for Policy Sets API".format(key))

    if len(vcs_repo) > 0:
    if not kwargs.get('oauth_token_id'):
    if not kwargs.get('oauth_client_name'):
    print("ERROR: A value is required for either 'oauth_token_id' or 'oauth_client_name' if a VCS repo identifier is specified.")
    sys.exit()
    else:
    ot_id = _get_oauth_token_id(hostname=hostname, header=header, org=org, oauth_client_name=kwargs.get('oauth_client_name'))
    vcs_repo['oauth-token-id'] = ot_id
    else:
    ot_id = kwargs.get('oauth_token_id')
    vcs_repo['oauth-token-id'] = ot_id
    attributes['vcs-repo'] = vcs_repo

    if kwargs.get('workspace_ids'):
    relationships = {}
    workspaces = {}
    workspaces_data = []
    for ws in kwargs.get('workspace_ids'):
    obj = { 'id': ws, 'type': 'workspaces' }
    workspaces_data.append(obj)
    workspaces['data'] = workspaces_data
    relationships['workspaces'] = workspaces
    data['relationships'] = relationships

    data['attributes'] = attributes
    payload['data'] = data

    r = requests.post(url=ps_endpoint, headers=header, data=json.dumps(payload))
    r.raise_for_status()
    return r
  18. alexbasista revised this gist Aug 28, 2020. 1 changed file with 15 additions and 15 deletions.
    30 changes: 15 additions & 15 deletions create_tfe_workspace.py
    Original file line number Diff line number Diff line change
    @@ -10,26 +10,26 @@ def create_workspace(hostname, org, name, **kwargs):

    ws_attributes_list = [
    'name',
    'agent-pool-id',
    'allow-destroy-plan',
    'auto-apply',
    'agent_pool_id',
    'allow_destroy_plan',
    'auto_apply',
    'description',
    'execution-mode',
    'file-triggers-enabled',
    'source-name',
    'source-url',
    'queue-all-runs',
    'speculative-enabled',
    'terraform-version',
    'trigger-prefixes'
    'working-directory',
    'execution_mode',
    'file_triggers_enabled',
    'source_name',
    'source_url',
    'queue_all_runs',
    'speculative_enabled',
    'terraform_version',
    'trigger_prefixes'
    'working_directory',
    ]

    ws_vcs_repo_attributes_list = [
    'oauth-token-id',
    'oauth_token_id',
    'branch',
    'default-branch',
    'ingress-submodules',
    'default_branch',
    'ingress_submodules',
    'identifier'
    ]

  19. alexbasista revised this gist Aug 28, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions create_tfe_workspace.py
    Original file line number Diff line number Diff line change
    @@ -39,7 +39,7 @@ def create_workspace(hostname, org, name, **kwargs):
    if kwargs.get('tfe_token'):
    token = kwargs.get('tfe_token')
    else:
    print('ERROR: Missing API token.')
    print('ERROR: Missing API token')
    sys.exit()

    header = {
    @@ -62,7 +62,7 @@ def create_workspace(hostname, org, name, **kwargs):
    elif key in ws_vcs_repo_attributes_list:
    vcs_repo[key] = value
    else:
    print('WARNING: "{}" is an invalid key for Workspaces API.'.format(key))
    print('WARNING: "{}" is an invalid key for Workspaces API'.format(key))

    if len(vcs_repo) > 0:
    attributes['vcs-repo'] = vcs_repo
  20. alexbasista revised this gist Aug 28, 2020. 1 changed file with 34 additions and 5 deletions.
    39 changes: 34 additions & 5 deletions create_tfe_workspace.py
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    import json
    import requests

    def create_tfe_workspace(hostname, org, name, **kwargs):
    def create_workspace(hostname, org, name, **kwargs):
    """
    POST /organizations/:organization_name/workspaces
    """
    @@ -43,8 +43,8 @@ def create_tfe_workspace(hostname, org, name, **kwargs):
    sys.exit()

    header = {
    "Authorization": "Bearer " + token,
    "Content-Type": "application/vnd.api+json"
    'Authorization': 'Bearer ' + token,
    'Content-Type': 'application/vnd.api+json'
    }

    ws_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'organizations', org, 'workspaces'])
    @@ -62,7 +62,7 @@ def create_tfe_workspace(hostname, org, name, **kwargs):
    elif key in ws_vcs_repo_attributes_list:
    vcs_repo[key] = value
    else:
    print('WARNING: {} is an invalid key for Workspaces API.'.format(key))
    print('WARNING: "{}" is an invalid key for Workspaces API.'.format(key))

    if len(vcs_repo) > 0:
    attributes['vcs-repo'] = vcs_repo
    @@ -72,6 +72,35 @@ def create_tfe_workspace(hostname, org, name, **kwargs):
    data['attributes'] = attributes
    payload['data'] = data

    if does_workspace_exist(hostname=hostname, org=org, name=name, token=token):
    print('ERROR: Workspace "{}" already exists'.format(name))
    sys.exit()
    else:
    pass

    r = requests.post(url=ws_endpoint, headers=header, data=json.dumps(payload))
    r.raise_for_status()
    return r
    return r


    def does_workspace_exist(hostname, org, name, token):
    """
    GET /organizations/:organization_name/workspaces/:name
    """

    header = {
    'Authorization': 'Bearer ' + token,
    'Content-Type': 'application/vnd.api+json'
    }

    ws_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'organizations', org, 'workspaces', name])

    r = requests.get(url=ws_endpoint, headers=header)

    if r.status_code == 404:
    return False
    elif r.status_code == 200:
    return True
    else:
    print('ERROR: Received unexpected reponse code: {}'.format(r.status_code))
    return True
  21. alexbasista created this gist Aug 28, 2020.
    77 changes: 77 additions & 0 deletions create_tfe_workspace.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    import os
    import sys
    import json
    import requests

    def create_tfe_workspace(hostname, org, name, **kwargs):
    """
    POST /organizations/:organization_name/workspaces
    """

    ws_attributes_list = [
    'name',
    'agent-pool-id',
    'allow-destroy-plan',
    'auto-apply',
    'description',
    'execution-mode',
    'file-triggers-enabled',
    'source-name',
    'source-url',
    'queue-all-runs',
    'speculative-enabled',
    'terraform-version',
    'trigger-prefixes'
    'working-directory',
    ]

    ws_vcs_repo_attributes_list = [
    'oauth-token-id',
    'branch',
    'default-branch',
    'ingress-submodules',
    'identifier'
    ]

    if os.getenv('TFE_TOKEN'):
    token = os.getenv('TFE_TOKEN')
    else:
    if kwargs.get('tfe_token'):
    token = kwargs.get('tfe_token')
    else:
    print('ERROR: Missing API token.')
    sys.exit()

    header = {
    "Authorization": "Bearer " + token,
    "Content-Type": "application/vnd.api+json"
    }

    ws_endpoint = '/'.join(['https:/', hostname, 'api', 'v2', 'organizations', org, 'workspaces'])

    payload = {}
    data = {}
    data['type'] = 'workspaces'
    attributes = {}
    vcs_repo = {}
    attributes['name'] = name

    for key, value in kwargs.items():
    if key in ws_attributes_list:
    attributes[key] = value
    elif key in ws_vcs_repo_attributes_list:
    vcs_repo[key] = value
    else:
    print('WARNING: {} is an invalid key for Workspaces API.'.format(key))

    if len(vcs_repo) > 0:
    attributes['vcs-repo'] = vcs_repo
    else:
    pass

    data['attributes'] = attributes
    payload['data'] = data

    r = requests.post(url=ws_endpoint, headers=header, data=json.dumps(payload))
    r.raise_for_status()
    return r