generated from terraform-ibm-modules/terraform-ibm-module-template
-
Notifications
You must be signed in to change notification settings - Fork 15
feat: Add validation for OCP AI add-ons #884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zikra-iqbal
wants to merge
7
commits into
main
Choose a base branch
from
issue_16974
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+203
−2
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e5d1af7
feat: Add validation for OCP AI add-ons
zikra-iqbal ed04820
Merge branch 'main' into issue_16974
imprateeksh 0b910ee
chore: Addressed review comments
zikra-iqbal 2d5779d
fix: pre-commit hooks
zikra-iqbal 28a6476
fix: Updated python script
zikra-iqbal f466207
Merge branch 'main' into issue_16974
zikra-iqbal f7a24d1
Addressed review comments
zikra-iqbal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| #!/usr/bin/env python3 | ||
| import http.client | ||
| import json | ||
| import sys | ||
|
|
||
|
|
||
| def parse_input(): | ||
| """ | ||
| Reads JSON input from stdin and parses it into a dictionary. | ||
| Returns: | ||
| dict: Parsed input data. | ||
| """ | ||
| try: | ||
| data = json.loads(sys.stdin.read()) | ||
| except json.JSONDecodeError as e: | ||
| raise ValueError("Invalid JSON input") from e | ||
| return data | ||
|
|
||
|
|
||
| def validate_inputs(data): | ||
| """ | ||
| Validates required inputs 'IAM_TOKEN' and 'REGION' from the parsed input. | ||
| Args: | ||
| data (dict): Input data parsed from JSON. | ||
| Returns: | ||
| tuple: A tuple containing (IAM_TOKEN, REGION). | ||
| """ | ||
| token = data.get("IAM_TOKEN") | ||
| if not token: | ||
| raise ValueError("IAM_TOKEN is required") | ||
|
|
||
| region = data.get("REGION") | ||
| if not region: | ||
| raise ValueError("REGION is required") | ||
|
|
||
| return token, region | ||
|
|
||
|
|
||
| def fetch_addon_versions(iam_token, region): | ||
| """ | ||
| Fetches openshift add-on versions using HTTP connection. | ||
| Args: | ||
| iam_token (str): IBM Cloud IAM token for authentication. | ||
| region (str): Region to query for add-ons. | ||
| Returns: | ||
| list: Parsed JSON response containing add-on information. | ||
| """ | ||
| url = "/global/v1/addons" | ||
| host = "containers.cloud.ibm.com" | ||
| headers = { | ||
| "Authorization": f"Bearer {iam_token}", | ||
| "Accept": "application/json", | ||
| "X-Region": region, | ||
| } | ||
|
|
||
| conn = http.client.HTTPSConnection(host) | ||
| try: | ||
| conn.request("GET", url, headers=headers) | ||
| response = conn.getresponse() | ||
| data = response.read().decode() | ||
|
|
||
| if response.status != 200: | ||
| raise RuntimeError( | ||
| f"API request failed: {response.status} {response.reason} - {data}" | ||
| ) | ||
|
|
||
| return json.loads(data) | ||
| except http.client.HTTPException as e: | ||
| raise RuntimeError("HTTP request failed") from e | ||
| finally: | ||
| conn.close() | ||
|
|
||
|
|
||
| def transform_addons(addons_data): | ||
| """ | ||
| Transforms the raw add-on data into a nested dictionary structured by add-on name and version. | ||
| Args: | ||
| addons_data: Raw data returned by the add-on API. | ||
| Returns: | ||
| dict: Transformed add-on data suitable for Terraform consumption. | ||
| """ | ||
| result = {} | ||
|
|
||
| for addon in addons_data: | ||
| name = addon.get("name") | ||
| version = addon.get("version") | ||
|
|
||
| supported_ocp = addon.get("supportedOCPRange", "unsupported") | ||
| supported_kube = addon.get("supportedKubeRange", "unsupported") | ||
|
|
||
| if name not in result: | ||
| result[name] = {} | ||
|
|
||
| result[name][version] = { | ||
| "supported_openshift_range": supported_ocp, | ||
| "supported_kubernetes_range": supported_kube, | ||
| } | ||
|
|
||
| if not result: | ||
| raise RuntimeError("No add-on data found.") | ||
|
|
||
| return result | ||
|
|
||
|
|
||
| def format_for_terraform(result): | ||
| """ | ||
| Converts the transformed add-on data into JSON strings for Terraform external data source consumption. | ||
| Args: | ||
| result (dict): Transformed add-on data. | ||
| Returns: | ||
| dict: A dictionary mapping add-on names to JSON strings of their version info. | ||
| """ | ||
| return {name: json.dumps(versions) for name, versions in result.items()} | ||
|
|
||
|
|
||
| def main(): | ||
| """ | ||
| Main execution function: reads input, validates, fetches API data, transforms it, | ||
| formats it for Terraform and prints the JSON output. | ||
| """ | ||
| data = parse_input() | ||
| iam_token, region = validate_inputs(data) | ||
|
|
||
| addons_data = fetch_addon_versions(iam_token, region) | ||
| transformed = transform_addons(addons_data) | ||
| output = format_for_terraform(transformed) | ||
|
|
||
| print(json.dumps(output)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need an environment variable override here to support other environments such as test.cloud.ibm.com. So you should refactor this to allow the user to pass an enviornmeent variable named
IBMCLOUD_CS_API_ENDPOINT. If user does not specify the enviornment variable, the default value should behttps://containers.test.cloud.ibm.com/globalas per the provider docs:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.