Module: Chef::Knife::KnifecosmicBase

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/cosmic_base.rb', line 23

def self.included(includer)
  includer.class_eval do

    deps do
      require 'knife-cosmic/connection'
    end

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

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

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

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

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

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

    def validate_base_options
      unless locate_config_value :cosmic_url
        ui.error "cosmic URL not specified"
        exit 1
      end
      unless locate_config_value :cosmic_api_key
        ui.error "cosmic API key not specified"
        exit 1
      end
      unless locate_config_value :cosmic_secret_key
        ui.error "cosmic Secret key not specified"
        exit 1
      end
    end

    def connection
      @connection ||= CosmicClient::Connection.new(
        locate_config_value(:cosmic_url),
        locate_config_value(:cosmic_api_key),
        locate_config_value(:cosmic_secret_key),
        locate_config_value(:cosmic_project),
        locate_config_value(:cosmic_no_ssl_verify),
        locate_config_value(:cosmic_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_cosmic_name?(name)
      !!(name && /^[a-zA-Z0-9][a-zA-Z0-9_\-#]*$/.match(name))
    end

  end
end