#!/usr/bin/env ruby # # Copyright (C) 2009 Red Hat, Inc. # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA require 'rubygems' require 'optparse' require 'uri' require 'lib/deltacloud' require 'lib/plain_formatter' include DeltaCloud::PlainFormatter options = { :verbose => false } @optparse = OptionParser.new do |opts| opts.banner = < options[:architecture]) if options[:architecture] params.merge!(:id => options[:id]) if options[:id] params.merge!(:state => options[:state]) if options[:state] client.send(options[:collection].to_s, params).each do |model| puts format(model) end exit(0) end if options[:collection] and options[:operation] invalid_usage("Unknown collection: #{options[:collection]}") unless collections.include?(options[:collection].to_sym) params = {} params.merge!(:id => options[:id]) if options[:id] # If collection is set and requested operation is 'show' just 'singularize' # collection name and print item with specified id (-i parameter) if options[:operation].eql?('show') puts format(client.send(options[:collection].gsub(/s$/, ''), options[:id])) exit(0) end # If collection is set and requested operation is create new instance, # --image-id, --hardware-profile and --name parameters are used # Returns created instance in plain form if options[:collection].eql?('instances') and options[:operation].eql?('create') invalid_usage("Missing image-id") unless options[:image_id] if options[:name] and ! client.feature?(:instances, :user_name) invalid_usage("Driver does not support user-supplied name") end params.merge!(:name => options[:name]) if options[:name] params.merge!(:image_id => options[:image_id]) if options[:image_id] params.merge!(:hwp_id => options[:hwp_id]) if options[:hwp_id] instance = client.create_instance(options[:image_id], params) puts format(instance) exit(0) end # All other operations above collections is done there: if options[:collection].eql?('instances') instance = client.instance(options[:id]) instance.send("#{options[:operation]}!".to_s) instance = client.instance(options[:id]) puts format(instance) exit(0) end end # If all above passed (eg. no parameters) puts @optparse