Sha256: 3c1f9599bb66f7f7d83a494e47b0a5ac403a227db0bfda6402c6e27d13daa38a

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

require 'active_support'
require 'active_support/core_ext'
require 'csv'
require 'fog'
require 'thor'

module Kashime
  class CLI < Thor

    desc 'ports', 'show all port list'
    option :only_availables, type: :boolean, aliases: :o, default: false
    def ports
      headers = %i(id mac_address fixed_ips device_id)

      ports = Fog::Network[:openstack].ports

      ports = ports.select {|port| port.device_id.empty? } if options['only_availables']

      ports = ports.map {|port| port.attributes.slice(*headers) }

      tsv = CSV.generate(headers: headers, write_headers: true, col_sep: "\t") do |csv|
        ports.each do |port|
          csv << CSV::Row.new(headers, port.values)
        end
      end

      puts tsv
    end

    desc 'create_port', 'create new port'
    def create_port(network_name)
      network = Fog::Network[:openstack].networks.find {|n| n.name == network_name }

      puts Fog::Network[:openstack].ports.create(network_id: network.id).inspect
    end

    desc 'cleanup_ports', 'delete all unattached ports'
    option 'dry-run', type: :boolean, aliases: :d, default: false
    def cleanup_ports
      Fog::Network[:openstack].ports.each do |port|
        if port.device_id.empty?
          puts "Deleting port id: #{port.id}"
          port.destroy unless options['dry-run']
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kashime-0.1.0 lib/kashime/cli.rb