Skip to content

Commit 6f45df6

Browse files
Validate endpoints before creating them
Use the `/endpoints/actions` validation API, which takes the `platform` and `connection info` instead of an `endpoint ID`, and validate before creating the endpoint, so invalid endpoints are no longer saved. Signed-off-by: Mihaela Balutoiu <mbalutoiu@cloudbasesolutions.com>
1 parent 0c84675 commit 6f45df6

3 files changed

Lines changed: 22 additions & 13 deletions

File tree

‎coriolisclient/cli/endpoints.py‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,20 +151,20 @@ def take_action(self, args):
151151
"--connection-secret, but not both")
152152

153153
conn_info = get_connection_info_from_args(args)
154-
endpoint = self.app.client_manager.coriolis.endpoints.create(
154+
endpoints = self.app.client_manager.coriolis.endpoints
155+
if not args.skip_validation:
156+
valid, message = endpoints.validate_connection(
157+
args.provider, conn_info, regions=args.regions)
158+
if not valid:
159+
raise exceptions.EndpointConnectionValidationFailed(message)
160+
161+
endpoint = endpoints.create(
155162
args.name,
156163
args.provider,
157164
conn_info,
158165
args.description,
159166
regions=args.regions)
160167

161-
if not args.skip_validation:
162-
valid, message = (
163-
self.app.client_manager.coriolis.endpoints.validate_connection(
164-
endpoint.id))
165-
if not valid:
166-
raise exceptions.EndpointConnectionValidationFailed(message)
167-
168168
return EndpointDetailFormatter().get_formatted_entity(endpoint)
169169

170170

@@ -292,6 +292,9 @@ def get_parser(self, prog_name):
292292
def take_action(self, args):
293293
endpoints = self.app.client_manager.coriolis.endpoints
294294
endpoint_id = endpoints.get_endpoint_id_for_name(args.id)
295-
valid, message = endpoints.validate_connection(endpoint_id)
295+
endpoint = endpoints.get(endpoint_id)
296+
valid, message = endpoints.validate_connection(
297+
endpoint.type, endpoint.connection_info.to_dict(),
298+
regions=endpoint.mapped_regions)
296299
if not valid:
297300
raise exceptions.EndpointConnectionValidationFailed(message)

‎coriolisclient/v1/endpoints.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,13 @@ def get_inventory_csv(self, endpoint, source_environment=None):
7676
resp = self.client.get(url, headers={'Accept': 'text/csv'})
7777
return resp.text
7878

79-
def validate_connection(self, endpoint):
79+
def validate_connection(self, platform, connection_info, regions=None):
8080
data = self.client.post(
81-
'/endpoints/%s/actions' % base.getid(endpoint),
82-
json={'validate-connection': None}).json()
81+
'/endpoints/actions',
82+
json={'validate-connection': {
83+
"platform": platform,
84+
"connection_info": connection_info,
85+
"mapped_regions": regions or []}}).json()
8386
validate_data = data["validate-connection"]
8487
return validate_data.get("valid"), validate_data.get("message")
8588

‎samples/endpoint_samples.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ def validate_endpoint(coriolis, endpoint, raise_on_error=True):
112112
:param endpoint: endpoint object or ID
113113
:return: (bool, str) tuple containing the result and possible errors
114114
"""
115-
is_valid, error_msg = coriolis.endpoints.validate_connection(endpoint)
115+
endpoint = coriolis.endpoints.get(endpoint)
116+
is_valid, error_msg = coriolis.endpoints.validate_connection(
117+
endpoint.type, endpoint.connection_info.to_dict(),
118+
regions=endpoint.mapped_regions)
116119

117120
if raise_on_error and not is_valid:
118121
raise Exception("Endpoint validation failed: %s" % error_msg)

0 commit comments

Comments
 (0)