Path: blob/develop/awscli/customizations/configure/addmodel.py
1567 views
# Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.12import json13import os1415from botocore.model import ServiceModel1617from awscli.customizations.commands import BasicCommand181920def _get_endpoint_prefix_to_name_mappings(session):21# Get the mappings of endpoint prefixes to service names from the22# available service models.23prefixes_to_services = {}24for service_name in session.get_available_services():25service_model = session.get_service_model(service_name)26prefixes_to_services[service_model.endpoint_prefix] = service_name27return prefixes_to_services282930def _get_service_name(session, endpoint_prefix):31if endpoint_prefix in session.get_available_services():32# Check if the endpoint prefix is a pre-existing service.33# If it is, use that endpoint prefix as the service name.34return endpoint_prefix35else:36# The service may have a different endpoint prefix than its name37# So we need to determine what the correct mapping may be.3839# Figure out the mappings of endpoint prefix to service names.40name_mappings = _get_endpoint_prefix_to_name_mappings(session)41# Determine the service name from the mapping.42# If it does not exist in the mapping, return the original endpoint43# prefix.44return name_mappings.get(endpoint_prefix, endpoint_prefix)454647def get_model_location(session, service_definition, service_name=None):48"""Gets the path of where a service-2.json file should go in ~/.aws/models4950:type session: botocore.session.Session51:param session: A session object5253:type service_definition: dict54:param service_definition: The json loaded service definition5556:type service_name: str57:param service_name: The service name to use. If this not provided,58this will be determined from a combination of available services59and the service definition.6061:returns: The path to where are model should be placed based on62the service definition and the current services in botocore.63"""64# Add the ServiceModel abstraction over the service json definition to65# make it easier to work with.66service_model = ServiceModel(service_definition)6768# Determine the service_name if not provided69if service_name is None:70endpoint_prefix = service_model.endpoint_prefix71service_name = _get_service_name(session, endpoint_prefix)72api_version = service_model.api_version7374# For the model location we only want the custom data path (~/.aws/models75# not the one set by AWS_DATA_PATH)76data_path = session.get_component('data_loader').CUSTOMER_DATA_PATH77# Use the version of the model to determine the file's naming convention.78service_model_name = (79'service-%d.json' % int(80float(service_definition.get('version', '2.0'))))81return os.path.join(data_path, service_name, api_version,82service_model_name)838485class AddModelCommand(BasicCommand):86NAME = 'add-model'87DESCRIPTION = (88'Adds a service JSON model to the appropriate location in '89'~/.aws/models. Once the model gets added, CLI commands and Boto3 '90'clients will be immediately available for the service JSON model '91'provided.'92)93ARG_TABLE = [94{'name': 'service-model', 'required': True, 'help_text': (95'The contents of the service JSON model.')},96{'name': 'service-name', 'help_text': (97'Overrides the default name used by the service JSON '98'model to generate CLI service commands and Boto3 clients.')}99]100101def _run_main(self, parsed_args, parsed_globals):102service_definition = json.loads(parsed_args.service_model)103104# Get the path to where the model should be written105model_location = get_model_location(106self._session, service_definition, parsed_args.service_name107)108109# If the service_name/api_version directories do not exist,110# then create them.111model_directory = os.path.dirname(model_location)112if not os.path.exists(model_directory):113os.makedirs(model_directory)114115# Write the model to the specified location116with open(model_location, 'wb') as f:117f.write(parsed_args.service_model.encode('utf-8'))118119return 0120121122