From 3f38d3fe0960c616ada6aed2b989634665896aa8 Mon Sep 17 00:00:00 2001 From: Anudeep Sharma Date: Wed, 5 Oct 2016 17:57:21 -0700 Subject: [PATCH] Updated sample --- .gitignore | 143 ++++++++++ CONTRIBUTING.md | 11 + Common/Utility.cs | 558 +++++++++++++++++++++++++++++++++++++ LICENSE | 21 ++ ManageStorageAccount.xproj | 21 ++ Program.cs | 134 +++++++++ README.md | 40 ++- project.json | 20 ++ 8 files changed, 947 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 CONTRIBUTING.md create mode 100644 Common/Utility.cs create mode 100644 LICENSE create mode 100644 ManageStorageAccount.xproj create mode 100644 Program.cs create mode 100644 project.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..75200f7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,143 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# Auth files +*.auth +*.azureauth + +# User-specific files +*.suo +*.user +*.sln.docstates +.vs/ +*.lock.json +developer/ + +# Build results +binaries/ +[Dd]ebug*/ +[Rr]elease/ +build/ +src/NuGet.Config +tools/7-zip/ + +[Tt]est[Rr]esult +[Bb]uild[Ll]og.* + +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.vspscc +*.vssscc +.builds + +*.pidb + +*.log +*.scc +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf + +# Visual Studio profiler +*.psess +*.vsp + +# Code analysis +*.CodeAnalysisLog.xml + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ + +*.[Rr]e[Ss]harper + +# NCrunch +*.ncrunch* +.*crunch*.local.xml + +# Installshield output folder +[Ee]xpress + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish + +# Publish Web Output +*.Publish.xml + +# Others +[Bb]in +[Oo]bj +TestResults +[Tt]est[Rr]esult* +*.Cache +ClientBin +[Ss]tyle[Cc]op.* +~$* +*.dbmdl +node_modules + +*.[Pp]ublish.xml + +Generated_Code #added for RIA/Silverlight projects + +# Build tasks +tools/*.dll + +# Sensitive files +*.keys +*.pfx + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML + +# NuGet +packages +packages/repositories.config + +# Mac development +.DS_Store + +# Specification DLLs +*.Specification.dll + +# Generated readme.txt files # +src/*/readme.txt + +build.out +.nuget/ + +# Azure Project +csx/ +*.GhostDoc.xml +pingme.txt diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..90fd341 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,11 @@ +# Contributing to Azure samples + +Thank you for your interest in contributing to Azure samples! + +## Ways to contribute + +You can contribute to [Azure samples](https://github.com/Azure-Samples/storage-dotnet-manage-storage-accounts) in a few different ways: + +- Submit feedback on [this sample page](https://azure.microsoft.com/documentation/samples/storage-dotnet-manage-storage-accounts/) whether it was helpful or not. +- Submit issues through [issue tracker](https://github.com/Azure-Samples/storage-dotnet-manage-storage-accounts/issues) on GitHub. We are actively monitoring the issues and improving our samples. +- If you wish to make code changes to samples, or contribute something new, please follow the [GitHub Forks / Pull requests model](https://help.github.com/articles/fork-a-repo/): Fork the sample repo, make the change and propose it back by submitting a pull request. \ No newline at end of file diff --git a/Common/Utility.cs b/Common/Utility.cs new file mode 100644 index 0000000..3c5d995 --- /dev/null +++ b/Common/Utility.cs @@ -0,0 +1,558 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +using Microsoft.Azure.Management.Batch.Fluent; +using Microsoft.Azure.Management.Compute.Fluent; +using Microsoft.Azure.Management.KeyVault.Fluent; +using Microsoft.Azure.Management.Network.Fluent; +using Microsoft.Azure.Management.Storage.Fluent; +using Microsoft.Azure.Management.Storage.Fluent.Models; +using System; +using System.Linq; +using System.Collections.Generic; +using System.Text; + +namespace Microsoft.Azure.Management.Samples.Common +{ + public static class Utilities + { + public static void PrintVirtualMachine(IVirtualMachine virtualMachine) + { + var storageProfile = new StringBuilder().Append("\n\tStorageProfile: "); + if (virtualMachine.StorageProfile.ImageReference != null) + { + storageProfile.Append("\n\t\tImageReference:"); + storageProfile.Append("\n\t\t\tPublisher: ").Append(virtualMachine.StorageProfile.ImageReference.Publisher); + storageProfile.Append("\n\t\t\tOffer: ").Append(virtualMachine.StorageProfile.ImageReference.Offer); + storageProfile.Append("\n\t\t\tSKU: ").Append(virtualMachine.StorageProfile.ImageReference.Sku); + storageProfile.Append("\n\t\t\tVersion: ").Append(virtualMachine.StorageProfile.ImageReference.Version); + } + + if (virtualMachine.StorageProfile.OsDisk != null) + { + storageProfile.Append("\n\t\tOSDisk:"); + storageProfile.Append("\n\t\t\tOSType: ").Append(virtualMachine.StorageProfile.OsDisk.OsType); + storageProfile.Append("\n\t\t\tName: ").Append(virtualMachine.StorageProfile.OsDisk.Name); + storageProfile.Append("\n\t\t\tCaching: ").Append(virtualMachine.StorageProfile.OsDisk.Caching); + storageProfile.Append("\n\t\t\tCreateOption: ").Append(virtualMachine.StorageProfile.OsDisk.CreateOption); + storageProfile.Append("\n\t\t\tDiskSizeGB: ").Append(virtualMachine.StorageProfile.OsDisk.DiskSizeGB); + if (virtualMachine.StorageProfile.OsDisk.Image != null) + { + storageProfile.Append("\n\t\t\tImage Uri: ").Append(virtualMachine.StorageProfile.OsDisk.Image.Uri); + } + if (virtualMachine.StorageProfile.OsDisk.Vhd != null) + { + storageProfile.Append("\n\t\t\tVhd Uri: ").Append(virtualMachine.StorageProfile.OsDisk.Vhd.Uri); + } + if (virtualMachine.StorageProfile.OsDisk.EncryptionSettings != null) + { + storageProfile.Append("\n\t\t\tEncryptionSettings: "); + storageProfile.Append("\n\t\t\t\tEnabled: ").Append(virtualMachine.StorageProfile.OsDisk.EncryptionSettings.Enabled); + storageProfile.Append("\n\t\t\t\tDiskEncryptionKey Uri: ").Append(virtualMachine + .StorageProfile + .OsDisk + .EncryptionSettings + .DiskEncryptionKey.SecretUrl); + storageProfile.Append("\n\t\t\t\tKeyEncryptionKey Uri: ").Append(virtualMachine + .StorageProfile + .OsDisk + .EncryptionSettings + .KeyEncryptionKey.KeyUrl); + } + } + + if (virtualMachine.StorageProfile.DataDisks != null) + { + var i = 0; + foreach (var disk in virtualMachine.StorageProfile.DataDisks) + { + storageProfile.Append("\n\t\tDataDisk: #").Append(i++); + storageProfile.Append("\n\t\t\tName: ").Append(disk.Name); + storageProfile.Append("\n\t\t\tCaching: ").Append(disk.Caching); + storageProfile.Append("\n\t\t\tCreateOption: ").Append(disk.CreateOption); + storageProfile.Append("\n\t\t\tDiskSizeGB: ").Append(disk.DiskSizeGB); + storageProfile.Append("\n\t\t\tLun: ").Append(disk.Lun); + if (disk.Vhd.Uri != null) + { + storageProfile.Append("\n\t\t\tVhd Uri: ").Append(disk.Vhd.Uri); + } + if (disk.Image != null) + { + storageProfile.Append("\n\t\t\tImage Uri: ").Append(disk.Image.Uri); + } + } + } + + var osProfile = new StringBuilder().Append("\n\tOSProfile: "); + osProfile.Append("\n\t\tComputerName:").Append(virtualMachine.OsProfile.ComputerName); + if (virtualMachine.OsProfile.WindowsConfiguration != null) + { + osProfile.Append("\n\t\t\tWindowsConfiguration: "); + osProfile.Append("\n\t\t\t\tProvisionVMAgent: ") + .Append(virtualMachine.OsProfile.WindowsConfiguration.ProvisionVMAgent); + osProfile.Append("\n\t\t\t\tEnableAutomaticUpdates: ") + .Append(virtualMachine.OsProfile.WindowsConfiguration.EnableAutomaticUpdates); + osProfile.Append("\n\t\t\t\tTimeZone: ") + .Append(virtualMachine.OsProfile.WindowsConfiguration.TimeZone); + } + + if (virtualMachine.OsProfile.LinuxConfiguration != null) + { + osProfile.Append("\n\t\t\tLinuxConfiguration: "); + osProfile.Append("\n\t\t\t\tDisablePasswordAuthentication: ") + .Append(virtualMachine.OsProfile.LinuxConfiguration.DisablePasswordAuthentication); + } + + var networkProfile = new StringBuilder().Append("\n\tNetworkProfile: "); + foreach (var networkInterfaceId in virtualMachine.NetworkInterfaceIds) + { + networkProfile.Append("\n\t\tId:").Append(networkInterfaceId); + } + + Console.WriteLine(new StringBuilder().Append("Virtual Machine: ").Append(virtualMachine.Id) + .Append("Name: ").Append(virtualMachine.Name) + .Append("\n\tResource group: ").Append(virtualMachine.ResourceGroupName) + .Append("\n\tRegion: ").Append(virtualMachine.Region) + .Append("\n\tTags: ").Append(FormatDictionary(virtualMachine.Tags)) + .Append("\n\tHardwareProfile: ") + .Append("\n\t\tSize: ").Append(virtualMachine.Size) + .Append(storageProfile) + .Append(osProfile) + .Append(networkProfile) + .ToString()); + } + + public static void PrintStorageAccountKeys(IList storageAccountKeys) + { + foreach (var storageAccountKey in storageAccountKeys) + { + Console.WriteLine($"Key {storageAccountKey.KeyName} = {storageAccountKey.Value}"); + } + } + + public static void PrintStorageAccount(IStorageAccount storageAccount) + { + Console.WriteLine($"{storageAccount.Name} created @ {storageAccount.CreationTime}"); + } + + public static string CreateRandomName(string namePrefix) + { + var root = Guid.NewGuid().ToString().Replace("-", ""); + return $"{namePrefix}{root.ToLower().Substring(0, 3)}{(DateTime.UtcNow.Millisecond % 10000000L)}"; + } + + public static void PrintAvailabilitySet(IAvailabilitySet resource) + { + Console.WriteLine(new StringBuilder().Append("Availability Set: ").Append(resource.Id) + .Append("Name: ").Append(resource.Name) + .Append("\n\tResource group: ").Append(resource.ResourceGroupName) + .Append("\n\tRegion: ").Append(resource.Region) + .Append("\n\tTags: ").Append(FormatDictionary(resource.Tags)) + .Append("\n\tFault domain count: ").Append(resource.FaultDomainCount) + .Append("\n\tUpdate domain count: ").Append(resource.UpdateDomainCount) + .ToString()); + } + + public static void PrintBatchAccount(IBatchAccount batchAccount) + { + var applicationsOutput = new StringBuilder().Append("\n\tapplications: "); + + if (batchAccount.Applications.Count > 0) + { + foreach (var applicationEntry in batchAccount.Applications) + { + var application = applicationEntry.Value; + var applicationPackages = new StringBuilder().Append("\n\t\t\tapplicationPackages : "); + + foreach (var applicationPackageEntry in application.ApplicationPackages) + { + var applicationPackage = applicationPackageEntry.Value; + var singleApplicationPackage = new StringBuilder().Append("\n\t\t\t\tapplicationPackage : " + applicationPackage.Name); + singleApplicationPackage.Append("\n\t\t\t\tapplicationPackageState : " + applicationPackage.State); + + applicationPackages.Append(singleApplicationPackage); + singleApplicationPackage.Append("\n"); + } + + var singleApplication = new StringBuilder().Append("\n\t\tapplication: " + application.Name); + singleApplication.Append("\n\t\tdisplayName: " + application.DisplayName); + singleApplication.Append("\n\t\tdefaultVersion: " + application.DefaultVersion); + singleApplication.Append(applicationPackages); + applicationsOutput.Append(singleApplication); + applicationsOutput.Append("\n"); + } + } + + Console.WriteLine(new StringBuilder().Append("BatchAccount: ").Append(batchAccount.Id) + .Append("Name: ").Append(batchAccount.Name) + .Append("\n\tResource group: ").Append(batchAccount.ResourceGroupName) + .Append("\n\tRegion: ").Append(batchAccount.Region) + .Append("\n\tTags: ").Append(FormatDictionary(batchAccount.Tags)) + .Append("\n\tAccountEndpoint: ").Append(batchAccount.AccountEndpoint) + .Append("\n\tPoolQuota: ").Append(batchAccount.PoolQuota) + .Append("\n\tActiveJobAndJobScheduleQuota: ").Append(batchAccount.ActiveJobAndJobScheduleQuota) + .Append("\n\tStorageAccount: ").Append(batchAccount.AutoStorage == null ? "No storage account attached" : batchAccount.AutoStorage.StorageAccountId) + .Append(applicationsOutput) + .ToString()); + } + + public static void PrintBatchAccountKey(BatchAccountKeys batchAccountKeys) + { + Console.WriteLine("Primary Key (" + batchAccountKeys.Primary + ") Secondary key = (" + + batchAccountKeys.Secondary + ")"); + } + + public static void PrintNetworkSecurityGroup(INetworkSecurityGroup resource) + { + var nsgOutput = new StringBuilder(); + nsgOutput.Append("NSG: ").Append(resource.Id) + .Append("Name: ").Append(resource.Name) + .Append("\n\tResource group: ").Append(resource.ResourceGroupName) + .Append("\n\tRegion: ").Append(resource.RegionName) + .Append("\n\tTags: ").Append(FormatDictionary(resource.Tags)); + + // Output security rules + foreach (var rule in resource.SecurityRules.Values) + { + nsgOutput.Append("\n\tRule: ").Append(rule.Name) + .Append("\n\t\tAccess: ").Append(rule.Access) + .Append("\n\t\tDirection: ").Append(rule.Direction) + .Append("\n\t\tFrom address: ").Append(rule.SourceAddressPrefix) + .Append("\n\t\tFrom port range: ").Append(rule.SourcePortRange) + .Append("\n\t\tTo address: ").Append(rule.DestinationAddressPrefix) + .Append("\n\t\tTo port: ").Append(rule.DestinationPortRange) + .Append("\n\t\tProtocol: ").Append(rule.Protocol) + .Append("\n\t\tPriority: ").Append(rule.Priority); + } + Console.WriteLine(nsgOutput.ToString()); + } + + public static void PrintVirtualNetwork(INetwork network) + { + var info = new StringBuilder(); + info.Append("Network: ").Append(network.Id) + .Append("Name: ").Append(network.Name) + .Append("\n\tResource group: ").Append(network.ResourceGroupName) + .Append("\n\tRegion: ").Append(network.Region) + .Append("\n\tTags: ").Append(FormatDictionary(network.Tags)) + .Append("\n\tAddress spaces: ").Append(FormatCollection(network.AddressSpaces)) + .Append("\n\tDNS server IPs: ").Append(FormatCollection(network.DnsServerIps)); + + // Output subnets + foreach (var subnet in network.Subnets.Values) + { + info.Append("\n\tSubnet: ").Append(subnet.Name) + .Append("\n\t\tAddress prefix: ").Append(subnet.AddressPrefix); + var subnetNsg = subnet.GetNetworkSecurityGroup(); + if (subnetNsg != null) + { + info.Append("\n\t\tNetwork security group: ").Append(subnetNsg.Id); + } + } + + Console.WriteLine(info.ToString()); + } + + public static void PrintIpAddress(IPublicIpAddress publicIpAddress) + { + Console.WriteLine(new StringBuilder().Append("Public IP Address: ").Append(publicIpAddress.Id) + .Append("Name: ").Append(publicIpAddress.Name) + .Append("\n\tResource group: ").Append(publicIpAddress.ResourceGroupName) + .Append("\n\tRegion: ").Append(publicIpAddress.Region) + .Append("\n\tTags: ").Append(FormatDictionary(publicIpAddress.Tags)) + .Append("\n\tIP Address: ").Append(publicIpAddress.IpAddress) + .Append("\n\tLeaf domain label: ").Append(publicIpAddress.LeafDomainLabel) + .Append("\n\tFQDN: ").Append(publicIpAddress.Fqdn) + .Append("\n\tReverse FQDN: ").Append(publicIpAddress.ReverseFqdn) + .Append("\n\tIdle timeout (minutes): ").Append(publicIpAddress.IdleTimeoutInMinutes) + .Append("\n\tIP allocation method: ").Append(publicIpAddress.IpAllocationMethod) + .ToString()); + } + + public static void PrintNetworkInterface(INetworkInterface resource) + { + var info = new StringBuilder(); + info.Append("NetworkInterface: ").Append(resource.Id) + .Append("Name: ").Append(resource.Name) + .Append("\n\tResource group: ").Append(resource.ResourceGroupName) + .Append("\n\tRegion: ").Append(resource.Region) + .Append("\n\tTags: ").Append(FormatDictionary(resource.Tags)) + .Append("\n\tInternal DNS name label: ").Append(resource.InternalDnsNameLabel) + .Append("\n\tInternal FQDN: ").Append(resource.InternalFqdn) + .Append("\n\tInternal domain name suffix: ").Append(resource.InternalDomainNameSuffix) + .Append("\n\tNetwork security group: ").Append(resource.NetworkSecurityGroupId) + .Append("\n\tApplied DNS servers: ").Append(FormatCollection(resource.AppliedDnsServers)) + .Append("\n\tDNS server IPs: "); + + // Output dns servers + foreach (var dnsServerIp in resource.DnsServers) + { + info.Append("\n\t\t").Append(dnsServerIp); + } + + info.Append("\n\t IP forwarding enabled: ").Append(resource.IsIpForwardingEnabled) + .Append("\n\tMAC Address:").Append(resource.MacAddress) + .Append("\n\tPrivate IP:").Append(resource.PrimaryPrivateIp) + .Append("\n\tPrivate allocation method:").Append(resource.PrimaryPrivateIpAllocationMethod) + .Append("\n\tPrimary virtual network ID: ").Append(resource.PrimaryIpConfiguration.NetworkId) + .Append("\n\tPrimary subnet name:").Append(resource.PrimaryIpConfiguration.SubnetName); + + Console.WriteLine(info.ToString()); + } + + public static void PrintLoadBalancer(ILoadBalancer loadBalancer) + { + var info = new StringBuilder(); + info.Append("Load balancer: ").Append(loadBalancer.Id) + .Append("Name: ").Append(loadBalancer.Name) + .Append("\n\tResource group: ").Append(loadBalancer.ResourceGroupName) + .Append("\n\tRegion: ").Append(loadBalancer.Region) + .Append("\n\tTags: ").Append(FormatDictionary(loadBalancer.Tags)) + .Append("\n\tBackends: ").Append(FormatCollection(loadBalancer.Backends.Keys)); + + // Show public IP addresses + info.Append("\n\tPublic IP address IDs: ") + .Append(loadBalancer.PublicIpAddressIds.Count); + foreach (var pipId in loadBalancer.PublicIpAddressIds) + { + info.Append("\n\t\tPIP id: ").Append(pipId); + } + + // Show TCP probes + info.Append("\n\tTCP probes: ") + .Append(loadBalancer.TcpProbes.Count); + foreach (var probe in loadBalancer.TcpProbes.Values) + { + info.Append("\n\t\tProbe name: ").Append(probe.Name) + .Append("\n\t\t\tPort: ").Append(probe.Port) + .Append("\n\t\t\tInterval in seconds: ").Append(probe.IntervalInSeconds) + .Append("\n\t\t\tRetries before unhealthy: ").Append(probe.NumberOfProbes); + + // Show associated load balancing rules + info.Append("\n\t\t\tReferenced from load balancing rules: ") + .Append(probe.LoadBalancingRules.Count); + foreach (var rule in probe.LoadBalancingRules.Values) + { + info.Append("\n\t\t\t\tName: ").Append(rule.Name); + } + } + + // Show HTTP probes + info.Append("\n\tHTTP probes: ") + .Append(loadBalancer.HttpProbes.Count); + foreach (var probe in loadBalancer.HttpProbes.Values) + { + info.Append("\n\t\tProbe name: ").Append(probe.Name) + .Append("\n\t\t\tPort: ").Append(probe.Port) + .Append("\n\t\t\tInterval in seconds: ").Append(probe.IntervalInSeconds) + .Append("\n\t\t\tRetries before unhealthy: ").Append(probe.NumberOfProbes) + .Append("\n\t\t\tHTTP request path: ").Append(probe.RequestPath); + + // Show associated load balancing rules + info.Append("\n\t\t\tReferenced from load balancing rules: ") + .Append(probe.LoadBalancingRules.Count); + foreach (var rule in probe.LoadBalancingRules.Values) + { + info.Append("\n\t\t\t\tName: ").Append(rule.Name); + } + } + + // Show load balancing rules + info.Append("\n\tLoad balancing rules: ") + .Append(loadBalancer.LoadBalancingRules.Count); + foreach (var rule in loadBalancer.LoadBalancingRules.Values) + { + info.Append("\n\t\tLB rule name: ").Append(rule.Name) + .Append("\n\t\t\tProtocol: ").Append(rule.Protocol) + .Append("\n\t\t\tFloating IP enabled? ").Append(rule.FloatingIpEnabled) + .Append("\n\t\t\tIdle timeout in minutes: ").Append(rule.IdleTimeoutInMinutes) + .Append("\n\t\t\tLoad distribution method: ").Append(rule.LoadDistribution.ToString()); + + var frontend = rule.Frontend; + info.Append("\n\t\t\tFrontend: "); + if (frontend != null) + { + info.Append(frontend.Name); + } + else + { + info.Append("(None)"); + } + + info.Append("\n\t\t\tFrontend port: ").Append(rule.FrontendPort); + + var backend = rule.Backend; + info.Append("\n\t\t\tBackend: "); + if (backend != null) + { + info.Append(backend.Name); + } + else + { + info.Append("(None)"); + } + + info.Append("\n\t\t\tBackend port: ").Append(rule.BackendPort); + + var probe = rule.Probe; + info.Append("\n\t\t\tProbe: "); + if (probe == null) + { + info.Append("(None)"); + } + else + { + info.Append(probe.Name).Append(" [").Append(probe.Protocol.ToString()).Append("]"); + } + } + + // Show frontends + info.Append("\n\tFrontends: ") + .Append(loadBalancer.Frontends.Count); + foreach (var frontend in loadBalancer.Frontends.Values) + { + info.Append("\n\t\tFrontend name: ").Append(frontend.Name) + .Append("\n\t\t\tInternet facing: ").Append(frontend.IsPublic); + if (frontend.IsPublic) + { + info.Append("\n\t\t\tPublic IP Address ID: ").Append(((IPublicFrontend)frontend).PublicIpAddressId); + } + else + { + info.Append("\n\t\t\tVirtual network ID: ").Append(((IPrivateFrontend)frontend).NetworkId) + .Append("\n\t\t\tSubnet name: ").Append(((IPrivateFrontend)frontend).SubnetName) + .Append("\n\t\t\tPrivate IP address: ").Append(((IPrivateFrontend)frontend).PrivateIpAddress) + .Append("\n\t\t\tPrivate IP allocation method: ").Append(((IPrivateFrontend)frontend).PrivateIpAllocationMethod); + } + + // Inbound NAT pool references + info.Append("\n\t\t\tReferenced inbound NAT pools: ") + .Append(frontend.InboundNatPools.Count); + foreach (var pool in frontend.InboundNatPools.Values) + { + info.Append("\n\t\t\t\tName: ").Append(pool.Name); + } + + // Inbound NAT rule references + info.Append("\n\t\t\tReferenced inbound NAT rules: ") + .Append(frontend.InboundNatRules.Count); + foreach (var rule in frontend.InboundNatRules.Values) + { + info.Append("\n\t\t\t\tName: ").Append(rule.Name); + } + + // Load balancing rule references + info.Append("\n\t\t\tReferenced load balancing rules: ") + .Append(frontend.LoadBalancingRules.Count); + foreach (var rule in frontend.LoadBalancingRules.Values) + { + info.Append("\n\t\t\t\tName: ").Append(rule.Name); + } + } + + // Show inbound NAT rules + info.Append("\n\tInbound NAT rules: ") + .Append(loadBalancer.InboundNatRules.Count); + foreach (var natRule in loadBalancer.InboundNatRules.Values) + { + info.Append("\n\t\tInbound NAT rule name: ").Append(natRule.Name) + .Append("\n\t\t\tProtocol: ").Append(natRule.Protocol.ToString()) + .Append("\n\t\t\tFrontend: ").Append(natRule.Frontend.Name) + .Append("\n\t\t\tFrontend port: ").Append(natRule.FrontendPort) + .Append("\n\t\t\tBackend port: ").Append(natRule.BackendPort) + .Append("\n\t\t\tBackend NIC ID: ").Append(natRule.BackendNetworkInterfaceId) + .Append("\n\t\t\tBackend NIC IP config name: ").Append(natRule.BackendNicIpConfigurationName) + .Append("\n\t\t\tFloating IP? ").Append(natRule.FloatingIpEnabled) + .Append("\n\t\t\tIdle timeout in minutes: ").Append(natRule.IdleTimeoutInMinutes); + } + + // Show inbound NAT pools + info.Append("\n\tInbound NAT pools: ") + .Append(loadBalancer.InboundNatPools.Count); + foreach (var natPool in loadBalancer.InboundNatPools.Values) + { + info.Append("\n\t\tInbound NAT pool name: ").Append(natPool.Name) + .Append("\n\t\t\tProtocol: ").Append(natPool.Protocol.ToString()) + .Append("\n\t\t\tFrontend: ").Append(natPool.Frontend.Name) + .Append("\n\t\t\tFrontend port range: ") + .Append(natPool.FrontendPortRangeStart) + .Append("-") + .Append(natPool.FrontendPortRangeEnd) + .Append("\n\t\t\tBackend port: ").Append(natPool.BackendPort); + } + + // Show backends + info.Append("\n\tBackends: ") + .Append(loadBalancer.Backends.Count); + foreach (var backend in loadBalancer.Backends.Values) + { + info.Append("\n\t\tBackend name: ").Append(backend.Name); + + // Show assigned backend NICs + info.Append("\n\t\t\tReferenced NICs: ") + .Append(backend.BackendNicIpConfigurationNames.Count); + foreach (var entry in backend.BackendNicIpConfigurationNames) + { + info.Append("\n\t\t\t\tNIC ID: ").Append(entry.Key) + .Append(" - IP Config: ").Append(entry.Value); + } + + // Show assigned virtual machines + var vmIds = backend.GetVirtualMachineIds(); + info.Append("\n\t\t\tReferenced virtual machine ids: ") + .Append(vmIds.Count); + foreach (string vmId in vmIds) + { + info.Append("\n\t\t\t\tVM ID: ").Append(vmId); + } + + // Show assigned load balancing rules + info.Append("\n\t\t\tReferenced load balancing rules: ") + .Append(FormatCollection(backend.LoadBalancingRules.Keys)); + } + + Console.WriteLine(info.ToString()); + } + + public static void PrintVault(IVault vault) + { + var info = new StringBuilder().Append("Key Vault: ").Append(vault.Id) + .Append("Name: ").Append(vault.Name) + .Append("\n\tResource group: ").Append(vault.ResourceGroupName) + .Append("\n\tRegion: ").Append(vault.Region) + .Append("\n\tSku: ").Append(vault.Sku.Name).Append(" - ").Append(KeyVault.Fluent.Models.Sku.Family) + .Append("\n\tVault URI: ").Append(vault.VaultUri) + .Append("\n\tAccess policies: "); + foreach (var accessPolicy in vault.AccessPolicies) + { + info.Append("\n\t\tIdentity:").Append(accessPolicy.ObjectId) + .Append("\n\t\tKey permissions: ").Append(FormatCollection(accessPolicy.Permissions.Keys)) + .Append("\n\t\tSecret permissions: ").Append(FormatCollection(accessPolicy.Permissions.Secrets)); + } + + Console.WriteLine(info.ToString()); + } + + private static string FormatDictionary(IDictionary dictionary) + { + if (dictionary == null) + { + return string.Empty; + } + + var outputString = new StringBuilder(); + + foreach (var entity in dictionary) + { + outputString.AppendLine($"{entity.Key}: {entity.Value}"); + } + + return outputString.ToString(); + } + + private static string FormatCollection(IEnumerable collection) + { + return string.Join(", ", collection); + } + } +} \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d8d98a8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Microsoft Corporation + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/ManageStorageAccount.xproj b/ManageStorageAccount.xproj new file mode 100644 index 0000000..552f900 --- /dev/null +++ b/ManageStorageAccount.xproj @@ -0,0 +1,21 @@ + + + + 14.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + + 558da063-48b4-48d9-a731-e018c03664a5 + ManageStorageAccount + .\obj + .\bin\ + v4.5.2 + + + + 2.0 + + + diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..1890b89 --- /dev/null +++ b/Program.cs @@ -0,0 +1,134 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +using Microsoft.Azure.Management.Fluent; +using Microsoft.Azure.Management.Resource.Fluent.Authentication; +using Microsoft.Azure.Management.Resource.Fluent.Core; +using Microsoft.Azure.Management.Samples.Common; +using System; + +namespace ManageStorageAccount +{ + /** + * Azure Storage sample for managing storage accounts - + * - Create a storage account + * - Get | regenerate storage account access keys + * - Create another storage account + * - List storage accounts + * - Delete a storage account. + */ + + public class Program + { + private static readonly string rgName = Utilities.CreateRandomName("rgSTMS"); + private static readonly string storageAccountName = Utilities.CreateRandomName("sa"); + private static readonly string storageAccountName2 = Utilities.CreateRandomName("sa2"); + + public static void Main(string[] args) + { + try + { + //================================================================= + // Authenticate + var credentials = AzureCredentials.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION")); + + var azure = Azure + .Configure() + .WithLogLevel(HttpLoggingDelegatingHandler.Level.BASIC) + .Authenticate(credentials) + .WithDefaultSubscription(); + + // Print selected subscription + Console.WriteLine("Selected subscription: " + azure.SubscriptionId); + + try + { + // ============================================================ + // Create a storage account + + Console.WriteLine("Creating a Storage Account"); + + var storageAccount = azure.StorageAccounts.Define(storageAccountName) + .WithRegion(Region.US_EAST) + .WithNewResourceGroup(rgName) + .Create(); + + Console.WriteLine("Created a Storage Account:"); + Utilities.PrintStorageAccount(storageAccount); + + // ============================================================ + // Get | regenerate storage account access keys + + Console.WriteLine("Getting storage account access keys"); + + var storageAccountKeys = storageAccount.GetKeys(); + + Utilities.PrintStorageAccountKeys(storageAccountKeys); + + Console.WriteLine("Regenerating first storage account access key"); + + storageAccountKeys = storageAccount.RegenerateKey(storageAccountKeys[0].KeyName); + + Utilities.PrintStorageAccountKeys(storageAccountKeys); + + // ============================================================ + // Create another storage account + + Console.WriteLine("Creating a 2nd Storage Account"); + + var storageAccount2 = azure.StorageAccounts.Define(storageAccountName2) + .WithRegion(Region.US_EAST) + .WithNewResourceGroup(rgName) + .Create(); + + Console.WriteLine("Created a Storage Account:"); + Utilities.PrintStorageAccount(storageAccount2); + + // ============================================================ + // List storage accounts + + Console.WriteLine("Listing storage accounts"); + + var storageAccounts = azure.StorageAccounts; + + var accounts = storageAccounts.ListByGroup(rgName); + + foreach (var account in accounts) + { + Console.WriteLine($"Storage Account {account.Name} created @ {account.CreationTime}"); + } + + // ============================================================ + // Delete a storage account + + Console.WriteLine($"Deleting a storage account - {accounts[0].Name} created @ {accounts[0].CreationTime}"); + + azure.StorageAccounts.Delete(accounts[0].Id); + + Console.WriteLine("Deleted storage account"); + } + catch (Exception ex) + { + Console.WriteLine(ex); + } + finally + { + if (azure.ResourceGroups.GetByName(rgName) != null) + { + Console.WriteLine("Deleting Resource Group: " + rgName); + azure.ResourceGroups.Delete(rgName); + Console.WriteLine("Deleted Resource Group: " + rgName); + } + else + { + Console.WriteLine("Did not create any resources in Azure. No clean up is necessary"); + } + } + } + catch (Exception ex) + { + Console.WriteLine(ex); + } + } + } +} \ No newline at end of file diff --git a/README.md b/README.md index ab32df9..90ad77f 100644 --- a/README.md +++ b/README.md @@ -1 +1,39 @@ -# storage-dotnet-manage-storage-accounts +--- +services: Storage +platforms: .Net +author: selvasingh +--- + +#Getting Started with Storage - Manage Storage Account - in .Net # + + Azure Storage sample for managing storage accounts - + - Create a storage account + - Get | regenerate storage account access keys + - Create another storage account + - List storage accounts + - Delete a storage account. + + +## Running this Sample ## + +To run this sample: + +Set the environment variable `AZURE_AUTH_LOCATION` with the full path for an auth file. See [how to create an auth file](https://github.com/Azure/azure-sdk-for-net/blob/Fluent/AUTH.md). + + git clone https://github.com/Azure-Samples/storage-dotnet-manage-storage-accounts.git + + cd storage-dotnet-manage-storage-accounts + + dotnet restore + + dotnet run + +## More information ## + +[Azure Management Libraries for C#](https://github.com/Azure/azure-sdk-for-net/tree/Fluent) +[Azure .Net Developer Center](https://azure.microsoft.com/en-us/develop/net/) +If you don't have a Microsoft Azure subscription you can get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212) + +--- + +This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. \ No newline at end of file diff --git a/project.json b/project.json new file mode 100644 index 0000000..7f7af5e --- /dev/null +++ b/project.json @@ -0,0 +1,20 @@ +{ + "version": "1.0.0-*", + "buildOptions": { + "emitEntryPoint": true + }, + + "dependencies": { + "Microsoft.Azure.Management.Fluent": "1.0.0-beta3", + "Microsoft.NETCore.App": { + "type": "platform", + "version": "1.0.0" + } + }, + + "frameworks": { + "netcoreapp1.0": { + "imports": "dnxcore50" + } + } +}