blob: 339ac2e6af81d9dab45be93984dc81f3cbecc675 [file] [log] [blame]
Alex Kleinf4dc4f52018-12-05 13:55:12 -07001# -*- coding: utf-8 -*-
2# Copyright 2018 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""The build API entry point."""
7
8from __future__ import print_function
9
Alex Klein5bcb4d22019-03-21 13:51:54 -060010import os
George Engelbrechtd3de8df2019-09-04 18:15:05 -060011import sys
Alex Kleinf4dc4f52018-12-05 13:55:12 -070012
Alex Klein69339cc2019-07-22 14:08:35 -060013from chromite.api import api_config as api_config_lib
Alex Klein2008aee2019-08-20 16:25:27 -060014from chromite.api import controller
Alex Klein146d4772019-06-20 13:48:25 -060015from chromite.api import router as router_lib
Alex Kleinf4dc4f52018-12-05 13:55:12 -070016from chromite.lib import commandline
Alex Klein2bfacb22019-02-04 11:42:17 -070017from chromite.lib import cros_build_lib
Michael Mortensen3e86c1e2019-11-21 15:51:54 -070018from chromite.lib import cros_logging as logging
19from chromite.lib import tee
Alex Klein00b1f1e2019-02-08 13:53:42 -070020from chromite.utils import matching
Alex Kleinf4dc4f52018-12-05 13:55:12 -070021
22
Alex Kleinf4dc4f52018-12-05 13:55:12 -070023def GetParser():
Alex Klein00b1f1e2019-02-08 13:53:42 -070024 """Build the argument parser."""
Alex Kleinf4dc4f52018-12-05 13:55:12 -070025 parser = commandline.ArgumentParser(description=__doc__)
26
Alex Klein2008aee2019-08-20 16:25:27 -060027 call_group = parser.add_argument_group(
28 'API Call Options',
29 'These options are used to execute an endpoint. When making a call every '
30 'argument in this group is required.')
31 call_group.add_argument(
32 'service_method',
33 nargs='?',
34 help='The "chromite.api.Service/Method" that is being called.')
35 call_group.add_argument(
36 '--input-json',
37 type='path',
Alex Kleinf4dc4f52018-12-05 13:55:12 -070038 help='Path to the JSON serialized input argument protobuf message.')
Alex Klein2008aee2019-08-20 16:25:27 -060039 call_group.add_argument(
40 '--output-json',
41 type='path',
Alex Kleinf4dc4f52018-12-05 13:55:12 -070042 help='The path to which the result protobuf message should be written.')
Michael Mortensen3e86c1e2019-11-21 15:51:54 -070043 call_group.add_argument(
44 '--tee-log',
45 type='path',
46 help='The path to which stdout and stderr should be teed to.')
Alex Klein2008aee2019-08-20 16:25:27 -060047
48 ux_group = parser.add_argument_group('Developer Options',
49 'Options to help developers.')
50 # Lists the full chromite.api.Service/Method, has both names to match
51 # whichever mental model people prefer.
52 ux_group.add_argument(
53 '--list-methods',
54 '--list-services',
55 action='store_true',
56 dest='list_services',
57 help='List the name of each registered "chromite.api.Service/Method".')
58
59 # Run configuration options.
60 test_group = parser.add_argument_group(
61 'Testing Options',
62 'These options are used to execute various tests against the API. These '
63 'options are mutually exclusive. Calling code can use these options to '
64 'validate inputs and test their handling of each return code case for '
65 'each endpoint.')
66 call_modifications = test_group.add_mutually_exclusive_group()
67 call_modifications.add_argument(
68 '--validate-only',
69 action='store_true',
70 default=False,
71 help='When set, only runs the argument validation logic. Calls produce '
72 'a return code of 0 iff the input proto comprises arguments that '
73 'are a valid call to the endpoint, or 1 otherwise.')
74 # See: api/faux.py for the mock call and error implementations.
75 call_modifications.add_argument(
76 '--mock-call',
77 action='store_true',
78 default=False,
79 help='When set, returns a valid, mock response rather than running the '
80 'endpoint. This allows API consumers to more easily test their '
81 'implementations against the version of the API being called. '
82 'This argument will always result in a return code of 0.')
83 call_modifications.add_argument(
84 '--mock-error',
85 action='store_true',
86 default=False,
87 help='When set, return a valid, mock error response rather than running '
88 'the endpoint. This allows API consumers to test their error '
89 'handling semantics against the version of the API being called. '
90 'This argument will always result in a return code of 2 iff the '
91 'endpoint ever produces a return code of 2, otherwise will always'
92 'produce a return code of 1.')
93 call_modifications.add_argument(
94 '--mock-invalid',
95 action='store_true',
96 default=False,
97 help='When set, return a mock validation error response rather than '
98 'running the endpoint. This allows API consumers to test their '
99 'validation error handling semantics against the version of the API '
100 'being called without having to understand how to construct an '
101 'invalid request. '
102 'This argument will always result in a return code of 1.')
Alex Kleinf4dc4f52018-12-05 13:55:12 -0700103
104 return parser
105
106
Alex Klein00b1f1e2019-02-08 13:53:42 -0700107def _ParseArgs(argv, router):
Alex Kleinf4dc4f52018-12-05 13:55:12 -0700108 """Parse and validate arguments."""
109 parser = GetParser()
110 opts = parser.parse_args(argv)
111
Alex Klein00b1f1e2019-02-08 13:53:42 -0700112 methods = router.ListMethods()
George Engelbrechtd3de8df2019-09-04 18:15:05 -0600113
114 if opts.list_services:
Alex Klein2008aee2019-08-20 16:25:27 -0600115 # We just need to print the methods and we're done.
George Engelbrechtd3de8df2019-09-04 18:15:05 -0600116 for method in methods:
117 print(method)
118 sys.exit(0)
119
Alex Klein2008aee2019-08-20 16:25:27 -0600120 # Positional service_method argument validation.
George Engelbrechtd3de8df2019-09-04 18:15:05 -0600121 if not opts.service_method:
122 parser.error('Must pass "Service/Method".')
123
124 parts = opts.service_method.split('/')
125 if len(parts) != 2:
126 parser.error(
Alex Klein2008aee2019-08-20 16:25:27 -0600127 'Must pass the correct format: (e.g. chromite.api.SdkService/Create).'
128 'Use --list-methods to see a full list.')
George Engelbrechtd3de8df2019-09-04 18:15:05 -0600129
Alex Klein00b1f1e2019-02-08 13:53:42 -0700130 if opts.service_method not in methods:
Alex Klein00aa8072019-04-15 16:36:00 -0600131 # Unknown method, try to match against known methods and make a suggestion.
132 # This is just for developer sanity, e.g. misspellings when testing.
Alex Klein2008aee2019-08-20 16:25:27 -0600133 matched = matching.GetMostLikelyMatchedObject(
134 methods, opts.service_method, matched_score_threshold=0.6)
Alex Klein00b1f1e2019-02-08 13:53:42 -0700135 error = 'Unrecognized service name.'
136 if matched:
137 error += '\nDid you mean: \n%s' % '\n'.join(matched)
138 parser.error(error)
139
Alex Kleinf4dc4f52018-12-05 13:55:12 -0700140 opts.service = parts[0]
141 opts.method = parts[1]
142
Alex Klein2008aee2019-08-20 16:25:27 -0600143 # --input-json and --output-json validation.
144 if not opts.input_json or not opts.output_json:
145 parser.error('--input-json and --output-json are both required.')
146
Alex Klein5bcb4d22019-03-21 13:51:54 -0600147 if not os.path.exists(opts.input_json):
148 parser.error('Input file does not exist.')
149
Alex Klein2008aee2019-08-20 16:25:27 -0600150 # Build the config object from the options.
151 opts.config = api_config_lib.ApiConfig(
152 validate_only=opts.validate_only,
153 mock_call=opts.mock_call,
154 mock_error=opts.mock_error)
Alex Klein69339cc2019-07-22 14:08:35 -0600155
Alex Kleinf4dc4f52018-12-05 13:55:12 -0700156 opts.Freeze()
157 return opts
158
159
Alex Kleinf4dc4f52018-12-05 13:55:12 -0700160def main(argv):
Michael Mortensen3e86c1e2019-11-21 15:51:54 -0700161 with cros_build_lib.ContextManagerStack() as stack:
Alex Kleinf4dc4f52018-12-05 13:55:12 -0700162
Michael Mortensen3e86c1e2019-11-21 15:51:54 -0700163 router = router_lib.GetRouter()
164 opts = _ParseArgs(argv, router)
Alex Klein00b1f1e2019-02-08 13:53:42 -0700165
Michael Mortensen3e86c1e2019-11-21 15:51:54 -0700166 if opts.tee_log:
167 stack.Add(tee.Tee, opts.tee_log)
168 logging.info('Teeing stdout and stderr to %s', opts.tee_log)
Alex Klein2008aee2019-08-20 16:25:27 -0600169
Michael Mortensen3e86c1e2019-11-21 15:51:54 -0700170 if opts.mock_invalid:
171 # --mock-invalid handling. We print error messages, but no output is ever
172 # set for validation errors, so we can handle it by just giving back the
173 # correct return code here.
174 return controller.RETURN_CODE_INVALID_INPUT
175
176 try:
177 return router.Route(opts.service, opts.method, opts.input_json,
178 opts.output_json, opts.config)
179 except router_lib.Error as e:
180 # Handle router_lib.Error derivatives nicely, but let anything else bubble
181 # up.
182 cros_build_lib.Die(e)