Class: Fog::DNS::Dynect::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/dynect/dns.rb,
lib/fog/dynect/requests/dns/get_zone.rb,
lib/fog/dynect/requests/dns/put_zone.rb,
lib/fog/dynect/requests/dns/post_zone.rb,
lib/fog/dynect/requests/dns/get_record.rb,
lib/fog/dynect/requests/dns/put_record.rb,
lib/fog/dynect/requests/dns/delete_zone.rb,
lib/fog/dynect/requests/dns/post_record.rb,
lib/fog/dynect/requests/dns/post_session.rb,
lib/fog/dynect/requests/dns/delete_record.rb,
lib/fog/dynect/requests/dns/get_node_list.rb,
lib/fog/dynect/requests/dns/get_all_records.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



34
35
36
37
38
# File 'lib/fog/dynect/dns.rb', line 34

def initialize(options={})
  @dynect_customer = options[:dynect_customer]
  @dynect_username = options[:dynect_username]
  @dynect_password = options[:dynect_password]
end

Class Method Details

.dataObject



40
41
42
43
44
# File 'lib/fog/dynect/dns.rb', line 40

def self.data
  @data ||= {
    :zones => {}
  }
end

.resetObject



46
47
48
# File 'lib/fog/dynect/dns.rb', line 46

def self.reset
  @data = nil
end

Instance Method Details

#auth_tokenObject



50
51
52
# File 'lib/fog/dynect/dns.rb', line 50

def auth_token
  @auth_token ||= Fog::Dynect::Mock.token
end

#dataObject



54
55
56
# File 'lib/fog/dynect/dns.rb', line 54

def data
  self.class.data
end

#delete_record(type, zone, fqdn, record_id) ⇒ Object



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
# File 'lib/fog/dynect/requests/dns/delete_record.rb', line 25

def delete_record(type, zone, fqdn, record_id)
  raise Fog::DNS::Dynect::NotFound unless zone = self.data[:zones][zone]

  raise Fog::DNS::Dynect::NotFound unless zone[:records][type].find { |record| record[:fqdn] == fqdn && record[:record_id] == record_id.to_i }

  zone[:records_to_delete] << {
    :type => type,
    :fqdn => fqdn,
    :record_id => record_id.to_i
  }

  response = Excon::Response.new
  response.status = 200

  response.body = {
    "status" => "success",
    "data" => {},
    "job_id" => Fog::Dynect::Mock.job_id,
    "msgs" => [{
      "INFO" => "delete: Record will be deleted on zone publish",
      "SOURCE" => "BLL",
      "ERR_CD" => nil,
      "LVL" => "INFO"
    }]
  }

  response
end

#delete_zone(zone) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fog/dynect/requests/dns/delete_zone.rb', line 21

def delete_zone(zone)
  self.data[:zones].delete(zone)

  response = Excon::Response.new
  response.status = 200
  response.body = {
    "status" => "success",
    "data" => {},
    "job_id" => Fog::Dynect::Mock.job_id,
    "msgs" => [{
      "ERR_CD" => '',
      "INFO" => '',
      "LVL" => '',
      "SOURCE" => ''
    }]
  }
  response
end

#get_all_records(zone, options = {}) ⇒ Object



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
# File 'lib/fog/dynect/requests/dns/get_all_records.rb', line 25

def get_all_records(zone, options = {})
  raise Fog::DNS::Dynect::NotFound unless zone = self.data[:zones][zone]

  response = Excon::Response.new
  response.status = 200

  data = [zone[:zone]]

  if fqdn = options[:fqdn]
    data = data | zone[:records].collect { |type, records| records.select { |record| record[:fqdn] == fqdn } }.flatten.compact
  else
    data = data | zone[:records].collect { |type, records| records.collect { |record| record[:fqdn] } }.flatten
  end

  response.body = {
    "status" => "success",
    "data" => data,
    "job_id" => Fog::Dynect::Mock.job_id,
    "msgs" => [{
      "INFO" => "get_tree: Here is your zone tree",
      "SOURCE" => "BLL",
      "ERR_CD" => nil,
      "LVL" => "INFO"
    }]
  }

  response
end

#get_node_list(zone, options = {}) ⇒ Object



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
# File 'lib/fog/dynect/requests/dns/get_node_list.rb', line 25

