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