Class: Restcall

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

Overview

contains restcalls to get and set data upon dyn API

Instance Method Summary collapse

Instance Method Details

#AddARecord(zone, arecord, ip) ⇒ Object

Adding new A record



709
710
711
712
713
714
# File 'lib/dyntool.rb', line 709

def AddARecord(zone,arecord,ip)
	@url = URI.parse("https://api2.dynect.net/REST/ARecord/#{zone}/#{arecord}")
	@record_data = { :rdata => { :address => "#{ip}" }, :ttl => "0" }
	PostDyn()
	PublishDyn(zone)
end

#AddCnameRecord(zone, cname, arecord, flag) ⇒ Object

Adding Cname record



737
738
739
740
741
742
743
744
745
746
747
748
749
750
# File 'lib/dyntool.rb', line 737

def AddCnameRecord(zone,cname,arecord,flag)
	@url = URI.parse("https://api2.dynect.net/REST/CNAMERecord/#{zone}/#{cname}")
	@record_data = { :rdata => { :cname => "#{arecord}" }, :ttl => "0" }
	if (flag == "post")
		puts "Adding cname record"
		puts "arecord #{arecord}, cname: #{cname}"
		PostDyn()
	elsif (flag == "put")
		puts "Changing cname record"
		puts "arecord #{arecord}, cname: #{cname}"
		PutDyn()
	end
               PublishDyn(zone)
end

#AddTxtRecord(zone, arecord, txt) ⇒ Object

Adding txt record



752
753
754
755
756
757
# File 'lib/dyntool.rb', line 752

def AddTxtRecord(zone,arecord,txt)
	@url = URI.parse("https://api2.dynect.net/REST/TXTRecord/#{zone}/#{arecord}")
	@record_data = { :rdata => { :txtdata => "#{txt}" }, :ttl => "0" }
	PostDyn()
               PublishDyn(zone)
end

#CheckARecord(zone, arecord) ⇒ Object



700
701
702
703
# File 'lib/dyntool.rb', line 700

def CheckARecord(zone,arecord)
	@dynquery = URI.parse("https://api2.dynect.net/REST/ARecord/#{zone}/#{arecord}")
	GetDyn()
end

#CheckCnameRecord(zone, cname) ⇒ Object



704
705
706
707
# File 'lib/dyntool.rb', line 704

def CheckCnameRecord(zone,cname)
	@dynquery = URI.parse("https://api2.dynect.net/REST/CNAMERecord/#{zone}/#{cname}")
	GetDyn()
end

#CheckNodes(zone, node, state) ⇒ Object

Get List of nodes to compare with given node



683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# File 'lib/dyntool.rb', line 683

def CheckNodes(zone,node,state)
	@url = URI.parse("https://api2.dynect.net/REST/NodeList/#{zone}/")
	GetDyn()
	@nodes = []
	@result['data'].each do |fqdn|
		@nodes << fqdn
	end
	if  state == "true"
		if ! @nodes.include? node
			abort("The node you entered does not exist")
		end
	elsif state == "false"
		if @nodes.include? node
		        abort("The node you entered already exist")
		end
	end
end

#CheckNodeService(service, fqdn) ⇒ Object

checking to which gee service geo node is attached



820
821
822
823
824
825
826
827
828
829
830
831
832
# File 'lib/dyntool.rb', line 820

def CheckNodeService(service,fqdn)
	@url = URI.parse("https://api2.dynect.net/REST/Geo/#{service}")
	@result = GetDyn()
	@nodes =  @result['data']['nodes']
	@nodes.each do |node|
		if (node['fqdn'] == "#{fqdn}") 
			puts "Yes!!!"
			puts "here are all the nodes that linked to that service:"
			PrintNodes(@nodes)
			exit(0)
		end
	end
end

#CheckZone(zone) ⇒ Object

Check if the zone exist in dyn configuration



675
676
677
678
679
680
681
# File 'lib/dyntool.rb', line 675

def CheckZone(zone)
	@url = URI.parse("https://api2.dynect.net/REST/Zone/#{zone}/")
	GetDyn()
	if  @result['status'] !=  "success"
		abort("The zone name is invalid")
	end
end

#CloseSessionObject

closing dyn session



670
671
672
673
# File 'lib/dyntool.rb', line 670

def CloseSession
	  @url = URI.parse('https://api2.dynect.net/REST/Session/')
                 @resp, @data = @http.delete(@url.path, @headers)
end

#CreateZone(zone, contacts) ⇒ Object



845
846
847
848
849
850
# File 'lib/dyntool.rb', line 845

def CreateZone(zone,contacts)
	@url = URI.parse("https://api2.dynect.net/REST/Zone/#{zone}/")
	@record_data = { :rname => "[email protected]", :ttl => "3600" }
	PostDyn()
	PublishDyn(zone)
end

#DeleteDynObject



787
788
789
790
791
# File 'lib/dyntool.rb', line 787

def DeleteDyn()
	@headers = { "Content-Type" => 'application/json', 'Auth-Token' => @auth_token }
	@resp, @data = @http.delete(@url.path, @headers)
	puts @data
end

#DeleteNode(zone, fqdn) ⇒ Object

deleting node



759
760
761
762
763
# File 'lib/dyntool.rb', line 759

def DeleteNode(zone,fqdn)
	@url = URI.parse("https://api2.dynect.net/REST/Node/#{zone}/#{fqdn}")
	DeleteDyn()
	PublishDyn(zone)
end

#DeleteService(service) ⇒ Object

deleting service



