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

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/azurerm/dns.rb,
lib/fog/azurerm/requests/dns/get_zone.rb,
lib/fog/azurerm/requests/dns/list_zones.rb,
lib/fog/azurerm/requests/dns/delete_zone.rb,
lib/fog/azurerm/requests/dns/check_for_zone.rb,
lib/fog/azurerm/requests/dns/get_record_set.rb,
lib/fog/azurerm/requests/dns/list_record_sets.rb,
lib/fog/azurerm/requests/dns/delete_record_set.rb,
lib/fog/azurerm/requests/dns/create_or_update_zone.rb,
lib/fog/azurerm/requests/dns/create_or_update_record_set.rb,
lib/fog/azurerm/requests/dns/get_records_from_record_set.rb

Overview

Real class for DNS Request

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Real

Returns a new instance of Real.



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

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



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

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)
    RestClient.get(
      resource_url,
      accept: 'application/json',
      content_type: 'application/json',
      authorization: token
    )
    true
  rescue RestClient::Exception => e
    body = JSON.parse(e.response)
    if (body['error']['code']) == 'ResourceNotFound'
      false
    else
      Fog::Logger.warning "Exception checking if the zone exists in resource group #{resource_group}"
      msg = "Exception checking if the zone exists: #{body['error']['code']}, #{body['error']['message']}"
      raise msg
    end
  end
end

#create_or_update_record_set(resource_group, name, zone_name, records, record_type, ttl) ⇒ Object



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
52
53
54
# File 'lib/fog/azurerm/requests/dns/create_or_update_record_set.rb', line 6

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

  case record_type
  when 'A'
    a_type_records_array = []
    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)
    response = RestClient.put(
      resource_url,
      body.to_json,
      accept: 'application/json',
      content_type: 'application/json',
      authorization: token
    )
    Fog::Logger.debug "RecordSet #{name} Created/Updated Successfully!"
    parsed_response = JSON.parse(response)
    parsed_response
  rescue Exception => e
    Fog::Logger.warning "Exception creating recordset #{name} in zone #{zone_name}."
    msg = "AzureDns::RecordSet - Exception is: #{e.message}"
    raise msg
  end
end

#create_or_update_zone(resource_group, name) ⇒ Object



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
# File 'lib/fog/azurerm/requests/dns/create_or_update_zone.rb', line 6

def create_or_update_zone(resource_group, name)
  Fog::Logger.debug "Creating/Updating Zone #{name} ..."
  resource_url = "#{AZURE_RESOURCE}/subscriptions/#{@subscription_id}/resourceGroups/#{resource_group}/providers/Microsoft.Network/dnsZones/#{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)
    response = RestClient.put(
      resource_url,
      body.to_json,
      accept: 'application/json',
      content_type: 'application/json',
      authorization: token
    )
    Fog::Logger.debug "Zone #{name} created successfully."
    parsed_response = JSON.parse(response)
    parsed_response
  rescue Exception => e
    Fog::Logger.warning "Exception creating zone #{name} in resource group #{resource_group}"
    msg = "AzureDns::Zone - Exception is: #{e.message}"
    raise msg
  end
end

#delete_record_set(resource_group, name, zone_name, record_type) ⇒ Object



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

def delete_record_set(resource_group, name, zone_name, record_type)
  resource_url = "#{AZURE_RESOURCE}/subscriptions/#{@subscription_id}/resourceGroups/#{resource_group}/providers/Microsoft.Network/dnsZones/#{zone_name}/#{record_type}/#{name}?api-version=2015-05-04-preview"
  Fog::Logger.debug "Deleting RecordSet #{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 #{name} Deleted Successfully!"
    true
  rescue Exception => e
    Fog::Logger.warning "Exception deleting record set #{name} from resource group #{resource_group}"
    msg = "AzureDns::RecordSet - Exception is: #{e.message}"
    raise msg
  end
end

#delete_zone(resource_group, name) ⇒ Object



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

def delete_zone(resource_group, name)
  Fog::Logger.debug "Deleting Zone #{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)
    RestClient.delete(
      resource_url,
      accept: 'application/json',
      content_type: 'application/json',
      authorization: token
    )
    Fog::Logger.debug "Zone #{name} deleted successfully."
    true
  rescue Exception => e
    Fog::Logger.warning "Exception deleting zone #{name} from resource group #{resource_group}"
    msg = "AzureDns::Zone - Exception is: #{e.message}"
    raise msg
  end
end

#get_record_set(resource_group, name, zone_name, record_type) ⇒ Object



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

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

  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
    Fog::Logger.warning "Exception trying to get existing #{record_type} records for the record set: #{name}"
    msg = "AzureDns::RecordSet - Exception is: #{e.message}"
    raise msg
  end
  JSON.parse(dns_response)
end

#get_records_from_record_set(resource_group, name, zone_name, record_type) ⇒ Object



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
# File 'lib/fog/azurerm/requests/dns/get_records_from_record_set.rb', line 6

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

  existing_records = []
  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
    Fog::Logger.warning "Exception trying to get existing #{record_type} records for the record set: #{name}"
    msg = "AzureDns::RecordSet - Exception is: #{e.message}"
    raise msg
  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
    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

#get_zone(resource_group, name) ⇒ Object



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

def get_zone(resource_group, name)
  resource_url = "#{AZURE_RESOURCE}/subscriptions/#{@subscription_id}/resourceGroups/#{resource_group}/providers/Microsoft.Network/dnsZones/#{name}?api-version=2016-04-01"
  Fog::Logger.debug "Getting a zone: #{name}"

  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
    Fog::Logger.warning "Exception trying to get existing zone: #{name}."
    msg = "AzureDns::RecordSet - Exception is: #{e.message}"
    raise msg
  end
  JSON.parse(dns_response)
end

#list_record_sets(resource_group, zone_name) ⇒ Object



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

def list_record_sets(resource_group, zone_name)
  resource_url = "#{AZURE_RESOURCE}/subscriptions/#{@subscription_id}/resourceGroups/#{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
    )
    parsed_record_set = JSON.parse(dns_response)
    parsed_record_set['value']
  rescue Exception => e
    Fog::Logger.warning "Exception listing recordsets in zone #{zone_name} in resource group #{resource_group}"
    msg = "AzureDns::RecordSet - Exception is: #{e.message}"
    raise msg
  end
end

#list_zonesObject



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

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