~ubuntu-branches/ubuntu/trusty/python-boto/trusty

« back to all changes in this revision

Viewing changes to bin/route53

  • Committer: Package Import Robot
  • Author(s): Eric Evans
  • Date: 2011-11-13 11:58:40 UTC
  • mfrom: (14.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20111113115840-ckzyt3h17uh8s41y
Tags: 2.0-2
Promote new upstream to unstable (Closes: #638931).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# Author: Chris Moyer
 
3
#
 
4
# route53 is similar to sdbadmin for Route53, it's a simple
 
5
# console utility to perform the most frequent tasks with Route53
 
6
 
 
7
def _print_zone_info(zoneinfo):
 
8
    print "="*80
 
9
    print "| ID:   %s" % zoneinfo['Id'].split("/")[-1]
 
10
    print "| Name: %s" % zoneinfo['Name']
 
11
    print "| Ref:  %s" % zoneinfo['CallerReference']
 
12
    print "="*80
 
13
    print zoneinfo['Config']
 
14
    print
 
15
    
 
16
 
 
17
def create(conn, hostname, caller_reference=None, comment=''):
 
18
    """Create a hosted zone, returning the nameservers"""
 
19
    response = conn.create_hosted_zone(hostname, caller_reference, comment)
 
20
    print "Pending, please add the following Name Servers:"
 
21
    for ns in response.NameServers:
 
22
        print "\t", ns
 
23
 
 
24
def delete_zone(conn, hosted_zone_id):
 
25
    """Delete a hosted zone by ID"""
 
26
    response = conn.delete_hosted_zone(hosted_zone_id)
 
27
    print response
 
28
 
 
29
def ls(conn):
 
30
    """List all hosted zones"""
 
31
    response = conn.get_all_hosted_zones()
 
32
    for zoneinfo in response['ListHostedZonesResponse']['HostedZones']:
 
33
        _print_zone_info(zoneinfo)
 
34
 
 
35
def get(conn, hosted_zone_id, type=None, name=None, maxitems=None):
 
36
    """Get all the records for a single zone"""
 
37
    response = conn.get_all_rrsets(hosted_zone_id, type, name, maxitems=maxitems)
 
38
    # If a maximum number of items was set, we limit to that number
 
39
    # by turning the response into an actual list (copying it)
 
40
    # instead of allowing it to page
 
41
    if maxitems:
 
42
        response = response[:]
 
43
    print '%-40s %-5s %-20s %s' % ("Name", "Type", "TTL", "Value(s)")
 
44
    for record in response:
 
45
        print '%-40s %-5s %-20s %s' % (record.name, record.type, record.ttl, record.to_print())
 
46
 
 
47
 
 
48
def add_record(conn, hosted_zone_id, name, type, values, ttl=600, comment=""):
 
49
    """Add a new record to a zone"""
 
50
    from boto.route53.record import ResourceRecordSets
 
51
    changes = ResourceRecordSets(conn, hosted_zone_id, comment)
 
52
    change = changes.add_change("CREATE", name, type, ttl)
 
53
    for value in values.split(','):
 
54
        change.add_value(value)
 
55
    print changes.commit()
 
56
 
 
57
def del_record(conn, hosted_zone_id, name, type, values, ttl=600, comment=""):
 
58
    """Delete a record from a zone"""
 
59
    from boto.route53.record import ResourceRecordSets
 
60
    changes = ResourceRecordSets(conn, hosted_zone_id, comment)
 
61
    change = changes.add_change("DELETE", name, type, ttl)
 
62
    for value in values.split(','):
 
63
        change.add_value(value)
 
64
    print changes.commit()
 
65
 
 
66
def add_alias(conn, hosted_zone_id, name, type, alias_hosted_zone_id, alias_dns_name, comment=""):
 
67
    """Add a new alias to a zone"""
 
68
    from boto.route53.record import ResourceRecordSets
 
69
    changes = ResourceRecordSets(conn, hosted_zone_id, comment)
 
70
    change = changes.add_change("CREATE", name, type)
 
71
    change.set_alias(alias_hosted_zone_id, alias_dns_name)
 
72
    print changes.commit()
 
73
 
 
74
def del_alias(conn, hosted_zone_id, name, type, alias_hosted_zone_id, alias_dns_name, comment=""):
 
75
    """Delete an alias from a zone"""
 
76
    from boto.route53.record import ResourceRecordSets
 
77
    changes = ResourceRecordSets(conn, hosted_zone_id, comment)
 
78
    change = changes.add_change("DELETE", name, type)
 
79
    change.set_alias(alias_hosted_zone_id, alias_dns_name)
 
80
    print changes.commit()
 
81
 
 
82
def change_record(conn, hosted_zone_id, name, type, values, ttl=600, comment=""):
 
83
    """Delete and then add a record to a zone"""
 
84
    from boto.route53.record import ResourceRecordSets
 
85
    changes = ResourceRecordSets(conn, hosted_zone_id, comment)
 
86
    response = conn.get_all_rrsets(hosted_zone_id, type, name, maxitems=1)[0]
 
87
    change1 = changes.add_change("DELETE", name, type, response.ttl)
 
88
    for old_value in response.resource_records:
 
89
        change1.add_value(old_value)
 
90
    change2 = changes.add_change("CREATE", name, type, ttl)
 
91
    for new_value in values.split(','):
 
92
        change2.add_value(new_value)
 
93
    print changes.commit()
 
94
 
 
95
def help(conn, fnc=None):
 
96
    """Prints this help message"""
 
97
    import inspect
 
98
    self = sys.modules['__main__']
 
99
    if fnc:
 
100
        try:
 
101
            cmd = getattr(self, fnc)
 
102
        except:
 
103
            cmd = None
 
104
        if not inspect.isfunction(cmd):
 
105
            print "No function named: %s found" % fnc
 
106
            sys.exit(2)
 
107
        (args, varargs, varkw, defaults) = inspect.getargspec(cmd)
 
108
        print cmd.__doc__
 
109
        print "Usage: %s %s" % (fnc, " ".join([ "[%s]" % a for a in args[1:]]))
 
110
    else:
 
111
        print "Usage: route53 [command]"
 
112
        for cname in dir(self):
 
113
            if not cname.startswith("_"):
 
114
                cmd = getattr(self, cname)
 
115
                if inspect.isfunction(cmd):
 
116
                    doc = cmd.__doc__
 
117
                    print "\t%-20s  %s" % (cname, doc)
 
118
    sys.exit(1)
 
119
 
 
120
 
 
121
if __name__ == "__main__":
 
122
    import boto
 
123
    import sys
 
124
    conn = boto.connect_route53()
 
125
    self = sys.modules['__main__']
 
126
    if len(sys.argv) >= 2:
 
127
        try:
 
128
            cmd = getattr(self, sys.argv[1])
 
129
        except:
 
130
            cmd = None
 
131
        args = sys.argv[2:]
 
132
    else:
 
133
        cmd = help
 
134
        args = []
 
135
    if not cmd:
 
136
        cmd = help
 
137
    try:
 
138
        cmd(conn, *args)
 
139
    except TypeError, e:
 
140
        print e
 
141
        help(conn, cmd.__name__)