Add CLI client interface to image upload

Closes #68
This commit is contained in:
Joshua Boniface 2020-02-09 20:42:56 -05:00
parent e419855911
commit 1de57ab6f3
3 changed files with 63 additions and 3 deletions

View File

@ -855,6 +855,29 @@ def ceph_volume_add(config, pool, volume, size):
return retstatus, response.json()['message']
def ceph_volume_upload(config, pool, volume, image_format, image_file):
"""
Upload a disk image to a Ceph volume
API endpoint: POST /api/v1/storage/ceph/volume/{pool}/{volume}/upload
API arguments: image_format={image_format}
API schema: {"message":"{data}"}
"""
params = {
'image_format': image_format
}
files = {
'file': open(image_file,'rb')
}
response = call_api(config, 'post', '/storage/ceph/volume/{}/{}/upload'.format(pool, volume), params=params, files=files)
if response.status_code == 200:
retstatus = True
else:
retstatus = False
return retstatus, response.json()['message']
def ceph_volume_remove(config, pool, volume):
"""
Remove Ceph volume

View File

@ -23,7 +23,7 @@
import requests
import click
def call_api(config, operation, request_uri, params=None, data=None):
def call_api(config, operation, request_uri, params=None, data=None, files=None):
# Craft the URI
uri = '{}://{}{}{}'.format(
config['api_scheme'],
@ -52,14 +52,16 @@ def call_api(config, operation, request_uri, params=None, data=None):
uri,
headers=headers,
params=params,
data=data
data=data,
files=files
)
if operation == 'put':
response = requests.put(
uri,
headers=headers,
params=params,
data=data
data=data,
files=files
)
if operation == 'patch':
response = requests.patch(

View File

@ -1726,6 +1726,40 @@ def ceph_volume_add(pool, name, size):
retcode, retmsg = pvc_ceph.ceph_volume_add(config, pool, name, size)
cleanup(retcode, retmsg)
###############################################################################
# pvc storage volume upload
###############################################################################
@click.command(name='upload', short_help='Upload a local image file to RBD volume.')
@click.argument(
'pool'
)
@click.argument(
'name'
)
@click.argument(
'image_file'
)
@click.option(
'-f', '--format', 'image_format',
default='raw', show_default=True,
help='The format of the source image.'
)
def ceph_volume_upload(pool, name, image_format, image_file):
"""
Upload a disk image file IMAGE_FILE to the RBD volume NAME in pool POOL.
The volume NAME must exist in the pool before uploading to it, and must be large enough to fit the disk image in raw format.
If the image format is "raw", the image is uploaded directly to the target volume without modification. Otherwise, it will be converted into raw format by "qemu-img convert" on the remote side before writing using a temporary volume. The image format must be a valid format recognized by "qemu-img", such as "vmdk" or "qcow2".
"""
if not os.path.exists(image_file):
click.echo("ERROR: File '{}' does not exist!".format(image_file))
exit(1)
retcode, retmsg = pvc_ceph.ceph_volume_upload(config, pool, name, image_format, image_file)
cleanup(retcode, retmsg)
###############################################################################
# pvc storage volume remove
###############################################################################
@ -3271,6 +3305,7 @@ ceph_pool.add_command(ceph_pool_remove)
ceph_pool.add_command(ceph_pool_list)
ceph_volume.add_command(ceph_volume_add)
ceph_volume.add_command(ceph_volume_upload)
ceph_volume.add_command(ceph_volume_resize)
ceph_volume.add_command(ceph_volume_rename)
ceph_volume.add_command(ceph_volume_clone)