Skip to content

Commit

Permalink
[Spring-Cloud] refactor spring-cloud create (#4291)
Browse files Browse the repository at this point in the history
* [Spring-Cloud] refactor spring-cloud create

* add more tests

* group parameters

* add mock reset in setup
  • Loading branch information
yuwzho committed Jan 5, 2022
1 parent f23a400 commit 07d4ad5
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 183 deletions.
35 changes: 0 additions & 35 deletions src/spring-cloud/azext_spring_cloud/_enterprise.py

This file was deleted.

19 changes: 12 additions & 7 deletions src/spring-cloud/azext_spring_cloud/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,36 @@ def load_arguments(self, _):
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', arg_type=sku_type, default='Standard')
c.argument('reserved_cidr_range', help='Comma-separated list of IP address ranges in CIDR format. The IP ranges are reserved to host underlying Azure Spring Cloud infrastructure, which should be 3 at least /16 unused IP ranges, must not overlap with any Subnet IP ranges.', validator=validate_vnet_required_parameters)
c.argument('vnet', help='The name or ID of an existing Virtual Network into which to deploy the Spring Cloud instance.', validator=validate_vnet_required_parameters)
c.argument('app_subnet', help='The name or ID of an existing subnet in "vnet" into which to deploy the Spring Cloud app. Required when deploying into a Virtual Network. Smaller subnet sizes are supported, please refer: https://aka.ms/azure-spring-cloud-smaller-subnet-vnet-docs', validator=validate_vnet_required_parameters)
c.argument('service_runtime_subnet', options_list=['--service-runtime-subnet', '--svc-subnet'], help='The name or ID of an existing subnet in "vnet" into which to deploy the Spring Cloud service runtime. Required when deploying into a Virtual Network.', validator=validate_vnet)
c.argument('service_runtime_network_resource_group', options_list=['--service-runtime-network-resource-group', '--svc-nrg'], help='The resource group where all network resources for Azure Spring Cloud service runtime will be created in.', validator=validate_node_resource_group)
c.argument('app_network_resource_group', options_list=['--app-network-resource-group', '--app-nrg'], help='The resource group where all network resources for apps will be created in.', validator=validate_node_resource_group)
c.argument('reserved_cidr_range', arg_group='VNet Injection', help='Comma-separated list of IP address ranges in CIDR format. The IP ranges are reserved to host underlying Azure Spring Cloud infrastructure, which should be 3 at least /16 unused IP ranges, must not overlap with any Subnet IP ranges.', validator=validate_vnet_required_parameters)
c.argument('vnet', arg_group='VNet Injection', help='The name or ID of an existing Virtual Network into which to deploy the Spring Cloud instance.', validator=validate_vnet_required_parameters)
c.argument('app_subnet', arg_group='VNet Injection', help='The name or ID of an existing subnet in "vnet" into which to deploy the Spring Cloud app. Required when deploying into a Virtual Network. Smaller subnet sizes are supported, please refer: https://aka.ms/azure-spring-cloud-smaller-subnet-vnet-docs', validator=validate_vnet_required_parameters)
c.argument('service_runtime_subnet', arg_group='VNet Injection', options_list=['--service-runtime-subnet', '--svc-subnet'], help='The name or ID of an existing subnet in "vnet" into which to deploy the Spring Cloud service runtime. Required when deploying into a Virtual Network.', validator=validate_vnet)
c.argument('service_runtime_network_resource_group', arg_group='VNet Injection', options_list=['--service-runtime-network-resource-group', '--svc-nrg'], help='The resource group where all network resources for Azure Spring Cloud service runtime will be created in.', validator=validate_node_resource_group)
c.argument('app_network_resource_group', arg_group='VNet Injection', options_list=['--app-network-resource-group', '--app-nrg'], help='The resource group where all network resources for apps will be created in.', validator=validate_node_resource_group)
c.argument('enable_java_agent',
arg_group='Application Insights',
arg_type=get_three_state_flag(),
help="Java in process agent is now GA-ed and used by default when Application Insights enabled. "
"This parameter is no longer needed and will be removed in future release.",
validator=validate_java_agent_parameters,
deprecate_info=c.deprecate(target='--enable-java-agent', hide=True))
c.argument('app_insights_key',
arg_group='Application Insights',
help="Connection string (recommended) or Instrumentation key of the existing Application Insights.",
validator=validate_tracing_parameters_asc_create)
c.argument('app_insights',
arg_group='Application Insights',
help="Name of the existing Application Insights in the same Resource Group. "
"Or Resource ID of the existing Application Insights in a different Resource Group.",
validator=validate_tracing_parameters_asc_create)
c.argument('sampling_rate',
type=float,
arg_group='Application Insights',
help="Sampling Rate of application insights. Minimum is 0, maximum is 100.",
validator=validate_tracing_parameters_asc_create)
c.argument('disable_app_insights',
arg_type=get_three_state_flag(),
arg_group='Application Insights',
help="Disable Application Insights, "
"if not disabled and no existing Application Insights specified with "
"--app-insights-key or --app-insights, "
Expand All @@ -83,7 +88,7 @@ def load_arguments(self, _):
c.argument('zone_redundant',
arg_type=get_three_state_flag(),
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 ",
"this could only be supported in several regions at the moment.",
default=False, is_preview=True)

with self.argument_context('spring-cloud update') as c:
Expand Down
2 changes: 1 addition & 1 deletion src/spring-cloud/azext_spring_cloud/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# pylint: disable=too-many-statements
def load_command_table(self, _):
spring_cloud_routing_util = CliCommandType(
operations_tmpl='azext_spring_cloud.tier_routing_spring_cloud#{}',
operations_tmpl='azext_spring_cloud.spring_cloud_instance#{}',
client_factory=cf_spring_cloud_20220101preview
)

Expand Down
81 changes: 12 additions & 69 deletions src/spring-cloud/azext_spring_cloud/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
from ._stream_utils import stream_logs
from azure.mgmt.core.tools import (parse_resource_id, is_valid_resource_id)
from ._utils import (_get_upload_local_file, _get_persistent_disk_size,
get_portal_uri, get_azure_files_info,
wait_till_end)
get_portal_uri, get_azure_files_info)
from knack.util import CLIError
from .vendored_sdks.appplatform.v2020_07_01 import models
from .vendored_sdks.appplatform.v2020_11_01_preview import models as models_20201101preview
Expand Down Expand Up @@ -60,78 +59,22 @@
LOG_RUNNING_PROMPT = "This command usually takes minutes to run. Add '--verbose' parameter if needed."


def spring_cloud_create(cmd, client, resource_group, name, location=None,
vnet=None, service_runtime_subnet=None, app_subnet=None, reserved_cidr_range=None,
service_runtime_network_resource_group=None, app_network_resource_group=None,
app_insights_key=None, app_insights=None, sampling_rate=None,
disable_app_insights=None, enable_java_agent=None,
sku=None, tags=None, zone_redundant=False, no_wait=False):
"""
Note: This is the command for create Spring-Cloud Standard and Basic tier. Refer tier_routing_spring_cloud.py for
the command definition. And _enteprise.py for Spring-Cloud Enterprise tier creation.
If app_insights_key, app_insights and disable_app_insights are all None,
will still create an application insights and enable application insights.
:param enable_java_agent: (TODO) In deprecation process, ignore the value now. Will delete this.
:param app_insights: application insights name or its resource id
:param app_insights_key: Connection string or Instrumentation key
"""
# TODO (jiec) Deco this method when we deco parameter "--enable-java-agent"
_warn_enable_java_agent(enable_java_agent)

poller = _create_service(cmd, client, resource_group, name,
location=location,
service_runtime_subnet=service_runtime_subnet,
app_subnet=app_subnet,
reserved_cidr_range=reserved_cidr_range,
service_runtime_network_resource_group=service_runtime_network_resource_group,
app_network_resource_group=app_network_resource_group,
zone_redundant=zone_redundant,
sku=sku,
tags=tags)
_update_application_insights_asc_create(cmd, resource_group, name, location,
app_insights_key, app_insights, sampling_rate,
disable_app_insights, no_wait)
return poller


def _create_service(cmd, client, resource_group, name, location=None,
service_runtime_subnet=None, app_subnet=None, reserved_cidr_range=None,
service_runtime_network_resource_group=None, app_network_resource_group=None,
zone_redundant=False,
sku=None, tags=None):
if location is None:
location = _get_rg_location(cmd.cli_ctx, resource_group)
properties = models_20220101preview.ClusterResourceProperties()

if service_runtime_subnet or app_subnet or reserved_cidr_range:
properties.network_profile = models_20220101preview.NetworkProfile(
service_runtime_subnet_id=service_runtime_subnet,
app_subnet_id=app_subnet,
service_cidr=reserved_cidr_range,
app_network_resource_group=app_network_resource_group,
service_runtime_network_resource_group=service_runtime_network_resource_group
)

properties.zone_redundant = zone_redundant
resource = models_20220101preview.ServiceResource(location=location, sku=sku, properties=properties, tags=tags)

poller = client.services.begin_create_or_update(
resource_group, name, resource)
logger.warning(" - Creating Service ..")
wait_till_end(cmd, poller)
return poller


def _warn_enable_java_agent(enable_java_agent):
def _warn_enable_java_agent(enable_java_agent, **_):
if enable_java_agent is not None:
logger.warn("Java in process agent is now GA-ed and used by default when Application Insights enabled. "
"The parameter '--enable-java-agent' is no longer needed and will be removed in future release.")


def _update_application_insights_asc_create(cmd, resource_group, name, location,
app_insights_key, app_insights, sampling_rate,
disable_app_insights, no_wait):
def _update_application_insights_asc_create(cmd,
resource_group,
name,
location,
app_insights_key=None,
app_insights=None,
sampling_rate=None,
disable_app_insights=None,
no_wait=None,
**_):
monitoring_setting_resource = models.MonitoringSettingResource()
if disable_app_insights is not True:
client_preview = get_mgmt_service_client(cmd.cli_ctx, AppPlatformManagementClient_20201101preview)
Expand Down
133 changes: 133 additions & 0 deletions src/spring-cloud/azext_spring_cloud/spring_cloud_instance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

# pylint: disable=wrong-import-order
# pylint: disable=unused-argument, logging-format-interpolation, protected-access, wrong-import-order, too-many-lines
from ._utils import (wait_till_end, _get_rg_location)
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 ._validators import (_parse_sku_name)
from knack.log import get_logger

logger = get_logger(__name__)


class DefaultSpringCloud:
def __init__(self, cmd, client, resource_group, name, location=None, **_):
self.cmd = cmd
self.client = client
self.resource_group = resource_group
self.name = name
self.location = location or _get_rg_location(cmd.cli_ctx, resource_group)

def create(self, **kwargs):
self.before_create(**kwargs)
resource = self.create_service(**kwargs)
self.after_create(**kwargs)
return resource

def before_create(self, **kwargs):
_warn_enable_java_agent(**kwargs)

def after_create(self, **kwargs):
_update_application_insights_asc_create(self.cmd,
self.resource_group,
self.name,
self.location,
**kwargs)

def create_service(self,
service_runtime_subnet=None,
app_subnet=None,
reserved_cidr_range=None,
service_runtime_network_resource_group=None,
app_network_resource_group=None,
zone_redundant=False,
sku=None,
tags=None,
**_):
properties = models.ClusterResourceProperties(
zone_redundant=zone_redundant
)

if service_runtime_subnet or app_subnet or reserved_cidr_range:
properties.network_profile = models.NetworkProfile(
service_runtime_subnet_id=service_runtime_subnet,
app_subnet_id=app_subnet,
service_cidr=reserved_cidr_range,
app_network_resource_group=app_network_resource_group,
service_runtime_network_resource_group=service_runtime_network_resource_group
)

resource = models.ServiceResource(location=self.location, sku=sku, properties=properties, tags=tags)
poller = self.client.services.begin_create_or_update(
self.resource_group, self.name, resource)
logger.warning(" - Creating Service ..")
wait_till_end(self.cmd, poller)
return poller


class EnterpriseSpringCloud(DefaultSpringCloud):
def before_create(self, **_):
pass

def after_create(self, no_wait=None, **kwargs):
pollers = [
# create sub components like Service registry, ACS, build service, etc.
]
pollers = [x for x in pollers if x]
if not no_wait:
wait_till_end(self.cmd, *pollers)


def _get_factory(cmd, client, resource_group, name, location=None, sku=None):
if _parse_sku_name(sku) == 'enterprise':
return EnterpriseSpringCloud(cmd, client, resource_group, name, location)
return DefaultSpringCloud(cmd, client, resource_group, name, location)


def spring_cloud_create(cmd, client, resource_group, name,
location=None,
vnet=None,
service_runtime_subnet=None,
app_subnet=None,
reserved_cidr_range=None,
service_runtime_network_resource_group=None,
app_network_resource_group=None,
app_insights_key=None,
app_insights=None,
sampling_rate=None,
disable_app_insights=None,
enable_java_agent=None,
sku=None,
tags=None,
zone_redundant=False,
no_wait=False):
"""
Because Standard/Basic tier vs. Enterprise tier creation are very different. Here routes the command to different
implementation according to --sku parameters.
"""
kwargs = {
'vnet': vnet,
'service_runtime_subnet': service_runtime_subnet,
'app_subnet': app_subnet,
'reserved_cidr_range': reserved_cidr_range,
'service_runtime_network_resource_group': service_runtime_network_resource_group,
'app_network_resource_group': app_network_resource_group,
'app_insights_key': app_insights_key,
'app_insights': app_insights,
'sampling_rate': sampling_rate,
'disable_app_insights': disable_app_insights,
'enable_java_agent': enable_java_agent,
'sku': sku,
'tags': tags,
'zone_redundant': zone_redundant,
'no_wait': no_wait
}

spring_cloud_factory = _get_factory(cmd, client, resource_group, name, location=location, sku=sku)
return spring_cloud_factory.create(**kwargs)
Loading

0 comments on commit 07d4ad5

Please sign in to comment.