From: marios <[email protected]>

A "network service" allows you to connect to a running instance. This will mean 
different things for different clouds; in ec2 this means setting up the 
firewall/security group to allow connection to a specific port. In terremark 
this means creating a new "node service". What a network_service consists of is 
still very much up for discussion. This patch series adds the relevant bits to 
core and a first implementation for the terremark driver (i.e. you should have 
a version of core with the terremark driver in order to apply this patch). 
Includes the haml for index and show (but not create yet).
---
 server/deltacloud.rb                               |    1 +
 server/lib/deltacloud/base_driver/base_driver.rb   |   16 +++++++++
 .../drivers/terremark/terremark_driver.rb          |   29 +++++++++++++++++
 server/lib/deltacloud/models/network_service.rb    |    9 +++++
 server/server.rb                                   |   20 ++++++++++--
 server/views/network_services/index.html.haml      |   33 ++++++++++++++++++++
 server/views/network_services/show.html.haml       |   21 ++++++++++++
 7 files changed, 126 insertions(+), 3 deletions(-)
 create mode 100644 server/lib/deltacloud/models/network_service.rb
 create mode 100644 server/views/network_services/index.html.haml
 create mode 100644 server/views/network_services/show.html.haml

diff --git a/server/deltacloud.rb b/server/deltacloud.rb
index 1c19a5d..2cac6a4 100644
--- a/server/deltacloud.rb
+++ b/server/deltacloud.rb
@@ -12,3 +12,4 @@ require 'deltacloud/models/instance'
 require 'deltacloud/models/instance_profile'
 require 'deltacloud/models/storage_snapshot'
 require 'deltacloud/models/storage_volume'
+require 'deltacloud/models/network_service'
diff --git a/server/lib/deltacloud/base_driver/base_driver.rb 
b/server/lib/deltacloud/base_driver/base_driver.rb
index a2b3bbe..e1e02c4 100644
--- a/server/lib/deltacloud/base_driver/base_driver.rb
+++ b/server/lib/deltacloud/base_driver/base_driver.rb
@@ -154,6 +154,22 @@ module Deltacloud
     def reboot_instance(credentials, id)
     end
 
+    def network_services(credentials, opts)
+      []
+    end
+
+    def network_service(credentials, opts)
+      net_services = network_services(credentials, opts)
+      return net_services.first unless net_services.empty?
+      nil
+    end
+
+    def add_network_service(credentials, opts)#needs to work out what params 
needed for instantiation
+    end
+    def delete_network_service(credentials, opts)
+    end
+
+
     def storage_volume(credentials, opts)
       volumes = storage_volumes(credentials, opts)
       return volumes.first unless volumes.empty?
