Skip to content

Commit

Permalink
[Tcs] Add app bind/unbind & Set patterns in app update (Azure#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ninpan-ms authored May 21, 2021
1 parent cc25629 commit 266d2ab
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/spring-cloud/azext_spring_cloud/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,27 @@
text: az spring-cloud tanzu app deploy -g myresourcegroup -s myservice -n myapp --artifact-path <my-Jar>
"""

helps['spring-cloud tanzu application-configuration-service'] = """
type: group
short-summary: Commands to manage Azure Spring Cloud Tanzu application-configuration-service.
"""

helps['spring-cloud tanzu application-configuration-service bind'] = """
type: command
short-summary: Bind app to Tanzu Application Configuration Service.
examples:
- name: Bind an app to Tanzu Application Configuration Service
text: az spring-cloud tanzu application-configuration-service bind -g myresourcegroup -s myservice --app myapp
"""

helps['spring-cloud tanzu application-configuration-service unbind'] = """
type: command
short-summary: Unbind app to Tanzu Application Configuration Service.
examples:
- name: Unbind an app to Tanzu Application Configuration Service
text: az spring-cloud tanzu application-configuration-service unbind -g myresourcegroup -s myservice --app myapp
"""

helps['spring-cloud create'] = """
type: command
short-summary: Create an Azure Spring Cloud.
Expand Down
12 changes: 11 additions & 1 deletion src/spring-cloud/azext_spring_cloud/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
validate_log_limit, validate_log_since, validate_sku, validate_jvm_options,
validate_vnet, validate_vnet_required_parameters, validate_node_resource_group,
validate_tracing_parameters, validate_app_insights_parameters, validate_java_agent_parameters,
validate_instance_count)
validate_instance_count, validate_tanzu_configuration_service_patterns)
from ._utils import ApiType

from .vendored_sdks.appplatform.v2020_07_01.models import RuntimeVersion, TestKeyType
Expand Down Expand Up @@ -54,10 +54,20 @@ def load_arguments(self, _):
c.argument('memory', help='Number of GB of memory per instance.')
c.argument('instance_count', type=int, help='Number of instance.', validator=validate_instance_count)
c.argument('env', env_type)
c.argument('patterns', type=str, help='Collection of patterns separate with \',\'', validator=validate_tanzu_configuration_service_patterns)

with self.argument_context('spring-cloud tanzu app deploy') as c:
c.argument('artifact-path', help='artifact path to deploy to this deployment.')

with self.argument_context('spring-cloud tanzu application-configuration-service') as c:
c.argument('service', service_name_type)

with self.argument_context('spring-cloud tanzu application-configuration-service bind') as c:
c.argument('app', app_name_type, help='Name of app to be binded with Application Configuration Service.', validator=validate_app_name)

with self.argument_context('spring-cloud tanzu application-configuration-service unbind') as c:
c.argument('app', app_name_type, help='Name of app to be unbinded with Application Configuration Service.', validator=validate_app_name)

with self.argument_context('spring-cloud create') as c:
c.argument('location', arg_type=get_location_type(self.cli_ctx), validator=validate_location)
c.argument('sku', type=str, validator=validate_sku, help='Name of SKU, the value is "Basic" or "Standard"')
Expand Down
25 changes: 25 additions & 0 deletions src/spring-cloud/azext_spring_cloud/_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,14 @@ def validate_node_resource_group(namespace):
_validate_resource_group_name(namespace.app_network_resource_group, 'app-network-resource-group')


def validate_tanzu_configuration_service_patterns(namespace):
pattern_list = namespace.patterns.split(',')
invalid_list = [p for p in pattern_list if not _is_valid_pattern(p)]
if invalid_list:
logger.warning('patterns "%s" are invalid', ','.join(invalid_list))
raise CLIError('--patterns should be the collection of patterns separated by comma, each pattern in the format of \'application\' or \'application/profile\'')


def _validate_resource_group_name(name, message_name):
if not name:
return
Expand All @@ -436,3 +444,20 @@ def _validate_route_table(namespace, vnet_obj):
or (not app_route_table_id and runtime_route_table_id):
raise CLIError(
'--service-runtime-subnet and --app-subnet should both associate with different route tables or neither.')


def _is_valid_pattern(pattern):
return _is_valid_app_name(pattern) or _is_valid_app_and_profile_name(pattern)


def _is_valid_app_name(pattern):
return match(r"^[a-zA-Z][-_a-zA-Z0-9]*$", pattern) is not None


def _is_valid_profile_name(profile):
return profile == "*" or _is_valid_app_name(profile)


def _is_valid_app_and_profile_name(pattern):
parts = pattern.split('/')
return len(parts) == 2 and _is_valid_app_name(parts[0]) and _is_valid_profile_name(parts[1])
4 changes: 4 additions & 0 deletions src/spring-cloud/azext_spring_cloud/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def load_command_table(self, _):
g.command('restart', 'tanzu_app_restart', supports_no_wait=True)
g.command('deploy', 'tanzu_app_deploy', supports_no_wait=True)

