Skip to content

Commit

Permalink
[Spring-Cloud] add Build Service related command for enterprise tier (#…
Browse files Browse the repository at this point in the history
…4295)

* [Spring-Cloud] Add Build service for enterprise tier

* fix

* fix

* Update src/spring-cloud/azext_spring_cloud/_params.py

Co-authored-by: Yuwei Zhou <[email protected]>

* Update src/spring-cloud/azext_spring_cloud/_params.py

Co-authored-by: Yuwei Zhou <[email protected]>

* fix

* fix

* fix

* fix

* fix

* Update src/spring-cloud/azext_spring_cloud/_buildservices.py

Co-authored-by: Yuwei Zhou <[email protected]>

* Update src/spring-cloud/azext_spring_cloud/_buildservices.py

Co-authored-by: Yuwei Zhou <[email protected]>

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* Update src/spring-cloud/azext_spring_cloud/_params.py

Co-authored-by: Xing Zhou <[email protected]>

* Update src/spring-cloud/azext_spring_cloud/_params.py

Co-authored-by: Xing Zhou <[email protected]>

* fix

* add delete test

* fix

* Update spring_cloud_instance.py

* Update commands.py

* fix

* fix

* Update _params.py

* fix

* fix test

* fix test

* fix test

Co-authored-by: Yuwei Zhou <[email protected]>
Co-authored-by: Xing Zhou <[email protected]>
  • Loading branch information
3 people authored Jan 13, 2022
1 parent 18eb1da commit 7f111bb
Show file tree
Hide file tree
Showing 12 changed files with 1,028 additions and 4,912 deletions.
52 changes: 52 additions & 0 deletions src/spring-cloud/azext_spring_cloud/_build_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# --------------------------------------------------------------------------------------------
# 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

import json
from azure.cli.core.util import sdk_no_wait
from .vendored_sdks.appplatform.v2022_01_01_preview import models

DEFAULT_BUILD_SERVICE_NAME = "default"
DEFAULT_BUILD_AGENT_POOL_NAME = "default"


def _update_default_build_agent_pool(cmd, client, resource_group, name, build_pool_size=None):
if build_pool_size is not None:
build_properties = models.BuildServiceAgentPoolProperties(
pool_size=models.BuildServiceAgentPoolSizeProperties(
name=build_pool_size))
agent_pool_resource = models.BuildServiceAgentPoolResource(
properties=build_properties)
return client.build_service_agent_pool.begin_update_put(
resource_group, name, DEFAULT_BUILD_SERVICE_NAME, DEFAULT_BUILD_AGENT_POOL_NAME, agent_pool_resource)


def create_or_update_builder(cmd, client, resource_group, service, name, builder_json=None, builder_file=None, no_wait=False):
builder = _update_builder(builder_file, builder_json)
builder_resource = models.BuilderResource(
properties=builder
)
return sdk_no_wait(no_wait, client.build_service_builder.begin_create_or_update,
resource_group, service, DEFAULT_BUILD_SERVICE_NAME, name, builder_resource)


def builder_show(cmd, client, resource_group, service, name):
return client.build_service_builder.get(resource_group, service, DEFAULT_BUILD_SERVICE_NAME, name)


def builder_delete(cmd, client, resource_group, service, name, no_wait=False):
return sdk_no_wait(no_wait, client.build_service_builder.begin_delete, resource_group, service, DEFAULT_BUILD_SERVICE_NAME, name)


def _update_builder(builder_file, builder_json):
if builder_file is not None:
with open(builder_file, 'r') as json_file:
builder = json.load(json_file)

if builder_json is not None:
builder = json.loads(builder_json)

