Openstack-glance partial implementations and examples of API image management

Source: Internet
Author: User

openstack-glance API

for convenience, you can customize a function first put it in the credentials.py .

# VI credentials.py

#!/usr/bin/env python

Import OS

Def get_keystone_creds ():

d = {}

d[' username ' =os.environ[' os_username ']

d[' password ' =os.environ[' Os_password ']

d[' auth_url ' =os.environ[' Os_auth_url ']

d[' tenant_name ') = os.environ[' Os_tenant_name ']

return D

Def get_nova_creds ():

d = {}

d[' username ' =os.environ[' os_username ']

d[' api_key ' =os.environ[' Os_password ']

d[' auth_url ' =os.environ[' Os_auth_url ']

d[' project_id ']= os.environ[' os_tenant_name ']

return D

Use API when

Import Keystoneclient.v2_0.client as Ksclient

Import Glanceclient

Import Glanceclient.v2.client as Glclient

From credentials import Get_keystone_creds

( 1 ) query virtual machine information   

images = glance.images.list () Get img the queue

images.next () output in sequence

Part of the code:

Creds = Get_keystone_creds ()

Keystone = Ksclient. Client (**creds)

Glance_endpoint=keystone.service_catalog.url_for (service_type= ' image ', endpoint_type= ' PublicURL ')

Glance = glclient. Client (Glance_endpoint,token=keystone.auth_token)

Images = Glance.images.list ()

Print Images

Print Images.next ()

(2) Upload img

With Open (Img_url) as Fimage: #img_url Image Storage Path

glance.images.create (name="Cirros_zy", is_public=true,disk_format="Qcow2", Container_ format="Bare", Data=fimage)

Part of the code:

Keystone = Ksclient. Client (**creds)

Glance_endpoint=keystone.service_catalog.url_for (service_type= ' image ', endpoint_type= ' PublicURL ')

Glance=glanceclient. Client (' 1 ', Glance_endpoint,token=keystone.auth_token)

With open (Img_url) as Fimage:

Glance.images.create (name= "Cirros_zy", is_public=true,disk_format= "Qcow2", container_format= "bare", data=fimage)

( 3 ) Delete img

Glance.images.delete (image_id)

( 4 ) member Bindings

Glance.image_members.create (Image_id,mem_id,can_share=true)

( 5 ) member Delete

Glance.image_members.delete (image_id,mem_id)

( 6 ) img Download

because glance v2 does not support the image download API , it is directly downloaded using the command line

The code is as follows:

Import OS

string_1 = ' glance image-download--file ' + image_url + ' + image_id

Os.system (string_1)

Openstack-glance partial implementations and examples of API image management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.