Class: Puppet::OneviewResource
- Inherits:
-
Provider
- Object
- Provider
- Puppet::OneviewResource
- Defined in:
- lib/puppet/provider/oneview_resource.rb
Overview
Base provider for OneView resources
Direct Known Subclasses
Class Method Summary collapse
- .api_version ⇒ Object
- .client ⇒ Object
- .instances ⇒ Object
- .ov_resource_type ⇒ Object
-
.resource_name ⇒ Object
Helpers.
- .resource_variant ⇒ Object
Instance Method Summary collapse
- #api_version ⇒ Object
- #client ⇒ Object
- #create(action = :create) ⇒ Object
-
#data ⇒ Object
TODO: Would be awesome to have this working for everything/most types.
-
#data_parse ⇒ Object
This method should be overwritten by resources which require the @data to be modified.
- #destroy(action = :delete) ⇒ Object
- #exists?(states = [nil, :found]) ⇒ Boolean
- #found ⇒ Object
-
#initialize(value = {}) ⇒ OneviewResource
constructor
A new instance of OneviewResource.
- #ov_resource_type ⇒ Object
- #resource_name ⇒ Object
- #resource_variant ⇒ Object
Constructor Details
#initialize(value = {}) ⇒ OneviewResource
Returns a new instance of OneviewResource.
25 26 27 28 29 30 31 |
# File 'lib/puppet/provider/oneview_resource.rb', line 25 def initialize(value = {}) super(value) @property_flush ||= {} @data ||= {} @client ||= client @resource_type ||= ov_resource_type end |
Class Method Details
.api_version ⇒ Object
129 130 131 |
# File 'lib/puppet/provider/oneview_resource.rb', line 129 def self.api_version login[:api_version] || 200 end |
.client ⇒ Object
33 34 35 |
# File 'lib/puppet/provider/oneview_resource.rb', line 33 def self.client OneviewSDK::Client.new(login) end |
.instances ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/puppet/provider/oneview_resource.rb', line 41 def self.instances resources = [] ov_resource_type.get_all(client).each { |n| resources.push(n) } resources.collect do |res| resource = {} resource[:data] = Hash[res.data.map { |k, v| [k.to_sym, v] }] resource[:ensure] = resource[:data][:state] == 'Active' ? :present : :absent resource[:name] = resource[:data][:name] new(resource) end end |
.ov_resource_type ⇒ Object
141 142 143 |
# File 'lib/puppet/provider/oneview_resource.rb', line 141 def self.ov_resource_type OneviewSDK.resource_named(resource_name, api_version, resource_variant) end |
.resource_name ⇒ Object
Helpers
114 115 116 117 118 119 |
# File 'lib/puppet/provider/oneview_resource.rb', line 114 def self.resource_name class_name = to_s class_name =~ /Oneview/ shift_prefix = Regexp.last_match.nil? ? 2 : 1 class_name.split('::')[2].split('_').drop(shift_prefix).collect(&:capitalize).join end |
.resource_variant ⇒ Object
125 126 127 |
# File 'lib/puppet/provider/oneview_resource.rb', line 125 def self.resource_variant to_s.split('::')[3].gsub(/Provider/, '') end |
Instance Method Details
#api_version ⇒ Object
133 134 135 |
# File 'lib/puppet/provider/oneview_resource.rb', line 133 def api_version self.class.api_version end |
#client ⇒ Object
37 38 39 |
# File 'lib/puppet/provider/oneview_resource.rb', line 37 def client self.class.client end |
#create(action = :create) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/puppet/provider/oneview_resource.rb', line 77 def create(action = :create) return true if resource_update Puppet.info "Performing #{action} of #{resource_name} #{@item['name']} using the following data: \n#{JSON.pretty_generate(@data)}." ov_resource = if action == :create @resource_type.new(@client, @data).create elsif action == :add @resource_type.new(@client, @data).add elsif action == :update raise 'This resource relies on others to be created.' end Puppet.debug "#{@resource_type} #{@item['name']} created successfully." @property_hash[:data] = ov_resource.data @property_hash[:ensure] = :present end |
#data ⇒ Object
TODO: Would be awesome to have this working for everything/most types. Future improvement. def self.prefetch(resources)
instances.each do |instance|
puts "instance name: #{instance.name}"
puts "resources instance.name: #{resources[instance.name]}"
puts "resources count: #{resources.count}"
resources[instance.name].provider = instance if resources[instance.name]
end
end
63 64 65 |
# File 'lib/puppet/provider/oneview_resource.rb', line 63 def data @property_hash[:data] end |
#data_parse ⇒ Object
This method should be overwritten by resources which require the @data to be modified.
107 |
# File 'lib/puppet/provider/oneview_resource.rb', line 107 def data_parse; end |
#destroy(action = :delete) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/puppet/provider/oneview_resource.rb', line 92 def destroy(action = :delete) Puppet.debug "Performing removal of #{resource_name} matching the following data: #{JSON.pretty_generate(@data)}." if action == :delete get_single_resource_instance.delete elsif action == :remove get_single_resource_instance.remove elsif action == :multiple_delete @resource_type.find_by(@client, @data).map(&:delete) elsif action == :multiple_remove @resource_type.find_by(@client, @data).map(&:remove) end @property_hash[:ensure] = :absent end |
#exists?(states = [nil, :found]) ⇒ Boolean
67 68 69 70 71 72 73 74 75 |
# File 'lib/puppet/provider/oneview_resource.rb', line 67 def exists?(states = [nil, :found]) prepare_environment @item = @resource_type.new(@client, @data) return true if empty_data_check(states) return false unless @item.retrieve! && @item.like?(@data) && !@data['new_name'] Puppet.debug "#{@resource_type} #{@item['name']} is up to date." true # @property_hash[:ensure] == :present # TODO: Future Improvement: Look into using property_hash for verifying existance globally end |
#found ⇒ Object
109 110 111 |
# File 'lib/puppet/provider/oneview_resource.rb', line 109 def found find_resources end |
#ov_resource_type ⇒ Object
145 146 147 |
# File 'lib/puppet/provider/oneview_resource.rb', line 145 def ov_resource_type self.class.ov_resource_type end |
#resource_name ⇒ Object
121 122 123 |
# File 'lib/puppet/provider/oneview_resource.rb', line 121 def resource_name self.class.resource_name end |
#resource_variant ⇒ Object
137 138 139 |
# File 'lib/puppet/provider/oneview_resource.rb', line 137 def resource_variant self.class.resource_variant end |