ruby_phpipam 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 03afbcf591d332ec1cb73fb534bff63ad7438162
4
+ data.tar.gz: f4cc8ad1794c73ae546b6328db10f5012552d82d
5
+ SHA512:
6
+ metadata.gz: 2ec050a20f449e859a366d4d854317a8676561c90a2a3691847d285e18b7aa9a326d55ac42784a9d61d0730e5fbdbaac17b1c3ef8e79c473ce781a4b6400e579
7
+ data.tar.gz: 4b74f2c12a427ce363e5a162b504b3b64c3a350a092471955693a6d93b6a1bd35fd878d8755bd96ce18105e50e363186927409b930ebd66c8806fa7aa21485c3
@@ -0,0 +1,19 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ # RVM and others
15
+ .ruby-version
16
+ .ruby-gemset
17
+
18
+ # dotenv file
19
+ .env
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruby-phpipam.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Amin Arria
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,173 @@
1
+ # ruby_phpipam
2
+
3
+ **Important Note**: GET actions have highest priority for development, but feel free to make a PR for the others.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ruby_phpipam', git: 'git://github.com/AminArria/ruby-phpipam'
11
+
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+
19
+ ## Usage
20
+
21
+ ### Configuration
22
+ ```ruby
23
+ Phpipam.configure do |config|
24
+ config.base_url = "http://my.phpipam.server/api/my_app"
25
+ config.username = "username"
26
+ config.password = "password"
27
+ end
28
+ ```
29
+
30
+ ### Authenticating
31
+ ```ruby
32
+ Phpipam.authenticate
33
+ ```
34
+
35
+ ### API Calls
36
+ In here you'll see the following:
37
+ ```
38
+ method_name() What it does What it returns
39
+ ```
40
+
41
+ #### Section
42
+ ```
43
+ self.get(id_or_name) Get section by ID or name Section object
44
+ self.get_all() Get all sections Array of section objects
45
+ subnets() Get all subnets from the section Array of subnet objects
46
+ ```
47
+
48
+ #### Subnet
49
+ ```
50
+ self.get(id) Get subnet by ID Subnet object
51
+ self.search(cidr) Search for subnet by CIDR Subnet object
52
+ usage() Get usage statistics for subnets Subnet object with usage statistics
53
+ addresses() Get all addresses from the subnet Array of address objects
54
+ first_free_ip() Get first usable IP String
55
+ slaves() Get inmediate slave subnets Array of subnet objects
56
+ slaves_recursive() Get all slave subnets Array of subnet objects (Includes self)
57
+ first_subnet(mask) Get first available subnet (CIDR) String
58
+ for given mask
59
+ all_subnets(mask) Get all posible subnets (CIDR) Array of strings
60
+ for given mask
61
+ ```
62
+
63
+ #### Address
64
+ ```
65
+ self.get(id) Get address by ID Address object
66
+ ```
67
+
68
+ ## Pending API endpoints
69
+ This are endpoints that aren't standarized into a method and a parsed response. You can still call them through ```Phpipam::Query.get(...)``` and get the raw response.
70
+
71
+ ### Sections Endpoints
72
+ ```
73
+ GET /api/my_app/sections/custom_fields/ Returns custom section fields
74
+ POST /api/my_app/sections/ Creates new section
75
+ PATCH /api/my_app/sections/ Updates section
76
+ DELETE /api/my_app/sections/ Deletes section with all belonging subnets and addresses
77
+ ```
78
+
79
+ ### Subnets Endpoints
80
+ ```
81
+ GET /api/my_app/subnets/{id}/addresses/{ip}/ Returns IP address from subnet
82
+ GET /api/my_app/subnets/custom_fields/ Returns all subnet custom fields
83
+ POST /api/my_app/subnets/ Creates new subnet
84
+ POST /api/my_app/subnets/{id}/first_subnet/{mask}/ Creates new child subnet inside subnet with specified mask
85
+ PATCH /api/my_app/subnets/ Updates Subnet
86
+ PATCH /api/my_app/subnets/{id}/resize/ Resizes subnet to new mask
87
+ PATCH /api/my_app/subnets/{id}/split/ Splits subnet to smaller subnets
88
+ PATCH /api/my_app/subnets/{id}/permissions/ Sets subnet permissions (?grouname1=ro&groupname2=3&43=1)
89
+ DELETE /api/my_app/subnets/{id}/ Deletes Subnet
90
+ DELETE /api/my_app/subnets/{id}/truncate/ Removes all addresses from subnet
91
+ DELETE /api/my_app/subnets/{id}/permissions/ Removes all permissions
92
+ ```
93
+
94
+ ### Addresses Endpoints
95
+ ```
96
+ GET /api/my_app/addresses/{id}/ping/ Checks address status
97
+ GET /api/my_app/addresses/{ip}/{subnetId}/ Returns address from subnet by ip address
98
+ GET /api/my_app/addresses/search/{ip}/ Searches for addresses in database, returns multiple if found
99
+ GET /api/my_app/addresses/search_hostname/{hostname}/ Searches for addresses in database by hostname, returns multiple if found
100
+ GET /api/my_app/addresses/first_free/{subnetId}/ Returns first available address (subnetId can be provided with parameters)
101
+ GET /api/my_app/addresses/custom_fields/ Returns custom fields
102
+ GET /api/my_app/addresses/tags/ Returns all tags
103
+ GET /api/my_app/addresses/tags/{id}/ Returns specific tag
104
+ GET /api/my_app/addresses/tags/{id}/addresses/ Returns addresses for specific tag
105
+ POST /api/my_app/addresses/ Creates new address
106
+ POST /api/my_app/addresses/first_free/{subnetId}/ Creates new address in subnets – first available (subnetId can be provided with parameters)
107
+ PATCH /api/my_app/addresses/{id}/ Updates address
108
+ DELETE /api/my_app/addresses/{id}/ Deletes address (use 'remove_dns=1' parameter to remove all related DNS records)
109
+ DELETE /api/my_app/addresses/{ip}/{subnetId}/ Deletes address by IP in specific subnet
110
+ ```
111
+
112
+ ### VLAN Endpoints
113
+ ```
114
+ GET /api/my_app/vlan/ Returns all Vlans
115
+ GET /api/my_app/vlan/{id}/ Returns specific Vlan
116
+ GET /api/my_app/vlan/{id}/subnets/ Returns all subnets attached tovlan
117
+ GET /api/my_app/vlan/{id}/subnets/{sectionId}/ Returns all subnets attached to vlan in specific section
118
+ GET /api/my_app/vlan/{id}/custom_fields/ Returns custom VLAN fields
119
+ GET /api/my_app/vlan/{id}/search/{number}/ Searches for VLAN
120
+ POST /api/my_app/vlan/ Creates new VLAN
121
+ PATCH /api/my_app/vlan/ Updates VLAN
122
+ DELETE /api/my_app/vlan/ Deletes VLAN
123
+ ```
124
+
125
+ ### VLAN Domains (L2 domains) Endpoints
126
+ ```
127
+ GET /api/my_app/l2domains/ Returns all L2 domains
128
+ GET /api/my_app/l2domains/{id}/ Returns specific L2 domain
129
+ GET /api/my_app/l2domains/{id}/vlans/ Returns all VLANs within L2 domain
130
+ GET /api/my_app/l2domains/custom_fields/ Returns all custom fields
131
+ POST /api/my_app/l2domains/ Creates new L2 domain
132
+ PATCH /api/my_app/l2domains/ Updates L2 domain
133
+ DELETE /api/my_app/l2domains/ Deletes L2 domain
134
+ ```
135
+
136
+ ### VRF Endpoints
137
+ ```
138
+ GET /api/my_app/vrf/ Returns all VRFs
139
+ GET /api/my_app/vrf/{id}/ Returns specific VRF
140
+ GET /api/my_app/vrf/{id}/subnets/ Returns all subnets within VRF
141
+ GET /api/my_app/vrf/custom_fields/ Returns all custom fields
142
+ POST /api/my_app/vrf/ Creates new VRF
143
+ PATCH /api/my_app/vrf/ Updates VRF
144
+ DELETE /api/my_app/vrf/ Deletes VRF
145
+ ```
146
+
147
+ ### Devices Endpoints
148
+ ```
149
+ GET /api/my_app/devices/ Returns all devices
150
+ GET /api/my_app/devices/{id}/ Returns specific device
151
+ GET /api/my_app/devices/{id}/subnets/ Returns all subnets within device
152
+ GET /api/my_app/devices/{id}/addresses/ Returns all addresses within device
153
+ GET /api/my_app/devices/search/{search_string}/ Searches for devices with {search_string} in any belonging field
154
+ POST /api/my_app/devices/ Creates new device
155
+ PATCH /api/my_app/devices/ Updates device
156
+ DELETE /api/my_app/devices/ Deletes device
157
+ ```
158
+
159
+ ## Development
160
+
161
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
162
+
163
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
164
+
165
+ ## Contributing
166
+
167
+ Bug reports and pull requests are welcome on GitHub at https://github.com/AminArria/ruby-phpipam.
168
+
169
+
170
+ ## License
171
+
172
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
173
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+
5
+ require "dotenv"
6
+ Dotenv.load
7
+
8
+ require "ruby_phpipam"
9
+
10
+ RubyPhpipam.configure do |config|
11
+ config.base_url = ENV["base_url"]
12
+ config.username = ENV["username"]
13
+ config.password = ENV["password"]
14
+ end
15
+ RubyPhpipam.authenticate
16
+
17
+ require "irb"
18
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+ # External gems
2
+ require 'httparty'
3
+
4
+ # Support Libraries
5
+ require "ruby_phpipam/version"
6
+ require "ruby_phpipam/configuration"
7
+ require "ruby_phpipam/authentication"
8
+ require "ruby_phpipam/exceptions"
9
+ require "ruby_phpipam/query"
10
+ require "ruby_phpipam/helper"
11
+
12
+ # API Libraries
13
+ require "ruby_phpipam/address"
14
+ require "ruby_phpipam/subnet"
15
+ require "ruby_phpipam/section"
16
+
17
+ module RubyPhpipam
18
+ class << self
19
+ attr_accessor :configuration
20
+ attr_accessor :auth
21
+ end
22
+
23
+ def self.configure
24
+ self.configuration ||= Configuration.new
25
+ yield(configuration)
26
+ end
27
+
28
+ def self.authenticate
29
+ self.auth ||= Authentication.new
30
+ end
31
+
32
+ def self.gen_url(url)
33
+ "#{RubyPhpipam.configuration.base_url}#{url}"
34
+ end
35
+ end
@@ -0,0 +1,31 @@
1
+ module RubyPhpipam
2
+ class Address
3
+ attr_reader :id, :subnetId, :ip, :is_gateway, :description, :hostname,
4
+ :mac, :owner, :tag, :PTRignore, :PTR, :deviceId, :port, :note,
5
+ :lastSeen, :excludePing, :editDate
6
+
7
+ def initialize(json)
8
+ @id = RubyPhpipam::Helper.to_type(json[:id], :int)
9
+ @subnetId = RubyPhpipam::Helper.to_type(json[:subnetId], :int)
10
+ @ip = json[:ip]
11
+ @is_gateway = RubyPhpipam::Helper.to_type(json[:is_gateway], :binary)
12
+ @description = json[:description]
13
+ @hostname = json[:hostname]
14
+ @mac = json[:mac]
15
+ @owner = json[:owner]
16
+ @tag = RubyPhpipam::Helper.to_type(json[:tag], :int)
17
+ @PTRignore = RubyPhpipam::Helper.to_type(json[:PTRignore], :binary)
18
+ @PTR = RubyPhpipam::Helper.to_type(json[:PTR], :int)
19
+ @deviceId = RubyPhpipam::Helper.to_type(json[:deviceId], :int)
20
+ @port = json[:port]
21
+ @note = json[:note]
22
+ @lastSeen = RubyPhpipam::Helper.to_type(json[:lastSeen], :date)
23
+ @excludePing = RubyPhpipam::Helper.to_type(json[:excludePing], :binary)
24
+ @editDate = RubyPhpipam::Helper.to_type(json[:editDate], :date)
25
+ end
26
+
27
+ def self.get(id)
28
+ Address.new(RubyPhpipam::Query.get("/addresses/#{id}/"))
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,38 @@
1
+ module RubyPhpipam
2
+ class Authentication
3
+ attr_reader :token, :expires
4
+
5
+ def initialize
6
+ response = HTTParty.post(RubyPhpipam.gen_url("/user/"),
7
+ { basic_auth: { username: RubyPhpipam.configuration.username,
8
+ password: RubyPhpipam.configuration.password
9
+ }
10
+ }
11
+ )
12
+
13
+ body = JSON.parse(response.body, symbolize_names: true)
14
+ unless body[:message].nil?
15
+ raise AuthenticationFailed, body[:message]
16
+ end
17
+
18
+ unless body[:success]
19
+ raise RequestFailed.new(body[:code], body[:message])
20
+ end
21
+
22
+ @token = body[:data][:token]
23
+ @expires = Time.strptime(body[:data][:expires], '%Y-%m-%d %H:%M:%S')
24
+ end
25
+
26
+ def validate_token!
27
+ if @expires <= Time.now
28
+ data = RubyPhpipam::Query.get("/user/")
29
+
30
+ if data[:expires] <= Time.now
31
+ # Pending re authentication
32
+ else
33
+ @expires = data[:expires]
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,11 @@
1
+ module RubyPhpipam
2
+ class Configuration
3
+ attr_accessor :base_url, :username, :password
4
+
5
+ def initialize
6
+ @base_url = nil
7
+ @username = nil
8
+ @password = nil
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,27 @@
1
+ module RubyPhpipam
2
+ # Abstract exception
3
+ class Error < StandardError; end
4
+
5
+ # Raised when authentication fails.
6
+ class AuthenticationFailed < Error; end
7
+
8
+ # Raised when trying to convert to a type that is not defined
9
+ class UndefinedType < Error; end
10
+
11
+ # Raised when cidr is not in cidr format
12
+ class WrongFormatSearch < Error; end
13
+
14
+ # Raised when response success is false.
15
+ class RequestFailed < Error
16
+ attr_reader :code, :message
17
+
18
+ def initialize(code = nil, message = nil)
19
+ @code = code
20
+ @message = message
21
+ end
22
+
23
+ def to_s
24
+ "Error code: #{code}, message: #{message}"
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,30 @@
1
+ module RubyPhpipam
2
+ class Helper
3
+ def self.to_type(value, type)
4
+ return nil if value.nil?
5
+
6
+ case type
7
+ when :int
8
+ value.to_i
9
+ when :binary
10
+ value != "0"
11
+ when :json
12
+ JSON.parse(value)
13
+ when :date
14
+ if value == "0000-00-00 00:00:00"
15
+ nil
16
+ else
17
+ Time.strptime(value, '%Y-%m-%d %H:%M:%S')
18
+ end
19
+ else
20
+ raise UndefinedType, 'given type is not defined'
21
+ end
22
+ end
23
+
24
+ def self.validate_cidr(cidr)
25
+ cidr_regex = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))$/
26
+
27
+ not cidr_regex.match(cidr).nil?
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,26 @@
1
+ module RubyPhpipam
2
+ class Query
3
+ def self.get(url)
4
+ RubyPhpipam.auth.validate_token!
5
+
6
+ response = HTTParty.get(RubyPhpipam.gen_url(url),
7
+ headers: {token: RubyPhpipam.auth.token}
8
+ )
9
+
10
+ body = JSON.parse(response.body, symbolize_names: true)
11
+
12
+ unless body[:success]
13
+ raise RequestFailed.new(body[:code], body[:message])
14
+ end
15
+
16
+ body[:data]
17
+ end
18
+
19
+ def self.get_array(url)
20
+ data = self.get(url)
21
+
22
+ return [] if data.nil?
23
+ return data
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,46 @@
1
+ module RubyPhpipam
2
+ class Section
3
+ attr_reader :id, :name, :description, :masterSection, :permissions,
4
+ :strictMode, :subnetOrdering, :order, :editDate, :showVLAN,
5
+ :showVRF, :showSupernetOnly, :DNS
6
+
7
+ def initialize(json)
8
+ @id = RubyPhpipam::Helper.to_type(json[:id], :int)
9
+ @name = json[:name]
10
+ @description = json[:description]
11
+ @masterSection = RubyPhpipam::Helper.to_type(json[:masterSection], :int)
12
+ @permissions = RubyPhpipam::Helper.to_type(json[:permissions], :json)
13
+ @strictMode = RubyPhpipam::Helper.to_type(json[:strictMode], :binary)
14
+ @subnetOrdering = json[:subnetOrdering]
15
+ @order = RubyPhpipam::Helper.to_type(json[:order], :int)
16
+ @editDate = RubyPhpipam::Helper.to_type(json[:editDate], :date)
17
+ @showVLAN = RubyPhpipam::Helper.to_type(json[:showVLAN], :binary)
18
+ @showVRF = RubyPhpipam::Helper.to_type(json[:showVRF], :binary)
19
+ @showSupernetOnly = RubyPhpipam::Helper.to_type(json[:showSupernetOnly], :binary)
20
+ @DNS = json[:DNS]
21
+ end
22
+
23
+ def self.get(id_or_name)
24
+ Section.new(RubyPhpipam::Query.get("/sections/#{id_or_name}/"))
25
+ end
26
+
27
+ def self.get_all
28
+ data = RubyPhpipam::Query.get("/sections/")
29
+
30
+ sections = []
31
+ data.each do |section|
32
+ sections << Section.new(section)
33
+ end
34
+
35
+ return sections
36
+ end
37
+
38
+ def subnets
39
+ data = RubyPhpipam::Query.get_array("/sections/#{@id}/subnets/")
40
+
41
+ data.map do |subnet|
42
+ RubyPhpipam::Subnet.new(subnet)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,115 @@
1
+ module RubyPhpipam
2
+ class Subnet
3
+ # Subnet attributes
4
+ attr_reader :id, :subnet, :mask, :description, :sectionId, :linked_subnet,
5
+ :vlanId, :vrfId, :masterSubnetId, :namerServerId, :showName,
6
+ :permissions, :DNSrecursive, :DNSrecords, :allowResquests,
7
+ :scanAgent, :pingSubnet, :discoverSubnet, :isFolder, :isFull,
8
+ :state, :threshold, :location, :editDate
9
+
10
+ # Subnet usage attributes
11
+ attr_reader :used, :maxhosts, :freehosts, :freehosts_percent
12
+
13
+ def initialize(json)
14
+ @id = RubyPhpipam::Helper.to_type(json[:id], :int)
15
+ @subnet = json[:subnet]
16
+ @mask = RubyPhpipam::Helper.to_type(json[:mask], :int)
17
+ @description = json[:description]
18
+ @sectionId = RubyPhpipam::Helper.to_type(json[:sectionId], :int)
19
+ @linked_subnet = RubyPhpipam::Helper.to_type(json[:linked_subnet], :int)
20
+ @vlanId = RubyPhpipam::Helper.to_type(json[:vlanId], :int)
21
+ @vrfId = RubyPhpipam::Helper.to_type(json[:vrfId], :int)
22
+ @masterSubnetId = RubyPhpipam::Helper.to_type(json[:masterSubnetId], :int)
23
+ @namerServerId = RubyPhpipam::Helper.to_type(json[:namerServerId], :int)
24
+ @showName = RubyPhpipam::Helper.to_type(json[:showName], :binary)
25
+ @permissions = RubyPhpipam::Helper.to_type(json[:permissions], :json)
26
+ @DNSrecursive = RubyPhpipam::Helper.to_type(json[:DNSrecursive], :binary)
27
+ @DNSrecords = RubyPhpipam::Helper.to_type(json[:DNSrecords], :binary)
28
+ @allowResquests = RubyPhpipam::Helper.to_type(json[:allowResquests], :binary)
29
+ @scanAgent = RubyPhpipam::Helper.to_type(json[:scanAgent], :binary)
30
+ @pingSubnet = RubyPhpipam::Helper.to_type(json[:pingSubnet], :binary)
31
+ @discoverSubnet = RubyPhpipam::Helper.to_type(json[:discoverSubnet], :binary)
32
+ @isFolder = RubyPhpipam::Helper.to_type(json[:isFolder], :binary)
33
+ @isFull = RubyPhpipam::Helper.to_type(json[:isFull], :binary)
34
+ @state = RubyPhpipam::Helper.to_type(json[:state], :int)
35
+ @threshold = RubyPhpipam::Helper.to_type(json[:threshold], :int)
36
+ @location = RubyPhpipam::Helper.to_type(json[:location], :int)
37
+ @editDate = RubyPhpipam::Helper.to_type(json[:editDate], :date)
38
+ end
39
+
40
+ def self.get(id)
41
+ Subnet.new(RubyPhpipam::Query.get("/subnets/#{id}/"))
42
+ end
43
+
44
+ def self.search(cidr)
45
+ unless RubyPhpipam::Helper.validate_cidr(cidr)
46
+ raise WrongFormatSearch, "CIDR doesn't match CIDR format x.x.x.x/y"
47
+ end
48
+
49
+ base, mask = cidr.split("/")
50
+
51
+ data = RubyPhpipam::Query.get("/subnets/cidr/#{base}/#{mask}/")
52
+
53
+ # Currently Rubyphpipam gives the resonse to this query as an array
54
+ # just containing the element.
55
+ return Subnet.new(data[0])
56
+ end
57
+
58
+ def usage
59
+ data = RubyPhpipam::Query.get("/subnets/#{id}/usage/")
60
+
61
+ @used = RubyPhpipam::Helper.to_type(data[:used], :int)
62
+ @maxhosts = RubyPhpipam::Helper.to_type(data[:maxhosts], :int)
63
+ @freehosts = RubyPhpipam::Helper.to_type(data[:freehosts], :int)
64
+ @freehosts_percent = data[:freehosts_percent]
65
+
66
+ return self
67
+ end
68
+
69
+ def addresses
70
+ data = RubyPhpipam::Query.get_array("/subnets/#{id}/addresses/")
71
+
72
+ data.map do |addr|
73
+ RubyPhpipam::Address.new(addr)
74
+ end
75
+ end
76
+
77
+ # This will need to wait for response to the issue created
78
+ # https://github.com/Rubyphpipam/Rubyphpipam/issues/1217
79
+ # def get_address(ip)
80
+ # data = RubyPhpipam::Query.get("/subnets/#{id}/addresses/#{ip}/")
81
+
82
+ # RubyPhpipam::Address.new(data)
83
+ # end
84
+
85
+ def first_free_ip
86
+ # Should this raise an exception if no address available or return nil?
87
+ # Currently it returns nil
88
+ data = RubyPhpipam::Query.get("/subnets/#{id}/first_free/")
89
+ end
90
+
91
+ def slaves
92
+ data = RubyPhpipam::Query.get_array("/subnets/#{id}/slaves/")
93
+
94
+ data.map do |subnet|
95
+ RubyPhpipam::Subnet.new(subnet)
96
+ end
97
+ end
98
+
99
+ def slaves_recursive
100
+ data = RubyPhpipam::Query.get_array("/subnets/#{id}/slaves_recursive/")
101
+
102
+ data.map do |subnet|
103
+ RubyPhpipam::Subnet.new(subnet)
104
+ end
105
+ end
106
+
107
+ def first_subnet(mask)
108
+ RubyPhpipam::Query.get("/subnets/#{id}/first_subnet/#{mask}/")
109
+ end
110
+
111
+ def all_subnets(mask)
112
+ RubyPhpipam::Query.get_array("/subnets/#{id}/all_subnets/#{mask}/")
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,3 @@
1
+ module RubyPhpipam
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ruby_phpipam/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby_phpipam"
8
+ spec.version = RubyPhpipam::VERSION
9
+ spec.authors = ["Amin Arria"]
10
+ spec.email = ["arria.amin@gmail.com"]
11
+
12
+ spec.summary = "Ruby wrapper for the phpipam API"
13
+ spec.description = "Ruby client for the phpipam API"
14
+ spec.homepage = "https://github.com/AminArria/ruby-phpipam"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "httparty"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.14"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ spec.add_development_dependency "dotenv"
30
+ spec.add_development_dependency "webmock"
31
+ spec.add_development_dependency "vcr"
32
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_phpipam
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Amin Arria
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: dotenv
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: vcr
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Ruby client for the phpipam API
112
+ email:
113
+ - arria.amin@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - bin/console
126
+ - bin/setup
127
+ - lib/ruby_phpipam.rb
128
+ - lib/ruby_phpipam/address.rb
129
+ - lib/ruby_phpipam/authentication.rb
130
+ - lib/ruby_phpipam/configuration.rb
131
+ - lib/ruby_phpipam/exceptions.rb
132
+ - lib/ruby_phpipam/helper.rb
133
+ - lib/ruby_phpipam/query.rb
134
+ - lib/ruby_phpipam/section.rb
135
+ - lib/ruby_phpipam/subnet.rb
136
+ - lib/ruby_phpipam/version.rb
137
+ - ruby_phpipam.gemspec
138
+ homepage: https://github.com/AminArria/ruby-phpipam
139
+ licenses:
140
+ - MIT
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 2.6.8
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: Ruby wrapper for the phpipam API
162
+ test_files: []