python yaml openstack how to access element of one group

69 views Asked by At

I have a yaml file like this:

clouds:

 all:
  gr1:
   id: '202401'
  gr2:
   id: '202402'
  gr3:
   id: '202403'
  gr4:
   id: '202404'

 group1:
  gr1:
   id: '202401'

 group2:
  gr2:
   id: '202402'

 group34:
  gr3:
   id: '202403'
  gr4:
   id: '202404'

This is my Python code:

import openstack
import yaml

with open('test.yaml', 'r') as f:
    reg = yaml.safe_load(f)

all_groups = []
for name in reg['clouds']['all']:
    all_groups.append(name)

group1 = []
for name in reg['clouds']['group1']:
    group1.append(name)

group34 = []
for name in reg['clouds']['group34']:
    group34.append(name)

# Initialize and turn on debug logging
openstack.enable_logging(debug=False)

# Initialize connection
for region in all_groups:
    conn = openstack.connect(cloud=region) # test.yaml

This is the error:

openstack.exceptions.ConfigException: Cloud gr1 was not found.

The error is correct because I have no clouds ==> gr1, and it should be like clouds ==> all ==> gr1, but I'm not sure how.

I even tried this (I'm thinking of something like this but not sure the correct way):

# Initialize connection
for region in all_groups:
    conn = openstack.connect(cloud=['all'][region]) # test.yaml

But got this error:

TypeError: list indices must be integers or slices, not str

How can I say in this context to access clouds ==> all ==> gr1 for the mentioned module I'm using.

I download the yaml file from my website which is like this:

clouds:

 gr1:
  id: '202401'
 gr2:
  id: '202402'

But I'm going to have file like in the beginning of the question.

1

There are 1 answers

1
larsks On

When you specify a cloud name, the OpenStack python modules look for a configuration file named clouds.yaml in a specific format. For this to work:

conn = openstack.connect(cloud='gr1')

You need to have a clouds.yaml file in either your current directory, in ~/.config/openstack, or in /etc/openstack, and that file needs to have the necessary configuration information for connecting to cloud gr1. That might look something like:

clouds:
  gr1:
    auth:
      auth_url: http://192.168.122.10:35357/
      project_name: demo
      username: demo
      password: secret
    region_name: RegionOne