Skip to content

Commit

Permalink
Ignore resources when disabling subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Iain-S committed Sep 7, 2023
1 parent db36754 commit e6f9b7a
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions controller_function/controller/subscription.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,45 @@
"""Manage subscription life cycle."""
import logging
import types
from uuid import UUID

from azure.core import exceptions as azure_exceptions
from azure.identity import DefaultAzureCredential
from azure.mgmt.subscription import SubscriptionClient

# pylint: disable=too-many-arguments

CREDENTIALS = DefaultAzureCredential()

SUBSCRIPTION_CLIENT = SubscriptionClient(credential=CREDENTIALS)


def new_post(
self,
url,
params=None,
headers=None,
content=None,
form_content=None,
stream_content=None,
):
"""Add IgnoreResourceCheck=True to the existing query params.
See also https://github.com/Azure/azure-sdk-for-python/issues/10814
"""
if url.endswith("cancel"):
params = {**(params or {}), **{"IgnoreResourceCheck": True}}

return self.original_post_method(
url,
params=params,
headers=headers,
content=content,
form_content=form_content,
stream_content=stream_content,
)


def enable_subscription(subscription_id: UUID) -> None:
"""Enable a subscription.
Expand Down Expand Up @@ -53,3 +82,16 @@ def disable_subscription(subscription_id: UUID) -> None:
)
else:
raise e


# Patch the original post method with our own so that we can add
# IgnoreResourceCheck=True to the query params. Without this,
# subscriptions with resources will not be disabled.
# pylint: disable=protected-access
SUBSCRIPTION_CLIENT.subscription._client.original_post_method = (
SUBSCRIPTION_CLIENT.subscription._client.post
)
SUBSCRIPTION_CLIENT.subscription._client.post = types.MethodType(
new_post, SUBSCRIPTION_CLIENT.subscription._client
)
# pylint: enable=protected-access

0 comments on commit e6f9b7a

Please sign in to comment.