Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added basic CLI, fixes #4

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,17 @@ Basic usage:
```ruby
require 'namesilo-client'

client = NamesiloClient::API.new(YOUR_API_KEY)
client = NamesiloClient::API.new('YOUR_API_KEY')
response = client.get_contact_list()
```

## CLI Usage

You can use CLI tool to interact with Namesilo:

```bash
export NAMESILO_API_KEY='YOUR_API_KEY'

namesilo domains # list all your domains
namesilo add_aa some.domain.com 1.2.3.4 # add or update DNS record
```
22 changes: 22 additions & 0 deletions bin/namesilo
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env ruby
require "bundler/setup"
require "namesilo_client"
require "cli"
require "nori"
require "thor"

NAMESILO_API_KEY = ENV['NAMESILO_API_KEY'] || fail("NAMESILO_API_KEY expected")

class MyCLI < Thor
desc "add_aa [domain] [ip]", "Assign domain to IP"
def add_aa(full_domain, ip)
NamesiloClient::Cli.new.add_aa(full_domain, ip)
end

desc "domains", 'print list of domains'
def domains
puts NamesiloClient::Cli.new.domains
end
end

MyCLI.start(ARGV)
54 changes: 54 additions & 0 deletions lib/cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module NamesiloClient
class Cli
def initialize
@client = NamesiloClient::API.new(NAMESILO_API_KEY)
end

def domains
@domains ||= list_domains["domains"]["domain"]
end

def add_aa(full_domain, ip)
splitted = full_domain.split('.')
domain = splitted.pop
domain = splitted.pop + '.' + domain
unless domains.include?(domain)
fail "Not found domain: #{domain}"
end
host = splitted.join('.')

existing_records_list = list_dns_records(domain)["resource_record"]

existing_record = existing_records_list.find do |record|
record["host"] == full_domain
end

if existing_record && existing_record["value"] == ip
puts "Such record already exists: \n#{existing_record}"
return
end

if existing_record
puts "Updating existing record. It was: \n#{existing_record}"
update_dns_record({rrid: existing_record['record_id'], domain: domain, rrhost: host, rrvalue: ip, rrtype: "A"})
else
puts "Adding new record"
# Add a DNS record
# Parameters:
# domain: The domain being updated
# rrtype: DNS record type, e.g. "A", "AAAA", "CNAME", "MX" and "TXT"
# rrhost: hostname for the new record
# rrvalue: The value for the resource record
# rrdistance: Only used for MX (default is 10 if not provided)
# rrttl: The TTL for the new record (default is 7207 if not provided)
add_dns_record({domain: domain, rrhost: host, rrvalue: ip, rrtype: "A"})
end
end

def method_missing(method, *args, &block)
body = @client.send(method, *args, &block)
parser = Nori.new
parser.parse(body)["namesilo"]["reply"]
end
end
end
48 changes: 24 additions & 24 deletions lib/namesilo_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

module NamesiloClient
class API

# Class constructor
def initialize(apikey)
@apikey = apikey
Expand All @@ -18,8 +18,8 @@ def initialize(apikey)
# Establish connection
def get_connection()
conn = Faraday.new(:url => @host) do |c|
c.use Faraday::Request::UrlEncoded
c.use Faraday::Adapter::NetHttp
c.use Faraday::Request::UrlEncoded
c.use Faraday::Adapter::NetHttp
end
end

Expand Down Expand Up @@ -135,7 +135,7 @@ def list_dns_records_array(domain)
record.type = r.xpath('type').text()
record.value = r.xpath('value').text()
record.ttl = r.xpath('ttl').text()
record.distance = r.xpath('distance').text()
record.distance = r.xpath('distance').text()
dns_records << record
end
dns_records
Expand All @@ -145,7 +145,7 @@ def list_dns_records_array(domain)
# Parameters:
# domain: The domain being updated
# rrtype: DNS record type, e.g. "A", "AAAA", "CNAME", "MX" and "TXT"
# rrhost: hostname for the new record
# rrhost: hostname for the new record
# rrvalue: The value for the resource record
# rrdistance: Only used for MX (default is 10 if not provided)
# rrttl: The TTL for the new record (default is 7207 if not provided)
Expand All @@ -156,7 +156,7 @@ def add_dns_record(params)
# Update DNS record
# Parameters:
# domain
# rrid: The unique ID of the resource record.
# rrid: The unique ID of the resource record.
# rrhost: The hostname
# rrvalue: The value for the resource record
# rrdistance: Only used for MX
Expand Down Expand Up @@ -300,11 +300,11 @@ def renew_domain(params)
# registerDomain
# Parameters
# domain(required): The domain to renew
# years(required): The number of years to renew the domain
# years(required): The number of years to renew the domain
#
# payment_id(optional)
# coupon(optional)
# private(optional): if the free WHOIS privacy service will be used or not
# private(optional): if the free WHOIS privacy service will be used or not
# auto_renew(optional)
# portfolio(optional): the name of the portfolio to link the registered domain with
# ns1-13(optional): up to 13 name servers to use for the domain registration
Expand All @@ -314,21 +314,21 @@ def register_domain(params)
end

