Class: CloudInteractor::Domain

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud_interactor/domain.rb,
lib/cloud_interactor/domain/list.rb,
lib/cloud_interactor/domain/read.rb,
lib/cloud_interactor/domain/create.rb,
lib/cloud_interactor/domain/update.rb,
lib/cloud_interactor/domain/destroy.rb,
lib/cloud_interactor/domain/read_record.rb,
lib/cloud_interactor/domain/list_records.rb,
lib/cloud_interactor/domain/create_record.rb,
lib/cloud_interactor/domain/update_record.rb,
lib/cloud_interactor/domain/destroy_record.rb

Overview

Constant Summary collapse

IDENTITY =
'domains'
RESOURCE =
'DNS'

Instance Method Summary collapse

Constructor Details

#initialize(main_obj, auth_hash, classes, options = {}) ⇒ Domain

Returns a new instance of Domain.



7
8
9
10
11
12
# File 'lib/cloud_interactor/domain.rb', line 7

def initialize main_obj, auth_hash, classes, options={} 
  @main_obj  = main_obj
  @auth_hash = auth_hash
  @options   = options
  @classes   = classes
end

Instance Method Details

#create(args, already_created = false) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/cloud_interactor/domain/create.rb', line 3

def create args, already_created=false
  read args, false

  unless @main_obj["specific_#{ IDENTITY }"].empty?
    puts "#{ IDENTITY } #{ args[IDENTITY.singularize] } already exists... returning."

    return false
  end

  @classes['auth'].auth_service(RESOURCE).instance_eval('zones').create(domain: args[IDENTITY.singularize], email: @auth_hash['cloud_authentication'][@options['preferred_cloud']]['email'])

  puts "Created #{ IDENTITY } #{ args[IDENTITY.singularize] }..."
end

#create_record(args, already_created = false) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cloud_interactor/domain/create_record.rb', line 3

def create_record args, already_created=false
  args['type']          ||= 'A'
  args['ttl']           ||= 300
  args['target_domain'] ||= @classes['clouds'].parse_provider_domain_record_name(args)
  args['target_domain']   = args[IDENTITY.singularize] if args['subdomain'].blank?

  read args, false

  @main_obj['specific_records'][args[IDENTITY.singularize]].each do |record_hash|
    already_created = true if record_hash['name'] == args['target_domain'] && record_hash['type'] == args['type']

    break if already_created
  end

  if already_created
    update_record args

  else
    specific_fog_object = @classes['auth'].auth_service(RESOURCE).instance_eval('zones').get @main_obj["specific_#{ IDENTITY }"].last['id']

    specific_fog_object.records.create(name: args['target_domain'], value: args['target_ip'], type: args['type'], ttl: args['ttl'])

    puts "Attached #{ args['subdomain'] } (#{ args['target_ip'] }) to #{ args[IDENTITY.singularize] } (#{ args['target_domain'] })..."
  end
end

#destroy(args) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/cloud_interactor/domain/destroy.rb', line 3

def destroy args
  read args, false

  if @main_obj["specific_#{ IDENTITY }"].empty? && @main_obj["specific_#{ IDENTITY }"].last[IDENTITY.singularize] != args[IDENTITY.singularize]
    puts "#{ IDENTITY } #{ args[IDENTITY.singularize] } doesn't exist... returning."

    return false
  end

  @classes['auth'].auth_service(RESOURCE).instance_eval('zones').get(@main_obj["specific_#{ IDENTITY }"].last['id']).destroy

  puts "Destroyed #{ IDENTITY.singularize } #{ args[IDENTITY.singularize] }..."
end

#destroy_record(args, already_exists = false) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cloud_interactor/domain/destroy_record.rb', line 3

def destroy_record args, already_exists=false
  read args, false

  @main_obj['specific_records'][args[IDENTITY.singularize]].each do |record_hash|
    already_exists = true if record_hash['name'] == @classes['clouds'].parse_provider_domain_record_name(args)

    args['id'] = record_hash['id']

    break if already_exists
  end

  raise "Subdomain not found for #{ args[IDENTITY.singularize] }" unless already_exists

  puts "Destroying #{ args['subdomain'] } from #{ args[IDENTITY.singularize] }..."

  specific_fog_object = @classes['auth'].auth_service(RESOURCE).instance_eval('zones').get @main_obj["specific_#{ IDENTITY }"].last['id']

  specific_fog_object.records.get(args['id']).destroy
end

#list(args = {}, output = true) ⇒ Object



3
4
5
6
7
# File 'lib/cloud_interactor/domain/list.rb', line 3

def list args={}, output=true
  @classes['helper'].generic_list_call 'zones', RESOURCE, output

  @main_obj[IDENTITY] = @main_obj['zones']
end

#list_records(args, output = false) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cloud_interactor/domain/list_records.rb', line 3