return builder
42 changes: 42 additions & 0 deletions src/spring-cloud/azext_spring_cloud/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,48 @@
text: az spring-cloud service-registry unbind --app MyApp -s MyService -g MyResourceGroup
"""

helps['spring-cloud build-service'] = """
type: group
short-summary: (Enterprise Tier Only) Commands to manage Build Service
"""

helps['spring-cloud build-service builder'] = """
type: group
short-summary: (Enterprise Tier Only) Commands to manage Builder Resource
"""

helps['spring-cloud build-service builder create'] = """
type: command
short-summary: Create a builder.
examples:
- name: Create a builder using JSON file.
text: az spring-cloud build-service builder create --name my-builder --builder-json MyJson.json --service clitest --resource-group cli
"""

helps['spring-cloud build-service builder update'] = """
type: command
short-summary: Update a builder.
examples:
- name: Update a builder using JSON file.
text: az spring-cloud build-service builder update --name my-builder --builder-json MyJson.json --service clitest --resource-group cli
"""

helps['spring-cloud build-service builder show'] = """
type: command
short-summary: Show a builder.
examples:
- name: Show a builder.
text: az spring-cloud build-service builder show --name my-builder --service clitest --resource-group cli
"""

helps['spring-cloud build-service builder delete'] = """
type: command
short-summary: Delete a builder.
examples:
- name: Delete a builder.
text: az spring-cloud build-service builder delete --name my-builder --service clitest --resource-group cli
"""

helps['spring-cloud application-configuration-service'] = """
type: group
short-summary: (Support Enterprise Tier Only) Commands to manage Application Configuration Service in Azure Spring Cloud.
Expand Down
30 changes: 29 additions & 1 deletion src/spring-cloud/azext_spring_cloud/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
validate_tracing_parameters_asc_create, validate_tracing_parameters_asc_update,
validate_app_insights_parameters, validate_instance_count, validate_java_agent_parameters,
validate_jar)
from ._validators_enterprise import (only_support_enterprise,
from ._validators_enterprise import (only_support_enterprise, validate_builder_resource, validate_builder_create,
validate_builder_update, validate_build_pool_size,
validate_git_uri, validate_acs_patterns, validate_config_file_patterns,
validate_routes, validate_gateway_instance_count,
validate_api_portal_instance_count,
Expand Down Expand Up @@ -99,6 +100,11 @@ def load_arguments(self, _):
help="Create your Azure Spring Cloud service in an Azure availability zone or not, "
"this could only be supported in several regions at the moment.",
default=False, is_preview=True)
c.argument('build_pool_size',
arg_type=get_enum_type(['S1', 'S2', 'S3', 'S4', 'S5']),
validator=validate_build_pool_size,
is_preview=True,
help='(Enterprise Tier Only) Size of build agent pool. See aka.ms/azure-spring-cloud-build-service-docs for size info.')
c.argument('enable_application_configuration_service',
action='store_true',
is_preview=True,
Expand Down Expand Up @@ -158,6 +164,10 @@ def load_arguments(self, _):
deprecate_info=c.deprecate(target='az spring-cloud update --disable-app-insights',
redirect='az spring-cloud app-insights update --disable',
hide=True))
c.argument('build_pool_size',
arg_type=get_enum_type(['S1', 'S2', 'S3', 'S4', 'S5']),
is_preview=True,
help='(Enterprise Tier Only) Size of build agent pool. See aka.ms/azure-spring-cloud-build-service-docs for size info.')

for scope in ['spring-cloud create', 'spring-cloud update']:
with self.argument_context(scope) as c:
Expand Down Expand Up @@ -471,6 +481,24 @@ def prepare_logs_argument(c):
help="Disable Application Insights.",
validator=validate_app_insights_parameters)

with self.argument_context('spring-cloud build-service builder') as c:
c.argument('service', service_name_type, validator=only_support_enterprise)

for scope in ['create', 'update']:
with self.argument_context('spring-cloud build-service builder {}'.format(scope)) as c:
c.argument('builder_json', help="The JSON array of builder.", validator=validate_builder_resource)
c.argument('builder_file', help="The file path of JSON array of builder.", validator=validate_builder_resource)

