Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
Merge pull request #115 from soulshake/diskattach
Browse files Browse the repository at this point in the history
Diskattach position
  • Loading branch information
sayoun committed Mar 31, 2015
2 parents 4347dca + 8c4112d commit 395cc3c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
9 changes: 7 additions & 2 deletions gandi/cli/commands/disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,18 @@ def detach(gandi, resource, background, force):
@cli.command()
@click.option('--bg', '--background', default=False, is_flag=True,
help='Run command in background mode (default=False).')
@click.option('-r', '--read-only', default=False, is_flag=True,
help='Attach disk as read-only')
@click.option('--position', '-p', type=click.INT,
help='Position where disk should be attached: 0 for system disk. '
'If there is already a disk attached at the specified position, it will be swapped.')
@click.option('--force', '-f', is_flag=True,
help='This is a dangerous option that will cause CLI to continue'
' without prompting. (default=False).')
@pass_gandi
@click.argument('disk', nargs=1, required=True)
@click.argument('vm', nargs=1, required=True)
def attach(gandi, disk, vm, background, force):
def attach(gandi, disk, vm, position, read_only, background, force):
""" Attach disk to vm.
disk can be a disk name, or ID
Expand All @@ -146,7 +151,7 @@ def attach(gandi, disk, vm, background, force):
if not proceed:
return

result = gandi.disk.attach(disk, vm, background)
result = gandi.disk.attach(disk, vm, background, position, read_only)
if background and result:
gandi.pretty_echo(result)

Expand Down
16 changes: 12 additions & 4 deletions gandi/cli/modules/disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,27 @@ def delete(cls, resources, background=False):
return opers

@classmethod
def _attach(cls, disk_id, vm_id):
def _attach(cls, disk_id, vm_id, options=None):
""" Attach a disk to a vm. """
oper = cls.call('hosting.vm.disk_attach', vm_id, disk_id)
options = options or {}
oper = cls.call('hosting.vm.disk_attach', vm_id, disk_id, options)
return oper

@classmethod
def attach(cls, disk, vm, background):
def attach(cls, disk, vm, background, position=None, read_only=False):
from gandi.cli.modules.iaas import Iaas as VM
vm_id = VM.usable_id(vm)

disk_id = cls.usable_id(disk)
disk_info = cls._info(disk_id)

options = {}
if position is not None:
options['position'] = position

if read_only:
options['access'] = 'read'

need_detach = disk_info.get('vms_id')
if need_detach:
if disk_info.get('vms_id') == [vm_id]:
Expand All @@ -209,7 +217,7 @@ def attach(cls, disk, vm, background):
cls.echo('Detaching your disk.')
cls.display_progress(detach_op)

oper = cls._attach(disk_id, vm_id)
oper = cls._attach(disk_id, vm_id, options)

if oper and not background:
cls.echo('Attaching your disk(s).')
Expand Down
2 changes: 1 addition & 1 deletion gandicli.man.rst
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ Details:

* ``gandi disk update resource`` modify the options of a virtual disk. Possible options are ``--kernel KERNEL`` to setup or update disk kernel, ``--cmdline TEXT`` to change kernel cmdline, ``--name TEXT`` for the label of the virtual disk, ``--size SIZE[M|G|T]`` for the new size of the disk, ``--snapshotprofile TEXT`` to select a profile of snapshot to apply to the disk for keeping multiple version of data in a timeline. All these modification can be done as background process using the option ``--background`` (or ``--bg``).

* ``gandi disk attach disk vm`` attach the given disk to the given vm, if the disk is currently attached, it will start by detaching it. Possible option is ``--force`` to skip all questions about detaching and attaching. All these modification can be done as background process using the option ``--background`` (or ``--bg``).
* ``gandi disk attach disk vm`` attach the given disk to the given vm, if the disk is currently attached, it will start by detaching it. Possible options: ``--force`` to skip all questions about detaching and attaching; ``--position INTEGER`` (or ``-p``) to specify the position at which the disk should be attached (0 for system disk); ``--read-only`` (or ``-r``) to attach the disk in read-only mode. All these modification can be done as background process using the option ``--background`` (or ``--bg``).

* ``gandi disk detach disk`` detach the disk from the vm it is currently attached. Possible option is ``--force`` to skip all questions about detaching. All these modification can be done as background process using the option ``--background`` (or ``--bg``).

Expand Down

0 comments on commit 395cc3c

Please sign in to comment.