2013-01-21 17:53:59 -05:00
|
|
|
# Getting started with Fog::DNS and Dreamhost (2013/01/21)
|
|
|
|
|
|
|
|
You'll need a [Dreamhost](http://www.dreamhost.com) account and API key
|
|
|
|
to use this.
|
|
|
|
|
|
|
|
See http://wiki.dreamhost.com/API.
|
|
|
|
|
|
|
|
Create an API key selecting **'All dns functions'** to be able to add/remove/list
|
|
|
|
records.
|
|
|
|
|
|
|
|
## Create the service
|
|
|
|
|
2013-01-22 15:08:50 -05:00
|
|
|
We need to create the service first, using the API key from our account:
|
2013-01-21 17:53:59 -05:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
require 'fog'
|
|
|
|
require 'pp'
|
|
|
|
|
|
|
|
dh = Fog::DNS.new( :provider => "Dreamhost",
|
2013-01-22 15:08:50 -05:00
|
|
|
:dreamhost_api_key => '6SHU5P2HLDAYECUM' )
|
|
|
|
```
|
|
|
|
|
|
|
|
## List all the DNS zones
|
|
|
|
|
|
|
|
This will list all the DNS zones avaialble in your account:
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
dh.zones.each do |zone|
|
|
|
|
puts zone.domain
|
|
|
|
end
|
2013-01-21 17:53:59 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
## Retrieve all the records
|
|
|
|
|
2013-01-22 15:08:50 -05:00
|
|
|
List all the records available in your Dreamhost account, accross all the zones:
|
2013-01-21 17:53:59 -05:00
|
|
|
|
|
|
|
```ruby
|
2013-01-22 15:08:50 -05:00
|
|
|
dh.records.each do |r|
|
2013-01-21 17:53:59 -05:00
|
|
|
puts r.name
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2013-01-22 15:08:50 -05:00
|
|
|
If you want to fetch all the records in a single zone:
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
zone = dh.zones.get 'fog-dream.com'
|
|
|
|
zone.records.each do |r|
|
|
|
|
# do something with the record
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2013-01-21 17:53:59 -05:00
|
|
|
See http://wiki.dreamhost.com/API/Dns_commands#dns-list_records
|
|
|
|
|
2013-01-22 15:08:50 -05:00
|
|
|
## Retrieve a single record
|
2013-01-21 17:53:59 -05:00
|
|
|
|
2013-01-22 15:08:50 -05:00
|
|
|
Get a single record and do something with the attributes:
|
2013-01-21 17:53:59 -05:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
rec = dh.records.get 'msn.jabber.groo.com'
|
|
|
|
rec.type # A, CNAME, TXT, etc
|
|
|
|
rec.zone # zone the record belongs to
|
|
|
|
rec.account_id # Dreamhost account ID
|
|
|
|
rec.comment # Record text comment
|
|
|
|
rec.value # record value
|
|
|
|
```
|
|
|
|
|
2013-01-22 15:08:50 -05:00
|
|
|
## Create a new A record
|
|
|
|
|
|
|
|
Let's create a new A record:
|
2013-01-21 17:53:59 -05:00
|
|
|
|
2013-01-22 15:08:50 -05:00
|
|
|
```
|
|
|
|
zone = dh.zones.get 'rbel.co'
|
|
|
|
zone.records.create :name => 'stuff.rbel.co',
|
|
|
|
:type => 'TXT',
|
|
|
|
:value => 'foobar bar bar'
|
2013-01-21 17:53:59 -05:00
|
|
|
```
|
|
|
|
|
2013-01-22 15:08:50 -05:00
|
|
|
Since Dreamhost API does not support the concept of zone,
|
|
|
|
you can also use this code to accomplish the same thing:
|
2013-01-21 17:53:59 -05:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
dh.records.create(
|
|
|
|
:name => 'stuff.rbel.co',
|
|
|
|
:type => 'A',
|
|
|
|
:value => '8.8.8.8'
|
|
|
|
)
|
|
|
|
```
|
|
|
|
|
|
|
|
## Destroy all the records in a zone
|
|
|
|
|
|
|
|
```ruby
|
2013-01-22 15:08:50 -05:00
|
|
|
(dh.zones.get 'rbel.co').records.each do |rec|
|
2013-01-21 17:53:59 -05:00
|
|
|
rec.destroy
|
|
|
|
end
|
|
|
|
```
|
2013-01-22 15:08:50 -05:00
|
|
|
|
|
|
|
## Resources
|
|
|
|
|
|
|
|
The Dreamhost API:
|
|
|
|
|
|
|
|
http://wiki.dreamhost.com/Application_programming_interface
|
|
|
|
|
|
|
|
DNS API commands:
|
|
|
|
|
|
|
|
http://wiki.dreamhost.com/API/Dns_commands
|