简体   繁体   中英

python sdk resource group

I am using python for obtaining vnets, subnets, and all IP addresses which are in use (and tags for RG).

I would like to use get_client_from_cli_profile and login via az login.

I have a number of subscriptions but I am not able to list all RGs from each subscription.

    def test():
    subscription_client = get_client_from_cli_profile(SubscriptionClient)
    resource_client = get_client_from_cli_profile(ResourceManagementClient)
    out = ''

    for g in subscription_client.subscriptions.list():
        for v in resource_client.resource_groups.list(subscription_id=g.subscription_id):
            out = out + '\n' + " " + g.subscription_id + " " + v.name

    return out

print(test())

This code shows the same RG for each subscription. Do you know how I can list all subnets within vnet? Do you know how I can list all IP addresses within each subnet? I would like to check which IP addresses are used.

For your requirement, the Authentication you used is not an appropriate one. You can see the description of the CLI profile:

Return a SDK client initialized with current CLI credentials, CLI default subscription and CLI default cloud.

This code shows the same RG for each subscription

As I know, there will be only one subscription set as the default. You can see that in the output when you log in. So you got the same groups even if you set different subscriptions. And take a look at the list function for the group:

Gets all the resource groups for a subscription.

It does not have the parameter as subscription_id. The parameter filter just used to filter the group in the same subscription.

You can change the code like this to loop the different subscriptions and get groups in different subscriptions:

import subprocess

def test():
  subscription_client = get_client_from_cli_profile(SubscriptionClient)
  out = ''

  for g in subscription_client.subscriptions.list():
    cmd = f"az account set --subscription {g.subscription_id}"
    result = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    out, err = result.communicate()
    resource_client = get_client_from_cli_profile(ResourceManagementClient)
      for v in resource_client.resource_groups.list():
          out = out + '\n' + " " + g.subscription_id + " " + v.name

  return out

Do you know how I can list all subnets within vnet? Do you know how I can list all IP addresses within each subnet?

To get the subnets of a VNet. You can use the list function. This function will give you the address prefix of the subnet. It means what IP address you can use. There is no function to list all the IP addresses of the subnet one by one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM