Skip to content

Commit

Permalink
Fixed style issues, various bug fixes (Azure#27)
Browse files Browse the repository at this point in the history
* Moved dapr arguments to env as a subgroup.

* Added env variable options.

* Changed revision mode set to revision set-mode.

* Added env var options to revision copy.

* Fixed revision copy bug related to env secret refs.

* Changed registry and secret delete to remove. Added registry param helps. Removed replica from table output and added trafficWeight.

* Updating warning text.

* Updated warning text once more.

* Made name optional for revision copy if from-revision flag is passed.

* Fixed whitespace style issues.

* Styled clients and utils to pass pylint.

* Finished client.py pylint fixes.

* Fixed pylint issues.

* Fixed flake8 commands and custom.

* Fixed flake issues in src.

* Added license header to _sdk_models.

* Added confirmation for containerapp delete.

Co-authored-by: Haroon Feisal <haroonfeisal@microsoft.com>
  • Loading branch information
2 people authored and calvinsID committed Mar 22, 2022
1 parent ddc07c0 commit 5fe8e5f
Show file tree
Hide file tree
Showing 12 changed files with 383 additions and 373 deletions.
3 changes: 2 additions & 1 deletion src/containerapp/azext_containerapp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
# pylint: disable=super-with-arguments

from azure.cli.core import AzCommandsLoader

Expand All @@ -16,7 +17,7 @@ def __init__(self, cli_ctx=None):
operations_tmpl='azext_containerapp.custom#{}',
client_factory=None)
super(ContainerappCommandsLoader, self).__init__(cli_ctx=cli_ctx,
custom_command_type=containerapp_custom)
custom_command_type=containerapp_custom)

def load_command_table(self, args):
from azext_containerapp.commands import load_command_table
Expand Down
4 changes: 3 additions & 1 deletion src/containerapp/azext_containerapp/_client_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
# pylint: disable=line-too-long, consider-using-f-string

from azure.cli.core.commands.client_factory import get_mgmt_service_client
from azure.cli.core.profiles import ResourceType
Expand All @@ -13,7 +14,6 @@
def ex_handler_factory(no_throw=False):
def _polish_bad_errors(ex):
import json
from knack.util import CLIError
try:
content = json.loads(ex.response.content)
if 'message' in content:
Expand Down Expand Up @@ -63,11 +63,13 @@ def cf_resource_groups(cli_ctx, subscription_id=None):
return get_mgmt_service_client(cli_ctx, ResourceType.MGMT_RESOURCE_RESOURCES,
subscription_id=subscription_id).resource_groups


def log_analytics_client_factory(cli_ctx):
from azure.mgmt.loganalytics import LogAnalyticsManagementClient

return get_mgmt_service_client(cli_ctx, LogAnalyticsManagementClient).workspaces


def log_analytics_shared_key_client_factory(cli_ctx):
from azure.mgmt.loganalytics import LogAnalyticsManagementClient

Expand Down
33 changes: 15 additions & 18 deletions src/containerapp/azext_containerapp/_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
# pylint: disable=line-too-long, super-with-arguments, too-many-instance-attributes, consider-using-f-string, no-else-return, no-self-use

import json
import time
import sys

from sys import api_version
from azure.cli.core.util import send_raw_request
from azure.cli.core.commands.client_factory import get_subscription_id
from knack.log import get_logger
Expand All @@ -15,8 +16,8 @@

API_VERSION = "2021-03-01"
NEW_API_VERSION = "2022-01-01-preview"
POLLING_TIMEOUT = 60 # how many seconds before exiting
POLLING_SECONDS = 2 # how many seconds between requests
POLLING_TIMEOUT = 60 # how many seconds before exiting
POLLING_SECONDS = 2 # how many seconds between requests


class PollingAnimation():
Expand All @@ -37,7 +38,7 @@ def flush(self):
sys.stdout.write("\033[K")


def poll(cmd, request_url, poll_if_status):
def poll(cmd, request_url, poll_if_status): # pylint: disable=inconsistent-return-statements
try:
start = time.time()
end = time.time() + POLLING_TIMEOUT
Expand All @@ -53,19 +54,17 @@ def poll(cmd, request_url, poll_if_status):
r = send_raw_request(cmd.cli_ctx, "GET", request_url)
r2 = r.json()

