Module: Chef::Knife::SceBase

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object

:nodoc: Would prefer to do this in a rational way, but can’t be done b/c of Mixlib::CLI’s design :(



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chef/knife/sce_base.rb', line 33

def self.included(includer)
  includer.class_eval do

    deps do
      require 'fog'
      require 'readline'
      require 'chef/json_compat'
    end

    option :ibm_username,
      :short => "-A USERNAME",
      :long => "--ibm-username USERNAME",
      :description => "Your IBM SCE username",
      :proc => Proc.new { |key| Chef::Config[:knife][:ibm_username] = key }

    option :ibm_password,
      :short => "-K PASSWORD",
      :long => "--ibm-password PASSWORD",
      :description => "Your SCE password",
      :proc => Proc.new { |key| Chef::Config[:knife][:ibm_password] = key }
  end
end

Instance Method Details

#connectionObject



56
57
58
59
60
61
62
63
64
# File 'lib/chef/knife/sce_base.rb', line 56

def connection
  @connection ||= begin
    connection = Fog::Compute.new(
      :provider => 'IBM',
      :ibm_username => Chef::Config[:knife][:ibm_username],
      :ibm_password => Chef::Config[:knife][:ibm_password]
    )
  end
end

#connection_storageObject



66
67
68
69
70
71
72
73
74
# File 'lib/chef/knife/sce_base.rb', line 66

def connection_storage
  @connection_storage ||= begin
    connection_storage = Fog::Storage.new(
      :provider => 'IBM',
      :ibm_username => Chef::Config[:knife][:ibm_username],
      :ibm_password => Chef::Config[:knife][:ibm_password]
    )
  end
end

#datacenter_idObject



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
# File 'lib/chef/knife/sce_base.rb', line 81

def datacenter_id
  id = nil
  if locate_config_value(:datacenter).to_s.is_number?
    location = connection.locations.get( locate_config_value(:datacenter) )
    if !location.nil?
      id = location.id.to_s
    else
      ui.error("Location ID #{locate_config_value(:datacenter)} is invalid.  Use knife sce location list to learn what IDs or textual locations are available.")
      exit 1
    end
  else
    connection.locations.all.each do |location|
      if location.name.to_s.split(",").first.downcase.eql?( locate_config_value(:datacenter).downcase )
        id = location.id.to_s
      end
    end
  end
  
  if id.nil?
    ui.error("Location #{locate_config_value(:datacenter)} is invalid.  Use knife sce location list to learn what IDs or textual locations are available.")
    exit 1
  end
  
  id
end

#locate_config_value(key) ⇒ Object



76
77
78
79
# File 'lib/chef/knife/sce_base.rb', line 76

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

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



107
108
109
110
111
# File 'lib/chef/knife/sce_base.rb', line 107

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

#validate!(keys = [:ibm_username, :ibm_password]) ⇒ Object



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

def validate!(keys=[:ibm_username, :ibm_password])
  errors = []
  keys.each do |k|
    pretty_key = k.to_s.gsub(/_/, ' ').gsub(/\w+/){ |w| (w =~ /(ssh)|(aws)/i) ? w.upcase  : w.capitalize }
    if Chef::Config[:knife][k].nil?
      errors << "You did not provide a valid '#{pretty_key}' value."
    end
  end
  if errors.each{|e| ui.error(e)}.any?
    exit 1
  end
end