Skip to content
This repository was archived by the owner on May 12, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions server/lib/deltacloud/collections/instances.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class Instances < Base
if driver.class.has_feature?(:instances, :authentication_key)
@opts[:keys] = driver.keys(credentials)
end
if driver.has_capability? :networks
@opts[:networks] = driver.networks(credentials)
end
end

get '/instances/:id/run' do
Expand All @@ -57,7 +60,11 @@ class Instances < Base
param :realm_id, :string, :optional
param :hwp_id, :string, :optional
param :keyname, :string, :optional
param :network_id, :string, :optional
param :subnet_id, :string, :optional
control do
params.merge!({:network_id => params["network_id"].split("+").first}) unless params["network_id"].empty?
params.merge!({:subnet_id => params["network_id"].split("+").last}) unless params["network_id"].empty?
@instance = driver.create_instance(credentials, params[:image_id], params)
if @instance.kind_of? Array
@elements = @instance
Expand Down
63 changes: 63 additions & 0 deletions server/lib/deltacloud/collections/networks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# 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.

module Deltacloud::Collections
class Networks < Base

include Deltacloud::Features

set :capability, lambda { |m| driver.respond_to? m }
check_features :for => lambda { |c, f| driver.class.has_feature?(c, f) }

get '/networks/new' do
respond_to do |format|
format.html { haml :"networks/new" }
end
end

collection :networks do

standard_show_operation
standard_index_operation

operation :create, :with_capability => :create_network do
param :address_block, :string, :optional
param :name, :string, :optional
control do
@network = driver.create_network(credentials, { :address_block => params[:address_block]})
respond_to do |format|
format.xml { haml :"networks/show" }
format.html { haml :"networks/show" }
format.json { xml_to_json("networks/show")}
end
end
end

operation :destroy, :with_capability => :destroy_network do
control do
driver.destroy_network(credentials, params[:id])
status 204
respond_to do |format|
format.xml
format.json
format.html { redirect(networks_url) }
end
end
end

end

end
end
65 changes: 65 additions & 0 deletions server/lib/deltacloud/collections/subnets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

# 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.

module Deltacloud::Collections
class Subnets < Base

include Deltacloud::Features

set :capability, lambda { |m| driver.respond_to? m }
check_features :for => lambda { |c, f| driver.class.has_feature?(c, f) }

get '/subnets/new' do
respond_to do |format|
format.html { haml :"subnets/new" }
end
end


collection :subnets do

standard_show_operation
standard_index_operation

operation :create, :with_capability => :create_subnet do
param :network_id, :string, :required
param :address_block, :string, :required
control do
@subnet = driver.create_subnet(credentials, { :network_id => params[:network_id], :address_block => params[:address_block]})
respond_to do |format|
format.xml { haml :"subnets/show"}
format.html { haml :"subnets/show" }
format.json { xml_to_json("subnets/show")}
end
end
end

operation :destroy, :with_capability => :destroy_subnet do
control do
driver.destroy_subnet(credentials, params[:id])
status 204
respond_to do |format|
format.xml
format.json
format.html { redirect(subnets_url) }
end
end
end

end

end
end
7 changes: 7 additions & 0 deletions server/lib/deltacloud/drivers/base_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@ def address(credentials, opts={})
addresses(credentials, opts).first if has_capability?(:addresses)
end

def network(credentials, opts={})
networks(credentials, opts).first if has_capability?(:networks)
end

def subnet(credentials, opts={})
subnets(credentials, opts).first if has_capability?(:subnets)
end

MEMBER_SHOW_METHODS = [ :realm, :image, :instance, :storage_volume, :bucket, :blob,
:key, :firewall ] unless defined?(MEMBER_SHOW_METHODS)
Expand Down
105 changes: 94 additions & 11 deletions server/lib/deltacloud/drivers/ec2/ec2_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,8 @@ def create_instance(credentials, image_id, opts={})
if opts[:metrics] and !opts[:metrics].empty?
instance_options[:monitoring_enabled] = true
end
if opts[:realm_id]
az, sn = opts[:realm_id].split(":")
if sn
instance_options[:subnet_id] = sn
else
instance_options[:availability_zone] = az
end
end
instance_options[:availability_zone] = opts[:realm_id] if opts[:realm_id]
instance_options[:subnet_id] = opts[:subnet_id] if opts[:subnet_id] #FIXME should we fail if no :network_id ? don't need it but need consistency in API...
instance_options[:key_name] = opts[:keyname] if opts[:keyname]
instance_options[:instance_type] = opts[:hwp_id] if opts[:hwp_id] && opts[:hwp_id].length > 0
firewalls = opts.inject([]){|res, (k,v)| res << v if k =~ /firewalls\d+$/; res}
Expand Down Expand Up @@ -835,6 +829,73 @@ def delete_firewall_rule(credentials, opts={})
end
end

