|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright IBM Corp. 2025, 2026 |
| 3 | +# SPDX-License-Identifier: MPL-2.0 |
| 4 | + |
| 5 | +"""Organization tags operations example. |
| 6 | +
|
| 7 | +Demonstrates: |
| 8 | +1. list() - list tags in an organization |
| 9 | +2. add_workspaces() - associate a workspace with a tag |
| 10 | +3. delete() - delete a tag from an organization |
| 11 | +
|
| 12 | +Usage: |
| 13 | + python examples/organization_tags.py --org my-org |
| 14 | + python examples/organization_tags.py --org my-org --tag-id tag-abc123 --workspace-id ws-xyz |
| 15 | +""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import argparse |
| 20 | +import os |
| 21 | + |
| 22 | +from pytfe import TFEClient, TFEConfig |
| 23 | +from pytfe.errors import TFEError |
| 24 | +from pytfe.models.organization_tags import ( |
| 25 | + AddWorkspacesToTagOptions, |
| 26 | + OrganizationTagsDeleteOptions, |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +def main() -> None: |
| 31 | + parser = argparse.ArgumentParser( |
| 32 | + description="Organization Tags demo for python-tfe SDK" |
| 33 | + ) |
| 34 | + parser.add_argument( |
| 35 | + "--address", default=os.getenv("TFE_ADDRESS", "https://app.terraform.io") |
| 36 | + ) |
| 37 | + parser.add_argument("--token", default=os.getenv("TFE_TOKEN", "")) |
| 38 | + parser.add_argument( |
| 39 | + "--org", |
| 40 | + default=os.getenv("TFE_ORG", ""), |
| 41 | + help="Organization name", |
| 42 | + ) |
| 43 | + parser.add_argument( |
| 44 | + "--tag-id", |
| 45 | + default=os.getenv("TFE_TAG_ID", ""), |
| 46 | + help="Tag ID for add/delete operations", |
| 47 | + ) |
| 48 | + parser.add_argument( |
| 49 | + "--workspace-id", |
| 50 | + default=os.getenv("TFE_WORKSPACE_ID", ""), |
| 51 | + help="Workspace ID to associate with tag", |
| 52 | + ) |
| 53 | + args = parser.parse_args() |
| 54 | + |
| 55 | + if not args.token: |
| 56 | + print("Error: TFE_TOKEN environment variable or --token required") |
| 57 | + return |
| 58 | + |
| 59 | + if not args.org: |
| 60 | + print("Error: TFE_ORG environment variable or --org required") |
| 61 | + return |
| 62 | + |
| 63 | + cfg = TFEConfig(address=args.address, token=args.token) |
| 64 | + client = TFEClient(cfg) |
| 65 | + |
| 66 | + # 1) List tags |
| 67 | + try: |
| 68 | + print("[LIST] Listing organization tags") |
| 69 | + print(f"[LIST] organization={args.org}") |
| 70 | + tags = list(client.organization_tags.list(args.org)) |
| 71 | + print(f"[LIST] total_tags={len(tags)}") |
| 72 | + for tag in tags: |
| 73 | + print( |
| 74 | + f"[LIST] id={tag.id}, name={tag.name}, instance_count={tag.instance_count}" |
| 75 | + ) |
| 76 | + if not tags: |
| 77 | + print("[LIST] no tags found") |
| 78 | + except TFEError as exc: |
| 79 | + print(f"[LIST] API error: {exc}") |
| 80 | + return |
| 81 | + |
| 82 | + if not args.tag_id: |
| 83 | + print("[ADD_WORKSPACES] skipped: set --tag-id or TFE_TAG_ID") |
| 84 | + print("[DELETE] skipped: set --tag-id or TFE_TAG_ID") |
| 85 | + return |
| 86 | + |
| 87 | + # 2) Add workspace to tag |
| 88 | + if args.workspace_id: |
| 89 | + print("[ADD_WORKSPACES] Associating a workspace to a tag") |
| 90 | + print( |
| 91 | + f"[ADD_WORKSPACES] organization={args.org}, tag_id={args.tag_id}, workspace_id={args.workspace_id}" |
| 92 | + ) |
| 93 | + try: |
| 94 | + client.organization_tags.add_workspaces( |
| 95 | + args.org, |
| 96 | + args.tag_id, |
| 97 | + AddWorkspacesToTagOptions(workspace_ids=[args.workspace_id]), |
| 98 | + ) |
| 99 | + print("[ADD_WORKSPACES] workspace associated") |
| 100 | + except TFEError as exc: |
| 101 | + print(f"[ADD_WORKSPACES] API error: {exc}") |
| 102 | + else: |
| 103 | + print("[ADD_WORKSPACES] skipped: set --workspace-id or TFE_WORKSPACE_ID") |
| 104 | + |
| 105 | + # 3) Delete tag |
| 106 | + print("[DELETE] Deleting a tag from the organization") |
| 107 | + print(f"[DELETE] organization={args.org}, tag_id={args.tag_id}") |
| 108 | + try: |
| 109 | + client.organization_tags.delete( |
| 110 | + args.org, |
| 111 | + OrganizationTagsDeleteOptions(ids=[args.tag_id]), |
| 112 | + ) |
| 113 | + print("[DELETE] tag deleted") |
| 114 | + except TFEError as exc: |
| 115 | + print(f"[DELETE] API error: {exc}") |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + main() |
0 commit comments