forked from hashicorp/python-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolicy_set_parameter.py
More file actions
216 lines (180 loc) · 7.68 KB
/
policy_set_parameter.py
File metadata and controls
216 lines (180 loc) · 7.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# Copyright IBM Corp. 2025, 2026
# SPDX-License-Identifier: MPL-2.0
from __future__ import annotations
import argparse
import os
from pytfe import TFEClient, TFEConfig
from pytfe.models import (
PolicySetParameterCreateOptions,
PolicySetParameterListOptions,
PolicySetParameterUpdateOptions,
)
def _print_header(title: str):
print("\n" + "=" * 80)
print(title)
print("=" * 80)
def main():
parser = argparse.ArgumentParser(
description="Policy Set Parameters demo for python-tfe SDK"
)
parser.add_argument(
"--address", default=os.getenv("TFE_ADDRESS", "https://app.terraform.io")
)
parser.add_argument("--token", default=os.getenv("TFE_TOKEN", ""))
parser.add_argument("--policy-set-id", required=True, help="Policy Set ID")
parser.add_argument(
"--page-size",
type=int,
default=100,
help="Page size for fetching parameters",
)
parser.add_argument("--create", action="store_true", help="Create a test parameter")
parser.add_argument("--read", action="store_true", help="Read a specific parameter")
parser.add_argument("--update", action="store_true", help="Update a parameter")
parser.add_argument("--delete", action="store_true", help="Delete a parameter")
parser.add_argument(
"--parameter-id", help="Parameter ID for read/update/delete operation"
)
parser.add_argument("--key", help="Parameter key for creation/update")
parser.add_argument("--value", help="Parameter value for creation/update")
parser.add_argument(
"--sensitive", action="store_true", help="Mark parameter as sensitive"
)
args = parser.parse_args()
cfg = TFEConfig(address=args.address, token=args.token)
client = TFEClient(cfg)
# 1) List all parameters for the policy set
_print_header(f"Listing parameters for policy set: {args.policy_set_id}")
options = PolicySetParameterListOptions(
page_size=args.page_size,
)
param_count = 0
for param in client.policy_set_parameters.list(args.policy_set_id, options):
param_count += 1
# Sensitive parameters will have masked values
value_display = "***SENSITIVE***" if param.sensitive else param.value
print(f"- {param.id}")
print(f"Key: {param.key}")
print(f"Value: {value_display}")
print(f"Category: {param.category.value}")
print(f"Sensitive: {param.sensitive}")
print()
if param_count == 0:
print("No parameters found.")
else:
print(f"Total: {param_count} parameters")
# 2) Read a specific parameter (if --read flag is provided)
if args.read:
if not args.parameter_id:
print("Error: --parameter-id is required for read operation")
return
_print_header(f"Reading parameter: {args.parameter_id}")
param = client.policy_set_parameters.read(args.policy_set_id, args.parameter_id)
print(f"Parameter ID: {param.id}")
print(f"Key: {param.key}")
value_display = "***SENSITIVE***" if param.sensitive else param.value
print(f"Value: {value_display}")
print(f"Category: {param.category.value}")
print(f"Sensitive: {param.sensitive}")
# 3) Update a parameter (if --update flag is provided)
if args.update:
if not args.parameter_id:
print("Error: --parameter-id is required for update operation")
return
_print_header(f"Updating parameter: {args.parameter_id}")
# First read the current parameter to show before state
current_param = client.policy_set_parameters.read(
args.policy_set_id, args.parameter_id
)
print("Before update:")
print(f"Key: {current_param.key}")
value_display = (
"***SENSITIVE***" if current_param.sensitive else current_param.value
)
print(f"Value: {value_display}")
print(f"Sensitive: {current_param.sensitive}")
# Update the parameter
update_options = PolicySetParameterUpdateOptions(
key=args.key if args.key else None,
value=args.value if args.value else None,
sensitive=args.sensitive if args.sensitive else None,
)
updated_param = client.policy_set_parameters.update(
args.policy_set_id, args.parameter_id, update_options
)
print("\nAfter update:")
print(f"Key: {updated_param.key}")
value_display = (
"***SENSITIVE***" if updated_param.sensitive else updated_param.value
)
print(f"Value: {value_display}")
print(f"Sensitive: {updated_param.sensitive}")
# 4) Delete a parameter (if --delete flag is provided)
if args.delete:
if not args.parameter_id:
print("Error: --parameter-id is required for delete operation")
return
_print_header(f"Deleting parameter: {args.parameter_id}")
# First read the parameter to show what's being deleted
try:
param_to_delete = client.policy_set_parameters.read(
args.policy_set_id, args.parameter_id
)
print("Parameter to delete:")
print(f"ID: {param_to_delete.id}")
print(f"Key: {param_to_delete.key}")
value_display = (
"***SENSITIVE***"
if param_to_delete.sensitive
else param_to_delete.value
)
print(f"Value: {value_display}")
print(f"Sensitive: {param_to_delete.sensitive}")
except Exception as e:
print(f"Error reading parameter: {e}")
return
# Delete the parameter
client.policy_set_parameters.delete(args.policy_set_id, args.parameter_id)
print(f"\n Successfully deleted parameter: {args.parameter_id}")
# List remaining parameters
_print_header("Listing parameters after deletion")
print("Remaining parameters:")
remaining_count = 0
for param in client.policy_set_parameters.list(args.policy_set_id):
remaining_count += 1
value_display = "***SENSITIVE***" if param.sensitive else param.value
print(f"- {param.key}: {value_display} (sensitive={param.sensitive})")
if remaining_count == 0:
print("No parameters remaining.")
else:
print(f"\nTotal: {remaining_count} parameters")
# 5) Create a new parameter (if --create flag is provided)
if args.create:
if not args.key:
print("Error: --key is required for create operation")
return
_print_header(f"Creating new parameter with key: {args.key}")
create_options = PolicySetParameterCreateOptions(
key=args.key,
value=args.value if args.value else "",
sensitive=args.sensitive,
)
new_param = client.policy_set_parameters.create(
args.policy_set_id, create_options
)
print(f"Created parameter: {new_param.id}")
print(f"Key: {new_param.key}")
value_display = "***SENSITIVE***" if new_param.sensitive else new_param.value
print(f"Value: {value_display}")
print(f"Category: {new_param.category.value}")
print(f"Sensitive: {new_param.sensitive}")
# List again to show the new parameter
_print_header("Listing parameters after creation")
param_count = 0
for param in client.policy_set_parameters.list(args.policy_set_id):
param_count += 1
value_display = "***SENSITIVE***" if param.sensitive else param.value
print(f"- {param.key}: {value_display} (sensitive={param.sensitive})")
print(f"\nTotal: {param_count} parameters")
if __name__ == "__main__":
main()