Module: Kontena::Plugin::Packet::FacilityOption

Included in:
Master::CreateCommand, Nodes::CreateCommand
Defined in:
lib/kontena/plugin/packet/facility_option.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
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
32
33
34
35
36
37
38
# File 'lib/kontena/plugin/packet/facility_option.rb', line 4

def self.included(base)
  base.option "--facility", "CODE", "Packet facility"
  base.class_eval do
    def default_facility
      require 'packet'
      client = Packet::Client.new(self.token || (self.respond_to?(:default_token) && self.default_token))

      facilities = []
      spinner "Retrieving a list of available facilities at Packet" do
        facilities = client.list_facilities
      end

      case facilities.size
      when 0
        abort 'You do not have access to any facilities on Packet'
      when 1
        unless Kontena.prompt.yes?("You have access to facility '#{facilities.first.name}'. Use?")
          abort 'Aborted'
        end
        facilities.first.code
      else
        Kontena.prompt.select "Packet facility:" do |menu|
          facilities.each do |facility|
            if facility.features.empty?
              feats = ""
            else
              feats = "(#{facility.features.join(',')})"
            end
            menu.choice "#{facility.name} #{feats}", facility.code
          end
        end
      end
    end
  end
end