Module: Chef::Knife::DigitalOceanBase

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object



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

def self.included(includer)
  includer.class_eval do
    category 'digital_ocean'

    # Lazy load our dependencies. Later calls to `Knife#deps` override
    # previous ones, so if the including class calls it, it needs to also
    # call our #load_deps, i.e:
    #
    #   Include Chef::Knife::DigitalOceanBase
    #
    #   deps do
    #     require 'foo'
    #     require 'bar'
    #     Chef::Knife::DigitalOceanBase.load_deps
    #   end
    #
    deps { Chef::Knife::DigitalOceanBase.load_deps }

    option :digital_ocean_client_id,
      :short       => '-K CLIENT_ID',
      :long        => '--digital_ocean_client_id CLIENT_ID',
      :description => 'Your DigitalOcean client_id',
      :proc        => Proc.new { |client_id| Chef::Config[:knife][:digital_ocean_client_id] = client_id }

    option :digital_ocean_api_key,
      :short       => '-A API_KEY',
      :long        => '--digital_ocean_api_key API_KEY',
      :description => 'Your DigitalOcean API_KEY',
      :proc        => Proc.new { |api_key| Chef::Config[:knife][:digital_ocean_api_key] = api_key }
  end
end

.load_depsObject



19
20
21
22
# File 'lib/chef/knife/digital_ocean_base.rb', line 19

def self.load_deps
  require 'digital_ocean'
  require 'highline'
end

Instance Method Details

#clientObject



60
61
62
63
# File 'lib/chef/knife/digital_ocean_base.rb', line 60

def client
  DigitalOcean::API.new(:client_id => Chef::Config[:knife][:digital_ocean_client_id],
                        :api_key   => Chef::Config[:knife][:digital_ocean_api_key])
end

#hObject



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

def h
  @highline ||= HighLine.new
end

#locate_config_value(key) ⇒ Object



79
80
81
82
# File 'lib/chef/knife/digital_ocean_base.rb', line 79

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

#validate!(keys = [:digital_ocean_client_id, :digital_ocean_api_key]) ⇒ Object



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

def validate!(keys=[:digital_ocean_client_id, :digital_ocean_api_key])
  errors = []

  keys.each do |k|
    if locate_config_value(k).nil?
      errors << "You did not provide a valid '#{k}' value. Please set knife[:#{k}] in your knife.rb or pass as an option."
    end
  end

  if errors.each{|e| ui.error(e)}.any?
    exit 1
  end
end