azure-api-managementazure-sdk-.net

Azure .Net SDK to generate APIM resource identifier / subscription


I'm looking at this SDK to generate APIM subscription key in my c# application.

I couldn't find the right way to get the class instance ApiManagementSubscriptionResource and invoke this method CreateResourceIdentifier(String, String, String, String). Anyone can shed some light on how can I call the method to generate subscription key from my c# application?

 SubscriptionResource subscription = await client.GetDefaultSubscriptionAsync();
 ResourceGroupCollection resourceGroups = subscription.GetResourceGroups();
 ResourceGroupResource resourceGroup = await resourceGroups.GetAsync("rg_name");
 var apiManagements = await resourceGroup.GetApiManagementServiceAsync("apim_name"); 

Solution

  • You're close, you just have to keep going.

    This worked for me ...

    var credential = new DefaultAzureCredential();
    var armClient = new ArmClient(credential);
    var subscription = await armClient.GetDefaultSubscriptionAsync();
    
    var resourceGroups = subscription.GetResourceGroups();
    var resourceGroup = await resourceGroups.GetAsync("rg-your-resource-group");
    var apimInstance = await resourceGroup.Value.GetApiManagementServiceAsync("apim-your-instance");
    
    var newSubscription = apimInstance.Value.GetApiManagementSubscriptions().CreateOrUpdate(Azure.WaitUntil.Completed, "your_generated_sid",
        new Azure.ResourceManager.ApiManagement.Models.ApiManagementSubscriptionCreateOrUpdateContent()
        {
            DisplayName = "Display Name",
            State = Azure.ResourceManager.ApiManagement.Models.SubscriptionState.Active,
            Scope = "/products/Unlimited"
        });
    

    ... you'll just need to work out your Scope, SID, DisplayName, etc.

    I suspect you'll want the primary key after it's created if you need to communicate back to the caller once setup.

    That can be obtained in the response ...

    var primaryKey = newSubscription.Value.Data.PrimaryKey;