Class: CivoCLI::DomainRecord

Inherits:
Thor
  • Object
show all
Defined in:
lib/domain_record.rb

Instance Method Summary collapse

Instance Method Details

#create(name, type, value) ⇒ Object



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
81
# File 'lib/domain_record.rb', line 44

def create(name, type, value)
  CivoCLI::Config.set_api_auth
  domain, part = find_domain(name)
  if domain
    type = case(type.downcase)
    when "cname", "canonical"
      "cname"
    when "mx", "mail"
      "mx"
    when "txt", "text"
      "txt"
    else
      "a"
    end.upcase
    out = "Created #{type.colorize(:green)} record #{part.colorize(:green)} for #{domain.name.colorize(:green)}"
    if options[:ttl] && options[:ttl] != ""
      out += " with a TTL of #{options[:ttl].colorize(:green)} seconds"
      if options[:priority] && options[:priority] != ""
        out += " and"
      end
    end
    if options[:priority] && options[:priority] != ""
      out += " with a priority of #{options[:priority].colorize(:green)}"
    end
    options[:ttl] ||= 600
    options[:priority] ||= 0

    record = Civo::DnsRecord.create(type: type, domain_id: domain.id, name: part, value: value, priority: options[:priority], ttl: options[:ttl])
    puts record.inspect
    puts "#{out} with ID #{record.id.colorize(:green)}"
  else
    puts "Unable to find the domain name for #{name}".colorize(:red)
  end
rescue Flexirest::HTTPException => e
  puts e.inspect
  puts e.result.reason.colorize(:red)
  exit 1
end

#list(domain_id) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/domain_record.rb', line 5

def list(domain_id)
  CivoCLI::Config.set_api_auth
  domain = Civo::DnsDomain.all.items.detect {|key| key.id == domain_id || key.name == domain_id}

  rows = []
  Civo::DnsRecord.all(domain_id: domain.id).items.each do |record|
    value = (record.value.length > 20 ? record.value[0, 17] + "..." : record.value)
    rows << [record.id, record.type.upcase, "#{record.name}.#{domain.name.colorize(:light_black)}", value, record.ttl, record.priority]
  end
  puts Terminal::Table.new headings: ['ID', 'Type', 'Name', 'Value', 'TTL', 'Priority'], rows: rows
end

#remove(id) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/domain_record.rb', line 85

def remove(id)
  CivoCLI::Config.set_api_auth
  Civo::DnsDomain.all.items.each do |d|
    Civo::DnsRecord.all(domain_id: d.id).items.each do |r|
      if r.id == id
        Civo::DnsRecord.remove(domain_id: d.id, id: r.id)
        puts "Removed the record #{r.name.colorize(:green)} record with ID #{r.id.colorize(:green)}"
      end
    end
  end
rescue Flexirest::HTTPException => e
  puts e.result&.reason&.colorize(:red)
  exit 1
end

#show(record_id) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/domain_record.rb', line 20

def show(record_id)
  CivoCLI::Config.set_api_auth
  Civo::DnsDomain.all.items.each do |domain|
    @domain = domain
    @record = Civo::DnsRecord.all(domain_id: domain.id).detect {|key| key.id == record_id || (key.domain_id == domain.id && key.name == record_id.gsub(/\.#{domain.name}$/, '')) }
    break if @record
  end
  puts "               ID: #{@record.id}"
  puts "             Type: #{@record.type.upcase}"
  puts "             Name: #{@record.name}.#{@domain.name.colorize(:light_black)}"
  puts "              TTL: #{@record.ttl}"
  puts "         Priority: #{@record.priority}"
  puts ""
  puts "-" * 29 + "VALUE" + "-" * 29
  puts @record.value
rescue Flexirest::HTTPException => e
  puts e.result.reason.colorize(:red)
  exit 1
end