Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/configure/addmodel.py
1567 views
1
# Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
import json
14
import os
15
16
from botocore.model import ServiceModel
17
18
from awscli.customizations.commands import BasicCommand
19
20
21
def _get_endpoint_prefix_to_name_mappings(session):
22
# Get the mappings of endpoint prefixes to service names from the
23
# available service models.
24
prefixes_to_services = {}
25
for service_name in session.get_available_services():
26
service_model = session.get_service_model(service_name)
27
prefixes_to_services[service_model.endpoint_prefix] = service_name
28
return prefixes_to_services
29
30
31
def _get_service_name(session, endpoint_prefix):
32
if endpoint_prefix in session.get_available_services():
33
# Check if the endpoint prefix is a pre-existing service.
34
# If it is, use that endpoint prefix as the service name.
35
return endpoint_prefix
36
else:
37
# The service may have a different endpoint prefix than its name
38
# So we need to determine what the correct mapping may be.
39
40
# Figure out the mappings of endpoint prefix to service names.
41
name_mappings = _get_endpoint_prefix_to_name_mappings(session)
42
# Determine the service name from the mapping.
43
# If it does not exist in the mapping, return the original endpoint
44
# prefix.
45
return name_mappings.get(endpoint_prefix, endpoint_prefix)
46
47
48
def get_model_location(session, service_definition, service_name=None):
49
"""Gets the path of where a service-2.json file should go in ~/.aws/models
50
51
:type session: botocore.session.Session
52
:param session: A session object
53
54
:type service_definition: dict
55
:param service_definition: The json loaded service definition
56
57
:type service_name: str
58
:param service_name: The service name to use. If this not provided,
59
this will be determined from a combination of available services
60
and the service definition.
61
62
:returns: The path to where are model should be placed based on
63
the service definition and the current services in botocore.
64
"""
65
# Add the ServiceModel abstraction over the service json definition to
66
# make it easier to work with.
67
service_model = ServiceModel(service_definition)
68
69
# Determine the service_name if not provided
70
if service_name is None:
71
endpoint_prefix = service_model.endpoint_prefix
72
service_name = _get_service_name(session, endpoint_prefix)
73
api_version = service_model.api_version
74
75
# For the model location we only want the custom data path (~/.aws/models
76
# not the one set by AWS_DATA_PATH)
77
data_path = session.get_component('data_loader').CUSTOMER_DATA_PATH
78
# Use the version of the model to determine the file's naming convention.
79
service_model_name = (
80
'service-%d.json' % int(
81
float(service_definition.get('version', '2.0'))))
82
return os.path.join(data_path, service_name, api_version,
83
service_model_name)
84
85
86
class AddModelCommand(BasicCommand):
87
NAME = 'add-model'
88
DESCRIPTION = (
89
'Adds a service JSON model to the appropriate location in '
90
'~/.aws/models. Once the model gets added, CLI commands and Boto3 '
91
'clients will be immediately available for the service JSON model '
92
'provided.'
93
)
94
ARG_TABLE = [
95
{'name': 'service-model', 'required': True, 'help_text': (
96
'The contents of the service JSON model.')},
97
{'name': 'service-name', 'help_text': (
98
'Overrides the default name used by the service JSON '
99
'model to generate CLI service commands and Boto3 clients.')}
100
]
101
102
def _run_main(self, parsed_args, parsed_globals):
103
service_definition = json.loads(parsed_args.service_model)
104
105
# Get the path to where the model should be written
106
model_location = get_model_location(
107
self._session, service_definition, parsed_args.service_name
108
)
109
110
# If the service_name/api_version directories do not exist,
111
# then create them.
112
model_directory = os.path.dirname(model_location)
113
if not os.path.exists(model_directory):
114
os.makedirs(model_directory)
115
116
# Write the model to the specified location
117
with open(model_location, 'wb') as f:
118
f.write(parsed_args.service_model.encode('utf-8'))
119
120
return 0
121
122