Class: Fog::DNS::AzureRM::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/azurerm/dns.rb,
lib/fog/azurerm/requests/dns/list_zones.rb,
lib/fog/azurerm/requests/dns/create_zone.rb,
lib/fog/azurerm/requests/dns/delete_zone.rb,
lib/fog/azurerm/requests/dns/check_for_zone.rb,
lib/fog/azurerm/requests/dns/list_record_sets.rb,
lib/fog/azurerm/requests/dns/create_record_set.rb,
lib/fog/azurerm/requests/dns/delete_record_set.rb,
lib/fog/azurerm/requests/dns/get_records_from_record_set.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Real



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fog/azurerm/dns.rb', line 34

def initialize(options)
  @tenant_id = options[:tenant_id]
  @client_id = options[:client_id]
  @client_secret = options[:client_secret]
  @subscription_id = options[:subscription_id]
  @resources = Fog::Resources::AzureRM.new(
      tenant_id: options[:tenant_id],
      client_id: options[:client_id],
      client_secret: options[:client_secret],
      subscription_id: options[:subscription_id])
end

Instance Method Details

#check_for_zone(resource_group, name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fog/azurerm/requests/dns/check_for_zone.rb', line 5

def check_for_zone(resource_group, name)
  resource_url = "#{AZURE_RESOURCE}/subscriptions/#{@subscription_id}/resourceGroups/#{resource_group}/providers/Microsoft.Network/dnsZones/#{name}?api-version=2015-05-04-preview"
  begin
    token = Fog::Credentials::AzureRM.get_token(@tenant_id, @client_id, @client_secret)
    dns_response = RestClient.get(
        resource_url,
        accept: 'application/json',
        content_type: 'application/json',
        authorization: token)
    dns_hash = JSON.parse(dns_response)
    if dns_hash.key?('id') && !dns_hash['id'].nil?
      true
    else
      false
    end
  rescue RestClient::Exception => e
    if e.http_code == 404
      false
    else
      body = JSON.parse(e.http_body)
      msg = "Exception checking if the zone exists: #{body['code']}, #{body['message']}"
      fail msg
    end
  end
end

#create_record_set(dns_resource_group, zone_name, record_set_name, records, record_type, ttl) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fog/azurerm/requests/dns/create_record_set.rb', line 5

def create_record_set(dns_resource_group, zone_name, record_set_name, records, record_type, ttl)
  resource_url = "#{AZURE_RESOURCE}/subscriptions/#{@subscription_id}/resourceGroups/#{dns_resource_group}/providers/Microsoft.Network/dnsZones/#{zone_name}/#{record_type}/#{record_set_name}?api-version=2015-05-04-preview"
  Fog::Logger.debug "Creating/Updating RecordSet #{record_set_name} of type '#{record_type}' in zone #{zone_name}"

  case record_type
    when 'A'
      a_type_records_array = Array.new
      records.each do |ip|
        a_type_records_array.push({'ipv4Address' => ip})
      end
      body = {
          :location => 'global',
          :tags => '',
          :properties => {
              :TTL => ttl,
              :ARecords => a_type_records_array
          }
      }
    when 'CNAME'
      body = {
          :location => 'global',
          :tags => '',
          :properties => {
              :TTL => ttl,
              :CNAMERecord => {
                  'cname' => records.first # because cname only has 1 value and we know the object is an array passed in.

              }
          }
      }
  end

  begin
    token = Fog::Credentials::AzureRM.get_token(@tenant_id, @client_id, @client_secret)
    RestClient.put(
        resource_url,
        body.to_json,
        accept: 'application/json',
        content_type: 'application/json',
        authorization: token
    )
    Fog::Logger.debug "RecordSet #{record_set_name} Created/Updated Successfully!"
  rescue Exception => e
    Fog::Logger.warning "Exception setting #{record_type} records for the record set: #{record_set_name}"
    msg = "AzureDns::RecordSet - Exception is: #{e.message}"
    raise msg
  end
end

#create_zone(dns_resource_group, zone_name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fog/azurerm/requests/dns/create_zone.rb', line 5

def create_zone(dns_resource_group, zone_name)
  Fog::Logger.debug "Creating Zone #{zone_name} ..."
  resource_url = "#{AZURE_RESOURCE}/subscriptions/#{@subscription_id}/resourceGroups/#{dns_resource_group}/providers/Microsoft.Network/dnsZones/#{zone_name}?api-version=2015-05-04-preview"
  
  body = {
      location: 'global',
      tags: {},
      properties: {}
  }

  begin
    token = Fog::Credentials::AzureRM.get_token(@tenant_id, @client_id, @client_secret)
    dns_response = RestClient.put(
        resource_url,
        body.to_json,
        accept: 'application/json',
        content_type: 'application/json',
        authorization: token)
    response_hash = JSON.parse(dns_response)
    Fog::Logger.debug "Zone #{zone_name} created successfully."
    response_hash
  rescue RestClient::Exception => e
    body = JSON.parse(e.http_body)
    if body.key?('error')
      body = body['error']
      msg = "Exception creating zone: #{body['code']}, #{body['message']}"
    else
      msg = "Exception creating zone: #{body['code']}, #{body['message']}"
    end

    raise msg
  end
end

#delete_record_set(record_set_name, dns_resource_group, zone_name, record_type) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fog/azurerm/requests/dns/delete_record_set.rb', line 5