# transferDomain
# Parameters
# Parameters
# domain(required)
#
# payment_id(optional)
# auth(optional): transfer authorization code
# private(optional): if you want the domain to utilize our free WHOIS privacy service
# auto_renew(optional)
# portfolio(optional)
# auto_renew(optional)
# portfolio(optional)
# coupon(optional)
# Passing Contact Information(optional): see https://www.namesilo.com/api_reference.php#transferDomain
# Passing Contact ID(optional): see https://www.namesilo.com/api_reference.php#transferDomain
def transfer_domain(params)
get_request('transferDomain?'+get_url_parameters(params)).body
end

# transferUpdateChangeEPPCode
# Parameters
# domain
Expand Down Expand Up @@ -399,7 +399,7 @@ def list_dns_sec_records(domain)
# alg: see: https://www.namesilo.com/api_reference.php#dnsSecAddRecord
def add_dns_sec_record(params)
get_request('dnsSecAddRecord?'+get_url_parameters(params)).body
end
end

# dnsSecDeleteRecord
# Parameters: as same as dnsSecAddRecord
Expand All @@ -409,11 +409,11 @@ def del_dns_sec_record(params)

# portfolioAdd
# Parameters:
# portfolio: The encoded name of the portfolio to add
# portfolio: The encoded name of the portfolio to add
def add_portfolio(portfolio)
params={'portfolio':portfolio}
get_request('portfolioAdd?'+get_url_parameters(params)).body
end
get_request('portfolioAdd?'+get_url_parameters(params)).body
end

# portfolioDelete
# Parameters:
Expand All @@ -439,7 +439,7 @@ def associate_domain_portfolio(portfolio,domains)
# ip2-ip13(optional)
def add_registered_name_server(params)
get_request('addRegisteredNameServer?'+get_url_parameters(params)).body
end
end

# modifyRegisteredNameServer
# Parameters:
Expand Down Expand Up @@ -493,11 +493,11 @@ def remove_auto_renewal(domain)
# protocol: http or https
# address: the web site address to forward to
# method: "301", "302" or "cloaked"
#
#
# Optional parameters:
# meta_title: The META title for cloaked forward
# meta_description
# meta_keywords: The META keywords for cloaked forward
# meta_keywords: The META keywords for cloaked forward
def forward_domain(params)
get_request('domainForward?'+get_url_parameters(params)).body
end
Expand Down Expand Up @@ -539,12 +539,12 @@ def unlock_domain(domain)
# domain
# email
# forward1: the first email address to foward emails
#
#
# Optional parameters
# forward2-5
# forward2-5
def forward_email(params)
get_request('configureEmailForward?'+get_url_parameters(params)).body
end
end

# deleteEmailForward: delete a email forward
# Parameters
Expand All @@ -565,7 +565,7 @@ def email_verification(email)

# addAccountFunds: increase NameSilo account funds balance
# Parameters
# amount: the amount in US Dollars
# amount: the amount in US Dollars
# payment_id: The ID of the verified credit card
def add_account_funds(amount,payment_id)
params={'amount':amount,'payment_id':payment_id}
Expand All @@ -587,7 +587,7 @@ def marketplace_add_sale(params)
# marketplaceLandingPageUpdate
# required parameters
# domain
#
#
# optional parameters: see https://www.namesilo.com/api_reference.php#marketplaceLandingPageUpdate
def marketplace_landing_page_update(params)
get_request('marketplaceLandingPageUpdate?'+get_url_parameters(params)).body
Expand Down
8 changes: 8 additions & 0 deletions namesilo_client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@ Gem::Specification.new do |s|
s.files = ["lib/namesilo_client.rb"]
s.homepage = 'http://rubygems.org/gems/namesilo_client'
s.license = 'MIT'
s.files = Dir.chdir(File.expand_path('..', __FILE__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
end
s.bindir = "bin"
s.executables << 'namesilo'

s.add_runtime_dependency 'faraday', '~> 0.13'
s.add_runtime_dependency 'addressable', '~> 2.5'
s.add_runtime_dependency 'nokogiri', '~> 1.10'
s.add_runtime_dependency "nori", "~> 2.6"
s.add_runtime_dependency "thor", "~> 0.20.3"
s.add_development_dependency 'rspec', '~> 3.7'
end