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.



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

def initialize(attrs={})
  load_attributes(attrs)
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



72
73
74
75
# File 'lib/infoblox/resource.rb', line 72

def self._return_fields
  remove = Infoblox.wapi_version < '1.2' ? :extattrs : :extensible_attributes
  ((self.remote_attrs + self.remote_read_only_attrs) - [remove]).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}


91
92
93
94
95
96
# File 'lib/infoblox/resource.rb', line 91

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



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

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}


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

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_reader(*args) ⇒ Object

Define a read-only attribute



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

def self.remote_attr_reader(*args)
  args.each do |a|
    attr_reader a
    remote_read_only_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



56
57
58
# File 'lib/infoblox/resource.rb', line 56

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



68
69
70
# File 'lib/infoblox/resource.rb', line 68

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

.remote_read_only_attrsObject



64
65
66
# File 'lib/infoblox/resource.rb', line 64

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

.remote_write_only_attrsObject



60
61
62
# File 'lib/infoblox/resource.rb', line 60

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.



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

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

.resource_uriObject



115
116
117
# File 'lib/infoblox/resource.rb', line 115

def self.resource_uri
  Infoblox.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



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

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

#get(params = self.class.default_params) ⇒ Object



141
142
143
144
145
# File 'lib/infoblox/resource.rb', line 141

def get(params=self.class.default_params)
  response = connection.get(resource_uri, params).body
  load_attributes(JSON.parse(response))
  self
end

#postObject Also known as: create



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

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

#putObject



147
148
149
150
# File 'lib/infoblox/resource.rb', line 147

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



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/infoblox/resource.rb', line 156

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



152
153
154
# File 'lib/infoblox/resource.rb', line 152

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