|
| 1 | +# Copyright IBM Corp. 2025, 2026 |
| 2 | +# SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +"""IP allowlist (CIDR range list) demo for the python-tfe SDK. |
| 5 | +
|
| 6 | +HCP Terraform's IP allowlist feature is exposed as the JSON:API |
| 7 | +``cidr-range-lists`` and ``cidr-ranges`` resources, surfaced here as |
| 8 | +``client.cidr_range_lists`` and ``client.cidr_ranges``. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import argparse |
| 14 | +import os |
| 15 | + |
| 16 | +from pytfe import TFEClient, TFEConfig |
| 17 | +from pytfe.models import ( |
| 18 | + CIDRRangeCreateOptions, |
| 19 | + CIDRRangeListCreateOptions, |
| 20 | + EnforcementScope, |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +def _print_header(title: str): |
| 25 | + print("\n" + "=" * 80) |
| 26 | + print(title) |
| 27 | + print("=" * 80) |
| 28 | + |
| 29 | + |
| 30 | +def main(): |
| 31 | + parser = argparse.ArgumentParser( |
| 32 | + description="IP allowlists (CIDR range lists) 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("--organization", default=os.getenv("TFE_ORG", "")) |
| 39 | + parser.add_argument( |
| 40 | + "--create", |
| 41 | + action="store_true", |
| 42 | + help="Create an allowlist (with --name) and add --cidr to it", |
| 43 | + ) |
| 44 | + parser.add_argument("--name", help="Name for the new IP allowlist") |
| 45 | + parser.add_argument( |
| 46 | + "--cidr", help="CIDR block to add (e.g. 192.168.1.0/24), with --create" |
| 47 | + ) |
| 48 | + parser.add_argument("--id", help="IP allowlist ID for --read / --delete") |
| 49 | + parser.add_argument("--read", action="store_true", help="Read one IP allowlist") |
| 50 | + parser.add_argument( |
| 51 | + "--delete", action="store_true", help="Delete the IP allowlist (--id)" |
| 52 | + ) |
| 53 | + args = parser.parse_args() |
| 54 | + |
| 55 | + if not args.token: |
| 56 | + print("TFE_TOKEN is not set") |
| 57 | + return 2 |
| 58 | + |
| 59 | + client = TFEClient(TFEConfig(address=args.address, token=args.token)) |
| 60 | + |
| 61 | + _print_header(f"Listing IP allowlists for {args.organization}") |
| 62 | + for crl in client.cidr_range_lists.list(args.organization): |
| 63 | + ranges = list(client.cidr_range_lists.list_cidr_ranges(crl.id)) |
| 64 | + print(f"- {crl.id} {crl.name} scope={crl.enforcement_scope}") |
| 65 | + for r in ranges: |
| 66 | + print(f" {r.id} {r.cidr_block}") |
| 67 | + |
| 68 | + if args.create: |
| 69 | + if not args.name: |
| 70 | + print("--name is required for --create") |
| 71 | + return 2 |
| 72 | + _print_header(f"Creating IP allowlist: {args.name}") |
| 73 | + crl = client.cidr_range_lists.create( |
| 74 | + args.organization, |
| 75 | + CIDRRangeListCreateOptions( |
| 76 | + name=args.name, |
| 77 | + enforcement_scope=EnforcementScope.SELECTED_AGENT_POOLS, |
| 78 | + ), |
| 79 | + ) |
| 80 | + print(f"Created {crl.id}") |
| 81 | + if args.cidr: |
| 82 | + cidr = client.cidr_range_lists.add_cidr_range( |
| 83 | + crl.id, CIDRRangeCreateOptions(cidr_block=args.cidr) |
| 84 | + ) |
| 85 | + print(f"Added CIDR range {cidr.id}: {cidr.cidr_block}") |
| 86 | + |
| 87 | + if args.read: |
| 88 | + if not args.id: |
| 89 | + print("--id is required for --read") |
| 90 | + return 2 |
| 91 | + _print_header(f"Reading IP allowlist: {args.id}") |
| 92 | + crl = client.cidr_range_lists.read(args.id) |
| 93 | + print(f"Name: {crl.name}") |
| 94 | + print(f"Description: {crl.description}") |
| 95 | + print(f"Enforcement scope: {crl.enforcement_scope}") |
| 96 | + |
| 97 | + if args.delete and args.id: |
| 98 | + _print_header(f"Deleting IP allowlist: {args.id}") |
| 99 | + client.cidr_range_lists.delete(args.id) |
| 100 | + print("Deleted.") |
| 101 | + |
| 102 | + return 0 |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + raise SystemExit(main()) |
0 commit comments