Module: Chef::Knife::KnifeCloudstackBase

Class Method Summary collapse

Class Method Details

.included(includer) ⇒ Object



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

def self.included(includer)
  includer.class_eval do

    deps do
      require 'knife-cloudstack/connection'
    end

    option :cloudstack_url,
           :short => "-U URL",
           :long => "--cloudstack-url URL",
           :description => "The CloudStack endpoint URL",
           :proc => Proc.new { |url| Chef::Config[:knife][:cloudstack_url] = url }

    option :cloudstack_api_key,
           :short => "-A KEY",
           :long => "--cloudstack-api-key KEY",
           :description => "Your CloudStack API key",
           :proc => Proc.new { |key| Chef::Config[:knife][:cloudstack_api_key] = key }

    option :cloudstack_secret_key,
           :short => "-K SECRET",
           :long => "--cloudstack-secret-key SECRET",
           :description => "Your CloudStack secret key",
           :proc => Proc.new { |key| Chef::Config[:knife][:cloudstack_secret_key] = key }

    option :cloudstack_project,
           :short => "-P PROJECT_NAME",
           :long => '--cloudstack-project PROJECT_NAME',
           :description => "Cloudstack Project in which to create server",
           :proc => Proc.new { |v| Chef::Config[:knife][:cloudstack_project] = v },
           :default => nil

    option :cloudstack_no_ssl_verify,
           :long => '--cloudstack-no-ssl-verify',
           :description => "Disable certificate verify on SSL",
           :boolean => true

    option :cloudstack_proxy,
           :long => '--cloudstack-proxy PROXY',
           :description => "Enable proxy configuration for cloudstack api access"

    def validate_base_options
      unless locate_config_value :cloudstack_url
        ui.error "Cloudstack URL not specified"
        exit 1
      end
      unless locate_config_value :cloudstack_api_key
        ui.error "Cloudstack API key not specified"
        exit 1
      end
      unless locate_config_value :cloudstack_secret_key
        ui.error "Cloudstack Secret key not specified"
        exit 1
      end
    end

    def connection
      @connection ||= CloudstackClient::Connection.new(
        locate_config_value(:cloudstack_url),
        locate_config_value(:cloudstack_api_key),
        locate_config_value(:cloudstack_secret_key),
        locate_config_value(:cloudstack_project),
        locate_config_value(:cloudstack_no_ssl_verify),
        locate_config_value(:cloudstack_proxy)
      )
    end

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

    def exit_with_error(error)
      ui.error error
      exit 1
    end

    def valid_cs_name?(name)
      !!(name && /^[a-zA-Z0-9][a-zA-Z0-9_\-#]*$/.match(name))
    end

  end
end