if not "properties" in r2 or not "provisioningState" in r2["properties"] or not r2["properties"]["provisioningState"].lower() == poll_if_status:
if "properties" not in r2 or "provisioningState" not in r2["properties"] or not r2["properties"]["provisioningState"].lower() == poll_if_status:
break
start = time.time()

animation.flush()
return r.json()
except Exception as e:
except Exception as e: # pylint: disable=broad-except
animation.flush()

if poll_if_status == "scheduledfordelete": # Catch "not found" errors if polling for delete
return

raise e
if not poll_if_status == "scheduledfordelete": # Catch "not found" errors if polling for delete
raise e


class ContainerAppClient():
Expand Down Expand Up @@ -144,7 +143,6 @@ def delete(cls, cmd, resource_group_name, name):

if r.status_code == 202:
logger.warning('Containerapp successfully deleted')
return

@classmethod
def show(cls, cmd, resource_group_name, name):
Expand Down Expand Up @@ -222,7 +220,6 @@ def list_by_resource_group(cls, cmd, resource_group_name, formatter=lambda x: x)

@classmethod
def list_secrets(cls, cmd, resource_group_name, name):
secrets = []

management_hostname = cmd.cli_ctx.cloud.endpoints.resource_manager
api_version = NEW_API_VERSION
Expand Down Expand Up @@ -338,6 +335,7 @@ def deactivate_revision(cls, cmd, resource_group_name, container_app_name, name)
r = send_raw_request(cmd.cli_ctx, "POST", request_url)
return r.json()


class ManagedEnvironmentClient():
@classmethod
def create(cls, cmd, resource_group_name, name, managed_environment_envelope, no_wait=False):
Expand Down Expand Up @@ -413,7 +411,7 @@ def delete(cls, cmd, resource_group_name, name, no_wait=False):
r = send_raw_request(cmd.cli_ctx, "DELETE", request_url)