def get_node_list(zone, options = {})
  raise Fog::DNS::Dynect::NotFound unless zone = self.data[:zones][zone]

  response = Excon::Response.new
  response.status = 200

  data = [zone[:zone]]

  if fqdn = options[:fqdn]
    data = data | zone[:records].collect { |type, records| records.select { |record| record[:fqdn] == fqdn } }.flatten.compact
  else
    data = data | zone[:records].collect { |type, records| records.collect { |record| record[:fqdn] } }.flatten
  end

  response.body = {
    "status" => "success",
    "data" => data,
    "job_id" => Fog::Dynect::Mock.job_id,
    "msgs" => [{
      "INFO" => "get_tree: Here is your zone tree",
      "SOURCE" => "BLL",
      "ERR_CD" => nil,
      "LVL" => "INFO"
    }]
  }

  response
end

#get_record(type, zone, fqdn, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/fog/dynect/requests/dns/get_record.rb', line 26

def get_record(type, zone, fqdn, options = {})
  raise ArgumentError unless [
    'AAAA', 'ANY', 'A', 'CNAME',
    'DHCID', 'DNAME', 'DNSKEY',
    'DS', 'KEY', 'LOC', 'MX',
    'NSA', 'NS', 'PTR', 'PX',
    'RP', 'SOA', 'SPF', 'SRV',
    'SSHFP', 'TXT'
  ].include? type
  raise Fog::DNS::Dynect::NotFound unless zone = self.data[:zones][zone]

  response = Excon::Response.new
  response.status = 200

  if record_id = options['record_id']
    raise Fog::DNS::Dynect::NotFound unless record = zone[:records][type].find { |record| record[:record_id] == record_id.to_i }
    response.body = {
      "status" => "success",
      "data" => {
        "zone" => record[:zone][:zone],
        "ttl" => record[:ttl],
        "fqdn" => record[:fqdn],
        "record_type" => type,
        "rdata" => record[:rdata],
        "record_id" => record[:record_id]
      },
      "job_id" => Fog::Dynect::Mock.job_id,
      "msgs" => [{
        "INFO" => "get: Found the record",
        "SOURCE" => "API-B",
        "ERR_CD" => nil,
        "LVL" => "INFO"
      }]
    }
  else
    records = if type == "ANY"
                zone[:records].values.flatten.select { |record| record[:fqdn] == fqdn }
              else
                zone[:records][type].select { |record| record[:fqdn] == fqdn }
              end
    response.body = {
      "status" => "success",
      "data" => records.collect { |record| "/REST/#{record[:type]}Record/#{record[:zone][:zone]}/#{record[:fqdn]}/#{record[:record_id]}" },
      "job_id" => Fog::Dynect::Mock.job_id,
      "msgs" => [{
        "INFO" => "detail: Found #{records.size} record",
        "SOURCE" => "BLL",
        "ERR_CD" => nil,
        "LVL" => "INFO"
      }]
    }
  end

  response
end

#get_zone(options = {}) ⇒ Object



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/dynect/requests/dns/get_zone.rb', line 23

def get_zone(options = {})
  if options['zone']
    raise Fog::DNS::Dynect::NotFound unless zone = self.data[:zones][options['zone']]
    data = {
      "zone_type" => zone[:zone_type],
      "serial_style" => zone[:serial_style],
      "serial" => zone[:serial],
      "zone" => zone[:zone]
    }
    info = "get: Your zone, #{zone[:zone]}"
  else
    data = self.data[:zones].collect { |zone, data| "/REST/Zone/#{zone}/" }
    info = "get: Your #{data.size} zones"
  end

  response = Excon::Response.new
  response.status = 200

  response.body = {
    "status" => "success",
    "data" => data,
    "job_id" => Fog::Dynect::Mock.job_id,
    "msgs" => [{
      "INFO" => info,
      "SOURCE" => "BLL",
      "ERR_CD" => nil,
      "LVL" => "INFO"
    }]
  }

  response
end

#post_record(type, zone, fqdn, rdata, options = {}) ⇒ Object



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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fog/dynect/requests/dns/post_record.rb', line 27

def post_record(type, zone, fqdn, rdata, options = {})
  raise Fog::DNS::Dynect::NotFound unless zone = self.data[:zones][zone]

  records = zone[:records]
  record_id = zone[:next_record_id]
  zone[:next_record_id] += 1

  record = {
    :type => type,
    :zone => zone,
    :fqdn => fqdn,
    :rdata => rdata,
    :ttl => options[:ttl] || zone[:ttl],
    :record_id => record_id
  }

  records[type] << record

  response = Excon::Response.new
  response.status = 200

  response.body = {
    "status" => "success",
    "data" => {
      "zone" => record[:zone][:zone],
      "ttl" => record[:ttl],
      "fqdn" => record[:fqdn],
      "record_type" => record[:type],
      "rdata" => record[:rdata],
      "record_id" => record[:record_id]
   },
   "job_id" => Fog::Dynect::Mock.job_id,
   "msgs" => [{
     "INFO"=>"add: Record added",
     "SOURCE"=>"BLL",
     "ERR_CD"=>nil,
     "LVL"=>"INFO"
   }]
  }

  response