with self.command_group('spring-cloud tanzu application-configuration-service', tanzu_util) as g:
g.command('bind', 'tanzu_configuration_service_bind_app')
g.command('unbind', 'tanzu_configuration_service_unbind_app')

with self.command_group('spring-cloud', client_factory=cf_app_services) as g:
g.custom_command('create', 'spring_cloud_create', supports_no_wait=True, client_factory=cf_spring_cloud)
g.custom_command('update', 'spring_cloud_update', supports_no_wait=True, client_factory=cf_spring_cloud)
Expand Down
48 changes: 48 additions & 0 deletions src/spring-cloud/azext_spring_cloud/tanzu.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
DEFAULT_DEPLOYMENT_NAME = "default"
DEPLOYMENT_CREATE_OR_UPDATE_SLEEP_INTERVAL = 5
APP_CREATE_OR_UPDATE_SLEEP_INTERVAL = 2
TANZU_CONFIGURATION_SERVICE_NAME = "ApplicationConfigurationService"
TANZU_CONFIGURATION_SERVICE_PROPERTY_PATTERN = "pattern"


def tanzu_get(cmd, client, resource_group, name):
Expand Down Expand Up @@ -84,6 +86,7 @@ def tanzu_app_update(cmd, client, resource_group, service, name,
memory=None,
instance_count=None,
env=None,
patterns=None,
no_wait=False):
'''tanzu_app_update
Update an existing app, if the app doesn't exist, this command exit with error.
Expand Down Expand Up @@ -122,6 +125,10 @@ def _get_default_settings():
deployment_properties.deployment_settings.memory = memory
if env:
deployment_properties.deployment_settings.environment_variables = env
if patterns:
deployment_properties.deployment_settings.addon_config = _set_pattern_for_deployment(patterns)
deployment_name = app.properties.deployment.name if app.properties.deployment else DEFAULT_DEPLOYMENT_NAME
sku = app_properties.deployment.sku if app_properties.deployment else models.Sku(capacity=1)
if instance_count:
deployment_sku.capacity = instance_count
return _app_create_or_update(cmd, client, resource_group, service, name, deployment_name,
Expand Down Expand Up @@ -173,6 +180,22 @@ def tanzu_app_deploy(cmd, client, resource_group, service, name, artifact_path,
return client.deployments.deploy(resource_group, service, name, deployment_name, build_result_id=build_result_id)


def tanzu_configuration_service_bind_app(cmd, client, resource_group, service, app_name):
'''tanzu_configuration_service_bind_app
Bind Application Configuration Service to an existing app to enable functionality.
If the app doesn't exist, this command exit with error.
'''
_tcs_bind_or_unbind_app(cmd, client, resource_group, service, app_name, True)


def tanzu_configuration_service_unbind_app(cmd, client, resource_group, service, app_name):
'''tanzu_configuration_service_unbind_app
Unbind Application Configuration Service to an existing app to disable functionality.
If the app doesn't exist, this command exit with error.
'''
_tcs_bind_or_unbind_app(cmd, client, resource_group, service, app_name, False)


def _app_create_or_update(cmd, client, resource_group, service, app_name, deployment_name,
app_properties, deployment_properties, deployment_sku,
no_wait, operation='Updating', create_deployment=True):
Expand Down Expand Up @@ -233,3 +256,28 @@ def _assert_deployment_exist_and_retrieve_name(cmd, client, resource_group, serv
raise CLIError('Deployment not found, create one by running "az spring-cloud tanzu app '
'update -g {} -s {} -n {}"'.format(resource_group, service, name))
return deployment.name


def _set_pattern_for_deployment(patterns):
return {
TANZU_CONFIGURATION_SERVICE_NAME: models.AddonProfile(
properties = {
TANZU_CONFIGURATION_SERVICE_PROPERTY_PATTERN: patterns
}
)
}


def _tcs_bind_or_unbind_app(cmd, client, resource_group, service, app_name, enabled):
# todo: replace put with patch for app update
app = _app_get(cmd, client, resource_group, service, app_name)
app.properties.addon_config = {
TANZU_CONFIGURATION_SERVICE_NAME: models.AddonProfile()
} if app.properties.addon_config is None else app.properties.addon_config

if app.properties.addon_config.get(TANZU_CONFIGURATION_SERVICE_NAME).enabled == enabled:
logger.warning('App {} has been {}binded'.format(app_name, '' if enabled else 'un'))
return

app.properties.addon_config[TANZU_CONFIGURATION_SERVICE_NAME].enabled = enabled
return client.apps.create_or_update(resource_group, service, app_name, app.properties)

0 comments on commit 266d2ab

Please sign in to comment.