You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltacloud.apache.org by mf...@apache.org on 2010/08/04 12:18:12 UTC
svn commit: r982188 - in /incubator/deltacloud/trunk/server: ./
lib/deltacloud/base_driver/ lib/deltacloud/drivers/ec2/
lib/deltacloud/drivers/gogrid/ lib/deltacloud/helpers/
lib/deltacloud/models/ views/instance_credentials/
Author: mfojtik
Date: Wed Aug 4 10:18:11 2010
New Revision: 982188
URL: http://svn.apache.org/viewvc?rev=982188&view=rev
Log:
Instance credentials managment (rev 2)
Added:
incubator/deltacloud/trunk/server/lib/deltacloud/models/instance_credential.rb
incubator/deltacloud/trunk/server/views/instance_credentials/
incubator/deltacloud/trunk/server/views/instance_credentials/index.html.haml
incubator/deltacloud/trunk/server/views/instance_credentials/index.xml.haml
incubator/deltacloud/trunk/server/views/instance_credentials/new.html.haml
incubator/deltacloud/trunk/server/views/instance_credentials/show.html.haml
incubator/deltacloud/trunk/server/views/instance_credentials/show.xml.haml
Modified:
incubator/deltacloud/trunk/server/deltacloud.rb
incubator/deltacloud/trunk/server/lib/deltacloud/base_driver/base_driver.rb
incubator/deltacloud/trunk/server/lib/deltacloud/base_driver/features.rb
incubator/deltacloud/trunk/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
incubator/deltacloud/trunk/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb
incubator/deltacloud/trunk/server/lib/deltacloud/helpers/application_helper.rb
incubator/deltacloud/trunk/server/server.rb
Modified: incubator/deltacloud/trunk/server/deltacloud.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/deltacloud.rb?rev=982188&r1=982187&r2=982188&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/deltacloud.rb (original)
+++ incubator/deltacloud/trunk/server/deltacloud.rb Wed Aug 4 10:18:11 2010
@@ -9,6 +9,7 @@ require 'deltacloud/models/base_model'
require 'deltacloud/models/realm'
require 'deltacloud/models/image'
require 'deltacloud/models/instance'
+require 'deltacloud/models/instance_credential'
require 'deltacloud/models/instance_profile'
require 'deltacloud/models/storage_snapshot'
require 'deltacloud/models/storage_volume'
Modified: incubator/deltacloud/trunk/server/lib/deltacloud/base_driver/base_driver.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/base_driver/base_driver.rb?rev=982188&r1=982187&r2=982188&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/base_driver/base_driver.rb (original)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/base_driver/base_driver.rb Wed Aug 4 10:18:11 2010
@@ -31,6 +31,16 @@ module Deltacloud
end
end
+ class BackendFeatureUnsupported < StandardError
+ attr_reader :code, :cause, :details
+ def initialize(code, cause, message, details)
+ super(message)
+ @code = code
+ @cause = cause
+ @details = details
+ end
+ end
+
class BaseDriver
def self.define_hardware_profile(name,&block)
Modified: incubator/deltacloud/trunk/server/lib/deltacloud/base_driver/features.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/base_driver/features.rb?rev=982188&r1=982187&r2=982188&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/base_driver/features.rb (original)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/base_driver/features.rb Wed Aug 4 10:18:11 2010
@@ -120,6 +120,13 @@ module Deltacloud
#
# Declaration of optional features
#
+ declare_feature :images, :owner_id do
+ description "Filter images using owner id"
+ operation :index do
+ param :owner_id, :string, :optional, nil, "Owner ID"
+ end
+ end
+
declare_feature :instances, :user_name do
description "Accept a user-defined name on instance creation"
operation :create do
Modified: incubator/deltacloud/trunk/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/drivers/ec2/ec2_driver.rb?rev=982188&r1=982187&r2=982188&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/drivers/ec2/ec2_driver.rb (original)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/drivers/ec2/ec2_driver.rb Wed Aug 4 10:18:11 2010
@@ -35,8 +35,13 @@ module Deltacloud
module EC2
class EC2Driver < Deltacloud::BaseDriver
+ def supported_collections
+ DEFAULT_COLLECTIONS + [ :instance_credentials ]
+ end
+
feature :instances, :user_data
feature :instances, :authentication_key
+ feature :images, :owner_id
define_hardware_profile('m1.small') do
cpu 1
@@ -270,6 +275,39 @@ class EC2Driver < Deltacloud::BaseDriver
snapshots
end
+ def instance_credential(credentials, opts=nil)
+ instance_credentials(credentials, opts).first
+ end
+
+ def instance_credentials(credentials, opts=nil)
+ ec2 = new_client( credentials )
+ opts[:key_name] = opts[:id] if opts and opts[:id]
+ keypairs = ec2.describe_keypairs(opts || {})
+ result = []
+ safely do
+ keypairs.keySet.item.each do |keypair|
+ result << convert_instance_credential(keypair)
+ end
+ end
+ result
+ end
+
+ def create_instance_credential(credentials, opts={})
+ instance_credential = InstanceCredential.new
+ ec2 = new_client( credentials )
+ safely do
+ instance_credential = convert_instance_credential(ec2.create_keypair(opts))
+ end
+ return instance_credential
+ end
+
+ def destroy_instance_credential(credentials, opts={})
+ safely do
+ ec2 = new_client( credentials )
+ ec2.delete_keypair(opts)
+ end
+ end
+
private
def new_client(credentials)
@@ -281,6 +319,16 @@ class EC2Driver < Deltacloud::BaseDriver
AWS::EC2::Base.new(opts)
end
+ def convert_instance_credential(instance_credential)
+ key=InstanceCredential.new({
+ :id => instance_credential['keyName'],
+ :fingerprint => instance_credential['keyFingerprint'],
+ :credential_type => :key
+ })
+ key.pem_rsa_key = instance_credential['keyMaterial'] if instance_credential['keyMaterial']
+ return key
+ end
+
def convert_image(ec2_image)
Image.new( {
:id=>ec2_image['imageId'],
Modified: incubator/deltacloud/trunk/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb?rev=982188&r1=982187&r2=982188&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb (original)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb Wed Aug 4 10:18:11 2010
@@ -44,7 +44,8 @@ class GogridDriver < Deltacloud::BaseDri
end
def supported_collections
- DEFAULT_COLLECTIONS.reject { |c| [ :storage_volumes, :storage_snapshots ].include?(c) }
+ DEFAULT_COLLECTIONS.reject! { |c| [ :storage_volumes, :storage_snapshots ].include?(c) }
+ DEFAULT_COLLECTIONS + [ :instance_credentials ]
end
def images(credentials, opts=nil)
@@ -175,6 +176,19 @@ class GogridDriver < Deltacloud::BaseDri
end
end
+ def instance_credential(credentials, opts=nil)
+ instance_credentials(credentials, opts).first
+ end
+
+ def instance_credentials(credentials, opts=nil)
+ gogrid = new_client( credentials )
+ creds = []
+ gogrid.request('support/password/list')['list'].each do |password|
+ creds << convert_instance_credential(password)
+ end
+ return creds
+ end
+
define_instance_states do
start.to( :pending ) .automatically
pending.to( :running ) .automatically
@@ -206,6 +220,15 @@ class GogridDriver < Deltacloud::BaseDri
return login_data
end
+ def convert_instance_credential(password)
+ InstanceCredential.new({
+ :id => password['id'],
+ :username => password['username'],
+ :password => password['password'],
+ :credential_type => :password
+ })
+ end
+
def convert_image(gg_image, owner_id=nil)
Image.new( {
:id=>gg_image['id'],
Modified: incubator/deltacloud/trunk/server/lib/deltacloud/helpers/application_helper.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/helpers/application_helper.rb?rev=982188&r1=982187&r2=982188&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/helpers/application_helper.rb (original)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/helpers/application_helper.rb Wed Aug 4 10:18:11 2010
@@ -106,4 +106,10 @@ module ApplicationHelper
end
end
+ def cdata(&block)
+ text = capture_haml(&block)
+ text.gsub!("\n", "\n ")
+ "<![CDATA[\n #{text}\n]]>"
+ end
+
end
Added: incubator/deltacloud/trunk/server/lib/deltacloud/models/instance_credential.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/models/instance_credential.rb?rev=982188&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/models/instance_credential.rb (added)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/models/instance_credential.rb Wed Aug 4 10:18:11 2010
@@ -0,0 +1,27 @@
+#
+# 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.
+
+class InstanceCredential < BaseModel
+
+ attr_accessor :credential_type
+ attr_accessor :fingerprint
+ attr_accessor :username
+ attr_accessor :password
+ attr_accessor :pem_rsa_key
+
+end
Modified: incubator/deltacloud/trunk/server/server.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/server.rb?rev=982188&r1=982187&r2=982188&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/server.rb (original)
+++ incubator/deltacloud/trunk/server/server.rb Wed Aug 4 10:18:11 2010
@@ -100,7 +100,6 @@ END
"owner_id" and "architecture" parameter
END
param :id, :string
- param :owner_id, :string
param :architecture, :string, :optional
control { filter_all(:images) }
end
@@ -300,3 +299,54 @@ collection :storage_volumes do
control { show(:storage_volume) }
end
end
+
+get '/api/instance_credentials/new' do
+ respond_to do |format|
+ format.html { haml :"instance_credentials/new" }
+ end
+end
+
+collection :instance_credentials do
+ description "Instance authentication credentials"
+
+ operation :index do
+ description "List all available credentials which could be used for instance authentication"
+ control { filter_all :instance_credentials }
+ end
+
+ operation :show do
+ description "Show details about given instance credential"
+ param :id, :string, :required
+ control { show :instance_credential }
+ end
+
+ operation :create do
+ description "Create a new instance credential if backend supports this"
+ param :name, :string, :required
+ control do
+ unless driver.respond_to?(:create_instance_credential)
+ raise Deltacloud::BackendFeatureUnsupported.new('501',
+ 'Creating instance credentials is not supported in backend')
+ end
+ @instance_credential = driver.create_instance_credential(credentials, { :key_name => params[:name] })
+ respond_to do |format|
+ format.html { haml :"instance_credentials/show" }
+ format.xml { haml :"instance_credentials/show" }
+ end
+ end
+ end
+
+ operation :destroy do
+ description "Destroy given instance credential if backend supports this"
+ param :id, :string, :required
+ control do
+ unless driver.respond_to?(:destroy_instance_credential)
+ raise Deltacloud::BackendFeatureUnsupported.new('501',
+ 'Creating instance credentials is not supported in backend')
+ end
+ driver.destroy_instance_credential(credentials, { :key_name => params[:id]})
+ redirect(instance_credentials_url)
+ end
+ end
+
+end
Added: incubator/deltacloud/trunk/server/views/instance_credentials/index.html.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/views/instance_credentials/index.html.haml?rev=982188&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/views/instance_credentials/index.html.haml (added)
+++ incubator/deltacloud/trunk/server/views/instance_credentials/index.html.haml Wed Aug 4 10:18:11 2010
@@ -0,0 +1,26 @@
+%h1 Instance Credentials
+
+%table.display
+ %thead
+ %tr
+ %th ID
+ %th Credentials details
+ %th Actions
+ %tbody
+ - @elements.each do |instance_credential|
+ %tr
+ %td
+ = link_to instance_credential.id, instance_credential_url( instance_credential.id )
+ %td
+ - if instance_credential.credential_type.eql?(:key)
+ = instance_credential.fingerprint
+ - if instance_credential.credential_type.eql?(:password)
+ = "#{instance_credential.username} - #{instance_credential.password}"
+ %td
+ - if driver.respond_to?(:destroy_instance_credential)
+ =link_to 'Destroy', destroy_instance_credential_url(instance_credential.id), :class => 'delete'
+ %tfoot
+ - if driver.respond_to?(:create_instance_credential)
+ %tr
+ %td{:colspan => 3, :style => "text-align:right;"}
+ =link_to 'Create »', "#{url_for('/api/instance_credentials/new')}", :class => 'button'
Added: incubator/deltacloud/trunk/server/views/instance_credentials/index.xml.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/views/instance_credentials/index.xml.haml?rev=982188&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/views/instance_credentials/index.xml.haml (added)
+++ incubator/deltacloud/trunk/server/views/instance_credentials/index.xml.haml Wed Aug 4 10:18:11 2010
@@ -0,0 +1,4 @@
+!!!XML
+%instance_credentials
+ - @elements.each do |c|
+ = haml :'instance_credentials/show', :locals => { :@instance_credential => c, :partial => true }
Added: incubator/deltacloud/trunk/server/views/instance_credentials/new.html.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/views/instance_credentials/new.html.haml?rev=982188&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/views/instance_credentials/new.html.haml (added)
+++ incubator/deltacloud/trunk/server/views/instance_credentials/new.html.haml Wed Aug 4 10:18:11 2010
@@ -0,0 +1,8 @@
+%h1 New Keypair
+
+%form{ :action => '/api/instance_credentials', :method => :post }
+ %p
+ %label
+ Name:
+ %input{ :name => 'name', :size => 30 }/
+ %input{ :type => :submit, :name => "commit", :value => "create" }/
Added: incubator/deltacloud/trunk/server/views/instance_credentials/show.html.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/views/instance_credentials/show.html.haml?rev=982188&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/views/instance_credentials/show.html.haml (added)
+++ incubator/deltacloud/trunk/server/views/instance_credentials/show.html.haml Wed Aug 4 10:18:11 2010
@@ -0,0 +1,22 @@
+%h1
+ = @instance_credential.id
+
+%dl
+ - if @instance_credential.credential_type.eql?(:key)
+ %di
+ %dt Fingerprint
+ %dd
+ = @instance_credential.fingerprint
+ - if @instance_credential.pem_rsa_key
+ %dt PEM key
+ %dd
+ %pre
+ = @instance_credential.pem_rsa_key
+ - if @instance_credential.credential_type.eql?(:password)
+ %di
+ %dt Username
+ %dd
+ = @instance_credential.username
+ %dt Password
+ %dd
+ = @instance_credential.password
Added: incubator/deltacloud/trunk/server/views/instance_credentials/show.xml.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/views/instance_credentials/show.xml.haml?rev=982188&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/views/instance_credentials/show.xml.haml (added)
+++ incubator/deltacloud/trunk/server/views/instance_credentials/show.xml.haml Wed Aug 4 10:18:11 2010
@@ -0,0 +1,22 @@
+- unless defined?(partial)
+ !!! XML
+%instance_credential{ :href => instance_credential_url(@instance_credential.id), :id => @instance_credential.id }
+ %actions
+ - if @instance_credential.respond_to?(:destroy_instance_credential)
+ %link{ :rel => "destroy", :method => "delete", :href => destroy_instance_credential_url(@instance_credential.id)}
+ %credential_type<
+ =@instance_credential.credential_type.to_s
+ - if @instance_credential.credential_type.eql?(:key)
+ %fingerprint<
+ =@instance_credential.fingerprint
+ - unless @instance_credential.pem_rsa_key.nil?
+ %pem<
+ =cdata do
+ =@instance_credential.pem_rsa_key
+ - if @instance_credential.credential_type.eql?(:password)
+ %username<
+ =cdata do
+ =@instance_credential.username
+ %password<
+ =cdata do
+ =@instance_credential.password