Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of how to use python 3.6 requests to register/upload/create an image through an Openstack Glance API
#! /usr/bin/env python3.6
import json
import os
import requests
import urllib
import sys
region = 'DFW'
auth_url = 'https://identity.api.rackspacecloud.com/v2.0/tokens'
username = 'your_username_goes_here'
api_key = 'your_api_token_goes_here'
image_file_path = os.path.join('.', 'test_ubuntu_17_10.vhd.gz')
# Get an auth token
token = None
tenant_id = None
service_url = None
auth_data = {
'auth': {
'RAX-KSKEY:apiKeyCredentials': {
'username': username,
'apiKey': api_key,
},
},
}
auth_headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'User-Agent': 'python-glanceclient',
}
response = requests.post(auth_url,
data=json.dumps(auth_data),
headers=auth_headers,
verify=False)
if response.status_code == 200:
data = response.text
auth = json.loads(data)
token = auth.get('access', {}).get('token', {}).get('id', None)
tenant_id = auth.get('access', {}).get('token', {}).get('tenant', {}).get('id', None)
service_catalog = auth.get('access', {}).get('serviceCatalog', {})
endpoints = []
for entry in service_catalog:
if entry.get('name', None) == 'cloudImages':
endpoints = entry.get('endpoints', [])
for endpoint in endpoints:
if endpoint.get('region', None).lower() == region.lower():
service_url = endpoint.get('publicURL', None)
if not token:
print("Error: Failed to acquire auth token")
sys.exit(1)
if not tenant_id:
print("Error: Failed to acquire tenant ID")
sys.exit(1)
if not service_url:
print("Error: Failed to acquire service url")
sys.exit(1)
# Register the image
url = urllib.parse.urljoin(service_url, str(tenant_id), 'images')
image_file_size = str(os.path.getsize(image_file_path))
parameters = {
'X-Image-Meta-Property-auto_disk_config': 'disabled',
'X-Image-Meta-Property-cache_in_nova': 'True',
'X-Image-Meta-Property-com.rackspace__1__build_config_options': 'rack_user_only,base_mgdops_config,mailgun,backup_agent_only,backup_defaults,monitoring_agent_only,monitoring_defaults',
'X-Image-Meta-container_format': 'ovf',
'X-Image-Meta-disk_format': 'vhd',
'X-Image-Meta-Property-flavor_classes': '*,!onmetal,!onmetal2',
'X-Image-Meta-Property-hypervisor_version_requires': '>=6.1',
'X-Image-Meta-Property-image_type': 'base',
'X-Image-Meta-is_public': 'True',
'X-Image-Meta-min_disk': '10',
'X-Image-Meta-min_ram': '256',
'X-Image-Meta-name': 'Ubuntu 17.10 (Zesty Zapus) (PVHVM)',
'X-Image-Meta-Property-org.openstack__1__architecture': 'x64',
'X-Image-Meta-Property-org.openstack__1__os_distro': 'com.ubuntu',
'X-Image-Meta-Property-org.openstack__1__os_version': '17.10',
'X-Image-Meta-Property-os_distro': 'ubuntu',
'X-Image-Meta-Property-os_type': 'linux',
'X-Image-Meta-Property-vm_mode': 'hvm',
'X-Image-Meta-id': '7f1307a7-06e2-45cb-8b07-c8f8480a3937',
'X-Image-Meta-owner': str(tenant_id),
'X-Image-Meta-size': image_file_size,
'X-Image-Meta-Property-com.rackspace__1__release_build_date': '2017-10-18_12-00-00',
}
headers = {
'X-Auth-Token': token,
'Content-Length': image_file_size,
'Content-Type': 'application/octet-stream',
'User-Agent': 'python-glanceclient',
}
headers.update(parameters)
image = None
image_uuid = None
with open(image_file_path, 'rb') as image_file:
try:
response = requests.post(url,
data=image_file,
headers=headers,
verify=False)
if response.status_code in [200, 201]:
data = response.text
image = json.loads(data)
image = image.get('image', {})
image_uuid = image.get('id', None)
except Exception as ex:
print("Error: Failed to create image: {error}".format(error=ex))
sys.exit(1)
print("Registered image {image_uuid} in Glance".format(image_uuid=image_uuid))
#eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment