class Chef::Knife::IonoscloudLabelList

Attributes

description[R]
required_options[R]

Public Class Methods

new(args = []) click to toggle source
Calls superclass method
# File lib/chef/knife/ionoscloud_label_list.rb, line 27
def initialize(args = [])
  super(args)
  @description =
  'List all Labels available to the user. Specify the type and required resource ID '\
  'to list labels for a specific resource instead.'
  @required_options = [:ionoscloud_username, :ionoscloud_password]
end

Public Instance Methods

run() click to toggle source
# File lib/chef/knife/ionoscloud_label_list.rb, line 35
def run
  $stdout.sync = true
  handle_extra_config
  validate_required_params(@required_options, config)

  label_api = Ionoscloud::LabelApi.new(api_client)
  opts = { depth: 1 }

  case config[:type]
  when 'datacenter'
    validate_required_params([:resource_id], config)
    labels = label_api.datacenters_labels_get(config[:resource_id], opts)
  when 'server'
    validate_required_params([:datacenter_id, :resource_id], config)
    labels = label_api.datacenters_servers_labels_get(config[:datacenter_id], config[:resource_id], opts)
  when 'volume'
    validate_required_params([:datacenter_id, :resource_id], config)
    labels = label_api.datacenters_volumes_labels_get(config[:datacenter_id], config[:resource_id], opts)
  when 'ipblock'
    validate_required_params([:resource_id], config)
    labels = label_api.ipblocks_labels_get(config[:resource_id], opts)
  when 'snapshot'
    validate_required_params([:resource_id], config)
    labels = label_api.snapshots_labels_get(config[:resource_id], opts)
  else
    if !config[:type].nil?
      ui.warn("#{config[:type]} is not a valid Resource Type. Returning all available labels.")
    end
    labels = label_api.labels_get(opts)
  end

  label_list = [
    ui.color('Resource ID', :bold),
    ui.color('Resource Type', :bold),
    ui.color('Label key', :bold),
    ui.color('Value', :bold),
  ]

  labels.items.each do |label|
    label_list << (label.properties.respond_to?(:resource_id) ? label.properties.resource_id : config[:resource_id])
    label_list << (label.properties.respond_to?(:resource_type) ? label.properties.resource_type : config[:type])
    label_list << label.properties.key
    label_list << label.properties.value
  end

  puts ui.list(label_list, :uneven_columns_across, 4)
end