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