Class: Puppet::Provider::Nsx

Inherits:
Puppet::Provider
  • Object
show all
Defined in:
lib/puppet/provider/nsx.rb

Overview

TODO: Depending on number of shared methods, we might make Puppet::Provider::Vcenter parent:

Instance Method Summary collapse

Instance Method Details

#all_portgroupsObject



217
218
219
# File 'lib/puppet/provider/nsx.rb', line 217

def all_portgroups
  datacenter.network
end

#all_vmsObject



195
196
197
198
199
200
201
# File 'lib/puppet/provider/nsx.rb', line 195

def all_vms
  @all_vms ||=
    begin
      vms = datacenter.vmFolder.inventory_flat('VirtualMachine' => [ 'name' ])
      vms.select{|obj, props| obj.is_a?(VIM::VirtualMachine)}
    end
end

#avail_scopesObject



162
163
164
# File 'lib/puppet/provider/nsx.rb', line 162

def avail_scopes
  @avail_scopes ||= get('api/2.0/services/usermgmt/scopingobjects')['scopingObjects']['object']
end

#connectionObject

Raises:

  • (Puppet::Error)


76
77
78
79
80
81
82
# File 'lib/puppet/provider/nsx.rb', line 76

def connection
  server = vc_info['ipAddress']
  raise Puppet::Error, "vSphere API connection failure: NSX #{resource[:transport]} not connected to vCenter." unless server
  connection = resource.catalog.resources.find{|x| x.class == Puppet::Type::Transport && x[:server] == server}
  raise Puppet::Error, "vSphere API connection failure: Linked vCenter in NSX Manager does not match hostname/ipaddress specification: #{server}" unless connection
  connection.to_hash
end

#datacenter(name = ) ⇒ Object



122
123
124
125
# File 'lib/puppet/provider/nsx.rb', line 122

def datacenter(name=resource[:datacenter_name])
  dc = vim.serviceInstance.find_datacenter(name) or raise Puppet::Error, "datacenter '#{name}' not found."
  dc
end

#datacenter_moref(name = ) ⇒ Object



127
128
129
130
# File 'lib/puppet/provider/nsx.rb', line 127

def datacenter_moref(name=resource[:datacenter_name])
  dc = datacenter
  dc._ref
end

#dvswitch(name = ) ⇒ Object



151
152
153
154
155
156
157
158
159
160
# File 'lib/puppet/provider/nsx.rb', line 151

def dvswitch(name=resource[:switch]['name'])
  @dvswitch ||= begin
    dvswitches = datacenter.networkFolder.children.select {|n|
      n.class == RbVmomi::VIM::VmwareDistributedVirtualSwitch
    }
    dv = dvswitches.find{|d| d.name == name}
    raise Puppet::Error, "dvswitch: #{name} was not found" if dv.nil?
    dv
  end
end

#edge_detailObject

Raises:

  • (Puppet::Error)


117
118
119
120
# File 'lib/puppet/provider/nsx.rb', line 117

def edge_detail
  raise Puppet::Error, "edge not available" unless @instance
  @edge_detail ||= nested_value(get("api/3.0/edges/#{@instance['id']}"), ['edge'])
end

#edge_summaryObject



112
113
114
115
# File 'lib/puppet/provider/nsx.rb', line 112

def edge_summary
  # TODO: This may exceed 256 pagesize limit.
  @edge_summary ||= ensure_array( nested_value( get('api/3.0/edges'), ['pagedEdgeList', 'edgePage', 'edgeSummary'] ) )
end

#ensure_array(value) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/puppet/provider/nsx.rb', line 96

def ensure_array(value)
  # Ensure results an array. If there's a single value the result is a hash, while multiple results in an array.
  case value
  when nil
    []
  when Array
    value
  when Hash
    [value]
  when Nori::StringWithAttributes
    [value]
  else
    raise Puppet::Error, "Unknown type for munging #{value.class}: '#{value}'"
  end
end

#nested_value(*args, &block) ⇒ Object



88
89
90
# File 'lib/puppet/provider/nsx.rb', line 88

def nested_value *args, &block
  PuppetX::VMware::Util.nested_value *args, &block
end

#nested_value_set(*args) ⇒ Object



92
93
94
# File 'lib/puppet/provider/nsx.rb', line 92

def nested_value_set *args
  PuppetX::VMware::Util::nested_value_set *args
end

#network_manager_versionObject

detect if nsx or vsm



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/puppet/provider/nsx.rb', line 183

def network_manager_version
  @network_manager_version ||=
    begin
      url_path = 'api/1.0/appliance-management/global/info'
      version_info = nested_value(get(url_path),['globalInfo','versionInfo']).select{|x| x =~ /Version/}.values.join('.')
    rescue
      # if this get works, assume 5.x is good
      get('api/2.0/global/config')
      version_info = '5.x'
    end
end