def list_records args, output=false
  puts "Querying #{ args["domain"] } for records..."

  specific_fog_object = @classes['auth'].auth_service(RESOURCE).instance_eval('zones').get @main_obj["specific_#{ IDENTITY }"].last['id']

  @main_obj['specific_records'] ||= {}
  @main_obj['specific_records'][args[IDENTITY.singularize]]  ||= []
  @main_obj["specific_#{ IDENTITY }"].last['records'] ||= []

  specific_fog_object.records.each do |record|
    record_obj = JSON.parse(record.to_json)

    @main_obj["specific_#{ IDENTITY }"].last['records'] << record_obj
    @main_obj['specific_records'][args[IDENTITY.singularize]] << record_obj
  end

  @main_obj['output']["records_for_#{ @main_obj["specific_#{ IDENTITY }"].last['domain'] }"] = @main_obj["specific_#{ IDENTITY }"].last['records']

  ap(@main_obj["specific_#{ IDENTITY }"].last['records']) if output
end

#read(args = {}, output = true) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cloud_interactor/domain/read.rb', line 3

def read args={}, output=true
  list [], false

  @classes['helper'].generic_read_parse args, IDENTITY, output, IDENTITY.singularize

  @main_obj["specific_#{ IDENTITY }"] ||= {}

  specific_identity = @classes['helper'].set_specific_identity args, IDENTITY.singularize

  @main_obj[IDENTITY].each do |identity_hash|
    next if !specific_identity.nil? && !identity_hash[IDENTITY.singularize].include?(specific_identity)

    self.list_records identity_hash
  end

  ap(@main_obj["specific_#{ IDENTITY }"]) if output

  puts("#{ specific_identity } not found in #{ IDENTITY }!") if @main_obj["specific_#{ IDENTITY }"].empty?
end

#read_record(args, output = true, strict_match = false) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cloud_interactor/domain/read_record.rb', line 3

def read_record args, output=true, strict_match=false

  specific_record = args['subdomain']

  read args, false

  @main_obj['specific_records'][args[IDENTITY.singularize]].each do |record_hash|
    if strict_match
      next unless record_hash['name'] == (specific_record)
    else
      next unless record_hash['name'].include?(specific_record)
    end

    @main_obj['specific_queried_domains'] ||= {}

    @main_obj['specific_queried_domains'][args[IDENTITY.singularize]] ||= []
    @main_obj['specific_queried_domains'][args[IDENTITY.singularize]]  << record_hash

    ap(record_hash) if output
  end

  puts("#{ args[IDENTITY.singularize] } does not have the subdomain #{ args['subdomain'] }!") if @main_obj["specific_queried_domains"].nil?
end

#run(method, args) ⇒ Object



14
15
16
# File 'lib/cloud_interactor/domain.rb', line 14

def run method, args
  self.send(method, args)
end

#update(args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/cloud_interactor/domain/update.rb', line 4

def update args
  read args, false

  if (@main_obj["specific_#{ IDENTITY }"].nil? || @main_obj["specific_#{ IDENTITY }"].empty?) && @main_obj["specific_#{ IDENTITY }"].last[IDENTITY.singularize] != args[IDENTITY.singularize]
    puts "#{ IDENTITY } #{ args[IDENTITY.singularize] } doesn't exist... returning."

    return false
  end

  @classes['auth'].auth_service(resource).instance_eval('zones').get(@main_obj["specific_#{ IDENTITY }"].last['id']).update(ttl: 5, email: @auth_hash['cloud_authentication'][@options['preferred_cloud']]['email'])

  puts "Updated #{ IDENTITY } #{ args[IDENTITY.singularize] }..."
end

#update_record(args, already_created = false) ⇒ Object



3
4
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
# File 'lib/cloud_interactor/domain/update_record.rb', line 3

def update_record args, already_created=false
  args['type']          ||= 'A'
  args['ttl']           ||= 300
  args['target_domain'] ||= @classes['clouds'].parse_provider_domain_record_name(args)
  args['target_domain']   = args[IDENTITY.singularize] if args['subdomain'].blank?

  read args, false

  puts "Updating #{ args['subdomain'] } for #{ args[IDENTITY.singularize] }..."

  @main_obj['specific_records'][args[IDENTITY.singularize]].each do |record_hash|
    already_created = true if record_hash['name'] == args['target_domain'] && record_hash['type'] == args['type']

    args['id'] = record_hash['id']

    break if already_created
  end

  if already_created
    specific_fog_object = @classes['auth'].auth_service(RESOURCE).instance_eval('zones').get @main_obj["specific_#{ IDENTITY }"].last['id']

    #the fact that there is no public update method is silly
    specific_record = specific_fog_object.records.get(args['id'])

    case @options['route_dns_changes_via']
    when /rackspace|dnsimple/
      specific_record.type  = args['type']
      specific_record.value = args['target_ip']
      specific_record.ttl   = args['ttl']

      specific_record.save
    else
      raise "Unsupported action #{ __method__ } for #{ @options['preferred_cloud'] }. Please create an issue on github or submit a PR to fix this issue."
    end

    puts "Updated #{ args['subdomain'] } (#{ args['target_ip'] }) to #{ args[IDENTITY.singularize] } (#{ args['target_domain'] })..."
  else
    create_record [ args ]
  end
end