@@ -34,6 +34,10 @@ class APIMSubscriptionNotFoundError(Exception):
3434 pass
3535
3636
37+ class APIMSubscriptionKeyRefreshError (Exception ):
38+ pass
39+
40+
3741class ApimSubscriptionsManager :
3842 _tenant_id : str = None
3943 _client_id : str = None
@@ -357,6 +361,7 @@ def get_subscription_for_user(self, user_id: str) -> Dict[str, Any]:
357361 :return: dictionary containing the details of the user's subscription. This includes the subscription ID, type,
358362 name, associated properties such as owner ID, scope, display name, state, creation date, start date, expiration date,
359363 end date, notification date, state comment and whether tracing is allowed.
364+
360365 Examples:
361366 >>> print(get_subscription_for_user("123-unique-id-for-subscription-123"))
362367 {
@@ -399,11 +404,8 @@ def get_subscription_secrets_for_user(self, user_id: str) -> Dict[str, str]:
399404 """
400405 Retrieves the primary and secondary keys for a user's subscription from the API Management (APIM) service.
401406
402- Args:
403- user_id (str): The unique identifier of the user for whom the subscription keys are to be retrieved.
404-
405- Returns:
406- dict: A dictionary containing the primary and secondary keys for the user's subscription.
407+ :param user_id: The unique identifier of the user for whom the subscription keys are to be retrieved.
408+ :return: dictionary containing the primary and secondary keys for the user's subscription.
407409
408410 Examples:
409411 >>> print(get_subscription_secrets_for_user("123-unique-id-for-subscription-123"))
@@ -428,3 +430,75 @@ def get_subscription_secrets_for_user(self, user_id: str) -> Dict[str, str]:
428430 raise APIMSubscriptionNotFoundError (
429431 f"Subscription for user with id { user_id } not found. Status code: { response .status_code } , "
430432 f"Response: { response .text } " )
433+
434+ def delete_subscription_for_user (self , user_id : str ) -> str :
435+ """
436+ Deletes a subscription for a user in the API Management (APIM) service.
437+
438+ :param user_id: The unique identifier of the user for whom the subscription is to be deleted.
439+
440+ :return: The unique identifier of the user for whom the subscription was deleted.
441+ Examples:
442+ >>> print(delete_subscription_for_user("123-unique-id-for-subscription-123"))
443+ '123-unique-id-for-subscription-123'
444+ """
445+
446+ url = (f"https://management.azure.com/subscriptions/{ self ._apim_subscription_id } /resourceGroups/"
447+ f"{ self ._apim_rg_name } /providers/Microsoft.ApiManagement/service/{ self ._apim_name } /subscriptions"
448+ f"/{ user_id } ?"
449+ f"api-version=2022-08-01" )
450+
451+ headers = self ._get_auth_headers ()
452+
453+ response = requests .delete (url , headers = headers )
454+
455+ if response .status_code == 200 :
456+ return user_id
457+ elif response .status_code == 204 :
458+ raise APIMSubscriptionNotFoundError (
459+ f"Subscription for user with id { user_id } not found or couldn't be deleted. Status code:"
460+ f" { response .status_code } , Response: { response .text } " )
461+ else :
462+ raise APIMSubscriptionNotFoundError (
463+ f"Subscription for user with id { user_id } not found or couldn't be deleted. Status code:"
464+ f" { response .status_code } , Response: { response .text } " )
465+
466+ def regenerate_subscription_for_user (self , user_id : str ) -> str :
467+ """
468+ Regenerates the subscription keys for a user in the API Management (APIM) service.
469+
470+ :param user_id: The unique identifier of the user for whom the subscription keys are to be regenerated.
471+
472+ :return: The unique identifier of the user for whom the subscription keys were regenerated.
473+ Examples:
474+ >>> print(regenerate_subscription_for_user("123-unique-id-for-subscription-123"))
475+ '123-unique-id-for-subscription-123'
476+ """
477+
478+ urls = [
479+ (
480+ f"https://management.azure.com/subscriptions/{ self ._apim_subscription_id } /resourceGroups/"
481+ f"{ self ._apim_rg_name } /"
482+ f"providers/Microsoft.ApiManagement/service/{ self ._apim_name } /subscriptions/{ user_id } /"
483+ f"regeneratePrimaryKey?api-version=2022-08-01" ),
484+ (
485+ f"https://management.azure.com/subscriptions/{ self ._apim_subscription_id } /resourceGroups/"
486+ f"{ self ._apim_rg_name } /"
487+ f"providers/Microsoft.ApiManagement/service/{ self ._apim_name } /subscriptions/{ user_id } /"
488+ f"regenerateSecondaryKey?api-version=2022-08-01" )
489+ ]
490+
491+ logging .debug (f"Urls are { urls } " )
492+ headers = self ._get_auth_headers ()
493+ responses = []
494+ for url in urls :
495+ response = requests .post (url , headers = headers )
496+ responses .append (response )
497+
498+ if all (response .status_code == 204 for response in responses ):
499+ return user_id
500+ else :
501+ raise APIMSubscriptionKeyRefreshError (
502+ f"Failed to refresh subscription keys for user with id { user_id } . "
503+ f"Status codes: { [response .status_code for response in responses ]} , "
504+ f"Responses: { [response .text for response in responses ]} " )
0 commit comments