Module: Chef::Knife::Cloud::VcairHelpers

Included in:
VcairImageList, VcairIpList, VcairNetworkList, VcairServerCreate, VcairServerShow, VcairVmDelete, VcairVmList
Defined in:
lib/chef/knife/vcair_helpers.rb

Instance Method Summary collapse

Instance Method Details

#available_networksObject



53
54
55
# File 'lib/chef/knife/vcair_helpers.rb', line 53

def available_networks
  org.networks.map { |network| "#{network.name} (#{network.id})"}
end

#config_value(key) ⇒ Object



96
97
98
99
# File 'lib/chef/knife/vcair_helpers.rb', line 96

def config_value(key)
  key = key.to_sym
  Chef::Config[:knife][key] || config[key]
end

#create_service_instanceObject



13
14
15
# File 'lib/chef/knife/vcair_helpers.rb', line 13

def create_service_instance
  VcairService.new
end

#get_id(value) ⇒ Object



101
102
103
# File 'lib/chef/knife/vcair_helpers.rb', line 101

def get_id(value)
  value['id']
end

#msg_pair(label, value, color = :cyan) ⇒ Object



105
106
107
108
109
# File 'lib/chef/knife/vcair_helpers.rb', line 105

def msg_pair(label, value, color=:cyan)
  if value && !value.to_s.empty?
    puts "#{ui.color(label, color)}: #{value}"
  end
end

#netObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/chef/knife/vcair_helpers.rb', line 30

def net
  if config_value(:vcair_net_id)
    Chef::Log.debug("Looking up network by ID: #{config_value(:vcair_net_id)}")
    begin
      @net ||= org.networks.get(config_value(:vcair_net_id))
    rescue => e
      raise "Unable to locate network ID #{config_value(:vcair_net_id)} -- #{e.message}"
    end
  elsif config_value(:vcair_net)
    Chef::Log.debug("Looking up network by name: #{config_value(:vcair_net)}")
    @net ||= org.networks.get_by_name(config_value(:vcair_net))
  else
    # Grab first non-isolated (bridged, natRouted) network
    Chef::Log.debug("No network specified, trying to locate one...")
    @net ||= org.networks.find { |n| n if !n.fence_mode.match("isolated") }
  end

  raise "No network found - available networks: #{available_networks.join(', ')}" if @net.nil?

  Chef::Log.debug("Using network #{@net.name} (#{@net.id})")
  @net
end

#network_configObject



89
90
91
92
93
# File 'lib/chef/knife/vcair_helpers.rb', line 89

def network_config
  @network_config ||= vapp.network_config.find do |n|
    n if n[:networkName].match(net.name)
  end
end

#orgObject



17
18
19
20
# File 'lib/chef/knife/vcair_helpers.rb', line 17

def org
  @org ||= @service.connection.organizations.get_by_name(
             config_value(:vcair_org))
end

#templateObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/chef/knife/vcair_helpers.rb', line 57

def template
  return @template if @template

  # TODO: find by catalog item ID and/or NAME
  # TODO: add option to search just public and/or private catalogs
  Chef::Log.debug("Searching catalogs for image #{config_value(:image)}...")
  org.catalogs.each do |catalog|
    Chef::Log.debug("Searching catalog #{catalog.name}...")

    images = catalog.catalog_items
    @template = images.find { |image| image.name == config_value(:image) }

    if @template.nil?
      Chef::Log.debug("Image not found in catalog #{catalog.name} - possible images: #{images.map(&:name).join(', ')}")
    else
      Chef::Log.debug("Image #{@template.name} (#{template.id}) found in catalog #{catalog.name} - search complete.")
      break
    end
  end

  raise "Unable to locate image #{config_value(:image)} in any catalog" if @template.nil?
  @template
end

#validate!(keys = [:vcair_username, :vcair_password, :vcair_api_host, :vcair_org, :vcair_api_version]) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/chef/knife/vcair_helpers.rb', line 111

def validate!(keys=[:vcair_username, :vcair_password, :vcair_api_host, :vcair_org, :vcair_api_version])
  errors = []
  keys.each do |k|
    pretty_key = k.to_s.gsub(/_/, ' ').gsub(/\w+/){ |w| (w =~ /(ssh)/i) ? w.upcase  : w.capitalize }
    if config_value(k).nil?
      errors << "You did not provide a valid '#{pretty_key}' value. Please set knife[:#{k}] in your knife.rb or pass as an option."
    end
  end

  if errors.each{|e| ui.error(e)}.any?
    exit 1
  end
end

#vappObject



81
82
83
# File 'lib/chef/knife/vcair_helpers.rb', line 81

def vapp
  @vapp ||= vdc.vapps.get_by_name(config_value(:chef_node_name))
end

#vdcObject



22
23
24
25
26
27
28
# File 'lib/chef/knife/vcair_helpers.rb', line 22

def vdc
  if config_value(:vcair_vdc)
    @vdc ||= org.vdcs.get_by_name(config_value(:vcair_vdc))
  else
    @vdc ||= org.vdcs.first
  end
end

#vmObject



85
86
87
# File 'lib/chef/knife/vcair_helpers.rb', line 85

def vm
  @vm ||= vapp.vms.find {|v| v.vapp_name == config_value(:chef_node_name)}
end