Module: Chef::Knife::AzureBase

Included in:
AzureImageList, AzureServerCreate, AzureServerDelete, AzureServerDescribe, AzureServerList
Defined in:
lib/chef/knife/azure_base.rb

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 :(



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

def self.included(includer)
  includer.class_eval do

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

    option :azure_subscription_id,
      :short => "-S ID",
      :long => "--azure-subscription-id ID",
      :description => "Your Azure subscription ID",
      :proc => Proc.new { |key| Chef::Config[:knife][:azure_subscription_id] = key }

    option :azure_mgmt_cert,
      :short => "-p FILENAME",
      :long => "--azure-mgmt-cert FILENAME",
      :description => "Your Azure PEM file name",
      :proc => Proc.new { |key| Chef::Config[:knife][:azure_mgmt_cert] = key }

    option :azure_host_name,
      :short => "-H HOSTNAME",
      :long => "--azure-host-name HOSTNAME",
      :description => "Your Azure host name",
      :proc => Proc.new { |key| Chef::Config[:knife][:azure_host_name] = key },
      :default => "management.core.windows.net"

    option :verify_ssl_cert,
      :long => "--verify-ssl-cert",
      :description => "Verify SSL Certificates for communication over HTTPS",
      :boolean => true,
      :default => false
  end
end

Instance Method Details

#connectionObject



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

def connection
  @connection ||= begin
                    connection = Azure::Connection.new(
                      :azure_subscription_id => locate_config_value(:azure_subscription_id),
                      :azure_mgmt_cert => locate_config_value(:azure_mgmt_cert),
                      :azure_host_name => locate_config_value(:azure_host_name),
                      :verify_ssl_cert => locate_config_value(:verify_ssl_cert)
                    )
                  end
end

#locate_config_value(key) ⇒ Object



76
77
78
79
# File 'lib/chef/knife/azure_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



81
82
83
84
85
# File 'lib/chef/knife/azure_base.rb', line 81

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

#validate!(keys = [:azure_subscription_id, :azure_mgmt_cert, :azure_host_name]) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/chef/knife/azure_base.rb', line 87

def validate!(keys=[:azure_subscription_id, :azure_mgmt_cert, :azure_host_name])
  errors = []

  keys.each do |k|
    pretty_key = k.to_s.gsub(/_/, ' ').gsub(/\w+/){ |w| (w =~ /(ssh)|(aws)/i) ? w.upcase  : w.capitalize }
    if locate_config_value(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