def delete_record_set(record_set_name, dns_resource_group, zone_name, record_type)
  resource_url = "#{AZURE_RESOURCE}/subscriptions/#{@subscription_id}/resourceGroups/#{dns_resource_group}/providers/Microsoft.Network/dnsZones/#{zone_name}/#{record_type}/#{record_set_name}?api-version=2015-05-04-preview"
  Fog::Logger.debug "Deleting RecordSet #{record_set_name} of type '#{record_type}' in zone #{zone_name}"

  begin
    token = Fog::Credentials::AzureRM.get_token(@tenant_id, @client_id, @client_secret)
    RestClient.delete(
        resource_url,
        accept: 'application/json',
        content_type: 'application/json',
        authorization: token
    )
    Fog::Logger.debug "RecordSet #{record_set_name} Deleted Successfully!"
  rescue RestClient::Exception => e
    body = JSON.parse(e.http_body)
    if body.key?('error')
      body = body['error']
      msg = "Exception deleting zone: #{body['code']}, #{body['message']}"
    else
      msg = "Exception deleting zone: #{body['code']}, #{body['message']}"
    end
    raise msg
  end
end

#delete_zone(zone_name, dns_resource_group) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fog/azurerm/requests/dns/delete_zone.rb', line 6

def delete_zone(zone_name, dns_resource_group)
  Fog::Logger.debug "Deleting Zone #{zone_name} ..."
  resource_url = "#{AZURE_RESOURCE}/subscriptions/#{@subscription_id}/resourceGroups/#{dns_resource_group}/providers/Microsoft.Network/dnsZones/#{zone_name}?api-version=2015-05-04-preview"
  begin
    token = Fog::Credentials::AzureRM.get_token(@tenant_id, @client_id, @client_secret)
    RestClient.delete(
        resource_url,
        accept: 'application/json',
        content_type: 'application/json',
        authorization: token)
    Fog::Logger.debug "Zone #{zone_name} deleted successfully."
    true
  rescue RestClient::Exception => e
    body = JSON.parse(e.http_body)
    if body.key?('error')
      body = body['error']
      msg = "Exception deleting zone: #{body['code']}, #{body['message']}"
    else
      msg = "Exception deleting zone: #{body['code']}, #{body['message']}"
    end
    raise msg
  end
end

#get_records_from_record_set(record_set_name, dns_resource_group, zone_name, record_type) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fog/azurerm/requests/dns/get_records_from_record_set.rb', line 5

def get_records_from_record_set(record_set_name, dns_resource_group, zone_name, record_type)
  resource_url = "#{AZURE_RESOURCE}/subscriptions/#{@subscription_id}/resourceGroups/#{dns_resource_group}/providers/Microsoft.Network/dnsZones/#{zone_name}/#{record_type}/#{record_set_name}?api-version=2015-05-04-preview"
  Fog::Logger.debug "Getting all records from RecordSet #{record_set_name} of type '#{record_type}' in zone #{zone_name}"

  existing_records = Array.new
  begin
    token = Fog::Credentials::AzureRM.get_token(@tenant_id, @client_id, @client_secret)
    dns_response = RestClient.get(
        resource_url,
        accept: 'application/json',
        content_type: 'application/json',
        authorization: token
    )
  rescue Exception => e
    if e.http_code == 404
      Fog::Logger.warning 'AzureDns::RecordSet - 404 code, record set does not exist.  returning empty array'
      return existing_records
    else
      Fog::Logger.warning "Exception trying to get existing #{record_type} records for the record set: #{record_set_name}"
      msg = "AzureDns::RecordSet - Exception is: #{e.message}"
      raise msg
    end
  end

  begin
    dns_hash = JSON.parse(dns_response)
    case record_type
      when 'A'
        dns_hash['properties']['ARecords'].each do |record|
          Fog::Logger.debug "AzureDns:RecordSet - A record is: #{record}"
          existing_records.push(record['ipv4Address'])
        end
      when 'CNAME'
        Fog::Logger.debug "AzureDns:RecordSet - CNAME record is: #{dns_hash['properties']['CNAMERecord']['cname']}"
        existing_records.push(dns_hash['properties']['CNAMERecord']['cname'])
    end
    return existing_records
  rescue Exception => e
    Fog::Logger.warning "Exception trying to parse response: #{dns_response}"
    msg = "AzureDns::RecordSet - Exception is: #{e.message}"
    raise msg
  end
end

#list_record_sets(dns_resource_group, zone_name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/fog/azurerm/requests/dns/list_record_sets.rb', line 5

def list_record_sets(dns_resource_group, zone_name)
  resource_url = "#{AZURE_RESOURCE}/subscriptions/#{@subscription_id}/resourceGroups/#{dns_resource_group}/providers/Microsoft.Network/dnsZones/#{zone_name}/recordsets?api-version=2015-05-04-preview"
  begin
    token = Fog::Credentials::AzureRM.get_token(@tenant_id, @client_id, @client_secret)
    dns_response = RestClient.get(
      resource_url,
      accept: 'application/json',
      content_type: 'application/json',
      authorization: token)
    response_hash = JSON.parse(dns_response)
    response_hash['value']
  rescue RestClient::Exception => e
    body = JSON.parse(e.http_body)
    if body.key?('error')
      body = body['error']
      msg = "Exception fetching record_sets: #{body['code']}, #{body['message']}"
    else
      msg = "Exception fetching record_sets: #{body['code']}, #{body['message']}"
    end
    raise msg
  end
end

#list_zones(dns_resource_group) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/azurerm/requests/dns/list_zones.rb', line 5

def list_zones
  zone_hash_array = []
  @resources.resource_groups.each do |rg|
    list_zones(rg.name).each do |zone_hash|
      zone_hash['resource_group'] = rg.name              
      zone_hash_array << zone_hash
    end
  end
  zone_hash_array
end