Path: blob/develop/awscli/customizations/ecs/filehelpers.py
1567 views
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.1213import json14import yaml1516from awscli.customizations.ecs import exceptions1718MAX_CHAR_LENGTH = 4619APP_PREFIX = 'AppECS-'20DGP_PREFIX = 'DgpECS-'212223def find_required_key(resource_name, obj, key):2425if obj is None:26raise exceptions.MissingPropertyError(27resource=resource_name, prop_name=key)2829result = _get_case_insensitive_key(obj, key)3031if result is None:32raise exceptions.MissingPropertyError(33resource=resource_name, prop_name=key)34else:35return result363738def _get_case_insensitive_key(target_obj, target_key):39key_to_match = target_key.lower()40key_list = target_obj.keys()4142for key in key_list:43if key.lower() == key_to_match:44return key454647def get_app_name(service, cluster, app_value):48if app_value is not None:49return app_value50else:51suffix = _get_ecs_suffix(service, cluster)52return APP_PREFIX + suffix535455def get_cluster_name_from_arn(arn):56return arn.split('/')[1]575859def get_deploy_group_name(service, cluster, dg_value):60if dg_value is not None:61return dg_value62else:63suffix = _get_ecs_suffix(service, cluster)64return DGP_PREFIX + suffix656667def _get_ecs_suffix(service, cluster):68if cluster is None:69cluster_name = 'default'70else:71cluster_name = cluster[:MAX_CHAR_LENGTH]7273return cluster_name + '-' + service[:MAX_CHAR_LENGTH]747576def parse_appspec(appspec_str):77try:78return json.loads(appspec_str)79except ValueError:80return yaml.safe_load(appspec_str)818283