#!/usr/bin/env ruby # # Copyright (C) 2009 Red Hat, Inc. # # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. The # ASF licenses this file to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance with the # License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. require 'rubygems' require 'optparse' require 'uri' require 'deltacloud' require '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') invalid_usage("Missing ID, must be provided with --id") unless options[:id] 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