From ea5651f53213d0f4931867572fdc2cee36dad019 Mon Sep 17 00:00:00 2001 From: Chaoyi Yuan Date: Fri, 29 Mar 2024 17:50:41 +0800 Subject: [PATCH 1/3] feat: support yaml file in register command --- .../azext_apic_extension/custom.py | 48 ++++++++++++++----- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/src/apic-extension/azext_apic_extension/custom.py b/src/apic-extension/azext_apic_extension/custom.py index b9d015ded75..a5959e53898 100644 --- a/src/apic-extension/azext_apic_extension/custom.py +++ b/src/apic-extension/azext_apic_extension/custom.py @@ -13,6 +13,7 @@ import os import sys import json +import yaml import requests from knack.log import get_logger import chardet @@ -50,10 +51,19 @@ def pre_operations(self): result = chardet.detect(data) encoding = result['encoding'] - with open(str(args.source_profile), 'r', encoding=encoding) as f: - data = json.load(f) - if data: - value = json.dumps(data) + if str(args.source_profile).endswith('.yaml') or str(args.source_profile).endswith('.yml'): + with open(str(args.source_profile), 'r', encoding=encoding) as f: + content = f.read() + data = yaml.safe_load(content) + if data: + value = content + + if (str(args.source_profile).endswith('.json')): + with open(str(args.source_profile), 'r', encoding=encoding) as f: + content = f.read() + data = json.loads(content) + if data: + value = content # If any of the fields are None, get them from self.args if value is None: @@ -269,14 +279,22 @@ def register_apic(cmd, api_location, resource_group, service_name, environment_n encoding = result['encoding'] # TODO - read other file types later - with open(str(api_location), 'r', encoding=encoding) as f: - data = json.load(f) - if data: - value = json.dumps(data) + if str(api_location).endswith('.yaml') or str(api_location).endswith('.yml'): + with open(str(api_location), 'r', encoding=encoding) as f: + content = f.read() + data = yaml.safe_load(content) + if data: + value = content + if (str(api_location).endswith('.json')): + with open(str(api_location), 'r', encoding=encoding) as f: + content = f.read() + data = json.loads(content) + if data: + value = content # If we could not read the file, return error if value is None: - logger.error('Could not load json file') + logger.error('Could not load spec file') return # Check if the first field is 'swagger', 'openapi', or something else and get the definition name and version @@ -295,9 +313,9 @@ def register_apic(cmd, api_location, resource_group, service_name, environment_n info = data['info'] if info: # Create API and Create API Version - extracted_api_name = info.get('title', 'Default API').replace(" ", "-").lower() + extracted_api_name = _generate_api_id(info.get('title', 'Default-API')).lower() extracted_api_description = info.get('description', 'API Description') - extracted_api_summary = info.get('summary', extracted_api_description) + extracted_api_summary = info.get('summary', str(extracted_api_description)[:200]) extracted_api_title = info.get('title', 'API Title').replace(" ", "-").lower() extracted_api_version = info.get('version', 'v1').replace(".", "-").lower() extracted_api_version_title = info.get('version', 'v1').replace(".", "-").lower() @@ -474,3 +492,11 @@ def register_apic(cmd, api_location, resource_group, service_name, environment_n CreateAPIDeployment(cli_ctx=cmd.cli_ctx)(command_args=api_deployment_args) logger.warning('API deployment was created successfully') + +def _generate_api_id(input: str) -> str: + import re + # Remove invalid characters + id = re.sub('[^a-zA-Z0-9-]', '', input) + # Remove leading and trailing hyphens + id = id.strip('-') + return id \ No newline at end of file From 23c681326a0938aca42fb15b4e04a5fd4df9c650 Mon Sep 17 00:00:00 2001 From: Chaoyi Yuan Date: Wed, 3 Apr 2024 09:54:57 +0800 Subject: [PATCH 2/3] feat: add example for yml support --- src/apic-extension/azext_apic_extension/_help.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/apic-extension/azext_apic_extension/_help.py b/src/apic-extension/azext_apic_extension/_help.py index b6bd068b769..1b151648a94 100644 --- a/src/apic-extension/azext_apic_extension/_help.py +++ b/src/apic-extension/azext_apic_extension/_help.py @@ -30,4 +30,5 @@ - name: Register api by providing spec file. text: | az apic api register -g api-center-test -s contosoeuap --api-location "examples/cli-examples/spec-examples/openai.json" --environment-name public + az apic api register -g api-center-test -s contosoeuap --api-location "examples/cli-examples/spec-examples/openai.yml" --environment-name public """ From 1753c00898edf8ac744ba3f788aafa461ab1a8e2 Mon Sep 17 00:00:00 2001 From: Chaoyi Yuan Date: Wed, 3 Apr 2024 09:56:07 +0800 Subject: [PATCH 3/3] doc: update readme with api register yml sample --- src/apic-extension/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/apic-extension/README.md b/src/apic-extension/README.md index a3c17e2279e..93d4b9f3b52 100644 --- a/src/apic-extension/README.md +++ b/src/apic-extension/README.md @@ -283,4 +283,5 @@ az apic metadata-schema export-metadata-schema -g api-center-test -s contosoeuap Register API or Quick Add ``` az apic api register -g api-center-test -s contosoeuap --api-location "C:/Users/arpishah/examples/cli-examples/spec-examples/openai.json" --environment-name public +az apic api register -g api-center-test -s contosoeuap --api-location "C:/Users/arpishah/examples/cli-examples/spec-examples/openai.yml" --environment-name public ``` \ No newline at end of file