Module: Chef::Knife::ProfitbricksBase

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/chef/knife/profitbricks_base.rb', line 7

def self.included(includer)
  includer.class_eval do
    deps do
      require 'profitbricks'
    end

    option :profitbricks_username,
      short: '-u USERNAME',
      long: '--username USERNAME',
      description: 'Your ProfitBricks username',
      proc: proc { |username| Chef::Config[:knife][:profitbricks_username] = username }

    option :profitbricks_password,
      short: '-p PASSWORD',
      long: '--password PASSWORD',
      description: 'Your ProfitBricks password',
      proc: proc { |password| Chef::Config[:knife][:profitbricks_password] = password }

    option :profitbricks_url,
      short: '-U URL',
      long: '--url URL',
      description: 'The ProfitBricks API URL',
      proc: proc { |url| Chef::Config[:knife][:profitbricks_url] = url }
  end
end

Instance Method Details

#connectionObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/chef/knife/profitbricks_base.rb', line 33

def connection
  ProfitBricks.configure do |config|
    config.username = Chef::Config[:knife][:profitbricks_username]
    config.password = Chef::Config[:knife][:profitbricks_password]
    config.url = Chef::Config[:knife][:profitbricks_url]
    config.debug = Chef::Config[:knife][:profitbricks_debug] || false
    config.global_classes = false
    config.headers = Hash.new
    config.headers['User-Agent'] = "Chef/#{::Chef::VERSION} knife-profitbricks/#{::Knife::ProfitBricks::VERSION}"
  end
end

#error_and_exit(message) ⇒ Object



55
56
57
58
# File 'lib/chef/knife/profitbricks_base.rb', line 55

def error_and_exit(message)
  ui.error message
  exit(1)
end

#get_image(image_name, image_type, image_location) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/chef/knife/profitbricks_base.rb', line 61

def get_image(image_name, image_type, image_location)
  images = ProfitBricks::Image.list
  min_image = nil
  images.each do |image|
    if image.properties['name'].downcase.include? image_name && image.properties['public'] == true && image.properties['imageType'] == image_type && image.properties['location'] == image_location
      min_image = image
    end
  end
  min_image
end

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



45
46
47
48
49
# File 'lib/chef/knife/profitbricks_base.rb', line 45

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

#validate_required_params(required_params, params) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/chef/knife/profitbricks_base.rb', line 51

def validate_required_params(required_params, params)
  missing_params = required_params.select do |param|
     params[param].nil?
   end
  def error_and_exit(message)
    ui.error message
    exit(1)
  end
end