Skip to content
This repository was archived by the owner on Jul 4, 2024. It is now read-only.
Open
30 changes: 27 additions & 3 deletions gandi/cli/commands/disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
@cli.command()
@click.option('--only-data', help='Only display data disks.', is_flag=True)
@click.option('--only-snapshot', help='Only display snapshots.', is_flag=True)
@click.option('--attached', help='Only display disks attached to a VM',
is_flag=True)
@click.option('--detached', help='Only display detached disks', is_flag=True)
@click.option('--type', help='Display types.', is_flag=True)
@click.option('--id', help='Display ids.', is_flag=True)
@click.option('--vm', help='Display vms.', is_flag=True)
Expand All @@ -26,13 +29,16 @@
@click.option('--limit', help='Limit number of results.', default=100,
show_default=True)
@pass_gandi
def list(gandi, only_data, only_snapshot, type, id, vm, snapshotprofile,
datacenter, limit):
def list(gandi, only_data, only_snapshot, attached, detached, type, id, vm,
snapshotprofile, datacenter, limit):
""" List disks. """
options = {
'items_per_page': limit,
}

if attached and detached:
raise UsageError('You cannot use both --attached and --detached.')

if only_data:
options.setdefault('type', []).append('data')
if only_snapshot:
Expand All @@ -56,7 +62,16 @@ def list(gandi, only_data, only_snapshot, type, id, vm, snapshotprofile,
result = gandi.disk.list(options)
vms = dict([(vm_['id'], vm_) for vm_ in gandi.iaas.list()])

for num, disk in enumerate(result):
# filter results per attached/detached
disks = []
for disk in result:
if attached and not disk['vms_id']:
continue
if detached and disk['vms_id']:
continue
disks.append(disk)

for num, disk in enumerate(disks):
if num:
gandi.separator_line()
output_disk(gandi, disk, [], vms, profiles, output_keys)
Expand Down Expand Up @@ -259,6 +274,15 @@ def create(gandi, name, vm, size, snapshotprofile, datacenter, source,
'please consider using another datacenter.' %
(datacenter, exc.date))

if vm:
vm_dc = gandi.iaas.info(vm)
vm_dc_id = vm_dc['datacenter_id']
dc_id = int(gandi.datacenter.usable_id(datacenter))
if vm_dc_id != dc_id:
gandi.echo('/!\ VM %s datacenter will be used instead of %s.'
% (vm, datacenter))
datacenter = vm_dc_id

output_keys = ['id', 'type', 'step']
name = name or randomstring('vdi')

Expand Down
14 changes: 13 additions & 1 deletion gandi/cli/commands/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import click

from gandi.cli.core.cli import cli
from gandi.cli.core.utils import output_contact_info, output_domain
from gandi.cli.core.utils import output_contact_info, output_domain, output_domain_check
from gandi.cli.core.params import pass_gandi


Expand Down Expand Up @@ -37,6 +37,18 @@ def info(gandi, resource):
return result


@cli.command()
@click.argument('resource', metavar='DOMAIN')
@pass_gandi
def check(gandi, resource):
"""Check domain availability and price."""

result_check = gandi.domain.check(resource)
output_domain_check(gandi, result_check)

return result_check


@cli.command()
@click.option('--domain', default=None, help='Name of the domain.')
@click.option('--duration', default=1, prompt=True,
Expand Down
23 changes: 23 additions & 0 deletions gandi/cli/core/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,29 @@ def output_hostedcert(gandi, hcert, output_keys, justify=12):
output_sub_line(gandi, 'type', vhost['type'], 10)


def output_domain_check(gandi, domain, justify=12):
""" Helper to output a domain information."""

if (domain['available'] == 'available'):
output_line(gandi, 'Domain', domain['extension'], justify)
for domains_avail in domain['prices']:
r_phase = str(domains_avail['action']['param']['tld_phase'])
for domain_prices in domains_avail['unit_price']:
output_line (gandi, 'Phase', r_phase, justify)
output_line (gandi, 'Min Duration', str(domain_prices['min_duration']), justify)
output_line (gandi, 'Max Duration', str(domain_prices['min_duration']), justify)
output_line (gandi, 'Price', str(domain_prices['price']), justify)
output_line (gandi, 'Currency', str(domain_prices['currency']), justify)
output_line (gandi, 'Price Type', str(domain_prices['price_type']), justify)
for phases in domain['phases']:
tldphase = "Start: " + str(phases['date_start']) + " "
tldphase += "Start Gandi: " + str(phases['date_start_gandi']) + " ";
tldphase += "End: " + str(phases['date_end']);
output_line(gandi, str(phases['phase']), tldphase, justify)
else:
output_line (gandi, 'Status', str(domain['available']), justify)


def output_domain(gandi, domain, output_keys, justify=12):
""" Helper to output a domain information."""
if 'nameservers' in domain:
Expand Down
14 changes: 14 additions & 0 deletions gandi/cli/modules/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Domain(GandiModule):

""" Module to handle CLI commands.

$ gandi domain check
$ gandi domain create
$ gandi domain info
$ gandi domain list
Expand All @@ -26,6 +27,19 @@ def info(cls, fqdn):
"""Display information about a domain."""
return cls.call('domain.info', fqdn)

@classmethod
def check(cls, fqdn):
"""Check domain availability and price."""
fqdn = fqdn.lower()

result = cls.call('domain.price', [fqdn])

while result[0]['available'] == 'pending':
time.sleep(1)
result = cls.call('domain.price', [fqdn])

return result[0]

@classmethod
def create(cls, fqdn, duration, owner, admin, tech, bill, nameserver,
background):
Expand Down
46 changes: 46 additions & 0 deletions gandi/cli/tests/commands/test_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,50 @@ def test_list_filter_datacenter(self):

self.assertEqual(result.exit_code, 0)

def test_list_attached(self):

result = self.invoke_with_exceptions(disk.list, ['--attached'])

self.assertEqual(result.output, """\
name : sys_1426759833
state : created
size : 3072
----------
name : sys_server01
state : created
size : 3072
----------
name : data
state : created
size : 3072
""")

self.assertEqual(result.exit_code, 0)

def test_list_detached(self):

result = self.invoke_with_exceptions(disk.list, ['--detached'])

self.assertEqual(result.output, """\
name : snaptest
state : created
size : 3072
""")

self.assertEqual(result.exit_code, 0)

def test_list_attached_detached_ko(self):
args = ['--detached', '--attached']
result = self.invoke_with_exceptions(disk.list, args)

self.assertEqual(result.output, """\
Usage: disk list [OPTIONS]

Error: You cannot use both --attached and --detached.
""")

self.assertEqual(result.exit_code, 2)

def test_info(self):
result = self.invoke_with_exceptions(disk.info, ['sys_server01'])

Expand Down Expand Up @@ -564,6 +608,7 @@ def test_create_vm(self):

self.assertEqual(re.sub(r'\[#+\]', '[###]',
result.output.strip()), """\
/!\ VM server01 datacenter will be used instead of LU-BI1.
Creating your disk.
\rProgress: [###] 100.00% 00:00:00 \
\nAttaching your disk.
Expand All @@ -572,6 +617,7 @@ def test_create_vm(self):
params = self.api_calls['hosting.disk.create'][0][0]
self.assertEqual(params['type'], 'data')
self.assertEqual(params['size'], 3072)
self.assertEqual(params['datacenter_id'], 1)
self.assertTrue(params['name'].startswith('vdi'))

def test_create_source(self):
Expand Down
20 changes: 20 additions & 0 deletions gandi/cli/tests/commands/test_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,23 @@ def test_available_with_exception_argument(self):
'--tech', 'TECH1-GANDI',
'--bill', 'BILL1-GANDI',
])

def test_domain_check(self):
result = self.invoke_with_exceptions(domain.check,
['idontlike.website'])
self.assertEqual(result.output, """Domain : idontlike.website
Phase : golive
Min Duration: 1
Max Duration: 1
Price : 0.99
Currency : EUR
Price Type : None
Phase : golive
Min Duration: 1
Max Duration: 1
Price : 16.16
Currency : EUR
Price Type : None
golive : Start: 2014-09-18 18:00:00 Start Gandi: 2014-07-01 10:19:56 End: None
""")
self.assertEqual(result.exit_code, 0)
5 changes: 4 additions & 1 deletion gandicli.man.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Namespaces:
* disk rollback Rollback a disk from a snapshot.
* disk update Update a disk.
* docker Manage docker instances.
* domain check Check domain availability and price.
* domain create Buy a domain.
* domain renew Renew a domain.
* domain info Display information about a domain.
Expand Down Expand Up @@ -212,7 +213,7 @@ Details:

* ``gandi disk info resource`` show a detailed view of a specific virtual disk identified as resource.

``gandi disk list`` show a list of virtual disk. Possible options to filter the list are : ``--only-data`` and ``--only-snapshot`` which limit the list to regular disk and to snapshots, ``--type`` add the type of the virtual disk, ``--id`` add the integer id of each virtual disk, ``--vm`` show the virtual machines by which the disk are used, ``--snapshotprofile`` show the profile of data retention associated, ``--datacenter`` which filter the output according to disk datacenter location and ``--limit INTEGER`` show only a limit amount of disks.
``gandi disk list`` show a list of virtual disk. Possible options to filter the list are : ``--only-data`` and ``--only-snapshot`` which limit the list to regular disk and to snapshots, ``--attached`` which limit the list to only attached disks, ``--detached`` which limit the list to only detached disks,``--type`` add the type of the virtual disk, ``--id`` add the integer id of each virtual disk, ``--vm`` show the virtual machines by which the disk are used, ``--snapshotprofile`` show the profile of data retention associated, ``--datacenter`` which filter the output according to disk datacenter location and ``--limit INTEGER`` show only a limit amount of disks.

* ``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, if optionnal + prefix is provided, size value will be added to current disk size, a size suffix (M for megabytes up to T for terabytes) is optional, megabytes is the default if no suffix is present, ``--snapshotprofile TEXT`` to select a profile of snapshot to apply to the disk for keeping multiple version of data in a timeline, ``--delete-snapshotprofile`` to remove snapshot profile associated to this virtual disk. All these modification can be done as background process using the option ``--background`` (or ``--bg``).

Expand All @@ -228,6 +229,8 @@ Details:

* ``gandi domain create domain.tld`` helps register a domain. Options are ``--domain domain.tld`` for the domain you want to get (/!\ this option is deprecated and will be removed upon next release), ``--duration INTEGER RANGE`` for the registration period, ``--owner TEXT``, ``--admin TEXT``, ``--tech TEXT``, ``--bill TEXT`` for the four contacts to pass to the creation process, ``--nameserver TEXT`` for adding custom nameservers. All these modification can be done as background process using the option ``--background`` (or ``--bg``).

* ``gandi domain check domain.tld`` Retrieve information about the specific domain availability and price.

* ``gandi domain renew domain.tld`` will renew a domain. Available option is ``--duration INTEGER RANGE`` for the registration period. All these modification can be done as background process using the option ``--background`` (or ``--bg``).

* ``gandi domain info domain.tld`` show information about the specific domain ``domain.tld`` : owner, admin, billing and technical contacts, fully qualified domain name, nameservers, associated zone, associated tags and more.
Expand Down