Skip to content

Commit

Permalink
Show commit id when get configuration files from ACS (#7444)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiec-msft authored Apr 28, 2024
1 parent ec44149 commit bced6ce
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/spring/HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Release History
===============
1.23.0
---
* For Application Configuration Service Version Gen2, show the Git revisions in command `az spring application-configuration-service config show`.

1.22.0
---
* Add argument `--custom-actuator-port` in `spring app update`, `spring app deploy` and `spring app deployment create` to support custom actuator port.
Expand Down
28 changes: 26 additions & 2 deletions src/spring/azext_spring/application_configuration_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
DEFAULT_NAME = "default"

CONFIGURATION_FILES = "configurationFiles"
METADATA = "metadata"
GIT_REVISIONS = "gitRevisions"

logger = get_logger(__name__)

Expand Down Expand Up @@ -351,7 +353,10 @@ def _split_config_lines(response_json):
]
}
"""
configuration_files = response_json[CONFIGURATION_FILES]
configuration_files = response_json.get(CONFIGURATION_FILES, None)

if configuration_files is None:
raise CLIError("Failed to parse configuration files response {}".format(response_json))

filename_to_multi_line_configs_dict = {}

Expand All @@ -365,10 +370,29 @@ def _split_config_lines(response_json):
if len(filename_to_multi_line_configs_dict) == 0:
raise CLIError("No configuration files found.")

return {
configuration_files_result = {
CONFIGURATION_FILES: filename_to_multi_line_configs_dict
}

_safe_append_git_revisions(configuration_files_result, response_json)

return configuration_files_result


def _safe_append_git_revisions(configuration_files_result, response_json):
try:
metadata = response_json.get(METADATA, None)
if (metadata is None) or (not isinstance(metadata, dict)):
return
git_revisions = metadata.get(GIT_REVISIONS, None)
if git_revisions:
configuration_files_result[METADATA] = {
GIT_REVISIONS: git_revisions
}
except Exception as e:
logger.debug("Failed to append Git revisions.", e)
pass


def _export_configs_to_files(response_json, folder_path):
absolute_folder_path = os.path.abspath(folder_path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
from ...application_configuration_service import (application_configuration_service_create,
application_configuration_service_update)

import json
import unittest
from argparse import Namespace

from azure.cli.core.azclierror import InvalidArgumentValueError, ArgumentUsageError

from .common.test_utils import get_test_cmd
from ..._validators_enterprise import validate_refresh_interval
from ...application_configuration_service import (application_configuration_service_create, _split_config_lines)

try:
import unittest.mock as mock
except ImportError:
from unittest import mock

from azure.cli.core.mock import DummyCli
from azure.cli.core import AzCommandsLoader
from azure.cli.core.commands import AzCliCommand

from knack.log import get_logger

logger = get_logger(__name__)
Expand Down Expand Up @@ -79,3 +77,81 @@ def test_validate_refresh_interval_parameter(self):
with self.assertRaises(ArgumentUsageError) as context:
validate_refresh_interval(ns)
self.assertEqual("--refresh-interval must be greater than or equal to 0.", str(context.exception))


class TestAcsShowConfigurationFiles(unittest.TestCase):
def test_show_configuration_files_for_acs_gen_1(self):
response_str = r"""
{
"configurationFiles": {
"application.properties": "properties-name: sample-test\nspring.config.activate.on-profile: default"
}
}
"""
result = _split_config_lines(json.loads(response_str))
expected_file_content = r"properties-name: sample-test\nspring.config.activate.on-profile: default"
file_content_arr = result.get("configurationFiles").get("application.properties")
self.assertTrue(isinstance(file_content_arr, list))
self.assertEquals(len(file_content_arr), 2)
self.assertTrue(r"properties-name: sample-test" in file_content_arr)
self.assertTrue(r"spring.config.activate.on-profile: default" in file_content_arr)

def test_show_configuration_files_for_acs_gen_2(self):
response_str = r"""
{
"configurationFiles": {
"application.properties": "properties-name: sample-test\nspring.config.activate.on-profile: default"
}
}
"""
result = _split_config_lines(json.loads(response_str))
expected_file_content = r"properties-name: sample-test\nspring.config.activate.on-profile: default"
file_content_arr = result.get("configurationFiles").get("application.properties")
self.assertTrue(isinstance(file_content_arr, list))
self.assertEquals(len(file_content_arr), 2)
self.assertTrue(r"properties-name: sample-test" in file_content_arr)
self.assertTrue(r"spring.config.activate.on-profile: default" in file_content_arr)
self.assertIsNone(result.get("metadata"))

def test_show_configuration_files_for_acs_gen_2_with_git_revision(self):
response_str = r'''
{
"configurationFiles": {
"application.properties": "properties-name: sample-test\nspring.config.activate.on-profile: default"
},
"metadata": {
"gitRevisions": "[{\"url\": \"https://sample.url\", \"revision\": \"main@sha1:sample-commit-id\"}]"
}
}
'''

result = _split_config_lines(json.loads(response_str))
expected_file_content = r"properties-name: sample-test\nspring.config.activate.on-profile: default"
file_content_arr = result.get("configurationFiles").get("application.properties")
self.assertTrue(isinstance(file_content_arr, list))
self.assertEquals(len(file_content_arr), 2)
self.assertTrue(r"properties-name: sample-test" in file_content_arr)
self.assertTrue(r"spring.config.activate.on-profile: default" in file_content_arr)
self.assertIsNotNone(result.get("metadata"))
metadata = result.get("metadata")
expected_revisions = "[{\"url\": \"https://sample.url\", \"revision\": \"main@sha1:sample-commit-id\"}]"
self.assertEquals(metadata.get("gitRevisions"), expected_revisions)

def test_show_configuration_files_for_acs_gen_2_with_null_metadata(self):
response_str = r'''
{
"configurationFiles": {
"application.properties": "properties-name: sample-test\nspring.config.activate.on-profile: default"
},
"metadata": null
}
'''

result = _split_config_lines(json.loads(response_str))
expected_file_content = r"properties-name: sample-test\nspring.config.activate.on-profile: default"
file_content_arr = result.get("configurationFiles").get("application.properties")
self.assertTrue(isinstance(file_content_arr, list))
self.assertEquals(len(file_content_arr), 2)
self.assertTrue(r"properties-name: sample-test" in file_content_arr)
self.assertTrue(r"spring.config.activate.on-profile: default" in file_content_arr)
self.assertIsNone(result.get("metadata"))
2 changes: 1 addition & 1 deletion src/spring/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

# TODO: Confirm this is the right version number you want and it matches your
# HISTORY.rst entry.
VERSION = '1.22.0'
VERSION = '1.23.0'

# The full list of classifiers is available at
# https://pypi.python.org/pypi?%3Aaction=list_classifiers
Expand Down

0 comments on commit bced6ce

Please sign in to comment.