Skip to content
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

feat!: remove file name param #43

Merged
merged 5 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/apic-extension/azext_apic_extension/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@
from .custom import ImportSpecificationExtension
from .custom import ExportSpecificationExtension
from .custom import ExportMetadataSchemaExtension
from .custom import CreateMetadataSchemaExtension
from .custom import UpdateMetadataSchemaExtension


def load_custom_commands(self, _): # pylint: disable=unused-argument
with self.command_group('apic api definition') as g:
self.command_table['apic api definition import-specification'] = ImportSpecificationExtension(loader=self)
self.command_table['apic api definition export-specification'] = ExportSpecificationExtension(loader=self)
with self.command_group('apic metadata') as g:
self.command_table['apic metadata create'] = CreateMetadataSchemaExtension(loader=self)
self.command_table['apic metadata update'] = UpdateMetadataSchemaExtension(loader=self)
self.command_table['apic metadata export'] = ExportMetadataSchemaExtension(loader=self)
with self.command_group('apic api') as g:
g.custom_command("register", "register_apic", is_preview=True)
127 changes: 1 addition & 126 deletions src/apic-extension/azext_apic_extension/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,58 +20,15 @@
from azure.cli.core.aaz._arg import AAZStrArg
from .command_patches import ImportAPIDefinitionExtension
from .command_patches import ExportAPIDefinitionExtension
from .command_patches import CreateMetadataExtension
from .command_patches import ExportMetadataExtension
from .aaz.latest.apic.metadata import Update as UpdateMetadataSchema

logger = get_logger(__name__)


class ImportSpecificationExtension(ImportAPIDefinitionExtension):
@classmethod
def _build_arguments_schema(cls, *args, **kwargs):
args_schema = super()._build_arguments_schema(*args, **kwargs)
args_schema.source_profile = AAZStrArg(
options=["--file-name"],
help='Name of the file from where to import the spec from.',
required=False,
registered=True
)
return args_schema

def pre_operations(self):
super().pre_operations()
args = self.ctx.args
data = None
value = None

# Load the JSON file
if args.source_profile:
with open(str(args.source_profile), 'rb') as f:
data = f.read()
result = chardet.detect(data)
encoding = result['encoding']

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:
value = args.value

# Reassign the values to self.args
args.value = value

# Check the size of 'value' if format is inline and raise error if value is greater than 3 mb
if args.format == 'inline':
Expand Down Expand Up @@ -134,88 +91,6 @@ def writeResultsToFile(self, results, file_name):
f.write(results)


class CreateMetadataSchemaExtension(CreateMetadataExtension):
@classmethod
def _build_arguments_schema(cls, *args, **kwargs):
args_schema = super()._build_arguments_schema(*args, **kwargs)
args_schema.source_profile = AAZStrArg(
options=["--file-name"],
help='Name of the file from that contains the metadata schema.',
required=False,
registered=True
)
return args_schema

def pre_operations(self):
args = self.ctx.args
data = None
value = args.schema

# Load the JSON file
if args.source_profile:
with open(str(args.source_profile), 'rb') as f:
data = f.read()
result = chardet.detect(data)
encoding = result['encoding']

if os.stat(str(args.source_profile)).st_size == 0:
raise ValueError('Metadtata schema file is empty. Please provide a valid metadata schema file.')

with open(str(args.source_profile), 'r', encoding=encoding) as f:
data = json.load(f)
if data:
value = json.dumps(data)

# If any of the fields are None, get them from self.args
if value is None:
logger.error('Please provide the schema to create the metadata schema'
'through --schema option or through --file-name option via a file.')

# Reassign the values to self.args
self.ctx.args.schema = value


class UpdateMetadataSchemaExtension(UpdateMetadataSchema):
@classmethod
def _build_arguments_schema(cls, *args, **kwargs):
args_schema = super()._build_arguments_schema(*args, **kwargs)
args_schema.source_profile = AAZStrArg(
options=["--file-name"],
help='Name of the file from that contains the metadata schema.',
required=False,
registered=True
)
return args_schema

def pre_operations(self):
args = self.ctx.args
data = None
value = args.schema

# Load the JSON file
if args.source_profile:
with open(str(args.source_profile), 'rb') as f:
rawdata = f.read()
result = chardet.detect(rawdata)
encoding = result['encoding']

if os.stat(str(args.source_profile)).st_size == 0:
raise ValueError('Metadtata schema file is empty. Please provide a valid metadata schema file.')

with open(str(args.source_profile), 'r', encoding=encoding) as f:
data = json.load(f)
if data:
value = json.dumps(data)

# If any of the fields are None, get them from self.args
if value is None:
logger.error('Please provide the schema to update the metadata schema '
'through --schema option or through --file-name option via a file.')

# Reassign the values to self.args
self.ctx.args.schema = value


class ExportMetadataSchemaExtension(ExportMetadataExtension):

@classmethod
Expand Down Expand Up @@ -435,7 +310,7 @@ def register_apic(cmd, api_location, resource_group, service_name, environment_n
'definition_id': extracted_definition_name,
'format': 'inline',
'specification': specification_details, # TODO write the correct spec object
'source_profile': api_location
'value': value
}

importAPISpecificationResults = ImportSpecificationExtension(cli_ctx=cmd.cli_ctx)(command_args=api_specification_args)
Expand Down
Loading