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.



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

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
61
# File 'lib/infoblox/resource.rb', line 58

def self._return_fields
  remove = Infoblox.wapi_version < '1.2' ? :extattrs : :extensible_attributes
  (self.remote_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}


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

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



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

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}


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

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.



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

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

.resource_uriObject



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

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



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

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

#get(params = {}) ⇒ Object



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

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

#postObject Also known as: create



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

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

#putObject



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

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



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

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



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

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