|
| 1 | +# -------------------------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | +# -------------------------------------------------------------------------------------------- |
| 5 | +from knack.log import get_logger |
| 6 | +from .base_converter import BaseConverter |
| 7 | + |
| 8 | +logger = get_logger(__name__) |
| 9 | + |
| 10 | + |
| 11 | +# Concrete Converter Subclass for Config Server |
| 12 | +class ACSConverter(BaseConverter): |
| 13 | + |
| 14 | + CONFIGURATION_KEY_PREFIX = "spring.cloud.config.server.git" |
| 15 | + KEY_URI = ".uri" |
| 16 | + KEY_LABEL = ".default-label" |
| 17 | + KEY_SEARCH_PATHS = ".search-paths" |
| 18 | + KEY_USERNAME = ".username" |
| 19 | + KEY_PASSWORD = ".password" |
| 20 | + KEY_PRIVATE_KEY = ".private-key" |
| 21 | + KEY_HOST_KEY = ".host-key" |
| 22 | + KEY_HOST_KEY_ALGORITHM = ".host-key-algorithm" |
| 23 | + KEY_PATTERN = ".pattern" |
| 24 | + |
| 25 | + def __init__(self, source): |
| 26 | + def transform_data(): |
| 27 | + if self.wrapper_data.is_support_ossconfigserver() is False and self.wrapper_data.is_support_acs(): |
| 28 | + acs = self.wrapper_data.get_resources_by_type('Microsoft.AppPlatform/Spring/configurationServices')[0] |
| 29 | + name = "config" |
| 30 | + configurations, params = self._get_configurations_and_params(acs) |
| 31 | + replicas = 2 |
| 32 | + return { |
| 33 | + "configServerName": name, |
| 34 | + "params": params, |
| 35 | + "configurations": configurations, |
| 36 | + "replicas": replicas |
| 37 | + } |
| 38 | + else: |
| 39 | + return None |
| 40 | + super().__init__(source, transform_data) |
| 41 | + |
| 42 | + def get_template_name(self): |
| 43 | + return "config_server.bicep" |
| 44 | + |
| 45 | + def _get_configurations_and_params(self, acs): |
| 46 | + configurations = [] |
| 47 | + params = [] |
| 48 | + |
| 49 | + git_repos = acs.get('properties', {}).get('settings', {}).get('gitProperty', {}).get('repositories', None) |
| 50 | + if git_repos is not None and len(git_repos) > 0: |
| 51 | + default_repo = git_repos[0] |
| 52 | + self._add_property_if_exists(configurations, self.CONFIGURATION_KEY_PREFIX + self.KEY_URI, default_repo.get('uri')) |
| 53 | + self._add_property_if_exists(configurations, self.CONFIGURATION_KEY_PREFIX + self.KEY_LABEL, default_repo.get('label')) |
| 54 | + self._add_property_if_exists(configurations, self.CONFIGURATION_KEY_PREFIX + self.KEY_SEARCH_PATHS, default_repo.get('searchPaths')) |
| 55 | + self._add_secret_config(self.CONFIGURATION_KEY_PREFIX + self.KEY_USERNAME, default_repo.get('username'), configurations, params) |
| 56 | + self._add_secret_config(self.CONFIGURATION_KEY_PREFIX + self.KEY_PASSWORD, default_repo.get('password'), configurations, params) |
| 57 | + self._add_secret_config(self.CONFIGURATION_KEY_PREFIX + self.KEY_PRIVATE_KEY, default_repo.get('privateKey'), configurations, params) |
| 58 | + self._add_secret_config(self.CONFIGURATION_KEY_PREFIX + self.KEY_HOST_KEY, default_repo.get('hostKey'), configurations, params) |
| 59 | + self._add_secret_config(self.CONFIGURATION_KEY_PREFIX + self.KEY_HOST_KEY_ALGORITHM, default_repo.get('hostKeyAlgorithm'), configurations, params) |
| 60 | + self._check_patterns(default_repo) |
| 61 | + |
| 62 | + for i in range(1, len(git_repos)): |
| 63 | + repo = git_repos[i] |
| 64 | + configuration_key_repo_prefix = self.CONFIGURATION_KEY_PREFIX + ".repos." + repo['name'] |
| 65 | + self._add_property_if_exists(configurations, configuration_key_repo_prefix + self.KEY_URI, repo.get('uri')) |
| 66 | + self._add_property_if_exists(configurations, configuration_key_repo_prefix + self.KEY_LABEL, repo.get('label')) |
| 67 | + self._add_property_if_exists(configurations, configuration_key_repo_prefix + self.KEY_SEARCH_PATHS, repo.get('searchPaths')) |
| 68 | + self._add_secret_config(configuration_key_repo_prefix + self.KEY_USERNAME, repo.get('username'), configurations, params) |
| 69 | + self._add_secret_config(configuration_key_repo_prefix + self.KEY_PASSWORD, repo.get('password'), configurations, params) |
| 70 | + self._add_secret_config(configuration_key_repo_prefix + self.KEY_PRIVATE_KEY, repo.get('privateKey'), configurations, params) |
| 71 | + self._add_secret_config(configuration_key_repo_prefix + self.KEY_HOST_KEY, repo.get('hostKey'), configurations, params) |
| 72 | + self._add_secret_config(configuration_key_repo_prefix + self.KEY_HOST_KEY_ALGORITHM, repo.get('hostKeyAlgorithm'), configurations, params) |
| 73 | + self._check_patterns(repo) |
| 74 | + |
| 75 | + return configurations, params |
| 76 | + |
| 77 | + def _add_property_if_exists(self, configurations, key, value): |
| 78 | + if value: |
| 79 | + if isinstance(value, (list, tuple)): |
| 80 | + value = ",".join(map(str, value)) |
| 81 | + configurations.append({ |
| 82 | + "propertyName": key, |
| 83 | + "value": value |
| 84 | + }) |
| 85 | + |
| 86 | + def _add_secret_config(self, key, value, configurations, params): |
| 87 | + if value: |
| 88 | + param_name = key.replace(".", "_").replace("-", "_") |
| 89 | + self._add_property_if_exists(configurations, key, param_name) |
| 90 | + params.append(param_name) |
| 91 | + |
| 92 | + def _check_patterns(self, repo): |
| 93 | + patterns = repo.get('patterns', []) |
| 94 | + if len(patterns) > 0: |
| 95 | + pattern_str = ",".join(map(str, patterns)) |
| 96 | + logger.info(f"The patterns '{pattern_str}' of the git repository '{repo.get('name')}' in Application Configuration Service not need in Config Server of Azure Container Apps.") |
0 commit comments