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 6import time 7from knack.util import CLIError 8from knack.log import get_logger 9 10from azure.cli.core.azclierror import (RequiredArgumentMissingError) 11 12from ._client_factory import web_client_factory 13 14logger = get_logger(__name__) 15 16 17def str2bool(v): 18 if v == 'true': 19 retval = True 20 elif v == 'false': 21 retval = False 22 else: 23 retval = None 24 return retval 25 26 27def _normalize_sku(sku): 28 sku = sku.upper() 29 if sku == 'FREE': 30 return 'F1' 31 if sku == 'SHARED': 32 return 'D1' 33 return sku 34 35 36def get_sku_name(tier): # pylint: disable=too-many-return-statements 37 tier = tier.upper() 38 if tier in ['F1', 'FREE']: 39 return 'FREE' 40 if tier in ['D1', "SHARED"]: 41 return 'SHARED' 42 if tier in ['B1', 'B2', 'B3', 'BASIC']: 43 return 'BASIC' 44 if tier in ['S1', 'S2', 'S3']: 45 return 'STANDARD' 46 if tier in ['P1', 'P2', 'P3']: 47 return 'PREMIUM' 48 if tier in ['P1V2', 'P2V2', 'P3V2']: 49 return 'PREMIUMV2' 50 if tier in ['P1V3', 'P2V3', 'P3V3']: 51 return 'PREMIUMV3' 52 if tier in ['PC2', 'PC3', 'PC4']: 53 return 'PremiumContainer' 54 if tier in ['EP1', 'EP2', 'EP3']: 55 return 'ElasticPremium' 56 if tier in ['I1', 'I2', 'I3']: 57 return 'Isolated' 58 if tier in ['I1V2', 'I2V2', 'I3V2']: 59 return 'IsolatedV2' 60 raise CLIError("Invalid sku(pricing tier), please refer to command help for valid values") 61 62 63def normalize_sku_for_staticapp(sku): 64 if sku.lower() == 'free': 65 return 'Free' 66 if sku.lower() == 'standard': 67 return 'Standard' 68 raise CLIError("Invalid sku(pricing tier), please refer to command help for valid values") 69 70 71def retryable_method(retries=3, interval_sec=5, excpt_type=Exception): 72 def decorate(func): 73 def call(*args, **kwargs): 74 current_retry = retries 75 while True: 76 try: 77 return func(*args, **kwargs) 78 except excpt_type as exception: # pylint: disable=broad-except 79 current_retry -= 1 80 if current_retry <= 0: 81 raise exception 82 time.sleep(interval_sec) 83 return call 84 return decorate 85 86 87def raise_missing_token_suggestion(): 88 pat_documentation = "https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line" 89 raise RequiredArgumentMissingError("GitHub access token is required to authenticate to your repositories. " 90 "If you need to create a Github Personal Access Token, " 91 "please run with the '--login-with-github' flag or follow " 92 "the steps found at the following link:\n{0}".format(pat_documentation)) 93 94 95def _get_location_from_resource_group(cli_ctx, resource_group_name): 96 from azure.cli.core.commands.client_factory import get_mgmt_service_client 97 from azure.cli.core.profiles import ResourceType 98 client = get_mgmt_service_client(cli_ctx, ResourceType.MGMT_RESOURCE_RESOURCES) 99 group = client.resource_groups.get(resource_group_name) 100 return group.location 101 102 103def _list_app(cli_ctx, resource_group_name=None): 104 client = web_client_factory(cli_ctx) 105 if resource_group_name: 106 result = list(client.web_apps.list_by_resource_group(resource_group_name)) 107 else: 108 result = list(client.web_apps.list()) 109 for webapp in result: 110 _rename_server_farm_props(webapp) 111 return result 112 113 114def _rename_server_farm_props(webapp): 115 # Should be renamed in SDK in a future release 116 setattr(webapp, 'app_service_plan_id', webapp.server_farm_id) 117 del webapp.server_farm_id 118 return webapp 119 120 121def _get_location_from_webapp(client, resource_group_name, webapp): 122 webapp = client.web_apps.get(resource_group_name, webapp) 123 if not webapp: 124 raise CLIError("'{}' app doesn't exist".format(webapp)) 125 return webapp.location 126