end

#post_sessionObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fog/dynect/requests/dns/post_session.rb', line 22

def post_session
  response = Excon::Response.new
  response.status = 200
  response.body = {
    "status" => "success",
    "data" => {
      "token" => auth_token,
      "version" => Fog::Dynect::Mock.version
    },
    "job_id" => Fog::Dynect::Mock.job_id,
    "msgs"=>[{
      "INFO"=>"login: Login successful",
      "SOURCE"=>"BLL",
      "ERR_CD"=>nil,
      "LVL"=>"INFO"
    }]
  }
  response
end

#post_zone(rname, ttl, zone, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
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
67
# File 'lib/fog/dynect/requests/dns/post_zone.rb', line 32

def post_zone(rname, ttl, zone, options = {})
  new_zone = self.data[:zones][zone] = {
    :next_record_id => 0,
    :records => Hash.new do |records_hash, type|
      records_hash[type] = []
    end,
    :records_to_delete => [],
    :rname => rname,
    :serial_style => options[:serial_style] || "increment",
    :serial => 0,
    :ttl => ttl,
    :zone => zone,
    :zone_type => "Primary"
  }

  response = Excon::Response.new
  response.status = 200
  response.body = {
    "status" => "success",
    "data" => {
      "zone_type" => new_zone[:zone_type],
      "serial_style" => new_zone[:serial_style],
      "serial" => new_zone[:serial],
      "zone" => zone
    },
    "job_id" => Fog::Dynect::Mock.job_id,
    "msgs" => [{
      "INFO" => "create: New zone #{zone} created.  Publish it to put it on our server.",
      "SOURCE" => "BLL",
      "ERR_CD" => nil,
      "LVL" => "INFO"
    }]
  }

  response
end

#put_record(type, zone, fqdn, rdata, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
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
67
68
69
70
71
72
73
# File 'lib/fog/dynect/requests/dns/put_record.rb', line 32

def put_record(type, zone, fqdn, rdata, options = {})
  raise Fog::DNS::Dynect::NotFound unless zone = self.data[:zones][zone]

  records = zone[:records]
  record_id = zone[:next_record_id]
  zone[:next_record_id] += 1

  record = {
    :type => type,
    :zone => zone,
    :fqdn => fqdn,
    :rdata => rdata,
    :ttl => options[:ttl] || zone[:ttl],
    :record_id => record_id
  }

  records[type] << record

  response = Excon::Response.new
  response.status = 200

  response.body = {
    "status" => "success",
    "data" => {
      "zone" => record[:zone][:zone],
      "ttl" => record[:ttl],
      "fqdn" => record[:fqdn],
      "record_type" => record[:type],
      "rdata" => record[:rdata],
      "record_id" => record[:record_id]
   },
   "job_id" => Fog::Dynect::Mock.job_id,
   "msgs" => [{
     "INFO"=>"add: Record added",
     "SOURCE"=>"BLL",
     "ERR_CD"=>nil,
     "LVL"=>"INFO"
   }]
  }

  response
end

#put_zone(zone, options = {}) ⇒ Object



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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fog/dynect/requests/dns/put_zone.rb', line 27

def put_zone(zone, options = {})
  raise Fog::DNS::Dynect::NotFound unless zone = self.data[:zones][zone]

  raise ArgumentError unless options.size == 1

  response = Excon::Response.new
  response.status = 200

  data = {}

  if options['freeze']
    zone['frozen'] = true
    info = "freeze: Your zone is now frozen"
  elsif options['publish']
    zone[:changes] = {}
    zone[:records_to_delete].each do |record|
      zone[:records][record[:type]].delete_if { |r| r[:fqdn] == record[:fqdn] && r[:record_id] == record[:record_id] }
    end
    zone[:records_to_delete] = []
    data = {
      "zone_type" => zone[:zone_type],
      "serial_style" => zone[:serial_style],
      "serial" => zone[:serial] += 1,
      "zone" => zone[:zone]
    }
    info = "publish: #{zone[:zone]} published"
  elsif options['thaw']
    zone[:frozen] = false
    info = "thaw: Your zone is now thawed, you may edit normally"
  else
    raise ArgumentError
  end

  response.body = {
    "status" => "success",
    "data" => data,
    "job_id" => Fog::Dynect::Mock.job_id,
    "msgs" => [{
      "INFO" => info,
      "SOURCE"=>"BLL",
      "ERR_CD"=>nil,
      "LVL"=>"INFO"
    }]
  }

  response
end

#reset_dataObject



58
59
60
# File 'lib/fog/dynect/dns.rb', line 58

def reset_data
  self.class.reset
end