You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by lu...@redhat.com on 2011/11/03 00:08:14 UTC

[PATCH] Implementation sketch: model layer for CIMI

From: David Lutterkort <lu...@redhat.com>


Signed-off-by: David Lutterkort <lu...@redhat.com>
---
 server/lib/cimi/model/base.rb             |  145 +++++++++++++++++++++++++++++
 server/lib/cimi/model/machine_template.rb |   21 ++++
 2 files changed, 166 insertions(+), 0 deletions(-)
 create mode 100644 server/lib/cimi/model/base.rb
 create mode 100644 server/lib/cimi/model/machine_template.rb

diff --git a/server/lib/cimi/model/base.rb b/server/lib/cimi/model/base.rb
new file mode 100644
index 0000000..5bc0677
--- /dev/null
+++ b/server/lib/cimi/model/base.rb
@@ -0,0 +1,145 @@
+# 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 Base
+
+  #
+  # Attributes describe how we extract values from XML/JSON
+  #
+  class Attribute
+    attr_reader :name
+
+    def initialize(name)
+      @name = name
+    end
+
+    def from_xml(values, xml)
+      values[name] = xml[name]
+    end
+
+    def from_json(values, json)
+      values[name] = json[name]
+    end
+  end
+
+  class Scalar < Attribute; end
+  class Ref < Scalar; end
+  class Array < Attribute
+    # For an array :things, we collect all <thing/> elements (XmlSimple
+    # actually does the collecting)
+    def from_xml(values, xml)
+      values[@name] = xml[@name.to_s.singularize.to_sym]
+    end
+  end
+
+  #
+  # We keep the values of the attributes in a hash
+  #
+  attr_reader :attribute_values
+
+  # Keep the list of all attributes in an array +attributes+; for each
+  # attribute, we also define a getter and a setter to access/change the
+  # value for that attribute
+  class << self
+    def attributes
+      @attributes ||= []
+    end
+
+    def attributes=(a)
+      @attributes = a
+    end
+
+    def inherited(child)
+      child.attributes = self.attributes.dup
+    end
+
+    def add_attributes(names, attr_klass)
+      names.each do |name|
+        attr = attr_klass.new(name)
+        @attributes << attr
+        # Define getter and setter
+        define_method(name) { attribute_values[name] }
+        define_method(:"#{name}=", newval) { attribute_values[name = newval] }
+      end
+    end
+  end
+
+  #
+  # The DSL for declaring various attributes
+  #
+  class << self
+
+    # A reference, i.e., a URI
+    def ref(*args)
+      add_attributes(args, Ref)
+    end
+
+    # A scalar attribute (string, integer, boolean)
+    def scalar(*args)
+      add_attributes(args, Scalar)
+    end
+
+    # An array of values; the type of the values depends on the array, but
+    # is in general a hash
+    def array(*args)
+      add_attributes(args, Array)
+    end
+  end
+
+  #
+  # Factory methods
+  #
+  def initialize(values)
+    @attribute_values = values
+  end
+
+  # Construct a new object from the XML representation +xml+
+  def self.from_xml(xml)
+    h = XmlSimple.xml_in(xml)
+    values = @attributes.inject({}) do |v, attr|
+      attr.from_xml(v, h)
+      v
+    end
+    new(values)
+  end
+
+  # Construct a new object
+  def self.from_json(json)
+    values = @attributes.inject({}) do |v, attr|
+      attr.from_json(v, json)
+    end
+    new(values)
+  end
+
+  #
+  # Serialize
+  #
+  def self.to_json
+    # FIXME: generate hash that is the JSON representation of this object
+  end
+
+  # FIXME: can we do to_xml ? We do not have metadata to distinguish
+  # between attributes and nested elements, especially for array attributes
+  # (e.g., look at <volume/> and <networkInterface/> in MachineTemplate
+
+  #
+  # Common attributes for all resources
+  #
+  ref :uri
+  ref :type_uri
+  scalar :name
+  scalar :created
+  array :properties
+end
diff --git a/server/lib/cimi/model/machine_template.rb b/server/lib/cimi/model/machine_template.rb
new file mode 100644
index 0000000..4b58a87
--- /dev/null
+++ b/server/lib/cimi/model/machine_template.rb
@@ -0,0 +1,21 @@
+# 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 MachineTemplate < Deltacloud::CIMI::Model::Base
+  ref   :machine_config, :machine_image, :machine_admin
+  array :volumes
+  array :volume_templates
+  array :network_interfaces
+end
-- 
1.7.6.4