Change-Id: Ic4dee73aa7c0f407fa3fd757edf52a923ba5707d
11 KiB
Python Bindings
The python-designateclient package comes with python bindings for the Designate API. This can be used to interact with the Designate API from any python program.
Introduction
Below is a simple example of how to instantiate and perform basic tasks using the bindings.
#!/usr/bin/env python
from designateclient.v1 import Client
# Create an instance of the client, providing the necessary credentials
= Client(
client ="https://example.com:5000/v2.0/",
auth_url="openstack",
username="yadayada",
password="123456789"
tenant_id
)
# Fetch a list of the domains this user/tenant has access to
= client.domains.list()
domains
# Iterate the list, printing some useful information
for domain in domains:
print "Domain ID: %s, Name: %s" % (domain.id, domain.name)
And the output this program might produce:
$ python /tmp/example.py
Domain ID: 467f97b4-f074-4839-ae85-1a61fccfb83d, Name: example-one.com.
Domain ID: 6d3bf479-8a93-47ae-8c65-3dff8dba1b0d, Name: example-two.com.
Authentication
Designate supports either Keystone authentication, or no authentication at all.
Keystone Authentication
Below is a sample of standard authentication with keystone:
#!/usr/bin/env python
from designateclient.v1 import Client
# Create an instance of the client, providing the necessary credentials
= Client(
client ="https://example.com:5000/v2.0/",
auth_url="openstack",
username="yadayada",
password="123456789"
tenant_id )
Below is a sample of standard authentication with keystone, but also explicitly providing the endpoint to use:
Note
This is useful when a development Designate instances authenticates against a production Keystone.
#!/usr/bin/env python
from designateclient.v1 import Client
# Create an instance of the client, providing the necessary credentials
= Client(
client ="https://example.com:5000/v2.0/",
auth_url="openstack",
username="yadayada",
password="123456789",
tenant_id="https://127.0.0.1:9001/v1/"
endpoint )
No Authentication
Below is a sample of interaction with a non authenticated designate:
#!/usr/bin/env python
from designateclient.v1 import Client
# Create an instance of the client, providing the endpoint directly
= Client(
client ="https://127.0.0.1:9001/v1/"
endpoint )
Working with Domains
The Domain Object
Object Properties:
Property | Description |
---|---|
id | Domain ID |
name | Domain Name (e.g. example.com.) |
Domain Responsible Person Email (e.g. fred@example.com) | |
ttl | Default TTL for records |
serial | Domain Server Number |
created_at | Date and time this domain was created at |
updated_at | Date and time this domain was last updated |
description | Domain Description |
Listing Domains
#!/usr/bin/env python
from designateclient.v1 import Client
# Create an instance of the client
= Client(
client ="https://127.0.0.1:9001/v1/"
endpoint
)
# List All Domains
= client.domains.list() domains
Fetching a Domain by ID
#!/usr/bin/env python
from designateclient.v1 import Client
# Create an instance of the client
= Client(
client ="https://127.0.0.1:9001/v1/"
endpoint
)
= 'fb505f10-25df-11e3-8224-0800200c9a66'
domain_id
# Fetch the domain
= client.domains.get(domain_id) domain
Creating a Domain
#!/usr/bin/env python
from designateclient.v1 import Client
from designateclient.v1.domains import Domain
# Create an instance of the client
= Client(
client ="https://127.0.0.1:9001/v1/"
endpoint
)
# Create a new Domain object
= Domain(name="example.com.", email="fred@example.com")
domain
# Send the Create Domain API call
= client.domains.create(domain) domain
Updating a Domain
#!/usr/bin/env python
from designateclient.v1 import Client
# Create an instance of the client
= Client(
client ="https://127.0.0.1:9001/v1/"
endpoint
)
= 'fb505f10-25df-11e3-8224-0800200c9a66'
domain_id
# Fetch the domain
= client.domains.get(domain_id)
domain
# Update a value on the Domain
= 300
domain.ttl
# Send the Update Domain API call
= client.domains.update(domain) domain
Deleting a Domain
#!/usr/bin/env python
from designateclient.v1 import Client
# Create an instance of the client
= Client(
client ="https://127.0.0.1:9001/v1/"
endpoint
)
= 'fb505f10-25df-11e3-8224-0800200c9a66'
domain_id
# Fetch the domain
= client.domains.delete(domain_id) domains
Working with Records
The Record Object
Object Properties:
Property | Description |
---|---|
id | Record ID |
domain_id | Domain ID |
name | Record Name (e.g. example.com.) |
type | Record Type (e.g. A, AAAA, CNAME, MX, SRV etc) |
data | Record Data (e.g. 127.0.0.1) |
priority | Rercord Priority (Valid only for MX and SRV records) |
ttl | Record TTL |
created_at | Date and time this record was created at |
updated_at | Date and time this record was last updated |
description | Record Description |
Listing Records
#!/usr/bin/env python
from designateclient.v1 import Client
# Create an instance of the client
= Client(
client ="https://127.0.0.1:9001/v1/"
endpoint
)
= 'fb505f10-25df-11e3-8224-0800200c9a66'
domain_id
# List All Records
= client.records.list(domain_id) records
Fetching a Record by ID
#!/usr/bin/env python
from designateclient.v1 import Client
# Create an instance of the client
= Client(
client ="https://127.0.0.1:9001/v1/"
endpoint
)
= 'fb505f10-25df-11e3-8224-0800200c9a66'
domain_id = 'bd3e8520-25e0-11e3-8224-0800200c9a66'
record_id
# Fetch the record
= client.records.get(domain_id, record_id) records
Creating a Record
#!/usr/bin/env python
from designateclient.v1 import Client
from designateclient.v1.records import Record
# Create an instance of the client
= Client(
client ="https://127.0.0.1:9001/v1/"
endpoint
)
= 'fb505f10-25df-11e3-8224-0800200c9a66'
domain_id
# Create a new Record object
= Record(name="www.example.com.", type="A", data="127.0.0.1")
record
# Send the Create Record API call
= client.records.create(domain_id, record) record
Updating a Record
#!/usr/bin/env python
from designateclient.v1 import Client
# Create an instance of the client
= Client(
client ="https://127.0.0.1:9001/v1/"
endpoint
)
= 'fb505f10-25df-11e3-8224-0800200c9a66'
domain_id = 'bd3e8520-25e0-11e3-8224-0800200c9a66'
record_id
# Fetch the record
= client.records.get(record_id)
record
# Update a value on the Record
= 300
record.ttl
# Send the Update Record API call
= client.records.update(domain_id, record) record
Deleting a Record
#!/usr/bin/env python
from designateclient.v1 import Client
# Create an instance of the client
= Client(
client ="https://127.0.0.1:9001/v1/"
endpoint
)
= 'fb505f10-25df-11e3-8224-0800200c9a66'
domain_id = 'bd3e8520-25e0-11e3-8224-0800200c9a66'
record_id
# Fetch the record
= client.records.delete(domain_id, record_id) records
Working with Servers
The Server Object
Object Properties:
Property | Description |
---|---|
id | Server ID |
name | Server Name (e.g. example.com.) |
created_at | Date and time this server was created at |
updated_at | Date and time this server was last updated |
Listing Servers
#!/usr/bin/env python
from designateclient.v1 import Client
# Create an instance of the client
= Client(
client ="https://127.0.0.1:9001/v1/"
endpoint
)
# List All Servers
= client.servers.list() servers
Fetching a Server by ID
#!/usr/bin/env python
from designateclient.v1 import Client
# Create an instance of the client
= Client(
client ="https://127.0.0.1:9001/v1/"
endpoint
)
= 'fb505f10-25df-11e3-8224-0800200c9a66'
server_id
# Fetch the server
= client.servers.get(server_id) server
Creating a Server
#!/usr/bin/env python
from designateclient.v1 import Client
from designateclient.v1.servers import Server
# Create an instance of the client
= Client(
client ="https://127.0.0.1:9001/v1/"
endpoint
)
# Create a new Server object
= Server(name="ns1.example.com.")
server
# Send the Create Server API call
= client.servers.create(server) server
Updating a Server
#!/usr/bin/env python
from designateclient.v1 import Client
# Create an instance of the client
= Client(
client ="https://127.0.0.1:9001/v1/"
endpoint
)
= 'fb505f10-25df-11e3-8224-0800200c9a66'
server_id
# Fetch the server
= client.servers.get(server_id)
server
# Update a value on the Server
= "ns2.example.com"
server.name
# Send the Update Server API call
= client.servers.update(server) server
Deleting a Server
#!/usr/bin/env python
from designateclient.v1 import Client
# Create an instance of the client
= Client(
client ="https://127.0.0.1:9001/v1/"
endpoint
)
= 'fb505f10-25df-11e3-8224-0800200c9a66'
server_id
# Fetch the server
= client.servers.delete(server_id) servers