Class: SoftLayer::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/softlayer/cli.rb

Constant Summary collapse

OUTPUT_TYPES =
%w[plain csv json]
COLUMNS =
%w[id type hostname ip datacenter cost username name email provision_date]
OBJECT_MASK =
'mask[billingItem[cancellationDate,recurringFee,hourlyRecurringFee,'\
'activeAssociatedChildren[recurringFee,hourlyRecurringFee],'\
'orderItem[order[userRecord[username,email,firstName,lastName]]]]]'

Instance Method Summary collapse

Instance Method Details

#cancel(*ids) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/softlayer/cli.rb', line 120

def cancel(*ids)
  # Create the softlayer client
  client_opts = {}
  client_opts[:username] = options['username'] if options['username']
  client_opts[:api_key] = options['api_key'] if options['api_key']
  SoftLayer::Client.default_client = SoftLayer::Client.new(client_opts)

  problem_ids = []
  done_ids = []
  while ids.size > 0
    server = nil
    begin
      server = SoftLayer::VirtualServer.server_with_id(ids.last)
    rescue
      begin
        server = SoftLayer::BareMetalServer.server_with_id(ids.last)
      rescue
      end
    end

    unless server
      STDERR.puts "No server with id #{id} was found."
      problem_ids << ids.pop
      next
    end

    begin
      server.cancel!
      done_ids << ids.pop
    rescue Exception => e
      STDERR.puts "Error cancelling server #{id}: #{e}"
      problem_ids << ids.pop
    end
  end

  puts "Cancelled #{done_ids.size} servers."
  if problem_ids.size > 0
    puts "Had trouble cancelling servers #{problem_ids.join(', ')}."
    exit 1
  end
end

#reportObject



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
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/softlayer/cli.rb', line 39

def report
  # Create the softlayer client
  client_opts = {}
  client_opts[:username] = options['username'] if options['username']
  client_opts[:api_key] = options['api_key'] if options['api_key']
  SoftLayer::Client.default_client = SoftLayer::Client.new(client_opts)

  search_opts = options['search'] ? {}.merge(options['search']) : {}
  search_opts.symbolize_keys!
  search_opts[:object_mask] = OBJECT_MASK
  search_opts[:result_limit] = { offset: 0, limit: 150 }

  column_opts = COLUMNS
  column_opts = options['columns'] if options['columns'].is_a? Array

  # Generate table
  rows = []
  %w[BareMetalServer VirtualServer].each do |class_name| # Do for both VMs and physical servers
    loop do
      servers = SoftLayer.const_get(class_name).find_servers(search_opts)
      servers.each do |server|
        # Ignore disconnected servers
        next if server.respond_to?(:status) && server.status == 'DISCONNECTED'
        # Ignore cancelled servers
        cancellation_date = server['billingItem'] && server['billingItem']['cancellationDate']
        next unless cancellation_date.nil? || cancellation_date.strip.empty?

        # Gather column data
        row = []
        row << server.id if column_opts.include? 'id'
        row << class_name if column_opts.include? 'type'
        row << server.fqdn if column_opts.include? 'hostname'
        row << (server['primaryIpAddress'] || server['primaryBackendIpAddress']) if column_opts.include? 'ip'
        row << server['datacenter']['longName'] if column_opts.include? 'datacenter'

        if server['billingItem']
          cost = server['billingItem']['recurringFee'].to_f
          if (server['billingItem']['hourlyRecurringFee'] &&
              !server['billingItem']['hourlyRecurringFee'].strip.empty?)
            cost = server['billingItem']['hourlyRecurringFee'].to_f * 24 * 30
          end
          server['billingItem']['activeAssociatedChildren'].each do |child|
            if child['hourlyRecurringFee'] && !child['hourlyRecurringFee'].strip.empty?
              cost += (child['hourlyRecurringFee'].to_f * 24 * 30)
            else
              cost += child['recurringFee'].to_f
            end
          end
          row << cost.round(2) if column_opts.include? 'cost'

          user_record = server['billingItem']['orderItem']['order']['userRecord']
          row << user_record['username'] if column_opts.include? 'username'
          row << "#{user_record['firstName']} #{user_record['lastName']}" if column_opts.include? 'name'
          row << user_record['email'] if column_opts.include? 'email'
        else # Allow report to continue even when no billing item is found
          row << '' if column_opts.include? 'cost'
          row << '' if column_opts.include? 'username'
          row << '' if column_opts.include? 'name'
          row << '' if column_opts.include? 'email'
        end

        row << server['provisionDate'] if column_opts.include? 'provision_date'

        rows << row
      end

      break if servers.size < search_opts[:result_limit][:limit]
      search_opts[:result_limit][:offset] += search_opts[:result_limit][:limit]
    end
  end

  # Output
  CLIFormatter.format(
    rows,
    options['format'],
    COLUMNS.reject { |c| !column_opts.include?(c) },
    options['output_file']
  )
end