with self.argument_context('spring-cloud build-service builder create') as c:
c.argument('name', help="The builder name.", validator=validate_builder_create)

with self.argument_context('spring-cloud build-service builder update') as c:
c.argument('name', help="The builder name.", validator=validate_builder_update)

for scope in ['show', 'delete']:
with self.argument_context('spring-cloud build-service builder {}'.format(scope)) as c:
c.argument('name', help="The builder name.")

for scope in ['application-configuration-service', 'service-registry',
'gateway', 'api-portal']:
with self.argument_context('spring-cloud {}'.format(scope)) as c:
Expand Down
41 changes: 40 additions & 1 deletion src/spring-cloud/azext_spring_cloud/_validators_enterprise.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from azure.cli.core.azclierror import (ArgumentUsageError, ClientRequestError,
InvalidArgumentValueError,
MutuallyExclusiveArgumentError)
from azure.core.exceptions import ResourceNotFoundError
from knack.log import get_logger

from ._resource_quantity import validate_cpu as validate_and_normalize_cpu
Expand All @@ -19,7 +20,7 @@
from ._util_enterprise import (
is_enterprise_tier, get_client
)
from ._validators import validate_instance_count
from ._validators import (validate_instance_count, _parse_sku_name)
from .buildpack_binding import (DEFAULT_BUILD_SERVICE_NAME)

logger = get_logger(__name__)
Expand All @@ -35,6 +36,44 @@ def not_support_enterprise(cmd, namespace):
raise ClientRequestError("'{}' doesn't support for Enterprise tier Spring instance.".format(namespace.command))


def validate_builder_create(cmd, namespace):
client = get_client(cmd)
try:
builder = client.build_service_builder.get(namespace.resource_group,
namespace.service,
DEFAULT_BUILD_SERVICE_NAME,
namespace.name)
if builder is not None:
raise ClientRequestError('Builder {} already exists.'.format(namespace.name))
except ResourceNotFoundError:
pass


def validate_builder_update(cmd, namespace):
client = get_client(cmd)
try:
client.build_service_builder.get(namespace.resource_group,
namespace.service,
DEFAULT_BUILD_SERVICE_NAME,
namespace.name)
except ResourceNotFoundError:
raise ClientRequestError('Builder {} does not exist.'.format(namespace.name))


def validate_builder_resource(namespace):
if namespace.builder_json is not None and namespace.builder_file is not None:
raise ClientRequestError("You can only specify either --builder-json or --builder-file.")


def validate_build_pool_size(namespace):
if _parse_sku_name(namespace.sku) == 'enterprise':
if namespace.build_pool_size is None:
namespace.build_pool_size = 'S1'
else:
if namespace.build_pool_size is not None:
raise ClientRequestError("You can only specify --build-pool-size with enterprise tier.")


def validate_cpu(namespace):
namespace.cpu = validate_and_normalize_cpu(namespace.cpu)

Expand Down
13 changes: 13 additions & 0 deletions src/spring-cloud/azext_spring_cloud/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ def load_command_table(self, _):
client_factory=cf_spring_cloud_20220101preview
)

builder_cmd_group = CliCommandType(
operations_tmpl="azext_spring_cloud._build_service#{}",
client_factory=cf_spring_cloud_20220101preview
)

