Class: Infoblox::Resource

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Resource

Returns a new instance of Resource.



112
113
114
115
116
# File 'lib/infoblox/resource.rb', line 112

def initialize(attrs={})
  attrs.each do |k,v|
    self.send("#{k}=", v)
  end
end

Instance Attribute Details

#_refObject

Returns the value of attribute _ref.



3
4
5
# File 'lib/infoblox/resource.rb', line 3

def _ref
  @_ref
end

#connectionObject

Returns the value of attribute connection.



3
4
5
# File 'lib/infoblox/resource.rb', line 3

def connection
  @connection
end

Class Method Details

._return_fieldsObject



58
59
60
# File 'lib/infoblox/resource.rb', line 58

def self._return_fields
  self.remote_attrs.join(",")
end

.all(connection, params = {}) ⇒ Object

Return an array of all records for this resource. You can use the default parameters _max_results an/or _return_fields as documented by Infoblox.

Example: return only 70 results

{"_max_results" => 70}

Example: return only 100 results, throw an error if there are more

{"_max_results" => -100}


76
77
78
79
80
81
# File 'lib/infoblox/resource.rb', line 76

def self.all(connection, params = {})
  params = default_params.merge(params)
  JSON.parse(connection.get(resource_uri, params).body).map do |item|
    new(item.merge({:connection => connection}))
  end
end

.default_paramsObject



62
63
64
# File 'lib/infoblox/resource.rb', line 62

def self.default_params
  {:_return_fields => self._return_fields}
end

.find(connection, params) ⇒ Object

Find resources with query parameters. You can use the default parameters _max_results an/or _return_fields as documented by Infoblox.

Example: return extensible attributes for every resource.

{"_return_fields" => "extensible_attributes"}

Example: filter resources by name, return 692 results or less

{"name~" => "foo.*bar", "_max_results" => 692}


93
94
95
96
97
98
# File 'lib/infoblox/resource.rb', line 93

def self.find(connection, params)
  params = default_params.merge(params)
  JSON.parse(connection.get(resource_uri, params).body).map do |item|
    new(item.merge({:connection => connection}))
  end
end

.remote_attr_accessor(*args) ⇒ Object

Define a writeable remote attribute, i.e. one that should show up in post / put operations.



18
19
20
21
22
23
# File 'lib/infoblox/resource.rb', line 18

def self.remote_attr_accessor(*args)
  args.each do |a|
    attr_accessor a
    remote_attrs << a
  end
end

.remote_attr_writer(*args) ⇒ Object

Define a remote attribute that is write-only



39
40
41
42
43
44
# File 'lib/infoblox/resource.rb', line 39

def self.remote_attr_writer(*args)
  args.each do |a|
    attr_accessor a
    remote_write_only_attrs << a
  end
end

.remote_attrsObject



46
47
48
# File 'lib/infoblox/resource.rb', line 46

def self.remote_attrs
  @remote_attrs ||= []
end

.remote_post_accessor(*args) ⇒ Object

Define a remote attribute that can only be sent during a POST operation.



29
30
31
32
33
34
# File 'lib/infoblox/resource.rb', line 29

def self.remote_post_accessor(*args)
  args.each do |a|
    attr_accessor a
    remote_post_attrs << a
  end
end

.remote_post_attrsObject



54
55
56
# File 'lib/infoblox/resource.rb', line 54

def self.remote_post_attrs
  @remote_post_attrs ||= []
end

.remote_write_only_attrsObject



50
51
52
# File 'lib/infoblox/resource.rb', line 50

def self.remote_write_only_attrs
  @remote_write_only_attrs ||= []
end

.resource_mapObject

A hash that maps Infoblox WAPI object identifiers to subclasses of Resource. Used by the Search resource for mapping response objects.



108
109
110
# File 'lib/infoblox/resource.rb', line 108

def self.resource_map
  @@resource_map ||= {}
end

.resource_uriObject



100
101
102
# File 'lib/infoblox/resource.rb', line 100

def self.resource_uri
  BASE_PATH + self.wapi_object
end

.wapi_object(obj = nil) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/infoblox/resource.rb', line 5

def self.wapi_object(obj=nil)
  if obj.nil? 
    @wapi_object
  else
    self.resource_map[obj] = self
    @wapi_object = obj
  end
end

Instance Method Details

#deleteObject



124
125
126
# File 'lib/infoblox/resource.rb', line 124

def delete
  connection.delete(resource_uri).status == 200
end

#get(params = {}) ⇒ Object



128
129
130
# File 'lib/infoblox/resource.rb', line 128

def get(params={})
  connection.get(resource_uri, params)
end

#postObject Also known as: create



118
119
120
121
# File 'lib/infoblox/resource.rb', line 118

def post
  self._ref = unquote(connection.post(resource_uri, remote_attribute_hash(write = true, post = true)).body)
  true
end

#putObject



132
133
134
135
# File 'lib/infoblox/resource.rb', line 132

def put
  self._ref = unquote(connection.put(resource_uri, remote_attribute_hash(write = true)).body)
  true
end

#remote_attribute_hash(write = false, post = false) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/infoblox/resource.rb', line 141

def remote_attribute_hash(write=false, post=false)
  {}.tap do |hsh|
    self.class.remote_attrs.each do |k|
      hsh[k] = self.send(k) unless self.send(k).nil?
    end
    self.class.remote_write_only_attrs.each do |k|
      hsh[k] = self.send(k) unless self.send(k).nil?
    end if write
    self.class.remote_post_attrs.each do |k|
      hsh[k] = self.send(k) unless self.send(k).nil?
    end if post
  end
end

#resource_uriObject



137
138
139
# File 'lib/infoblox/resource.rb', line 137

def resource_uri
  self._ref.nil? ? self.class.resource_uri : (BASE_PATH + self._ref)
end