Module: AliDns

Extended by:
AliDns
Included in:
AliDns
Defined in:
app/apis/ali_dns.rb

Instance Method Summary collapse

Instance Method Details

#add_acme_record(domain, value) ⇒ Object



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
# File 'app/apis/ali_dns.rb', line 39

def add_acme_record(domain, value)
  domain_arr = domain.split('.')
  root_domain = domain_arr[-2..-1].join('.')
  rr = ['_acme-challenge', *domain_arr[0...-2]].join('.')

  body = {
    action: 'AddDomainRecord'
  }
  body.merge! params: {
    DomainName: root_domain,
    Type: 'TXT',
    RR: rr,
    value: value
  }
  body.merge! opts: {
    method: 'POST',
    timeout: 15000
  }

  begin
    response = client.request(**body)
  rescue StandardError => e
  ensure
    records(root_domain)
  end
end

#check_record(domain, value) ⇒ Object



32
33
34
35
36
37
# File 'app/apis/ali_dns.rb', line 32

def check_record(domain, value)
  result = records(domain).dig('DomainRecords', 'Record')
  if result
    result.find { |i| i['Type'] == 'TXT' && i['Value'] == value }
  end
end

#clientObject



9
10
11
12
13
14
15
16
# File 'app/apis/ali_dns.rb', line 9

def client
  @client = RPCClient.new(
    endpoint: 'https://alidns.cn-hangzhou.aliyuncs.com',
    api_version: '2015-01-09',
    access_key_id: SETTING.aliyun[:key],
    access_key_secret: SETTING.aliyun[:secret]
  )
end

#records(domain) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/apis/ali_dns.rb', line 18

def records(domain)
  body = {
    action: 'DescribeDomainRecords'
  }
  body.merge! params: {
    DomainName: domain
  }
  body.merge! opts: {
    method: 'POST',
    timeout: 15000
  }
  client.request(**body)
end