Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/preview.py
1566 views
1
# Copyright 2013 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
"""This module enables the preview-mode customization.
14
15
If a service is marked as being in preview mode, then any attempts
16
to call operations on that service will print a message pointing
17
the user to alternate solutions. A user can still access this
18
service by enabling the service in their config file via:
19
20
[preview]
21
servicename=true
22
23
or by running:
24
25
aws configure set preview.servicename true
26
27
Also any service that is marked as being in preview will *not*
28
be listed in the help docs, unless the service has been enabled
29
in the config file as shown above.
30
31
"""
32
import logging
33
import sys
34
import textwrap
35
36
37
logger = logging.getLogger(__name__)
38
39
40
PREVIEW_SERVICES = [
41
'sdb',
42
]
43
44
45
def register_preview_commands(events):
46
events.register('building-command-table.main', mark_as_preview)
47
48
49
def mark_as_preview(command_table, session, **kwargs):
50
# These are services that are marked as preview but are
51
# explicitly enabled in the config file.
52
allowed_services = _get_allowed_services(session)
53
for preview_service in PREVIEW_SERVICES:
54
is_enabled = False
55
if preview_service in allowed_services:
56
# Then we don't need to swap it as a preview
57
# service, the user has specifically asked to
58
# enable this service.
59
logger.debug("Preview service enabled through config file: %s",
60
preview_service)
61
is_enabled = True
62
original_command = command_table[preview_service]
63
preview_cls = type(
64
'PreviewCommand',
65
(PreviewModeCommandMixin, original_command.__class__), {})
66
command_table[preview_service] = preview_cls(
67
cli_name=original_command.name,
68
session=session,
69
service_name=original_command.service_model.service_name,
70
is_enabled=is_enabled)
71
# We also want to register a handler that will update the
72
# description in the docs to say that this is a preview service.
73
session.get_component('event_emitter').register_last(
74
'doc-description.%s' % preview_service,
75
update_description_with_preview)
76
77
78
def update_description_with_preview(help_command, **kwargs):
79
style = help_command.doc.style
80
style.start_note()
81
style.bold(PreviewModeCommandMixin.HELP_SNIPPET.strip())
82
# bcdoc does not currently allow for what I'd like to do
83
# which is have a code block like:
84
#
85
# ::
86
# [preview]
87
# service=true
88
#
89
# aws configure set preview.service true
90
#
91
# So for now we're just going to add the configure command
92
# to enable this.
93
style.doc.write("You can enable this service by running: ")
94
# The service name will always be the first element in the
95
# event class for the help object
96
service_name = help_command.event_class.split('.')[0]
97
style.code("aws configure set preview.%s true" % service_name)
98
style.end_note()
99
100
101
def _get_allowed_services(session):
102
# For a service to be marked as preview, it must be in the
103
# [preview] section and it must have a value of 'true'
104
# (case insensitive).
105
allowed = []
106
preview_services = session.full_config.get('preview', {})
107
for preview, value in preview_services.items():
108
if value == 'true':
109
allowed.append(preview)
110
return allowed
111
112
113
class PreviewModeCommandMixin(object):
114
ENABLE_DOCS = textwrap.dedent("""\
115
However, if you'd like to use the "aws {service}" commands with the
116
AWS CLI, you can enable this service by adding the following to your CLI
117
config file:
118
119
[preview]
120
{service}=true
121
122
or by running:
123
124
aws configure set preview.{service} true
125
126
""")
127
HELP_SNIPPET = ("AWS CLI support for this service is only "
128
"available in a preview stage.\n")
129
130
def __init__(self, *args, **kwargs):
131
self._is_enabled = kwargs.pop('is_enabled')
132
super(PreviewModeCommandMixin, self).__init__(*args, **kwargs)
133
134
def __call__(self, args, parsed_globals):
135
if self._is_enabled or self._is_help_command(args):
136
return super(PreviewModeCommandMixin, self).__call__(
137
args, parsed_globals)
138
else:
139
return self._display_opt_in_message()
140
141
def _is_help_command(self, args):
142
return args and args[-1] == 'help'
143
144
def _display_opt_in_message(self):
145
sys.stderr.write(self.HELP_SNIPPET)
146
sys.stderr.write("\n")
147
# Then let them know how to enable this service.
148
sys.stderr.write(self.ENABLE_DOCS.format(service=self._service_name))
149
return 1
150
151