Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix app bind/unbind #34

Merged
merged 1 commit into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/spring-cloud/azext_spring_cloud/_enterprise.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,11 @@ def _request_upload_url(client, resource_group, service, name):


def _get_addon_configs(config_file_patterns):
patterns = models.AddonProfile(
properties = {
addon_configs = {
APPLICATION_CONFIGURATION_SERVICE_NAME: {
APPLICATION_CONFIGURATION_SERVICE_PROPERTY_PATTERN: config_file_patterns
}
)
addon_configs = {}
addon_configs[APPLICATION_CONFIGURATION_SERVICE_NAME] = patterns
}
return addon_configs


Expand Down
8 changes: 4 additions & 4 deletions src/spring-cloud/azext_spring_cloud/_validators_enterprise.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ def validate_memory(namespace):

def validate_git_uri(namespace):
uri = namespace.uri
if uri and (uri.startswith("https://") or uri.startswith("git@")):
return
raise CLIError("Git URI should start with \"https://\" or \"git@\"")
if uri and (not uri.startswith("https://")) and (not uri.startswith("git@")):
raise CLIError("Git URI should start with \"https://\" or \"git@\"")


def validate_config_file_patterns(namespace):
Expand All @@ -52,7 +51,8 @@ def validate_config_file_patterns(namespace):


def validate_acs_patterns(namespace):
_validate_patterns(namespace.patterns)
if namespace.patterns:
_validate_patterns(namespace.patterns)


def _validate_patterns(patterns):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,29 @@
# pylint: disable=unused-argument, logging-format-interpolation, protected-access, wrong-import-order, too-many-lines
import json

from ._enterprise import app_get_enterprise
from ._util_enterprise import (is_enterprise_tier, get_client)
from .vendored_sdks.appplatform.v2022_01_01_preview import models as models
from azure.cli.core.commands import cached_put
from azure.cli.core.commands.client_factory import get_subscription_id
from azure.cli.core.util import sdk_no_wait
from knack.log import get_logger
from knack.util import CLIError
from msrestazure.tools import resource_id

APPLICATION_CONFIGURATION_SERVICE_NAME = "ApplicationConfigurationService"
APPLICATION_CONFIGURATION_SERVICE_NAME = "applicationConfigurationService"
RESOURCE_ID = "resourceId"

RESOURCE_TYPE = "configurationServices"
DEFAULT_NAME = "default"

logger = get_logger(__name__)

def application_configuration_service_show(cmd, client, service, resource_group):
return client.configuration_services.get(resource_group, service)
return client.configuration_services.get(resource_group, service, DEFAULT_NAME)


def application_configuration_service_clear(cmd, client, service, resource_group):
properties = models.ConfigurationServiceGitProperty()
acs_resource = models.ConfigurationServiceResource(properties=properties)
return client.configuration_services.begin_create_or_update(resource_group, service, acs_resource)
return client.configuration_services.begin_create_or_update(resource_group, service, DEFAULT_NAME, acs_resource)


def application_configuration_service_git_add(cmd, client, service, resource_group,
Expand All @@ -51,7 +54,7 @@ def application_configuration_service_git_add(cmd, client, service, resource_gro
_validate_acs_settings(client, resource_group, service, acs_resource.properties.settings)

logger.warning("[2/2] Adding item to Application Configuration Service settings, (this operation can take a while to complete)")
return sdk_no_wait(no_wait, client.configuration_services.begin_create_or_update, resource_group, service, acs_resource)
return sdk_no_wait(no_wait, client.configuration_services.begin_create_or_update, resource_group, service, DEFAULT_NAME, acs_resource)


def application_configuration_service_git_update(cmd, client, service, resource_group, name,
Expand All @@ -67,30 +70,29 @@ def application_configuration_service_git_update(cmd, client, service, resource_
strict_host_key_checking=None,
no_wait=False):
acs_resource = _get_or_default_acs_resource(client, resource_group, service)

repo = _get_existing_repo(acs_resource.properties.settings.git_property.repositories, name)
repo = _replace_repo_with_input(repo, patterns, uri, label, search_paths, username, password, host_key, host_key_algorithm, private_key, strict_host_key_checking)

_validate_acs_settings(client, resource_group, service, acs_resource.properties.settings)

logger.warning("[2/2] Updating item of Application Configuration Service settings, (this operation can take a while to complete)")
return sdk_no_wait(no_wait, client.configuration_services.begin_create_or_update, resource_group, service, acs_resource)
return sdk_no_wait(no_wait, client.configuration_services.begin_create_or_update, resource_group, service, DEFAULT_NAME, acs_resource)


def application_configuration_service_git_remove(cmd, client, service, resource_group, name, no_wait=False):
acs_resource = _get_or_default_acs_resource(acs_resource)
acs_resource = _get_or_default_acs_resource(client, resource_group, service)

repo = _get_existing_repo(acs_resource.properties.settings.git_property.repositories, name)
acs_resource.properties.settings.git_property.repositories.remove(repo)

_validate_acs_settings(client, resource_group, service, acs_resource.properties.settings)

logger.warning("[2/2] Removing item of Application Configuration Service settings, (this operation can take a while to complete)")
return sdk_no_wait(no_wait, client.configuration_services.begin_create_or_update, resource_group, service, acs_resource)
return sdk_no_wait(no_wait, client.configuration_services.begin_create_or_update, resource_group, service, DEFAULT_NAME, acs_resource)


def application_configuration_service_git_list(cmd, client, service, resource_group):
acs_resource = client.configuration_services.get(resource_group, service)
acs_resource = client.configuration_services.get(resource_group, service, DEFAULT_NAME)
acs_settings = acs_resource.properties.settings

if not acs_settings or not acs_settings.git_property or not acs_settings.git_property.repositories:
Expand All @@ -111,19 +113,33 @@ def _acs_bind_or_unbind_app(cmd, client, service, resource_group, app_name, enab
app = client.apps.get(resource_group, service, app_name)
app.properties.addon_configs = _get_app_addon_configs_with_acs(app.properties.addon_configs)

if app.properties.addon_configs[APPLICATION_CONFIGURATION_SERVICE_NAME].enabled == enabled:
if (app.properties.addon_configs[APPLICATION_CONFIGURATION_SERVICE_NAME][RESOURCE_ID] != "") == enabled:
logger.warning('App "{}" has been {}binded'.format(app_name, '' if enabled else 'un'))
return

app.properties.addon_configs[APPLICATION_CONFIGURATION_SERVICE_NAME].enabled = enabled
acs_id = resource_id(
subscription=get_subscription_id(cmd.cli_ctx),
resource_group=resource_group,
namespace='Microsoft.AppPlatform',
type='Spring',
name=service,
child_type_1=RESOURCE_TYPE,
child_name_1=DEFAULT_NAME
)
if enabled:
app.properties.addon_configs[APPLICATION_CONFIGURATION_SERVICE_NAME][RESOURCE_ID] = acs_id
else:
app.properties.addon_configs[APPLICATION_CONFIGURATION_SERVICE_NAME][RESOURCE_ID] = ""
return client.apps.begin_update(resource_group, service, app_name, app)


def _get_app_addon_configs_with_acs(addon_configs):
if addon_configs is None:
addon_configs = {}
if addon_configs.get(APPLICATION_CONFIGURATION_SERVICE_NAME) is None:
addon_configs[APPLICATION_CONFIGURATION_SERVICE_NAME] = models.AddonProfile()
addon_configs[APPLICATION_CONFIGURATION_SERVICE_NAME] = {}
if addon_configs[APPLICATION_CONFIGURATION_SERVICE_NAME].get(RESOURCE_ID) is None:
addon_configs[APPLICATION_CONFIGURATION_SERVICE_NAME][RESOURCE_ID] = ""
return addon_configs


Expand Down Expand Up @@ -154,7 +170,7 @@ def _get_existing_repo(repos, name):


def _get_or_default_acs_resource(client, resource_group, service):
acs_resource = client.configuration_services.get(resource_group, service)
acs_resource = client.configuration_services.get(resource_group, service, DEFAULT_NAME)
if acs_resource is None:
acs_resource = models.ConfigurationServiceResource()
acs_resource.properties = _get_acs_properties(acs_resource.properties)
Expand Down Expand Up @@ -193,7 +209,7 @@ def _validate_acs_settings(client, resource_group, service, acs_settings):
return

try:
result = sdk_no_wait(False, client.configuration_services.begin_validate, resource_group, service, acs_settings).result()
result = sdk_no_wait(False, client.configuration_services.begin_validate, resource_group, service, DEFAULT_NAME, acs_settings).result()
except Exception as err: # pylint: disable=broad-except
raise CLIError("{0}. You may raise a support ticket if needed by the following link: https://docs.microsoft.com/azure/spring-cloud/spring-cloud-faq?pivots=programming-language-java#how-can-i-provide-feedback-and-report-issues".format(err))

Expand Down
46 changes: 34 additions & 12 deletions src/spring-cloud/azext_spring_cloud/service_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@
# --------------------------------------------------------------------------------------------

# pylint: disable=unused-argument, logging-format-interpolation, protected-access, wrong-import-order, too-many-lines
from ._enterprise import app_get_enterprise
from ._util_enterprise import (is_enterprise_tier, get_client)
from .vendored_sdks.appplatform.v2022_01_01_preview import models as models
from azure.cli.core.commands import cached_put
from azure.cli.core.util import sdk_no_wait
from azure.cli.core.commands.client_factory import get_subscription_id
from knack.log import get_logger
from knack.util import CLIError
from msrestazure.tools import resource_id

SERVICE_REGISTRY_NAME = "ServiceRegistry"
SERVICE_REGISTRY_NAME = "serviceRegistry"
RESOURCE_ID = "resourceId"

RESOURCE_TYPE = "serviceRegistries"
DEFAULT_NAME = "default"

logger = get_logger(__name__)

def service_registry_show(cmd, client, service, resource_group):
return client.service_registries.get(resource_group, service)
return client.service_registries.get(resource_group, service, DEFAULT_NAME)


def service_registry_bind(cmd, client, service, resource_group, app):
Expand All @@ -30,13 +31,34 @@ def service_registry_unbind(cmd, client, service, resource_group, app):

def _service_registry_bind_or_unbind_app(cmd, client, service, resource_group, app_name, enabled):
app = client.apps.get(resource_group, service, app_name)
app.properties.addon_configs = {
SERVICE_REGISTRY_NAME: models.AddonProfile()
} if app.properties.addon_configs is None else app.properties.addon_configs
app.properties.addon_configs = _get_app_addon_configs_with_service_registry(app.properties.addon_configs)

if app.properties.addon_configs[SERVICE_REGISTRY_NAME].enabled == enabled:
if (app.properties.addon_configs[SERVICE_REGISTRY_NAME][RESOURCE_ID] != "") == enabled:
logger.warning('App "{}" has been {}binded'.format(app_name, '' if enabled else 'un'))
return

app.properties.addon_configs[SERVICE_REGISTRY_NAME].enabled = enabled
service_registry_id = resource_id(
subscription=get_subscription_id(cmd.cli_ctx),
resource_group=resource_group,
namespace='Microsoft.AppPlatform',
type='Spring',
name=service,
child_type_1=RESOURCE_TYPE,
child_name_1=DEFAULT_NAME
)

if enabled:
app.properties.addon_configs[SERVICE_REGISTRY_NAME][RESOURCE_ID] = service_registry_id
else:
app.properties.addon_configs[SERVICE_REGISTRY_NAME][RESOURCE_ID] = ""
return client.apps.begin_update(resource_group, service, app_name, app)


def _get_app_addon_configs_with_service_registry(addon_configs):
if addon_configs is None:
addon_configs = {}
if addon_configs.get(SERVICE_REGISTRY_NAME) is None:
addon_configs[SERVICE_REGISTRY_NAME] = {}
if addon_configs[SERVICE_REGISTRY_NAME].get(RESOURCE_ID) is None:
addon_configs[SERVICE_REGISTRY_NAME][RESOURCE_ID] = ""
return addon_configs