-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 💡 Use validator to validate or default deployment (#4188)
In most secenarios under `az spring-cloud app`, it can accept a parameter deployment. If the deployment is set, the command will update apps/deployments resource. Otherwise, it will look for one deployment under the app meets deployment.properties.active = true to be operated. In today's code, each command handle such logic to fetch or use default value. This PR abstract this logic to validator, and make custom command can be focus on the operation logic
- Loading branch information
Showing
5 changed files
with
173 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# -------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
# -------------------------------------------------------------------------------------------- | ||
|
||
# pylint: disable=too-few-public-methods, unused-argument, redefined-builtin | ||
|
||
from azure.cli.core.azclierror import InvalidArgumentValueError | ||
from msrestazure.azure_exceptions import CloudError | ||
from ._client_factory import cf_spring_cloud | ||
|
||
|
||
# pylint: disable=line-too-long,raise-missing-from | ||
NO_PRODUCTION_DEPLOYMENT_ERROR = "No production deployment found, use --deployment to specify deployment or create deployment with: az spring-cloud app deployment create" | ||
|
||
|
||
def fulfill_deployment_param(cmd, namespace): | ||
client = cf_spring_cloud(cmd.cli_ctx) | ||
if not namespace.name or not namespace.service or not namespace.resource_group: | ||
return | ||
if namespace.deployment: | ||
namespace.deployment = _ensure_deployment_exist(client, namespace.resource_group, namespace.service, namespace.name, namespace.deployment) | ||
else: | ||
namespace.deployment = _ensure_active_deployment_exist_and_get(client, namespace.resource_group, namespace.service, namespace.name) | ||
|
||
|
||
def _ensure_deployment_exist(client, resource_group, service, app, deployment): | ||
try: | ||
return client.deployments.get(resource_group, service, app, deployment) | ||
except CloudError: | ||
raise InvalidArgumentValueError('Deployment {} not found under app {}'.format(deployment, app)) | ||
|
||
|
||
def _ensure_active_deployment_exist_and_get(client, resource_group, service, name): | ||
deployment_resource = _get_active_deployment(client, resource_group, service, name) | ||
if not deployment_resource: | ||
raise InvalidArgumentValueError(NO_PRODUCTION_DEPLOYMENT_ERROR) | ||
return deployment_resource | ||
|
||
|
||
def _get_active_deployment(client, resource_group, service, name): | ||
deployments = client.deployments.list(resource_group, service, name) | ||
return next(iter(x for x in deployments if x.properties.active), None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.