buildpack_binding_cmd_group = CliCommandType(
operations_tmpl="azext_spring_cloud.buildpack_binding#{}",
client_factory=cf_spring_cloud_20220101preview
Expand Down Expand Up @@ -280,6 +285,14 @@ def load_command_table(self, _):
with self.command_group('spring-cloud', exception_handler=handle_asc_exception):
pass

with self.command_group('spring-cloud build-service builder',
custom_command_type=builder_cmd_group,
exception_handler=handle_asc_exception, is_preview=True) as g:
g.custom_command('create', 'create_or_update_builder', supports_no_wait=True)
g.custom_command('update', 'create_or_update_builder', supports_no_wait=True)
g.custom_show_command('show', 'builder_show')
g.custom_command('delete', 'builder_delete', supports_no_wait=True, confirmation=True)

with self.command_group('spring-cloud build-service builder buildpack-binding',
custom_command_type=buildpack_binding_cmd_group,
exception_handler=handle_asc_exception, is_preview=True) as g:
Expand Down
6 changes: 5 additions & 1 deletion src/spring-cloud/azext_spring_cloud/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import base64
from collections import defaultdict
from ._log_stream import LogStream
from ._build_service import _update_default_build_agent_pool

logger = get_logger(__name__)
DEFAULT_DEPLOYMENT_NAME = "default"
Expand Down Expand Up @@ -85,7 +86,7 @@ def _update_application_insights_asc_create(cmd,


def spring_cloud_update(cmd, client, resource_group, name, app_insights_key=None, app_insights=None,
disable_app_insights=None, sku=None, tags=None, no_wait=False):
disable_app_insights=None, sku=None, tags=None, build_pool_size=None, no_wait=False):
"""
TODO (jiec) app_insights_key, app_insights and disable_app_insights are marked as deprecated.
Will be decommissioned in future releases.
Expand All @@ -107,6 +108,9 @@ def spring_cloud_update(cmd, client, resource_group, name, app_insights_key=None
_update_application_insights_asc_update(cmd, resource_group, name, location,
app_insights_key, app_insights, disable_app_insights, no_wait)

_update_default_build_agent_pool(
cmd, client, resource_group, name, build_pool_size)

# update service tags
if tags is not None:
updated_resource.tags = tags
Expand Down
6 changes: 6 additions & 0 deletions src/spring-cloud/azext_spring_cloud/spring_cloud_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
from .vendored_sdks.appplatform.v2022_01_01_preview import models
from knack.log import get_logger
from .custom import (_warn_enable_java_agent, _update_application_insights_asc_create)
from ._build_service import _update_default_build_agent_pool
from .buildpack_binding import create_default_buildpack_binding_for_application_insights
from ._tanzu_component import (create_application_configuration_service,
create_service_registry,
create_gateway,
create_api_portal)


from ._validators import (_parse_sku_name)
from knack.log import get_logger

Expand Down Expand Up @@ -83,6 +85,8 @@ def before_create(self, **_):
def after_create(self, no_wait=None, **kwargs):
pollers = [
# create sub components like Service registry, ACS, build service, etc.
_update_default_build_agent_pool(
self.cmd, self.client, self.resource_group, self.name, kwargs['build_pool_size']),
_enable_app_insights(self.cmd, self.client, self.resource_group, self.name, self.location, **kwargs),
create_application_configuration_service(self.cmd, self.client, self.resource_group, self.name, **kwargs),
create_service_registry(self.cmd, self.client, self.resource_group, self.name, **kwargs),
Expand Down Expand Up @@ -116,6 +120,7 @@ def spring_cloud_create(cmd, client, resource_group, name,
sku=None,
tags=None,
zone_redundant=False,
build_pool_size=None,
enable_application_configuration_service=False,
enable_service_registry=False,
enable_gateway=False,
Expand All @@ -142,6 +147,7 @@ def spring_cloud_create(cmd, client, resource_group, name,
'sku': sku,
'tags': tags,
'zone_redundant': zone_redundant,
'build_pool_size': build_pool_size,
'enable_application_configuration_service': enable_application_configuration_service,
'enable_service_registry': enable_service_registry,
'enable_gateway': enable_gateway,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"stack": {
"id": "io.buildpacks.stacks.bionic",
"version": "base"
},
"buildpackGroups": [
{
"name": "mix",
"buildpacks": [
{
"id": "tanzu-buildpacks/java-azure"
}
]
}
]
}
Loading

0 comments on commit 7f111bb

Please sign in to comment.