I hope you can help me in the code, i have a trouble to generate the code to create a Resource Group, but for some reason i can't understand what is the error... i use the code of the github: https://github.com/Microsoft/azure-content/blob/master/articles/virtual-machines/arm-template-deployment.md i need to work this example for the project in the work... i'll be very grateful if can help me to find the solution of this code...
Here the code:
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Azure.Management.Resources;
using Microsoft.Azure.Management.Resources.Models;
using Microsoft.WindowsAzure;
namespace pruebaazure
{
class Program
{
static void Main(string[] args)
{
var token = GetAuthorizationHeader();
var credential = new Microsoft.WindowsAzure.TokenCloudCredentials("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxef", token);
CreateResourceGroup(credential);
Console.ReadLine();
CreateTemplateDeployment(credential);
Console.ReadLine();
DeleteResourceGroup(credential);
Console.ReadLine();
}
private static string GetAuthorizationHeader()
{
ClientCredential cc = new ClientCredential("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx8", "xxxxxxxK2");
var context = new AuthenticationContext("https://login.windows.net/xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx4");
var result = context.AcquireToken("https://management.azure.com/", cc);
if (result == null)
{
throw new InvalidOperationException("Failed to obtain the JWT token");
}
string token = result.AccessToken;
return token;
}
public async static void CreateResourceGroup(TokenCloudCredentials credential)
{
Console.WriteLine("Creating the resource group...");
var resourceGroup = new ResourceGroup { Location = "West US" };
using (var resourceManagementClient = new ResourceManagementClient(credential))
{
var rgResult = await resourceManagementClient.ResourceGroups.CreateOrUpdateAsync("mytestrg1", resourceGroup);
Console.WriteLine(rgResult.StatusCode);
}
}
public async static void CreateTemplateDeployment(TokenCloudCredentials credential)
{
Console.WriteLine("Creating the template deployment...");
var deployment = new Deployment();
deployment.Properties = new DeploymentProperties
{
Mode = DeploymentMode.Incremental,
TemplateLink = new TemplateLink
{
Uri = new Uri("https://xxxxxxxxxxxxya.blob.core.windows.net/templates/VirtualMachineTemplate.json").ToString()
},
ParametersLink = new ParametersLink
{
Uri = new Uri("https://xxxxxxxxxxxxxya.blob.core.windows.net/templates/Parameters.json").ToString()
}
};
using (var templateDeploymentClient = new ResourceManagementClient(credential))
{
var dpResult = await templateDeploymentClient.Deployments.CreateOrUpdateAsync("mytestrg1", "mytestdp1", deployment);
Console.WriteLine(dpResult.StatusCode);
}
}
public async static void DeleteResourceGroup(TokenCloudCredentials credential)
{
using (var resourceGroupClient = new ResourceManagementClient(credential))
{
var rgResult = await resourceGroupClient.ResourceGroups.DeleteAsync("mytestrg1");
Console.WriteLine(rgResult.StatusCode);
}
}
}
}
Parameters.json
{
"contentVersion": "1.0.0.0",
"parameters": {
"vmName": { "value": "mytestvm1" },
"newStorageAccountName": { "value": "mytestsa1" },
"storageAccountType": { "value": "Standard_LRS" },
"publicIPaddressName": { "value": "mytestip1" },
"location": { "value": "West US" },
"vmStorageAccountContainerName": { "value": "vhds" },
"vmSize": { "value": "Standard_A1" },
"subscriptionId": { "value": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxef" },
"vmSourceImageName": { "value": "sourceimgtest1" },
"adminUserName": { "value": "mytestacct1" },
"adminPassword": { "value": "mytestpass1" },
"virtualNetworkName": { "value": "mytestvn1" },
"dnsName": { "value": "mytestdns1" },
"nicName": { "value": "mytestnic1" }
}
}
VirtualMachineTemplate.json
{
"$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/VM.json",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "String",
"defaultValue": "West US",
"allowedValues": [ "West US", "East US" ]
},
"newStorageAccountName": { "type": "string" },
"storageAccountType": {
"type": "string",
"defaultValue": "Standard_LRS",
"allowedValues": [ "Standard_LRS", "Standard_GRS" ]
},
"publicIPAddressName": { "type": "string" },
"publicIPAddressType": {
"type": "string",
"defaultValue": "Dynamic",
"allowedValues": [ "Dynamic" ]
},
"vmStorageAccountContainerName": {
"type": "string",
"defaultValue": "vhds"
},
"vmName": { "type": "string" },
"vmSize": {
"type": "string",
"defaultValue": "Standard_A0",
"allowedValues": [ "Standard_A0", "Standard_A1" ]
},
"vmSourceImageName": { "type": "string" },
"adminUserName": { "type": "string" },
"adminPassword": { "type": "securestring" },
"virtualNetworkName": { "type": "string" },
"addressPrefix": {
"type": "string",
"defaultValue": "10.0.0.0/16"
},
"subnet1Name": {
"type": "string",
"defaultValue": "Subnet-1"
},
"subnet2Name": {
"type": "string",
"defaultValue": "Subnet-2"
},
"subnet1Prefix": {
"type": "string",
"defaultValue": "10.0.0.0/24"
},
"subnet2Prefix": {
"type": "string",
"defaultValue": "10.0.1.0/24"
},
"dnsName": { "type": "string" },
"subscriptionId": { "type": "string" },
"nicName": { "type": "string" }
},
"resources": [
{
"apiVersion": "2014-12-01-preview",
"type": "Microsoft.Storage/storageAccounts",
"name": "[parameters('newStorageAccountName')]",
"location": "[parameters('location')]",
"properties": { "accountType": "[parameters('storageAccountType')]" }
},
{
"apiVersion": "2014-12-01-preview",
"type": "Microsoft.Network/publicIPAddresses",
"name": "[parameters('publicIPAddressName')]",
"location": "[parameters('location')]",
"properties": {
"publicIPAllocationMethod": "[parameters('publicIPAddressType')]",
"dnsSettings": { "domainNameLabel": "[parameters('dnsName')]" }
}
},
{
"apiVersion": "2014-12-01-preview",
"type": "Microsoft.Network/virtualNetworks",
"name": "[parameters('virtualNetworkName')]",
"location": "[parameters('location')]",
"properties": {
"addressSpace": { "addressPrefixes": [ "[parameters('addressPrefix')]" ] },
"subnets": [
{
"name": "[parameters('subnet1Name')]",
"properties": { "addressPrefix": "[parameters('subnet1Prefix')]" }
},
{
"name": "[parameters('subnet2Name')]",
"properties": { "addressPrefix": "[parameters('subnet2Prefix')]" }
}
]
}
},
{
"apiVersion": "2014-12-01-preview",
"type": "Microsoft.Network/networkInterfaces",
"name": "[parameters('nicName')]",
"location": "[parameters('location')]",
"dependsOn": [
"[concat('Microsoft.Network/publicIPAddresses/', parameters('publicIPAddressName'))]",
"[concat('Microsoft.Network/virtualNetworks/', parameters('virtualNetworkName'))]"
],
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"privateIPAllocationMethod": "Dynamic",
"publicIPAddress": {
"id": "[resourceId('Microsoft.Network/publicIPAddresses', parameters('publicIPAddressName'))]"
},
"subnet": { "id": "[variables('subnet1Ref')]" }
}
}
]
}
},
{
"apiVersion": "2014-12-01-preview",
"type": "Microsoft.Compute/virtualMachines",
"name": "[parameters('vmName')]",
"location": "[parameters('location')]",
"dependsOn": [
"[concat('Microsoft.Storage/storageAccounts/', parameters('newStorageAccountName'))]",
"[concat('Microsoft.Network/networkInterfaces/', parameters('nicName'))]"
],
"properties": {
"hardwareProfile": { "vmSize": "[parameters('vmSize')]" },
"osProfile": {
"computername": "[parameters('vmName')]",
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPassword')]",
"windowsProfile": { "provisionVMAgent": "true" }
},
"storageProfile": {
"sourceImage": { "id": "[variables('sourceImageName')]" },
"destinationVhdsContainer": "[concat('http://',parameters('newStorageAccountName'),'.blob.core.windows.net/', parameters('vmStorageAccountContainerName'),'/')]"
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces',parameters('nicName'))]"
}
]
}
}
}
],
"variables": {
"sourceImageName": "[concat('/',parameters('subscriptionId'),'/services/images/',parameters('vmSourceImageName'))]",
"vnetID": "[resourceId('Microsoft.Network/virtualNetworks',parameters('virtualNetworkName'))]",
"subnet1Ref": [concat(variables('vnetID'),'/subnets/',parameters('subnet1Name'))]"
}
}
}
The problem is in program.cs where
public async static void CreateResourceGroup(TokenCloudCredentials credential)
{
Console.WriteLine("Creating the resource group...");
var resourceGroup = new ResourceGroup { Location = "West US" };
using (var resourceManagementClient = new ResourceManagementClient(credential))
{
var rgResult = await resourceManagementClient.ResourceGroups.CreateOrUpdateAsync("mytestrg1", resourceGroup);
Console.WriteLine(rgResult.StatusCode);
}
}
in:
(var resourceManagementClient = new ResourceManagementClient(credential))
the error is:
cannot convert from Microsoft.WindowsAzure.TokenCloudCredentials credentials to Microsoft.Rest.CerviceClientCredentials
and
Console.WriteLine(rgResult.StatusCode);
The error is:
ResourcesGroup does not contain the definition for "StatusCode" and no extension method "StatusCode" accepting a first argument of type 'ResourceGroup' Could be found
and
public async static void CreateTemplateDeployment(TokenCloudCredentials credential)
the error is the same cannot convert from Microsoft.WindowsAzure.TokenCloudCredentials credentials to Microsoft.Rest.CerviceClientCredentials in here
using (var templateDeploymentClient = new ResourceManagementClient(credential))
{
var dpResult = await templateDeploymentClient.Deployments.CreateOrUpdateAsync("mytestrg1", "mytestdp1", deployment);
Console.WriteLine(dpResult.StatusCode);
}
and for this too:
public async static void DeleteResourceGroup(TokenCloudCredentials credential)
{
using (var resourceGroupClient = new ResourceManagementClient(credential))
{
var rgResult = await resourceGroupClient.ResourceGroups.DeleteAsync("mytestrg1");
Console.WriteLine(rgResult.StatusCode);
}
}