This repository has been archived by the owner on Aug 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathdefault.rb
227 lines (199 loc) · 6.97 KB
/
default.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# Copyright (C) 2013 VMware, Inc.
require 'set'
require 'pathname' # WORK_AROUND #14073 and #7788
module_lib = Pathname.new(__FILE__).parent.parent.parent.parent
require File.join module_lib, 'puppet/provider/vcenter'
require File.join module_lib, 'puppet_x/vmware/mapper'
require File.join module_lib, 'puppet_x/vmware/vmware_lib/puppet_x/vmware/util'
require File.join module_lib, 'puppet_x/vmware/vmware_lib/puppet/property/vmware'
Puppet::Type.type(:vc_dvswitch).provide(:vc_dvswitch, :parent => Puppet::Provider::Vcenter) do
@doc = "Manages vCenter Distributed Virtual Switch"
def create
@creating = true
@create_message ||= []
# build the spec for CreateDVS_Task
create_spec = RbVmomi::VIM::DVSCreateSpec.new
create_spec.configSpec = RbVmomi::VIM::DVSConfigSpec.new
create_spec.configSpec.name = basename
# find the network folder and invoke the task
dc = vim.serviceInstance.find_datacenter(parent)
task_create_dvs = dc.networkFolder.CreateDVS_Task(:spec => create_spec)
task_create_dvs.wait_for_completion
# now rename the default uplink portgroup so it's easy to find
if task_create_dvs.info.state == 'success'
@dvswitch = task_create_dvs.info.result
@dvswitch.config.uplinkPortgroup.first.
Rename_Task(:newName => "#{basename}-uplink-pg").wait_for_completion
@create_message << "uplink portgroup renamed to \"#{basename}-uplink-pg\""
end
@flush_required = false
end
def create_message
@create_message ||= []
"created (#{@create_message.join "; "})"
end
def destroy
@dvswitch = nil
dc = vim.serviceInstance.find_datacenter(parent)
dvswitches = dc.networkFolder.children.select {|n| n.class == RbVmomi::VIM::VmwareDistributedVirtualSwitch}
dvswitches.find{|d| d.name == basename}.Destroy_Task.wait_for_completion
end
def exists?
dvswitch
end
map ||= PuppetX::VMware::Mapper.new_map('VMwareDVSConfigSpecMap')
map.leaf_list.each do |leaf|
define_method(leaf.prop_name) do
value = PuppetX::VMware::Mapper::munge_to_tfsyms.call(
PuppetX::VMware::Util::nested_value(config_is_now, leaf.path_is_now)
)
end
define_method("#{leaf.prop_name}=".to_sym) do |value|
PuppetX::VMware::Util::nested_value_set(config_should, leaf.path_should, value)
properties_rcvd.add leaf.prop_name
properties_reqd.merge leaf.requires
@flush_required = true
end
end
def flush_prep
# dvswitch requires matching configVersion
unless @creating
config_should[:configVersion] = config_is_now[:configVersion]
end
# To change some properties, the API requires others that may not have
# changed. If not, they must be fetched from the type. When additional
# properties are fetched, new items may be added to required_properties,
# so iteration is required.
Puppet.debug "requiring: #{@properties_rcvd.inspect} were received"
properties_rcvd_old = Set.new
while properties_rcvd != properties_rcvd_old
properties_rcvd_old = properties_rcvd.dup
properties_reqd.subtract properties_rcvd
unless properties_reqd.empty?
Puppet.debug "requiring: #{@properties_reqd.inspect} are required"
# properties_reqd may change
# properties_rcvd will change unless resource has no value for property
properties_reqd.dup.each{|p|
self.send "#{p}=".to_sym, @resource[p] unless @resource[p].nil?
}
end
end
properties_reqd.subtract properties_rcvd
unless @properties_reqd.empty?
fail "required properties missing - #{@properties_reqd.inspect}"
end
# create RbVmomi objects with properties in place of hashes with keys
Puppet.debug "'is_now' is #{config_is_now.inspect}'}"
Puppet.debug "'should' is #{config_should.inspect}'}"
spec = map.objectify config_should
Puppet.debug "'object' is #{spec.inspect}'}"
spec
end
def flush
return unless @flush_required
spec = flush_prep
begin
dvswitch.ReconfigureDvs_Task(:spec => spec).wait_for_completion
rescue Exception => e
fail "#{e.inspect}"
end
end
# not private: used by insyncInheritablePolicy
define_method(:map) do
@map ||= map
end
def mo_ref_by_name opts
name = opts[:name]
type = opts[:type]
scope = opts[:scope] || :datacenter
myfile = File.expand_path(__FILE__) + ":mo_ref_by_name"
if type == VIM::HostSystem
case scope
when :datacenter
list = vim.serviceInstance.content.
dvSwitchManager.QueryCompatibleHostForNewDvs(
:container => datacenter,
:recursive => true
)
raise 'No ESX hosts compatible for DVS found' if list.size == 0
else
fail "#{myfile}: scope \"#{scope}\" unimplemented for #{type}"
end
elsif type == VIM::DistributedVirtualSwitch
case scope
when :dvswitch
list = [dvswitch]
when :datacenter
list = datacenter.networkFolder.children.
select{|child| child.class == type}
else
fail "#{myfile}: scope \"#{scope}\" unimplemented for #{type}"
end
elsif type == VIM::DistributedVirtualPortgroup
case scope
when :dvswitch
list = dvswitch.portgroup
when :datacenter
list = datacenter.networkFolder.children.
select{|child| child.class == type}
else
fail "#{myfile}: scope \"#{scope}\" unimplemented for #{type}"
end
elsif type == VIM::DistributedVirtualPort
fail "#{myfile}: unimplemented for #{type}"
else
fail "#{myfile}: unimplemented for #{type}"
end
if list
obj = list.find{|o| o.name == name}
obj._ref unless obj.nil?
else
nil
end
end
def fixup_is type, is, should
# see comments for class VMware_Array_VIM_Object
# in vmware_lib/lib/puppet/property/vmware.rb
if type == VIM::DistributedVirtualSwitchHostMemberConfigSpec
if config_is_now
config_is_now.host.map{|element| element.config}
else
nil
end
else
Puppet.notice "fixup_is: unexpected type #{type.inspect}" unless
[
VIM::NumericRange,
].include? type
is
end
end
private
def properties_rcvd
@properties_rcvd ||= Set.new
end
def properties_reqd
@properties_reqd ||= Set.new
end
def config_is_now
@config_is_now ||=
(@creating ? {} : map.annotate_is_now(dvswitch.config))
end
def config_should
@config_should ||= {}
end
def datacenter
@datacenter||= vim.serviceInstance.find_datacenter(parent)
end
def dvswitch
@dvswitch ||= begin
dc = vim.serviceInstance.find_datacenter(parent)
dvswitches = dc.networkFolder.children.select {|n|
n.class == RbVmomi::VIM::VmwareDistributedVirtualSwitch
}
dvswitches.find{|d| d.name == basename}
end
Puppet.debug "found dvswitch: #{@dvswitch.class} '#{@dvswitch.name}'" if @dvswitch
@dvswitch
end
end