-1

I want to create resources like CosmosDB, Azure Kubernetes service, etc I went through the following document :

https://learn.microsoft.com/en-us/rest/api/resources/resources/create-or-update

I see that the request URL has parameters like :-

https://management.azure.com/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{parentResourcePath}/{resourceType}/{resourceName}?api-version=2021-04-01

Where can I find the values for the fields like resourceProviderNamespace, parentResourcePath, resourceType, etc for each of the resources like cosmosDB, AKS, etc?

Also the properties that each of the resources expect, like location, backup, etc ??

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
Jerald Baker
  • 1,121
  • 1
  • 12
  • 48
  • There are resource specific API documentation as well. For example, see this for Cosmos DB - https://learn.microsoft.com/en-us/rest/api/cosmos-db-resource-provider/2021-04-15/database-accounts/create-or-update. – Gaurav Mantri Aug 10 '21 at 14:10
  • The one u have mentioned is present only for CosmosDB in the name of CosmosDB Resource Provider. Need this info for all the other Azure resources. – Jerald Baker Aug 10 '21 at 14:14
  • You will need to check the reference for each resource type you're creating. Every resource type has some common properties and then some unique properties specific for that resource type. – Gaurav Mantri Aug 10 '21 at 14:17
  • For this resource "Farmbeats" for example - https://learn.microsoft.com/en-us/rest/api/agfood/ - I can't find the value for those fields – Jerald Baker Aug 10 '21 at 14:20
  • Can you edit your question and include what operations you are trying to perform? – Gaurav Mantri Aug 10 '21 at 14:23
  • I am trying to create (deploy) an azure resource via Rest API. I just need the values for the fields I've mentioned for each of the resources. I am unable to find documentation on that. Is there something unclear in my request? – Jerald Baker Aug 10 '21 at 14:29
  • 1
    I think yes. So essentially there are two kinds of operations - control plane and data plane. For control plane operations, you need to use resource groups, resource providers etc. For data plane operations these are not required. Here's the link for the control plane operations for Farmbeats resources - https://learn.microsoft.com/en-us/rest/api/agfood/#azure-resource-manager-rest-operation-groups. – Gaurav Mantri Aug 10 '21 at 14:32

2 Answers2

1

First of all you need clientID, clientSecret and tenentID of your Azure account. Make sure you have given reuired permission to access and modify azure resources via API. Hint Use rbac command in azure cloudshell.

// Get Access token to use azure management API end points.
public static string GetAzureAccessToken()
        {
            // Get Access token for azure api
            string AccessToken = "";
            var tenantId = System.Configuration.ConfigurationManager.AppSettings["AzureTenantID"];
            var clientId = System.Configuration.ConfigurationManager.AppSettings["AzureClientID"];
            var secret = System.Configuration.ConfigurationManager.AppSettings["AzureSecret"];
            var resourceUrl = "https://management.azure.com/";
            var requestUrl = $"https://login.microsoftonline.com/{tenantId}/oauth2/token";

            // in real world application, please use Typed HttpClient from ASP.NET Core DI
            var httpClient = new System.Net.Http.HttpClient();

            var dict = new Dictionary<string, string>
                                                {
                                                    { "grant_type", "client_credentials" },
                                                    { "client_id", clientId },
                                                    { "client_secret", secret },
                                                    { "resource", resourceUrl }
                                                };

            var requestBody = new System.Net.Http.FormUrlEncodedContent(dict);
            var response = httpClient.PostAsync(requestUrl, requestBody).Result;

            if (response != null)
            {
                response.EnsureSuccessStatusCode();
                string responseContent = response.Content.ReadAsStringAsync().Result;
                if (!string.IsNullOrEmpty(responseContent))
                {
                    var TokenResponse = JsonConvert.DeserializeObject<AzureTokenResponseModel>(responseContent);
                    if (TokenResponse != null)
                    {
                        AccessToken += TokenResponse?.access_token;
                    }
                }
            }

            return AccessToken;
        }

//  create azure resource group by calling API
var clientResourceGroup = new RestClient($"https://management.azure.com/subscriptions/{SubscriptionID}/resourcegroups/{ResourceGroupName}?api-version=2022-05-01");
                        clientResourceGroup.Timeout = -1;
                        var requestResourceGroup = new RestRequest(Method.PUT);
                        requestResourceGroup.AddHeader("Authorization", "Bearer " + GetAzureAccessToken());
                        requestResourceGroup.AddHeader("Content-Type", "application/json");
                        var bodyResourceGroup = @"{'location':'" + AzLocation + @"','Name':'" + ResourceGroupName + @"'}}";
                        requestResourceGroup.AddParameter("application/json", bodyResourceGroup, ParameterType.RequestBody);
                        IRestResponse responseResourceGroup = clientResourceGroup.Execute(requestResourceGroup);

Post down here if still facing difficulties. We can create other azure resources like storage account, functions, app service etc..

0

As suggested by Gaurav Mantri, you can refer to Azure FarmBeats control plane and data plane operations

Thank you and AnuragSharma-MSFT and MarcoPapst-5675. Posting your suggestion as an answer to help community members.

You can refer to the following script for CosmosDB role assignment:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "roleDefinitionId": {
            "type": "string",
            "metadata": {
                "description": "Name of the Role Definition"
            }
        },
        "roleAssignmentName": {
            "type": "string",
            "metadata": {
                "description": "Name of the Assignment"
            }
        },
        "scope": {
            "type": "string",
            "metadata": {
                "description": "Scope of the Role Assignment"
            }
        },
        "principalId": {
            "type": "string",
            "metadata": {
                "description": "Object ID of the AAD identity. Must be a GUID."
            }
        }
    },
    "variables": { },
    "resources": [
        {
            "name": "[concat(parameters('roleAssignmentName'), '/', guid(parameters('scope')))]",
            "type": "Microsoft.DocumentDB/databaseAccounts/sqlRoleAssignments",
            "apiVersion": "2021-04-15",
            "properties": {
                "roleDefinitionId": "[parameters('roleDefinitionId')]",
                "principalId": "[parameters('principalId')]",
                "scope": "[parameters('scope')]"
            }
        }
    ]
}

You can refer to Create a CosmosDB Role Assignment using an ARM Template, Azure REST API get resource parentResourcePath parameter and Resources - Get By Id

Ecstasy
  • 1,866
  • 1
  • 9
  • 17