765
766
767
768
769
# File 'lib/dyntool.rb', line 765

def DeleteService(service)
	@url = URI.parse("https://api2.dynect.net/REST/Geo/#{service}/")
	DeleteDyn()
	#PublishDyn(zone)
end

#GenerateGeoNode(service, zone, geonode) ⇒ Object

Generating geo node and attaching to service



729
730
731
732
733
734
735
# File 'lib/dyntool.rb', line 729

def GenerateGeoNode(service,zone,geonode)
	puts "Creating geo node"
	@url = URI.parse("https://api2.dynect.net/REST/GeoNode/#{service}")
	@record_data =  { :fqdn => "#{geonode}", :zone => "#{zone}" }
	PostDyn()
	PublishDyn(zone)
end

#GenerateGeoService(service, record_data, flag) ⇒ Object

Generating or modifying Geo service



716
717
718
719
720
721
722
723
724
725
726
727
# File 'lib/dyntool.rb', line 716

def GenerateGeoService(service,record_data,flag)
	@record_data = record_data
	@url = URI.parse("https://api2.dynect.net/REST/Geo/#{service}/")
	if flag == "create"
		puts "Creating geo service"
		PostDyn()
	else
		puts "Changing geo service"
		PutDyn()
	end
        #PublishDyn(zone)
end

#GetCnameService(cname, zone) ⇒ Object

Gets cname and zone and return the geo node that the cname point to



834
835
836
837
838
839
840
841
842
843
844
# File 'lib/dyntool.rb', line 834

def GetCnameService(cname,zone)
	@url = URI.parse("https://api2.dynect.net/REST/CNAMERecord/#{zone}/#{cname}/")
	@result = GetDyn()
	@cn_data = @result['data']
	@cn_array = @cn_data[0].split("/")
	@id = @cn_array.last
	@url = URI.parse("https://api2.dynect.net/REST/CNAMERecord/#{zone}/#{cname}/#{@id}")
	@result = GetDyn()
	@geonode = @result['data']['rdata']['cname'].chomp('.')
	return(@geonode)
end

#GetDynObject

get method to dyn



771
772
773
774
775
776
# File 'lib/dyntool.rb', line 771

def GetDyn()
	@headers = { "Content-Type" => 'application/json', 'Auth-Token' => @auth_token }
	@resp, @data = @http.get(@url.path, @headers)
	@result = JSON.parse(@data)
	return(@result)
end

#GetGeoData(service) ⇒ Object



813
814
815
816
817
818
# File 'lib/dyntool.rb', line 813

def GetGeoData(service)
	puts "Getting geo data"
	@url = URI.parse("https://api2.dynect.net/REST/Geo/#{service}")
	@result = GetDyn()
	return(@result)
end

#GetServicesObject

return list of geo service defined for the account



802
803
804
805
806
807
808
809
810
811
812
# File 'lib/dyntool.rb', line 802

def GetServices()
	puts "Getting the list of geo services"
	@url = URI.parse("https://api2.dynect.net/REST/Geo/")
	@result = GetDyn()
	@services = @result['data']
	@gservices = []
	@services.each do |service|
		@gservices << service.gsub(/\/REST\/Geo\/|\//,"")
	end
	return(@gservices)
end

#OpenSession(customer, username, password) ⇒ Object

Open a session to syn Restful API



649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'lib/dyntool.rb', line 649

def OpenSession(customer,username,password)
	# Set up our HTTP object with the required host and path	
	 @url = URI.parse('https://api2.dynect.net/REST/Session/')
	 @headers = { "Content-Type" => 'application/json' }
	 @http = Net::HTTP.new(@url.host, @url.port)
	 @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
	 @http.use_ssl = true
	 @session_data = { :customer_name => customer, :user_name => username, :password => password }
	 @resp, @data = @http.post(@url.path, @session_data.to_json, @headers)
	 @result = JSON.parse(@data)
	 if @result['status'] == 'success'
		 @auth_token = @result['data']['token']
	 else
	         puts "Command Failed:\n"
	 # the messages returned from a failed command are a list
	         @result['msgs'][0].each{|key, value| print key, " : ", value, "\n"}
		 exit(2)
	 end
	 return(@result)
end

#PostDynObject

post method to dyn



778
779
780
781
# File 'lib/dyntool.rb', line 778

def PostDyn()
	@headers = { "Content-Type" => 'application/json', 'Auth-Token' => @auth_token }
	@resp, @data = @http.post(@url.path, @record_data.to_json, @headers)
end

#PrintNodes(nodes) ⇒ Object



851
852
853
854
855
# File 'lib/dyntool.rb', line 851

def PrintNodes(nodes)
	nodes.each do |node|
		puts node['fqdn']
	end
end

#PublishDyn(zone) ⇒ Object

publishing data to dyn



793
794
795
796
797
798
799
800
# File 'lib/dyntool.rb', line 793

def PublishDyn(zone)
	puts "Publishing to Dyn" 
	@url = URI.parse("https://api2.dynect.net/REST/Zone/#{zone}/")
	@publish_data = { "publish" => "true" }
               @resp, @data = @http.put(@url.path, @publish_data.to_json, @headers)
	@result = JSON.parse(@data)
	puts @result['status']
end

#PutDynObject

put method to dyn



783
784
785
786
# File 'lib/dyntool.rb', line 783

def PutDyn()
	@headers = { "Content-Type" => 'application/json', 'Auth-Token' => @auth_token }
	@resp, @data = @http.put(@url.path, @record_data.to_json, @headers)
end