Skip to content
This repository was archived by the owner on Jul 4, 2024. It is now read-only.

Commit b696d18

Browse files
author
Thomas Kuiper
committed
add domain check function which returns if a domain is availiable and the price (done during #pyhack Tokyo)
1 parent 38fde82 commit b696d18

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

gandi/cli/commands/domain.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import click
44

55
from gandi.cli.core.cli import cli
6-
from gandi.cli.core.utils import output_contact_info, output_domain
6+
from gandi.cli.core.utils import output_contact_info, output_domain, output_domain_check
77
from gandi.cli.core.params import pass_gandi
88

99

@@ -37,6 +37,18 @@ def info(gandi, resource):
3737
return result
3838

3939

40+
@cli.command()
41+
@click.argument('resource', metavar='DOMAIN', required=False)
42+
@pass_gandi
43+
def check(gandi, resource):
44+
"""Check domain availability and price."""
45+
46+
result = gandi.domain.check(resource)
47+
output_domain_check(gandi, result, justify=12)
48+
49+
return result
50+
51+
4052
@cli.command()
4153
@click.option('--domain', default=None, help='Name of the domain.')
4254
@click.option('--duration', default=1, prompt=True,

gandi/cli/core/utils/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,29 @@ def output_hostedcert(gandi, hcert, output_keys, justify=12):
449449
output_sub_line(gandi, 'type', vhost['type'], 10)
450450

451451

452+
def output_domain_check(gandi, domain, justify=12):
453+
""" Helper to output a domain information."""
454+
455+
if (domain['available'] == 'available'):
456+
output_line(gandi, 'Domain', domain['extension'], justify)
457+
for domains_avail in domain['prices']:
458+
r_phase = str(domains_avail['action']['param']['tld_phase'])
459+
for domain_prices in domains_avail['unit_price']:
460+
output_line (gandi, 'Phase', r_phase, justify)
461+
output_line (gandi, 'Min Duration', str(domain_prices['min_duration']), justify)
462+
output_line (gandi, 'Max Duration', str(domain_prices['min_duration']), justify)
463+
output_line (gandi, 'Price', str(domain_prices['price']), justify)
464+
output_line (gandi, 'Currency', str(domain_prices['currency']), justify)
465+
output_line (gandi, 'Price Type', str(domain_prices['price_type']), justify)
466+
for phases in domain['phases']:
467+
tldphase = "Start: " + str(phases['date_start']) + " "
468+
tldphase += "Start Gandi: " + str(phases['date_start_gandi']) + " ";
469+
tldphase += "End: " + str(phases['date_end']);
470+
output_line(gandi, str(phases['phase']), tldphase, justify)
471+
else:
472+
output_line (gandi, 'Status', str(domain['available']), justify)
473+
474+
452475
def output_domain(gandi, domain, output_keys, justify=12):
453476
""" Helper to output a domain information."""
454477
if 'nameservers' in domain:

gandi/cli/modules/domain.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Domain(GandiModule):
1010

1111
""" Module to handle CLI commands.
1212
13+
$ gandi domain check
1314
$ gandi domain create
1415
$ gandi domain info
1516
$ gandi domain list
@@ -26,6 +27,19 @@ def info(cls, fqdn):
2627
"""Display information about a domain."""
2728
return cls.call('domain.info', fqdn)
2829

30+
@classmethod
31+
def check(cls, fqdn):
32+
"""Check domain availability and price."""
33+
fqdn = fqdn.lower()
34+
35+
result = cls.call('domain.price', [fqdn])
36+
37+
while result[0]['available'] == 'pending':
38+
time.sleep(1)
39+
result = cls.call('domain.price', [fqdn])
40+
41+
return (result[0])
42+
2943
@classmethod
3044
def create(cls, fqdn, duration, owner, admin, tech, bill, nameserver,
3145
background):

gandicli.man.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Namespaces:
8585
* disk rollback Rollback a disk from a snapshot.
8686
* disk update Update a disk.
8787
* docker Manage docker instances.
88+
* domain check Check domain availability and price.
8889
* domain create Buy a domain.
8990
* domain renew Renew a domain.
9091
* domain info Display information about a domain.

0 commit comments

Comments
 (0)