#nsx_edge_moref(name = ) ⇒ Object

Raises:

  • (Puppet::Error)


175
176
177
178
179
180
# File 'lib/puppet/provider/nsx.rb', line 175

def nsx_edge_moref(name=resource[:scope_name])
  edges = edge_summary || []
  instance = edges.find{|x| x['name'] == name}
  raise Puppet::Error, "NSX Edge #{name} does not exist." unless instance
  instance['id']
end

#nsx_scope_moref(type = , name = ) ⇒ Object

Raises:

  • (Puppet::Error)


166
167
168
169
170
171
172
173
# File 'lib/puppet/provider/nsx.rb', line 166

def nsx_scope_moref(type=resource[:scope_type], name=resource[:scope_name])
  type_name    = PuppetX::VMware::Util.camelize(type.to_s, :upper)
  # one off since first letter in global is upper case
  name         = 'Global' if type_name == 'GlobalRoot'
  instance     = avail_scopes.find{|x| x['objectTypeName'] == type_name and x['name'] == name}
  raise Puppet::Error, "scope: #{name} or type: #{type.to_s} not found" unless instance
  instance['objectId']
end

#portgroup_id_from_name(name, type) ⇒ Object

Raises:

  • (Puppet::Error)


228
229
230
231
232
233
# File 'lib/puppet/provider/nsx.rb', line 228

def portgroup_id_from_name(name,type)
  pg  = all_portgroups.find{|x| x.name == name and x.class.to_s == type} 
  msg = "No portgroup with the name: #{name} of the type: #{type} was found\n"
  raise Puppet::Error, msg if pg.nil?
  pg._ref
end

#portgroup_moref(portgroup, islogical = false) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/puppet/provider/nsx.rb', line 139

def portgroup_moref(portgroup, islogical=false)
  if islogical
    result = vxlan_wire_config(portgroup)
    raise(Puppet::Error, "Fatal Error: Logical switch portgroup: '#{portgroup}' was not found") if result.nil?
    result['objectId']
  else
    result = datacenter.network.find{|pg| pg.name == portgroup }
    raise(Puppet::Error, "Fatal Error: Portgroup: '#{portgroup}' was not found") if result.nil?
    result._ref
  end
end

#portgroup_name_from_id(id, type) ⇒ Object

Raises:

  • (Puppet::Error)


221
222
223
224
225
226
# File 'lib/puppet/provider/nsx.rb', line 221

def portgroup_name_from_id(id,type)
  pg  = all_portgroups.find{|x| x._ref == id and x.class.to_s == type} 
  msg = "No portgroup with the id: #{id} with the type: #{type} was found\n"
  raise Puppet::Error, msg if pg.nil?
  pg.name
end

#restObject



40
41
42
43
# File 'lib/puppet/provider/nsx.rb', line 40

def rest
  @transport ||= PuppetX::Puppetlabs::Transport.retrieve(:resource_ref => resource[:transport], :catalog => resource.catalog, :provider => 'nsx')
  @transport.rest
end

#vc_infoObject



84
85
86
# File 'lib/puppet/provider/nsx.rb', line 84

def vc_info
  @vc_info ||= get('api/2.0/global/config')['vsmGlobalConfig']['vcInfo']
end

#vimObject

We need the corresponding vCenter connection once NSX is connected



71
72
73
74
# File 'lib/puppet/provider/nsx.rb', line 71

def vim
  @vsphere_transport ||= PuppetX::Puppetlabs::Transport.retrieve(:resource_hash => connection, :provider => 'vsphere')
  @vsphere_transport.vim
end

#vm_id_from_name(name) ⇒ Object

Raises:

  • (Puppet::Error)


210
211
212
213
214
215
# File 'lib/puppet/provider/nsx.rb', line 210

def vm_id_from_name(name)
  vm = all_vms.find{|x| x[0].name == name} 
  msg = "No vm with the name of: #{name} was found\n"
  raise Puppet::Error, msg if vm.nil?
  vm[0]._ref
end

#vm_name_from_id(id) ⇒ Object

Raises:

  • (Puppet::Error)


203
204
205
206
207
208
# File 'lib/puppet/provider/nsx.rb', line 203

def vm_name_from_id(id)
  vm = all_vms.find{|x| x[0]._ref == id} 
  msg = "No vm with the id of: #{id} was found\n"
  raise Puppet::Error, msg if vm.nil?
  vm[0].name
end

#vxlan_wire_config(vxlan_wire_name) ⇒ Object



132
133
134
135
136
137
# File 'lib/puppet/provider/nsx.rb', line 132

def vxlan_wire_config(vxlan_wire_name)
  wire_url = '/api/2.0/vdn/virtualwires'
  results = ensure_array( nested_value(get(wire_url), %w{virtualWires dataPage virtualWire}))
  wire_config = results.find{|virtualWire| virtualWire['name'] == vxlan_wire_name}
  wire_config
end