1

I want to create a Cognitive Search Service account (with Basic configuration) programmatically using C# and delete it on demand. Is there any documentation out there for doing this?

I was able to find documentation for creating and deleting indexes, but I need to go one level higher and create and delete the account instead. I need to do this to cut costs. For example, when I'm not actively dev testing, I need to delete the service account to avoid charges.

user246392
  • 2,661
  • 11
  • 54
  • 96

3 Answers3

0

You can manage your Azure resources using ARM templates through the REST API. To get the ARM template for the Cognitive Search Service you can create a sample service and downloading the template from Export template under Settings on Azure portal.

8163264128
  • 747
  • 4
  • 8
0

You could try the Azure CLI. This has all endpoints for creating and deleting the search service.

Reference : https://learn.microsoft.com/en-us/cli/azure/search/service?view=azure-cli-latest

az login -u <username> -p <password>
#SKU List --> https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.management.search.models.skuname?view=azure-dotnet 
az search service create --name "<SEARCH SERVICE NAME>"  --resource-group "<RESOURCE GROUPNAME>" --sku "<SKU ENUM>" --subscription "YOUR SUBSCRIPTION ID"

Script can be coupled with a C# application and invoking it on demand.

Alternatively, I was checking what was the underlying endpoint that was invoked by the Azure CLI. You could refer my post for more information on the 'How To'.

Request : enter image description here

Request Body : enter image description here

You can also try hitting this endpoint directly from your C# Application.

The same thing is applicable for the Delete as well.

az search service delete --name
                         --resource-group
                         [--subscription]
                         [--yes]

enter image description here

Satya V
  • 3,811
  • 1
  • 6
  • 9
  • Thanks. This is great. Is it possible to do this with the .NET SDK directly without having to call an HTTP endpoint or using Azure CLI? I know that I'm able to create storage accounts and blobs with the `CloudStorageAccount` class. – user246392 Aug 24 '20 at 14:43
  • Basically, I want to use my client id and client secret information for authentication instead of username and password. – user246392 Aug 24 '20 at 14:56
  • Per my research there is no .NET SDK that you can consume directly at this point. But I think you could still hit the above end points by getting a token using the App Authentication - Client secret and ID.(Without Username & Password) - Haven't tried it myself. But should be doable is what I think. – Satya V Aug 24 '20 at 15:43
  • Thanks. Do you have any documentation on getting a token with the client secret and id and using it to authenticate REST calls? – user246392 Aug 24 '20 at 15:48
  • https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow --- the generic version. You could also refer this for Graph : https://learn.microsoft.com/en-us/graph/auth-v2-service for more understanding. – Satya V Aug 24 '20 at 16:00
  • I tested the CLI and I was able to create a search service account. I then deleted it 10 mins later. 10 more mins later, I wanted to create it again, but the CLI is stuck with the "Running .." message. Is there some kind of throttling on the Azure side that I'm not aware of? – user246392 Aug 24 '20 at 17:47
  • Not that I am aware of. There could be a possibility of a temp glitch. Also, please check at the portal end whether the service was created. – Satya V Aug 25 '20 at 05:29
  • Just wanted to be sure that whether the service was created. As far as I researched I don t see any reason you should be throttled – Satya V Aug 25 '20 at 05:41
  • I was able to create the service. Thanks. – user246392 Aug 25 '20 at 13:03
0

I haven't used Cognitive and C#, but you can find Azure Resource Management Package From here.

enter image description here

And you will find operations for Account.

enter image description here

Maybe you can search it by keywords above in Github, and find this piece of code.

       //public static CognitiveServicesAccount CreateAndValidateAccountWithOnlyRequiredParameters(CognitiveServicesManagementClient cognitiveServicesMgmtClient, string rgName, string skuName, string accountType = Kind.Recommendations, string location = null)
        //{
        //    // Create account with only required params
        //    var accountName = TestUtilities.GenerateName("csa");
        //    var parameters = new CognitiveServicesAccountCreateParameters
        //    {
        //        Sku = new Microsoft.Azure.Management.CognitiveServices.Models.Sku { Name = skuName },
        //        Kind = accountType,
        //        Location = location ?? DefaultLocation,
        //        Properties = new object(),
        //    };
        //    var account = cognitiveServicesMgmtClient.CognitiveServicesAccounts.Create(rgName, accountName, parameters);
        //    VerifyAccountProperties(account, false, accountType, skuName, location ?? DefaultLocation);

        //    return account;
        //}
Jess Chen
  • 3,136
  • 1
  • 26
  • 35