if no_wait:
return # API doesn't return JSON (it returns no content)
return # API doesn't return JSON (it returns no content)
elif r.status_code in [200, 201, 202, 204]:
url_fmt = "{}/subscriptions/{}/resourceGroups/{}/providers/Microsoft.App/managedEnvironments/{}?api-version={}"
request_url = url_fmt.format(
Expand Down Expand Up @@ -506,6 +504,7 @@ def list_by_resource_group(cls, cmd, resource_group_name, formatter=lambda x: x)

return env_list


class GitHubActionClient():
@classmethod
def create_or_update(cls, cmd, resource_group_name, name, github_action_envelope, headers, no_wait=False):
Expand Down Expand Up @@ -552,7 +551,6 @@ def show(cls, cmd, resource_group_name, name):
r = send_raw_request(cmd.cli_ctx, "GET", request_url)
return r.json()

#TODO
@classmethod
def delete(cls, cmd, resource_group_name, name, headers, no_wait=False):
management_hostname = cmd.cli_ctx.cloud.endpoints.resource_manager
Expand All @@ -569,7 +567,7 @@ def delete(cls, cmd, resource_group_name, name, headers, no_wait=False):
r = send_raw_request(cmd.cli_ctx, "DELETE", request_url, headers=headers)

if no_wait:
return # API doesn't return JSON (it returns no content)
return # API doesn't return JSON (it returns no content)
elif r.status_code in [200, 201, 202, 204]:
url_fmt = "{}/subscriptions/{}/resourceGroups/{}/providers/Microsoft.App/containerApps/{}/sourcecontrols/current?api-version={}"
request_url = url_fmt.format(
Expand All @@ -588,10 +586,10 @@ def delete(cls, cmd, resource_group_name, name, headers, no_wait=False):
logger.warning('Containerapp github action successfully deleted')
return


class DaprComponentClient():
@classmethod
def create_or_update(cls, cmd, resource_group_name, environment_name, name, dapr_component_envelope, no_wait=False):
#create_or_update.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.App/managedEnvironments/{environmentName}/daprComponents/{name}'}

management_hostname = cmd.cli_ctx.cloud.endpoints.resource_manager
api_version = NEW_API_VERSION
Expand Down Expand Up @@ -639,7 +637,7 @@ def delete(cls, cmd, resource_group_name, environment_name, name, no_wait=False)
r = send_raw_request(cmd.cli_ctx, "DELETE", request_url)

if no_wait:
return # API doesn't return JSON (it returns no content)
return # API doesn't return JSON (it returns no content)
elif r.status_code in [200, 201, 202, 204]:
url_fmt = "{}/subscriptions/{}/resourceGroups/{}/providers/Microsoft.App/managedEnvironments/{}/daprComponents/{}?api-version={}"
request_url = url_fmt.format(
Expand Down Expand Up @@ -705,4 +703,3 @@ def list(cls, cmd, resource_group_name, environment_name, formatter=lambda x: x)
app_list.append(formatted)

return app_list

6 changes: 4 additions & 2 deletions src/containerapp/azext_containerapp/_github_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
# pylint: disable=consider-using-f-string

from azure.cli.core.azclierror import (ValidationError, CLIInternalError, UnclassifiedUserFault)
from knack.log import get_logger
Expand All @@ -22,6 +23,7 @@
"workflow"
]


def get_github_access_token(cmd, scope_list=None): # pylint: disable=unused-argument
if scope_list:
for scope in scope_list:
Expand Down Expand Up @@ -81,6 +83,6 @@ def get_github_access_token(cmd, scope_list=None): # pylint: disable=unused-arg
return parsed_confirmation_response['access_token'][0]
except Exception as e:
raise CLIInternalError(
'Error: {}. Please try again, or retrieve personal access token from the Github website'.format(e))
'Error: {}. Please try again, or retrieve personal access token from the Github website'.format(e)) from e

raise UnclassifiedUserFault('Activation did not happen in time. Please try again')
raise UnclassifiedUserFault('Activation did not happen in time. Please try again')
11 changes: 5 additions & 6 deletions src/containerapp/azext_containerapp/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
examples:
- name: Set a container app to single revision mode.
text: |
az containerapp revision set-mode -n MyContainerapp -g MyResourceGroup --mode Single
az containerapp revision set-mode -n MyContainerapp -g MyResourceGroup --mode Single
"""

helps['containerapp revision copy'] = """
Expand Down Expand Up @@ -368,7 +368,7 @@
examples:
- name: Show a container app's ingress traffic configuration.
text: |
az containerapp ingress traffic show -n MyContainerapp -g MyResourceGroup
az containerapp ingress traffic show -n MyContainerapp -g MyResourceGroup
"""

# Registry Commands
Expand All @@ -392,7 +392,7 @@
examples:
- name: List container registries configured in a container app.
text: |
az containerapp registry list -n MyContainerapp -g MyResourceGroup
az containerapp registry list -n MyContainerapp -g MyResourceGroup
"""

helps['containerapp registry set'] = """
Expand All @@ -403,7 +403,6 @@
text: |
az containerapp registry set -n MyContainerapp -g MyResourceGroup \\
--server MyExistingContainerappRegistry.azurecr.io --username MyRegistryUsername --password MyRegistryPassword
"""

helps['containerapp registry remove'] = """
Expand Down Expand Up @@ -454,10 +453,10 @@
examples:
- name: Add secrets to a container app.
text: |
az containerapp secret set -n MyContainerapp -g MyResourceGroup --secrets MySecretName1=MySecretValue1 MySecretName2=MySecretValue2
az containerapp secret set -n MyContainerapp -g MyResourceGroup --secrets MySecretName1=MySecretValue1 MySecretName2=MySecretValue2
- name: Update a secret.
text: |
az containerapp secret set -n MyContainerapp -g MyResourceGroup --secrets MyExistingSecretName=MyNewSecretValue
az containerapp secret set -n MyContainerapp -g MyResourceGroup --secrets MyExistingSecretName=MyNewSecretValue
"""

helps['containerapp github-action'] = """
Expand Down
Loading

0 comments on commit 5fe8e5f

Please sign in to comment.