#Deltacloud Networks == Amazon VPC
def networks(credentials, opts={})
ec2 = new_client(credentials)
networks = []
safely do
subnets = subnets(credentials) #get all subnets once
ec2.describe_vpcs.each do |vpc|
vpc_subnets = subnets.inject([]){|res,cur| res<<cur if cur.network==vpc[:vpc_id] ;res} #collect subnets for this.network
networks << convert_vpc(vpc, vpc_subnets)
end
end
networks = filter_on(networks, :id, opts)
end

def create_network(credentials, opts={})
ec2 = new_client(credentials)
safely do
network = ec2.create_vpc(opts[:address_block]).first
convert_vpc(network)
end
end

def destroy_network(credentials, network_id)
ec2 = new_client(credentials)
safely do
ec2.delete_vpc(network_id)
end
end

def subnets(credentials, opts={})
ec2 = new_client(credentials)
subnets = []
safely do
ec2.describe_subnets.each do |sn|
subnets << convert_subnet(sn)
end
end
subnets = filter_on(subnets, :id, opts)
end

def create_subnet(credentials, opts={})
ec2 = new_client(credentials)
safely do
subnet = ec2.create_subnet(opts[:network_id], opts[:address_block])
convert_subnet(subnet)
end
end

def destroy_subnet(credentials, opts={})
# this method need to be called
# with the subnet_id as parameter
# in order to work

# ec2 = new_client(credentials)
# safely do
# ec2.delete_subnet(opts[:vpc_id])
# end
# Subnet.delete(:name=>opts[:vpc_id])
#
end

def ports(credentials, opts={})
#
# Need more investigations
#
end

def providers(credentials, opts={})
ec2 = new_client(credentials)
@providers ||= ec2.describe_regions.map{|r| Provider.new( {:id=>r, :name=>r,
Expand Down Expand Up @@ -966,7 +1027,7 @@ def convert_instance(instance)
unless instance[:subnet_id].empty?
realm_id = "#{realm_id}:#{instance[:subnet_id]}"
end
Instance.new(
inst_params = {
:id => instance[:aws_instance_id],
:name => instance[:aws_image_id],
:state => convert_state(instance[:aws_state]),
Expand All @@ -981,8 +1042,11 @@ def convert_instance(instance)
:private_addresses => [InstanceAddress.new(instance[:private_dns_name], :type => :hostname)],
:firewalls => instance[:aws_groups],
:storage_volumes => instance[:block_device_mappings].map{|vol| {vol.values.first=>vol.keys.first } },
:create_image => can_create_image
)
:create_image => can_create_image }
if instance[:vpc_id]
inst_params.merge!(:network_bindings => [{:network=>instance[:vpc_id], :subnet=>instance[:subnet_id], :ip_address=> instance[:aws_private_ip_address]}])
end
Instance.new(inst_params)
end

def convert_key(key)
Expand Down Expand Up @@ -1159,6 +1223,25 @@ def convert_state(ec2_state)
end
end

def convert_vpc(vpc, subnets=[])
addr_blocks = subnets.inject([]){|res,cur| res << cur.address_block ; res}
Network.new({ :id => vpc[:vpc_id],
:name => vpc[:vpc_id],
:state=> vpc[:state],
:subnets => subnets.inject([]){|res,cur| res << cur.id ;res},
:address_blocks=> (addr_blocks.empty? ? [vpc[:cidr_block]] : addr_blocks) })
end

def convert_subnet(subnet)
Subnet.new({ :id => subnet[:subnet_id],
:name => subnet[:subnet_id],
:network =>subnet[:vpc_id],
:address_block => subnet[:cidr_block],
:state => subnet[:state] })
end



exceptions do

on /root device is not supported for the instance/ do
Expand Down
9 changes: 9 additions & 0 deletions server/lib/deltacloud/drivers/mock/data/networks/net1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
:id: net1
:name: network1
:subnets:
- subnet1
- subnet2
:address_blocks:
- 192.168.0.0/16
:state: UP
9 changes: 9 additions & 0 deletions server/lib/deltacloud/drivers/mock/data/networks/net2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
:id: net2
:name: network2
:subnets:
- subnet3
- subnet4
:address_blocks:
- 10.0.0.0/8
:state: UP
6 changes: 6 additions & 0 deletions server/lib/deltacloud/drivers/mock/data/subnets/subnet1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
:id: subnet1
:name: subnet1
:network: net1
:address_block: 192.168.1.0/24
:state: UP
6 changes: 6 additions & 0 deletions server/lib/deltacloud/drivers/mock/data/subnets/subnet2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
:id: subnet2
:name: subnet2
:network: net1
:address_block: 192.168.2.0/24
:state: UP
6 changes: 6 additions & 0 deletions server/lib/deltacloud/drivers/mock/data/subnets/subnet3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
:id: subnet3
:name: subnet3
:network: net2
:address_block: 10.1.0.0/16
:state: UP
6 changes: 6 additions & 0 deletions server/lib/deltacloud/drivers/mock/data/subnets/subnet4.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
:id: subnet4
:name: subnet4
:network: net2
:address_block: 10.2.0.0/16
:state: UP
Loading