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/get_record_set.rb,
lib/fog/azurerm/requests/dns/list_record_sets.rb,
lib/fog/azurerm/requests/dns/check_zone_exists.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/check_record_set_exists.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.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fog/azurerm/dns.rb', line 40

def initialize(options)
  begin
    require 'azure_mgmt_dns'
  rescue LoadError => e
    retry if require('rubygems')
    raise e.message
  end

  options[:environment] = 'AzureCloud' if options[:environment].nil?

  credentials = Fog::Credentials::AzureRM.get_credentials(options[:tenant_id], options[:client_id], options[:client_secret], options[:environment])
  telemetry = "fog-azure-rm/#{Fog::AzureRM::VERSION}"
  @dns_client = ::Azure::ARM::Dns::DnsManagementClient.new(credentials, resource_manager_endpoint_url(options[:environment]))
  @dns_client.subscription_id = options[:subscription_id]
  @dns_client.add_user_agent_information(telemetry)
  @tenant_id = options[:tenant_id]
  @client_id = options[:client_id]
  @client_secret = options[:client_secret]
  @environment = options[:environment]
  @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],
    environment: options[:environment]
  )
end

Instance Method Details

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



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

def check_record_set_exists(resource_group, name, zone_name, record_type)
  msg = "Checking Record set #{name}"
  Fog::Logger.debug msg
  begin
    @dns_client.record_sets.get(resource_group, zone_name, name, record_type)
    Fog::Logger.debug "Record set #{name} exists."
    true
  rescue MsRestAzure::AzureOperationError => e
    if resource_not_found?(e)
      Fog::Logger.debug "Record set #{name} doesn't exist."
      false
    else
      raise_azure_exception(e, msg)
    end
  end
end

#check_zone_exists(resource_group, name) ⇒ Object



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

def check_zone_exists(resource_group, name)
  msg = "Getting Zone #{name} from Resource Group #{resource_group}."
  Fog::Logger.debug msg
  begin
    zone = @dns_client.zones.get(resource_group, name)
  rescue MsRestAzure::AzureOperationError => e
    if resource_not_found?(e)
      Fog::Logger.debug "Zone #{name} doesn't exist."
      return false
    else
      raise_azure_exception(e, msg)
    end
  rescue => e
    Fog::Logger.debug e[:error][:code]
  end
  !zone.nil?
end

#create_or_update_record_set(record_set_params, type) ⇒ Object



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

def create_or_update_record_set(record_set_params, type)
  msg = "Creating/updating Recordset #{record_set_params[:name]} in Resource Group: #{record_set_params[:resource_group]}."
  Fog::Logger.debug msg
  recordset_object = get_record_set_object(record_set_params, type)
  begin
    record_set = @dns_client.record_sets.create_or_update(record_set_params[:resource_group], record_set_params[:zone_name], record_set_params[:name], type, recordset_object)
  rescue MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  Fog::Logger.debug "Record Set #{record_set_params[:name]} created/updated successfully."
  record_set
end

#create_or_update_zone(zone_params) ⇒ Object



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

def create_or_update_zone(zone_params)
  msg = "Creating/updating Zone #{zone_params[:name]} in Resource Group: #{zone_params[:resource_group]}."
  Fog::Logger.debug msg
  zone_object = get_zone_object(zone_params)
  begin
    zone = @dns_client.zones.create_or_update(zone_params[:resource_group], zone_params[:name], zone_object)
  rescue MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  Fog::Logger.debug "Zone #{zone_params[:name]} created/updated successfully."
  zone
end

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



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

def delete_record_set(resource_group, name, zone_name, record_type)
  msg = "Deleting Record Set #{name} from Resource Group #{resource_group}."
  Fog::Logger.debug msg
  begin
    @dns_client.record_sets.delete(resource_group, zone_name, name, record_type)
  rescue MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  Fog::Logger.debug "Record Set #{name} Deleted Successfully."
  true
end

#delete_zone(resource_group, name) ⇒ Object



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

def delete_zone(resource_group, name)
  msg = "Deleting Zone #{name} from Resource Group #{resource_group}."
  Fog::Logger.debug msg
  begin
    @dns_client.zones.delete(resource_group, name)
  rescue MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  Fog::Logger.debug "Zone #{name} Deleted Successfully."
  true
end

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



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

def get_record_set(resource_group, name, zone_name, record_type)
  msg = "Getting Record Set #{name} from Resource Group #{resource_group}."
  Fog::Logger.debug msg
  begin
    record_set = @dns_client.record_sets.get(resource_group, zone_name, name, record_type)
  rescue MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  record_set
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
# 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)
  msg = "Getting all records from RecordSet #{name} of type '#{record_type}' in zone #{zone_name}"
  Fog::Logger.debug msg
  begin
    record_set = @dns_client.record_sets.get(resource_group, zone_name, name, record_type)
  rescue MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end

  case record_type
  when 'A'
    record_set.arecords
  when 'CNAME'
    record_set.cname_record
  end
end

#get_zone(resource_group, name) ⇒ Object



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

def get_zone(resource_group, name)
  msg = "Getting Zone #{name} from Resource Group #{resource_group}."
  Fog::Logger.debug msg
  begin
    zone = @dns_client.zones.get(resource_group, name)
  rescue MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  zone
end

#list_record_sets(resource_group, zone_name) ⇒ Object



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

def list_record_sets(resource_group, zone_name)
  msg = 'Getting list of Record sets.'
  Fog::Logger.debug msg
  begin
    zones = @dns_client.record_sets.list_by_dns_zone(resource_group, zone_name)
  rescue MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  zones
end

#list_zonesObject



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

def list_zones
  msg = 'Getting list of Zones.'
  Fog::Logger.debug msg
  begin
    zones = @dns_client.zones.list
  rescue MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  zones
end