You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltacloud.apache.org by ma...@apache.org on 2013/04/26 17:47:10 UTC

[13/14] git commit: Network API rev 3 - Mock Driver implementation

Network API rev 3 - Mock Driver implementation


Project: http://git-wip-us.apache.org/repos/asf/deltacloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltacloud/commit/83dcee9d
Tree: http://git-wip-us.apache.org/repos/asf/deltacloud/tree/83dcee9d
Diff: http://git-wip-us.apache.org/repos/asf/deltacloud/diff/83dcee9d

Branch: refs/heads/master
Commit: 83dcee9d39e92bb7dc59053ee88e44001e7f10e1
Parents: 1cef953
Author: marios <ma...@redhat.com>
Authored: Fri Apr 12 12:20:29 2013 +0300
Committer: marios <ma...@redhat.com>
Committed: Fri Apr 26 18:01:55 2013 +0300

----------------------------------------------------------------------
 server/lib/deltacloud/drivers/base_driver.rb      |   11 ++
 server/lib/deltacloud/drivers/mock/mock_driver.rb |  102 +++++++++++++++-
 2 files changed, 111 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/83dcee9d/server/lib/deltacloud/drivers/base_driver.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/drivers/base_driver.rb b/server/lib/deltacloud/drivers/base_driver.rb
index 85a2148..8e1c6df 100644
--- a/server/lib/deltacloud/drivers/base_driver.rb
+++ b/server/lib/deltacloud/drivers/base_driver.rb
@@ -259,6 +259,17 @@ module Deltacloud
       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
+
+    def network_interface(credentials, opts={})
+      network_interfaces(credentials, opts).first if has_capability?(:network_interfaces)
+    end
 
     MEMBER_SHOW_METHODS = [ :realm, :image, :instance, :storage_volume, :bucket, :blob,
                             :key, :firewall ] unless defined?(MEMBER_SHOW_METHODS)

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/83dcee9d/server/lib/deltacloud/drivers/mock/mock_driver.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/drivers/mock/mock_driver.rb b/server/lib/deltacloud/drivers/mock/mock_driver.rb
index 51a568c..feb3a01 100644
--- a/server/lib/deltacloud/drivers/mock/mock_driver.rb
+++ b/server/lib/deltacloud/drivers/mock/mock_driver.rb
@@ -227,7 +227,6 @@ module Deltacloud::Drivers::Mock
         :actions => instance_actions_for((initial_state == "STARTED" ? "RUNNING" : initial_state)),
         :user_data => opts[:user_data] ? Base64::decode64(opts[:user_data]) : nil
       }
-
       @client.store(:instances, instance)
       Instance.new( instance )
     end
@@ -254,6 +253,11 @@ module Deltacloud::Drivers::Mock
 
     def destroy_instance(credentials, id)
       check_credentials( credentials )
+      #also destroy the instance network_interface(s) if any:
+      inst = @client.load_collection(:instances, id)
+      inst[:network_interfaces].each do |network_interface|
+        destroy_network_interface(credentials, network_interface)
+      end
       @client.destroy(:instances, id)
     end
 
@@ -561,6 +565,100 @@ module Deltacloud::Drivers::Mock
       metric
     end
 
+    def networks(credentials, opts={})
+      check_credentials(credentials)
+      networks = @client.build_all(Network)
+      filter_on(networks, opts, :id)
+    end
+
+    def create_network(credentials, opts={})
+      check_credentials(credentials)
+      id = opts[:name] || "net_#{Time.now.to_i}"
+      net_hash = { :id => id,
+                   :name => id,
+                   :address_blocks => [opts[:address_block]],
+                   :state => "UP"}
+      @client.store(:networks, net_hash)
+      Network.new(net_hash)
+    end
+
+    def destroy_network(credentials, network_id)
+      check_credentials(credentials)
+      net = network(credentials, {:id => network_id})
+      #also destroy subnets:
+      net.subnets.each do |sn|
+        destroy_subnet(credentials, sn)
+      end
+      @client.destroy(:networks, network_id)
+    end
+
+    def subnets(credentials, opts={})
+      check_credentials(credentials)
+      subnets = @client.build_all(Subnet)
+      filter_on(subnets, opts, :id)
+    end
+
+    def create_subnet(credentials, opts={})
+      check_credentials(credentials)
+      id = opts[:name] || "subnet_#{Time.now.to_i}"
+      snet_hash = { :id => id,
+                   :name => id,
+                   :address_block => opts[:address_block],
+                   :network => opts[:network_id],
+                   :state => "UP"}
+      @client.store(:subnets, snet_hash)
+      #also update network:
+      net = @client.load_collection(:networks, opts[:network_id])
+      net[:subnets] ||=[]
+      net[:subnets]  << snet_hash[:id]
+      @client.store(:networks, net)
+      Subnet.new(snet_hash)
+    end
+
+    def destroy_subnet(credentials, subnet_id)
+      check_credentials(credentials)
+      snet = subnet(credentials, {:id => subnet_id})
+      #also update network:
+      net = @client.load_collection(:networks, snet.network)
+      net[:subnets].delete(subnet_id)
+      @client.store(:networks, net)
+      @client.destroy(:subnets, subnet_id)
+    end
+
+    def network_interfaces(credentials, opts={})
+      check_credentials(credentials)
+      network_interfaces = @client.build_all(NetworkInterface)
+      filter_on(network_interfaces, opts, :id)
+    end
+
+    def create_network_interface(credentials, opts={})
+      check_credentials(credentials)
+      id = opts[:name] || "nic_#{Time.now.to_i}"
+      nic_hash = {:id => id, :name => id, :instance => opts[:instance],
+                  :network => opts[:network]}
+      #need an IP address from the subnet cidr range:
+      snet = @client.load_collection(:subnets,  opts[:subnet])
+      cidr = IPAddr.new(snet[:address_block])
+      nic_hash[:ip_address] = cidr.to_range.to_a.sample.to_s #sloppy, choose random address - hey it's mock!
+      #need to update instance nics:
+      inst = @client.load_collection(:instances, opts[:instance])
+      inst[:network_interfaces] ||= []
+      inst[:network_interfaces] << id
+      @client.store(:instances, inst)
+      @client.store(:network_interfaces, nic_hash)
+      NetworkInterface.new(nic_hash)
+    end
+
+    def destroy_network_interface(credentials, nic_id)
+      check_credentials(credentials)
+      #need to update the instance too
+      nic = @client.load_collection(:network_interfaces, nic_id)
+      inst = @client.load_collection(:instances, nic[:instance])
+      inst[:network_interfaces].delete(nic_id)
+      @client.store(:instances, inst)
+      @client.destroy(:network_interfaces, nic_id)
+    end
+
     private
 
     def check_credentials(credentials)
@@ -647,7 +745,7 @@ module Deltacloud::Drivers::Mock
         status 403
       end
 
-      on /BucketNotExist/ do
+      on /(BucketNotExist || NotFound)/ do
         status 404
       end