azure-sdk-for-python icon indicating copy to clipboard operation
azure-sdk-for-python copied to clipboard

Force cancellation of Azure subscription even if it has existing resources

Open szaher opened this issue 6 years ago • 20 comments

  • Package Name: azure-mgmt-subscription
  • Package Version: 0.5.0
  • Operating System: Ubuntu 18.04 LTS
  • Python Version: python 3.6.9

Describe the bug When cancelling Azure subscription the request fails if the subscription has resources. There should be a flag or something to force cancellation of subscription even if it has resources ( as it's the case in the portal)

To Reproduce Steps to reproduce the behavior:

  1. create an azure subscription from library or EA portal
  2. try to cancel the subscription using the azure-mgmt-subscription library

Expected behavior When cancelling the subscription; there should be a flag or header to ignore existing resources within this subscription.

Screenshots If applicable, add screenshots to help explain your problem.

Additional context

from azure.mgmt import subscription
from azure.common import credentials
sub_client = subscription.SubscriptionClient(creds)
sub_client.subscriptions.cancel(subscription_id=subscription_id,
                                                        custom_headers={"Cache-Control": "no-cache"})
there should be something like 
sub_client.subscriptions.cancel(subscription_id=subscription_id, ignore_existing_resources=True,
                                                        custom_headers={"Cache-Control": "no-cache"})

szaher avatar Apr 13 '20 20:04 szaher

@szaher thanks for the feedback, @nickzhums could you take a look at this?

kaerm avatar Apr 13 '20 23:04 kaerm

assign to Zim to check why the flag is not available in sdk while it's supported in portal. thanks.

RodgeFu avatar Apr 17 '20 04:04 RodgeFu

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @anuragdalmia, @shilpigautam, @ramaganesan-rg.

ghost avatar Apr 19 '20 13:04 ghost

Investigating

anuragdalmia avatar Jun 04 '20 19:06 anuragdalmia

This behavior is currently by design. We explicitly did not enable this to prevent accidental cancellations of subscriptions with active resources that could impact prod services. If you would like to cancel subscriptions, you should be able to delete all resources and then cancel the subscriptions via the APIs. The reason we support this in UX and not in API is that the UX has enough warnings and functionality to review what is being deleted which will not be the case via API.

I will be closing this for now. Please re-open if you disagree and share the functionality that doesnt get addressed by existing APIs.

anuragdalmia avatar Jun 04 '20 22:06 anuragdalmia

@anuragdalmia I totally disagree! It is very important to me, to disable a Subscription with REST. I cannot understand the reasoning that this step is automatically too dangerous. A canceled subscription is "read-only", nothing is deleted. It can be restored within 30 days. I can also delete a resource group with REST or Poweshell. Please include the possibility to deactivate the subscription programmatically, even if there are resources in it!

Gill-Bates avatar Dec 01 '20 10:12 Gill-Bates

@Gill-Bates apologies for the delay. Can you help me understand your specific scenario where you need to do this. This was a supported functionality before but we have had production systems come down for customers when a bug via APIs has brought down their entire production service. Hence as feedback from other customers, this is something that we explicitly blocked after assessing the risk/reward for this feature. So would love to understand your scenario in detail especially if this is an operation you plan on running frequently.

anuragdalmia avatar Dec 24 '20 17:12 anuragdalmia

Hello,

@anuragdalmia We would also like to have a force setting. We have an internal type of Subscription called dev-sandbox. There are pre-defined budgets for such subscriptions. It is very-very important to us to stay within those budget boundaries what we set. We would like to use the REST API to cancel a subscription once the budget is reached in an automated way. This would enable us to have an automation which could provide the end users the following features & functionalities:

  • Form based automation to request a budget increase (with auto enable for the sub)
  • Automation to cancel a sub when a budget is reached
  • Automation to enable a sub at the beginning of the month (when the budget period resets)
  • Keep the resources intact and be able to continue the work after enabling the sub

I don't really see a reason why you could not implement a simple additional property in the POST with the necessary documentation (including warnings).

Also I see another future scenario: Since we already support internally to request subscriptions on-demand I think we will reach the maturity sooner or later to have a need for the opposite and to automate the cancellation as well for subscriptions which end users are not needing anymore. The real benefit of the 90 days period coming with the cancellation is that the resources can stay inside the subscription and user has that time period to realize if something is missing or forgot to backup something.

Dilergore avatar Jan 14 '21 10:01 Dilergore

@anuragdalmia Disabled Subscription will stop generating costs. I want to disable a Subscription, when the Budget is reached, to make sure, no additional costs are generated. Deleting the resources would be very bad.

Gill-Bates avatar Jan 14 '21 11:01 Gill-Bates

Thanks @Gill-Bates , @Dilergore for the detailed explanations. Let me review this and get back with options.

anuragdalmia avatar Jan 14 '21 17:01 anuragdalmia

@anuragdalmia Thank You!

Moreover let me add to this a bit more:

I think originally you have the 90 days cancellation period with keeping the resource to give the end users time to figure out whether they really do not need the subscription and its contents. In the same time you do not offer any other way to get rid of a subscription. So there is as well an illogical thinking here because either I should be able to cancel the sub as it is and keep it intact or delete it forever if I cannot do that. You said that we could delete all the resources first, but if I delete everything then the subscription won't be different from a new one I could create so basically the sub could go away instantly. (I get that other than resources there can be other settings like some preview flags or manually turned on settings from the MS side but at this point I do not think it matters that much).

On the top of this if a customer creates an automation for this today they need to delete everything to do it. So then my question becomes: what is better? If they delete everything and cancel and realize they screwed up or if they just cancel and then they realize the mistake and they can enable so everything continues as it was... See?

Dilergore avatar Jan 15 '21 06:01 Dilergore

Hi @anuragdalmia,

Have you had a chance to look into this?

Dilergore avatar Feb 15 '21 09:02 Dilergore

@Dilergore yes, we are still looking into this. This ask is officially in our backlog and we are figuring out a way to prioritize this. Will keep you posted.

anuragdalmia avatar Feb 16 '21 06:02 anuragdalmia

I am facing the same issue using the Azure Rest API, the Cancel API call fails with the error "There are existing resources associated with subscription. Delete them before cancelling the subscription".

We have a similar use case using Sandbox Subscriptions. We find it safer to Cancel the Subscription instead of deleting resources from it.

saursinha avatar Mar 07 '21 10:03 saursinha

This behaviour can be achived by adding parameter IgnoreResourceCheck=true to URI

https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Subscription/cancel?IgnoreResourceCheck=true&api-version=2019-03-01-preview

This is basically as it's done via portal. I just hope Microsoft won't disable this behaviour. In my opinion, it's much more secure than just deleting all resources.

Vegoo89 avatar Jun 17 '21 13:06 Vegoo89

What permissions are required to cancel subscriptions?

I created this role:

{
    "properties": {
        "roleName": "cancel-role",
        "description": "",
        "assignableScopes": [
            "/subscriptions/{sub_id}"
        ],
        "permissions": [
            {
                "actions": [
                    "Microsoft.Subscription/cancel/action",
                    "Microsoft.Subscription/enable/action"
                ],
                "notActions": [],
                "dataActions": [],
                "notDataActions": []
            }
        ]
    }
}

This API endpoint works:

/subscriptions/{subscriptionId}/providers/Microsoft.Subscription/enable

This one doesn't:

/subscriptions/{subscriptionId}/providers/Microsoft.Subscription/cancel

I get the following error message:

Only subscription owners can perform this operation. Contact your subscription owner to perform this operation

I need to assign a role to a managed identity which can cancel subscriptions but not do anything else.

Strangely giving the role Action permission * and notAction Microsoft.Subscription/cancel/action does not stop you from being able to cancel subscriptions.

OscartGiles avatar Aug 12 '21 15:08 OscartGiles

Hi @szaher. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text “/unresolve” to remove the “issue-addressed” label and continue the conversation.

ghost avatar Dec 15 '21 08:12 ghost

Hi @szaher, since you haven’t asked that we “/unresolve” the issue, we’ll close this out. If you believe further discussion is needed, please add a comment “/unresolve” to reopen the issue.

ghost avatar Dec 28 '21 10:12 ghost

Hi erwinkramer, only the original author of the issue can ask that it be unresolved. Please open a new issue with your scenario and details if you would like to discuss this topic with the team.

ghost avatar Jun 03 '22 08:06 ghost

Hi erwinkramer, only the original author of the issue can ask that it be unresolved. Please open a new issue with your scenario and details if you would like to discuss this topic with the team.

ghost avatar Jun 07 '22 16:06 ghost