0

I am looking for a way, in Python to use the OpenStack SDK to get specific metadata about an instance for a given region and project. I am looking to only get the id, name, ip address and key name. I was thinking of starting with "compute"

zadroga
  • 3
  • 3

1 Answers1

0

I have something rudimentary working using the openstack swift python api as follows:

"swiftsearch.py"

import logging
import pprint
from optparse import OptionParser

from swiftclient.service import SwiftService, SwiftError

logger = None
debugLevel = 0

def swift_search(container, search_field, search_value):
    global logger
    global debugLevel

    stat_search_field = 'x-object-meta-'+search_field

    with SwiftService() as swift:
        objects = []
        try:
            list_parts_gen = swift.list(container=container)
            for page in list_parts_gen:
                if page["success"]:
                    for item in page["listing"]:
                        if  (debugLevel > 0): print('list: {} item: {}'.format(item['name'], item))
                        objects.append(item['name'])
                else:
                    raise page["error"]

            if (debugLevel > 0): print('list: objects.len:',len(objects))

            header_data = {}
            stats_it = swift.stat(container=container, objects=objects)
            for stat_res in stats_it:
                if stat_res['success']:
                    # header_data[stat_res['object']] = stat_res['headers']
                    if (stat_search_field in stat_res['headers']):
                        if (debugLevel > 0): print('stat_search_field:',stat_search_field, stat_res['headers'][stat_search_field])

                        if (search_value is None):
                            header_data[stat_res['object']] = {
                                stat_search_field: stat_res['headers'][stat_search_field]
                            }
                        elif (stat_res['headers'][stat_search_field] == search_value):
                            header_data[stat_res['object']] = {
                                stat_search_field: stat_res['headers'][stat_search_field]
                            }
                else:
                    logger.error(
                        'Failed to retrieve stats for %s' % stat_res['object']
                    )
            pprint.pprint(header_data)

        except SwiftError as e:
            logger.error(e.value)

def main():
    global logger
    global debugLevel

    parser = OptionParser()

    parser.add_option("--container", type="string",\
                      help="container name to search", \
                      dest="container",
                      default='user_uploads')

    parser.add_option("--field", type="string",\
                      help="stat field to query", \
                      dest="field",
                      default='stnid')

    parser.add_option("--value", type="string",\
                      help="stat field match value", \
                      dest="value",
                      default=None)

    parser.add_option("--debuglevel", action="store", \
                      type="int", nargs=1, dest="debuglevel", default=0)

    (options, args) = parser.parse_args()
    container = options.container
    search_field = options.field
    search_value = options.value
    debugLevel = options.debuglevel

    logging.basicConfig(level=logging.ERROR)
    logging.getLogger("requests").setLevel(logging.CRITICAL)
    logging.getLogger("swiftclient").setLevel(logging.CRITICAL)
    logger = logging.getLogger(__name__)

    swift_search(container, search_field, search_value)

if __name__ == "__main__":
    main()

here are the results from running it:

python ./swiftsearch.py --container user_uploads --field=stnid --value=AMOSD
{'AMOSD-gust_spd_max.png': {'x-object-meta-stnid': 'AMOSD'}}

also, you must upload your objects with metadata attached like as follows:

upload --meta stnid:AMOSD --object-name AMOSD-gust_spd_max.png user_uploads AMOSD-gust_spd_max.png

also for my tests, I was using the docker container for a single instance openstack swift instance: https://hub.docker.com/r/fnndsc/docker-swift-onlyone

This solution isn't great in that it has to get the list of all elements and stat all elements in the container to get their metadata to filter on - an order 2n algorithm.

user3788120
  • 447
  • 4
  • 4