diff --git a/server/lib/deltacloud/drivers/terremark/terremark_driver.rb 
b/server/lib/deltacloud/drivers/terremark/terremark_driver.rb
index ae3c554..e5d650b 100644
--- a/server/lib/deltacloud/drivers/terremark/terremark_driver.rb
+++ b/server/lib/deltacloud/drivers/terremark/terremark_driver.rb
@@ -50,6 +50,35 @@ VAPP_STATE_MAP = { "0" =>  "PENDING", "1" =>  "PENDING", "2" 
=>  "STOPPED", "4"
   #storage_disks [1..15]
 
 #--
+# NETWORK SERVICES
+#--
+  def network_services(credentials, opts=nil)
+    network_services = []
+    terremark_client = new_client(credentials)
+    services = 
terremark_client.get_internet_services(terremark_client.default_vdc_id).body['InternetServices'].each
 do |service|
+    s_id = service['Id'].to_s
+    public_ip = service['PublicIpAddress']['Name']
+    public_port = service['Port']
+    protocol = service['Protocol']
+    nodes = terremark_client.get_node_services(s_id).body['NodeServices']
+    unless nodes.empty?
+      nodes.each do |node|
+        #puts "Id is #{node['Id']}, public_ip is #{public_ip}, public_port is 
#{public_port}, Private IP is #{node['IpAddress']}, private port is 
#{node['Port']} and protocol is #{protocol}"
+        network_services << NetworkService.new({ :id => s_id,
+                                                 :public_address => public_ip,
+                                                 :public_port => public_port,
+                                                 :private_address => 
node['IpAddress'],
+                                                 :private_port => node['Port'],
+                                                 :protocol => protocol
+                                                })
+      end #nodes.each do
+    end #unless nodes empty
+   end #services.each do
+   network_services = filter_on( network_services, :id, opts )
+   network_services
+  end
+
+#--
 # IMAGES
 #--
 #aka "vapp_templates"
diff --git a/server/lib/deltacloud/models/network_service.rb 
b/server/lib/deltacloud/models/network_service.rb
new file mode 100644
index 0000000..91430bb
--- /dev/null
+++ b/server/lib/deltacloud/models/network_service.rb
@@ -0,0 +1,9 @@
+class NetworkService < BaseModel
+
+  attr_accessor :public_address
+  attr_accessor :public_port
+  attr_accessor :private_address
+  attr_accessor :private_port
+  attr_accessor :protocol
+
+end
\ No newline at end of file
diff --git a/server/server.rb b/server/server.rb
index 9f91d86..6467b80 100644
--- a/server/server.rb
+++ b/server/server.rb
@@ -48,9 +48,9 @@ def filter_all(model)
     @elements = driver.send(model.to_sym, credentials, filter)
     instance_variable_set(:"@#{model}", @elements)
     respond_to do |format|
+      format.json { convert_to_json(singular, @elements) }
       format.html { haml :"#{model}/index" }
       format.xml { haml :"#{model}/index" }
-      format.json { convert_to_json(singular, @elements) }
     end
 end
 
@@ -58,9 +58,9 @@ def show(model)
   @element = driver.send(model, credentials, { :id => params[:id]} )
   instance_variable_set("@#{model}", @element)
   respond_to do |format|
+    format.json { convert_to_json(model, @element) }
     format.html { haml :"#{model.to_s.pluralize}/show" }
     format.xml { haml :"#{model.to_s.pluralize}/show" }
-    format.json { convert_to_json(model, @element) }
   end
 end
 
@@ -109,6 +109,20 @@ get '/api\/?' do
 end
 
 # Rabbit DSL
+collection :network_services do
+  description "Allow user to expose a public IP address/port and map these 
onto an internal IP address/port. eg expose ssh access [port 22, tcp]."
+  operation :index do
+    description 'Show all available network services'
+    control { filter_all(:network_services) }
+  end
+
+  operation :show do
+    description 'Show a network_service identified by its id'
+    param :id,            :string, :required
+    control { show(:network_service) }
+  end
+end
+
 
 collection :realms do
   description "Within a cloud provider a realm represents a boundary 
containing resources. The exact definition of a realm is left to the cloud 
provider. In some cases, a realm may represent different datacenters, different 
continents, or different pools of resources within a single datacenter. A cloud 
provider may insist that resources must all exist within a single realm in 
order to cooperate. For instance, storage volumes may only be allowed to be 
mounted to instances within the same realm."
@@ -205,9 +219,9 @@ end
 def instance_action(name)
   @instance = driver.send(:"#{name}_instance", credentials, params[:id])
   respond_to do |format|
+    format.json {convert_to_json(:instance, @instance) }
     format.html { haml :"instances/show" }
     format.xml { haml :"instances/show" }
-    format.json {convert_to_json(:instance, @instance) }
   end
 end
 
diff --git a/server/views/network_services/index.html.haml 
b/server/views/network_services/index.html.haml
new file mode 100644
index 0000000..3738b38
--- /dev/null
+++ b/server/views/network_services/index.html.haml
@@ -0,0 +1,33 @@
+%h1
+  Network Services
+
+%table.display
+  %thead
+    %tr
+      %th
+        ID
+      %th
+        Public Address
+      %th
+        Public Port
+      %th
+        Private Address
+      %th
+        Private Port
+      %th
+        Protocol
+  %tbody
+    - for netservice in @network_services
+      %tr
+        %td
+          = link_to netservice.id, network_service_url( netservice.id )
+        %td
+          = netservice.public_address
+        %td
+          = netservice.public_port
+        %td
+          = netservice.private_address
+        %td
+          = netservice.private_port
+        %td
+          = netservice.protocol
diff --git a/server/views/network_services/show.html.haml 
b/server/views/network_services/show.html.haml
new file mode 100644
index 0000000..128cf0d
--- /dev/null
+++ b/server/views/network_services/show.html.haml
@@ -0,0 +1,21 @@
+%h1 Network Service
+%h2
+  = @network_service.id
+
+%dl
+  %di
+    %dt Public IP
+    %dd
+      = @network_service.public_address
+    %dt Public Port
+    %dd
+      = @network_service.public_port
+    %dt Private IP
+    %dd
+      = @network_service.private_address
+    %dt Private Port
+    %dd
+      = @network_service.private_port
+    %dt Protocol
+    %dd
+      = @network_service.protocol
\ No newline at end of file
-- 
1.6.6.1

_______________________________________________
deltacloud-devel mailing list
[email protected]
https://fedorahosted.org/mailman/listinfo/deltacloud-devel

Reply via email to