private async Task <GenericResource> CreateNetworkInterface(ResourceIdentifier subnetId) { var nicName = Recording.GenerateAssetName("testNic-"); ResourceIdentifier nicId = new ResourceIdentifier($"{_resourceGroup.Id}/providers/Microsoft.Network/networkInterfaces/{nicName}"); var input = new GenericResourceData(DefaultLocation) { Properties = new Dictionary <string, object>() { { "ipConfigurations", new List <object>() { new Dictionary <string, object>() { { "name", "internal" }, { "properties", new Dictionary <string, object>() { { "subnet", new Dictionary <string, object>() { { "id", subnetId.ToString() } } } } } } } } } }; var operation = await _genericResourceCollection.CreateOrUpdateAsync(true, nicId, input); return(operation.Value); }
GenericResource IOperationSource <GenericResource> .CreateResult(Response response, CancellationToken cancellationToken) { using var document = JsonDocument.Parse(response.ContentStream); var data = GenericResourceData.DeserializeGenericResourceData(document.RootElement); return(new GenericResource(_armClient, data)); }
internal static ResourceListResult DeserializeResourceListResult(JsonElement element) { Optional <IReadOnlyList <GenericResourceData> > value = default; Optional <string> nextLink = default; foreach (var property in element.EnumerateObject()) { if (property.NameEquals("value")) { if (property.Value.ValueKind == JsonValueKind.Null) { property.ThrowNonNullablePropertyIsNull(); continue; } List <GenericResourceData> array = new List <GenericResourceData>(); foreach (var item in property.Value.EnumerateArray()) { array.Add(GenericResourceData.DeserializeGenericResource(item)); } value = array; continue; } if (property.NameEquals("nextLink")) { nextLink = property.Value.GetString(); continue; } } return(new ResourceListResult(Optional.ToList(value), nextLink.Value)); }
protected async Task <ArmOperation <GenericResource> > StartCreateGenericAvailabilitySetAsync(ResourceIdentifier rgId) { var genericResources = Client.GetGenericResources(); GenericResourceData data = ConstructGenericAvailabilitySet(); var asetId = rgId.AppendProviderResource("Microsoft.Compute", "availabilitySets", Recording.GenerateAssetName("test-aset")); return(await genericResources.CreateOrUpdateAsync(WaitUntil.Started, asetId, data)); }
protected async Task <GenericResource> CreateGenericVirtualNetwork(SubscriptionResource subscription, ResourceGroupResource rg, string vnName) // TODO: remove subscription parameter { GenericResourceData input = ConstructGenericVirtualNetworkData(); ResourceIdentifier vnId = rg.Id.AppendProviderResource("Microsoft.Network", "virtualNetworks", vnName); ArmOperation <GenericResource> lro = await Client.GetGenericResources().CreateOrUpdateAsync(WaitUntil.Completed, vnId, input); return(lro.Value); }
protected async Task <ResourceCreateOrUpdateByIdOperation> StartCreateGenericAvailabilitySetAsync(ResourceIdentifier rgId) { var genericResources = (await Client.GetDefaultSubscriptionAsync().ConfigureAwait(false)).GetGenericResources(); GenericResourceData data = ConstructGenericAvailabilitySet(); var asetId = rgId.AppendProviderResource("Microsoft.Compute", "availabilitySets", Recording.GenerateAssetName("test-aset")); return(await genericResources.CreateOrUpdateAsync(asetId, data, false)); }
private async Task <GenericResource> CreateUserAssignedIdentityAsync() { string userAssignedIdentityName = Recording.GenerateAssetName("testMsi-"); ResourceIdentifier userIdentityId = new ResourceIdentifier($"{_resourceGroup.Id}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{userAssignedIdentityName}"); var input = new GenericResourceData(DefaultLocation); var response = await Client.GetGenericResources().CreateOrUpdateAsync(WaitUntil.Completed, userIdentityId, input); return(response.Value); }
public void InvalidDeserializationTest() { string json = "{\"notId\":\"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testRg/providers/Microsoft.ClassicStorage/storageAccounts/account1\",\"location\":\"eastus\",\"managedBy\":\"ManagedByForResource\",\"name\":\"account1\",\"plan\":{\"name\":\"NameForPlan\",\"publisher\":\"PublisherForPlan\",\"product\":\"ProductForPlan\",\"promotionCode\":\"PromotionCodeForPlan\",\"version\":\"VersionForPlan\"},\"sku\":{\"name\":\"NameForSku\",\"tier\":\"Basic\",\"size\":\"SizeForSku\",\"family\":\"FamilyForSku\",\"capacity\":15464547},\"tags\":{},\"type\":\"Microsoft.ClassicStorage/storageAccounts\"}"; JsonElement element = JsonDocument.Parse(json).RootElement; GenericResourceData data = GenericResourceData.DeserializeGenericResource(element); Assert.IsNull(data.Id); Assert.IsNull(data.Kind); }
protected async Task <GenericResource> CreateGenericAvailabilitySetAsync(ResourceIdentifier rgId) { var genericResources = Client.GetGenericResources(); GenericResourceData data = ConstructGenericAvailabilitySet(); var asetId = rgId.AppendProviderResource("Microsoft.Compute", "availabilitySets", Recording.GenerateAssetName("test-aset")); var op = await genericResources.CreateOrUpdateAsync(true, asetId, data); return(op.Value); }
public void InvalidSerializationTest() { string expected = "{\"properties\":{\"location\":\"eastus\",\"tags\":{}}}"; ResourceIdentifier id = Id; GenericResourceData data = new GenericResourceData(id, id.Name, id.ResourceType, Location.EastUS, new Dictionary <string, string>(), null, null, null, null, null, null, null, null, null); var json = JsonHelper.SerializePropertiesToString(data); Assert.AreEqual(expected, json); }
public void SerializationTestType1() { string expected = File.ReadAllText(Path.Combine(TestContext.CurrentContext.TestDirectory, "Unit", "TestAssets", "GenericResourceData", "SerializationTestType1.json")); ResourceIdentifier id = Id; Plan plan = new Plan("NameForPlan", "PublisherForPlan", "ProductForPlan", "PromotionCodeForPlan", "VersionForPlan"); Sku sku = new Sku("NameForSku", SkuTier.Basic, "SizeForSku", "FamilyForSku", 15464547); GenericResourceData data = new GenericResourceData(id, id.Name, id.ResourceType, Location.EastUS, new Dictionary <string, string>(), plan, null, "KindForResource", "ManagedByForResource", sku, null, null, null, null); var json = JsonHelper.SerializePropertiesToString(data, indented: true) + Environment.NewLine; Assert.AreEqual(expected, json); }
protected static GenericResourceData ConstructGenericAvailabilitySet() { var data = new GenericResourceData(Location.WestUS2); data.Sku = new Sku("Aligned"); var propertyBag = new Dictionary <string, object>(); propertyBag.Add("platformUpdateDomainCount", 5); propertyBag.Add("platformFaultDomainCount", 2); data.Properties = propertyBag; return(data); }
protected static GenericResourceData ConstructGenericAvailabilitySet() { var data = new GenericResourceData(AzureLocation.WestUS2); data.Sku = new ResourcesSku() { Name = "Aligned" }; var propertyBag = new Dictionary <string, object>(); propertyBag.Add("platformUpdateDomainCount", 5); propertyBag.Add("platformFaultDomainCount", 2); data.Properties = BinaryData.FromObjectAsJson(propertyBag); return(data); }
public void SerializationTestType2() { string expected = File.ReadAllText(Path.Combine(TestContext.CurrentContext.TestDirectory, "Unit", "TestAssets", "GenericResourceData", "SerializationTestType2.json")); ResourceIdentifier id = new ResourceIdentifier(Id); var plan = new Plan("NameForPlan", "PublisherForPlan", "ProductForPlan", "PromotionCodeForPlan", "VersionForPlan"); var kind = "KindForResource"; var managedBy = "ManagedByForResource"; var sku = new Resources.Models.Sku("NameForSku", SkuTier.Basic.ToString(), "SizeForSku", "FamilyForSku", "ModelForSku", 15464547); GenericResourceData genericResource = new GenericResourceData(id, id.Name, id.ResourceType, Location.EastUS, new Dictionary <string, string>(), plan, null, kind, managedBy, sku, null, null, null, null); genericResource.Tags.Add("key1", "value1"); genericResource.Tags.Add("key2", "value2"); var json = JsonHelper.SerializePropertiesToString(genericResource, indented: true) + Environment.NewLine; Assert.AreEqual(expected, json); }
private GenericResourceData ConstructGenericVirtualNetworkData() { var virtualNetwork = new GenericResourceData(AzureLocation.WestUS2) { Properties = new JsonObject() { { "addressSpace", new JsonObject() { { "addressPrefixes", new List <string>() { "10.0.0.0/16" } } } } } }; return(virtualNetwork); }
// WEIRD: second level resources cannot use GenericResourceCollection to create. // Exception thrown: System.InvalidOperationException : An invalid resource id was given /subscriptions/db1ab6f0-4769-4b27-930e-01e2ef9c123c/resourceGroups/testRG-4544/providers/Microsoft.Network/virtualNetworks/testVNet-9796/subnets/testSubnet-1786 private async Task <GenericResource> CreateSubnet(ResourceIdentifier vnetId) { var subnetName = Recording.GenerateAssetName("testSubnet-"); ResourceIdentifier subnetId = new ResourceIdentifier($"{vnetId}/subnets/{subnetName}"); var input = new GenericResourceData(DefaultLocation) { Properties = new Dictionary <string, object>() { { "addressPrefixes", new List <string>() { "10.0.2.0/24" } } } }; var operation = await _genericResourceCollection.CreateOrUpdateAsync(true, subnetId, input); return(operation.Value); }
protected async Task <DeploymentScriptData> GetDeploymentScriptDataAsync() { //The user assigned identities was created firstly in Portal due to the unexpected behavior of using generic resource to create the user assigned identities. string rgName4Identities = "rg-for-DeployScript"; ResourceGroupData rgData = new ResourceGroupData(Location.WestUS2); Subscription sub = await Client.GetDefaultSubscriptionAsync(); var lro = await sub.GetResourceGroups().CreateOrUpdateAsync(rgName4Identities, rgData); ResourceGroup rg4Identities = lro.Value; GenericResourceData userAssignedIdentitiesData = ConstructGenericUserAssignedIdentities(); ResourceIdentifier userAssignedIdentitiesId = rg4Identities.Id.AppendProviderResource("Microsoft.ManagedIdentity", "userAssignedIdentities", "test-user-assigned-msi"); var lro2 = await sub.GetGenericResources().CreateOrUpdateAsync(userAssignedIdentitiesId, userAssignedIdentitiesData); GenericResource userAssignedIdentities = lro2.Value; var managedIdentity = new ManagedServiceIdentity() { Type = "UserAssigned", UserAssignedIdentities = { { userAssignedIdentitiesId, new UserAssignedIdentity() } } }; string AzurePowerShellVersion = "2.7.0"; TimeSpan RetentionInterval = new TimeSpan(1, 2, 0, 0, 0); string ScriptContent = "param([string] $helloWorld) Write-Output $helloWorld; $DeploymentScriptOutputs['output'] = $helloWorld"; string ScriptArguments = "'Hello World'"; return(new AzurePowerShellScript(Location.WestUS2, RetentionInterval, AzurePowerShellVersion) { Identity = managedIdentity, ScriptContent = ScriptContent, Arguments = ScriptArguments }); }
protected async Task <GenericResource> CreateVirtualNetwork() { var vnetName = Recording.GenerateAssetName("testVNet-"); var subnetName = Recording.GenerateAssetName("testSubnet-"); ResourceIdentifier vnetId = new ResourceIdentifier($"{_resourceGroup.Id}/providers/Microsoft.Network/virtualNetworks/{vnetName}"); var addressSpaces = new Dictionary <string, object>() { { "addressPrefixes", new List <string>() { "10.0.0.0/16" } } }; var subnet = new Dictionary <string, object>() { { "name", subnetName }, { "properties", new Dictionary <string, object>() { { "addressPrefix", "10.0.2.0/24" } } } }; var subnets = new List <object>() { subnet }; var input = new GenericResourceData(DefaultLocation) { Properties = new Dictionary <string, object>() { { "addressSpace", addressSpaces }, { "subnets", subnets } } }; var operation = await _genericResourceCollection.CreateOrUpdateAsync(true, vnetId, input); return(operation.Value); }
public async Task <GenericResource> CreateWindowsVM(string vmName, string networkInterfaceName, string location, ResourceGroup resourceGroup) { var vnet = await CreateVirtualNetwork(Recording.GenerateAssetName("vnet_"), Recording.GenerateAssetName("subnet_"), location, resourceGroup.GetVirtualNetworks()); var networkInterface = await CreateNetworkInterface(networkInterfaceName, null, vnet.Data.Subnets[0].Id, location, Recording.GenerateAssetName("ipconfig_"), resourceGroup.GetNetworkInterfaces()); var vmId = new ResourceIdentifier($"{resourceGroup.Id}/providers/Microsoft.Compute/virtualMachines/{vmName}"); Subscription subscription = await ArmClient.GetDefaultSubscriptionAsync(); var genericResouces = subscription.GetGenericResources(); GenericResourceData data = new GenericResourceData(location) { Properties = new Dictionary <string, object> { { "hardwareProfile", new Dictionary <string, object> { { "vmSize", "Standard_D1_v2" } } }, { "storageProfile", new Dictionary <string, object> { { "imageReference", new Dictionary <string, object> { { "sku", "2016-Datacenter" }, { "publisher", "MicrosoftWindowsServer" }, { "version", "latest" }, { "offer", "WindowsServer" } } }, { "osDisk", new Dictionary <string, object> { { "name", $"{vmName}_os_disk" }, { "osType", "Windows" }, { "caching", "ReadWrite" }, { "createOption", "FromImage" }, { "managedDisk", new Dictionary <string, object> { { "storageAccountType", "Standard_LRS" } } } } } } }, { "osProfile", new Dictionary <string, object> { { "adminUsername", Recording.GenerateAssetName("admin") }, { "adminPassword", Recording.GenerateAlphaNumericId("adminPass") }, { "computerName", vmName } } }, { "networkProfile", new Dictionary <string, object> { { "networkInterfaces", new List <object> { new Dictionary <string, object> { { "id", networkInterface.Id.ToString() }, { "properties", new Dictionary <string, object> { { "primary", true } } } } } } } } } }; var operation = InstrumentOperation(await ArmClient.GetGenericResources().CreateOrUpdateAsync(true, vmId, data)); operation.WaitForCompletion(); return(operation.Value); }
private static GenericResourceData ConstructGenericUserAssignedIdentities() { var userAssignedIdentities = new GenericResourceData(Location.WestUS2); return(userAssignedIdentities); }
public async Task <GenericResource> CreateLinuxVM(string vmName, string networkInterfaceName, string location, ResourceGroup resourceGroup) { Subscription subscription = await ArmClient.GetDefaultSubscriptionAsync(); var vnet = await CreateVirtualNetwork(Recording.GenerateAssetName("vnet_"), Recording.GenerateAssetName("subnet_"), location, resourceGroup.GetVirtualNetworks()); var networkInterface = await CreateNetworkInterface(networkInterfaceName, null, vnet.Data.Subnets[0].Id, location, Recording.GenerateAssetName("ipconfig_"), resourceGroup.GetNetworkInterfaces()); var adminUsername = Recording.GenerateAssetName("admin"); var vmId = new ResourceIdentifier($"{resourceGroup.Id}/providers/Microsoft.Compute/virtualMachines/{vmName}"); var genericResouces = subscription.GetGenericResources(); var data = new GenericResourceData(location) { Properties = new Dictionary <string, object> { { "hardwareProfile", new Dictionary <string, object> { { "vmSize", "Standard_F2" } } }, { "storageProfile", new Dictionary <string, object> { { "imageReference", new Dictionary <string, object> { { "sku", "16.04-LTS" }, { "publisher", "Canonical" }, { "version", "latest" }, { "offer", "UbuntuServer" } } }, { "osDisk", new Dictionary <string, object> { { "name", $"{vmName}_os_disk" }, { "osType", "Linux" }, { "caching", "ReadWrite" }, { "createOption", "FromImage" }, { "managedDisk", new Dictionary <string, object> { { "storageAccountType", "Standard_LRS" } } } } } } }, { "osProfile", new Dictionary <string, object> { { "adminUsername", adminUsername }, { "computerName", vmName }, { "linuxConfiguration", new Dictionary <string, object> { { "ssh", new Dictionary <string, object> { { "publicKeys", new List <object> { new Dictionary <string, object> { { "path", $"/home/{adminUsername}/.ssh/authorized_keys" }, { "keyData", dummySSHKey } } } } } }, { "disablePasswordAuthentication", true }, } }, } }, { "networkProfile", new Dictionary <string, object> { { "networkInterfaces", new List <object> { new Dictionary <string, object> { { "id", networkInterface.Id.ToString() }, { "properties", new Dictionary <string, object> { { "primary", true } } } } } } } } } }; var operation = InstrumentOperation(await ArmClient.GetGenericResources().CreateOrUpdateAsync(true, vmId, data)); operation.WaitForCompletion(); return(operation.Value); }