Skip to content

Latest commit

 

History

History
55 lines (43 loc) · 1.63 KB

5.7. Querying Azure Resources.md

File metadata and controls

55 lines (43 loc) · 1.63 KB

Querying Azure Resources

Using Azure CLI

  • Azure CLI uses -query argument to execute a JMESPath query

    • JMESPath => JSON query language.
    • -query argument is supported by all commands in the Azure CLI.
  • Return type

    • JSON Array, no order guarantee.
  • Projection

    • E.g. select in LINQ
      az vm list --query '[].{name:name image:storageProfile.imageReference.offer}'
  • Filtering

    • E.g. where in LINQ
      az vm list --query "[?starts\_with(storageProfile.imageReference.offer, 'WindowsServer')]"
  • Combine project + filter

      az vm list --query "[?starts\_with(storageProfile.imageReference.offer, 'Ubuntu')].{name:name, id:vmId}

Using fluent Azure SDK

  • Better option if you intend to write code to find connection information for a specific application instance.
  • Flow:
    • Connect

      • You need azure.auth file (JSON file describing, secret, key url's etc)
      • You can create like this: az ad sp create-for-rbac --sdk-auth > azure.auth
      • Then Azure azure = Azure.Authenticate("azure.auth").WithDefaultSubscription();
    • See VMs

        var vms = await azure.VirtualMachines.ListAsync();
        foreach(var vm in vms)
        {
          Console.WriteLine(vm.Name);
        }
    • Gather virtual machine metadata to determine the IP address

        INetworkInterface targetNic = targetVm.GetPrimaryNetworkInterface();
        INicIPConfiguration targetIpConfig = targetNic.PrimaryIPConfiguration;
        IPublicIPAddress targetIpAddress = targetIpConfig.GetPublicIPAddress();
        Console.WriteLine($"IP Address:\t{targetIpAddress.IPAddress}");