not sure about this - i mean what about those drivers/clouds that don't
'do' addresses at all, i.e. set :capability, lambda { |t| true }On 02/01/13 14:23, [email protected] wrote: > From: Michal Fojtik <[email protected]> > > > Signed-off-by: Michal fojtik <[email protected]> > --- > server/lib/cimi/collections/address_templates.rb | 30 ++++++++- > server/lib/cimi/models/address_template.rb | 79 > +++++++++++++++++++++++- > server/support/cimi/address_template.xml | 12 ++++ > 3 files changed, 115 insertions(+), 6 deletions(-) > create mode 100644 server/support/cimi/address_template.xml > > diff --git a/server/lib/cimi/collections/address_templates.rb > b/server/lib/cimi/collections/address_templates.rb > index 2b55e63..338d0f4 100644 > --- a/server/lib/cimi/collections/address_templates.rb > +++ b/server/lib/cimi/collections/address_templates.rb > @@ -16,11 +16,11 @@ > module CIMI::Collections > class AddressTemplates < Base > > - set :capability, lambda { |m| driver.respond_to? m } > + set :capability, lambda { |t| true } > > collection :address_templates do > > - operation :index, :with_capability => :address_templates do > + operation :index do > description 'List all AddressTemplates in the > AddressTemplateCollection' > control do > address_templates = > AddressTemplate.list(self).filter_by(params['$select']) > @@ -31,7 +31,7 @@ module CIMI::Collections > end > end > > - operation :show, :with_capability => :address_templates do > + operation :show do > description 'Show a specific AddressTemplate' > control do > address_template = CIMI::Model::AddressTemplate.find(params[:id], > self) > @@ -42,6 +42,30 @@ module CIMI::Collections > end > end > > + operation :create do > + description "Create new AddressTemplate" > + control do > + if grab_content_type(request.content_type, request.body) == :json > + new_address_template = > CIMI::Model::AddressTemplate.create_from_json(request.body.read, self) > + else > + new_address_template = > CIMI::Model::AddressTemplate.create_from_xml(request.body.read, self) > + end > + headers_for_create new_address_template > + respond_to do |format| > + format.json { new_address_template.to_json } > + format.xml { new_address_template.to_xml } > + end > + end > + end > + > + operation :destroy do > + description "Delete a specified machine template" > + control do > + CIMI::Model::MachineTemplate.delete!(params[:id], self) > + no_content_with_status(200) > + end > + end > + > end > > end > diff --git a/server/lib/cimi/models/address_template.rb > b/server/lib/cimi/models/address_template.rb > index 5ca6850..a1d4789 100644 > --- a/server/lib/cimi/models/address_template.rb > +++ b/server/lib/cimi/models/address_template.rb > @@ -39,16 +39,89 @@ class CIMI::Model::AddressTemplate < CIMI::Model::Base > > def self.find(id, context) > if id==:all > - context.driver.address_templates(context.credentials, {:env=>context}) > + if context.driver.respond_to? :address_templates > + context.driver.address_templates(context.credentials, > {:env=>context}) > + else > + Deltacloud::Database::AddressTemplate.all( > + 'provider.driver' => driver_symbol.to_s, > + 'provider.url' => current_provider > + ).map { |t| from_db(t, context) } > + end > else > - context.driver.address_templates(context.credentials, {:id=>id, > :env=>context}) > + if context.driver.respond_to? :address_template > + context.driver.address_template(context.credentials, id, > :env=>context) > + else > + template = Deltacloud::Database::AddressTemplate.first( > + 'provider.driver' => driver_symbol.to_s, > + 'provider.url' => current_provider, > + :id => id > + ) > + raise CIMI::Model::NotFound unless template > + from_db(template, context) > + end > end > end > > - def self.create(request_body, context, type) > + def self.create_from_json(body, context) > + json = JSON.parse(body) > + new_template = current_db.address_templates.new( > + :name => json['name'], > + :description => json['description'], > + :ip => json['ip'], > + :allocation => json['allocation'], > + :default_gateway => json['default_gateway'], > + :dns => json['dns'], > + :protocol => json['protocol'], > + :mask => json['mask'], > + :ent_properties => json['properties'].to_json, > + :be_kind => 'address_template', > + :be_id => '' > + ) > + new_template.save! > + from_db(new_template, context) > + end > + > + def self.create_from_xml(body, context) > + xml = XmlSimple.xml_in(body) > + new_template = current_db.address_templates.new( > + :name => xml['name'].first, > + :description => xml['description'].first, > + :ip => xml['ip'].first, > + :allocation => xml['allocation'].first, > + :default_gateway => xml['default_gateway'].first, > + :dns => xml['dns'].first, > + :protocol => xml['protocol'].nil? ? nil : xml['protocol'].first, > + :mask => xml['mask'].first, > + :ent_properties => xml['property'].inject({}) { |r, p| > r[p['key']]=p['content']; r }, > + :be_kind => 'machine_template', > + :be_id => '' > + ) > + new_template.save! > + from_db(new_template, context) > end > > def self.delete!(id, context) > + current_db.address_templates.first(:id => id).destroy > + end > + > + private > + > + def self.from_db(model, context) > + self.new( > + :id => context.address_template_url(model.id), > + :name => model.name, > + :description => model.description, > + :ip => model.ip, > + :allocation => model.allocation, > + :default_gateway => model.default_gateway, > + :dns => model.dns, > + :protocol => model.protocol, > + :mask => model.mask, > + :property => model.ent_properties, > + :operations => [ > + { :href => context.destroy_address_template_url(model.id), :rel => > 'http://schemas.dmtf.org/cimi/1/action/delete' } > + ] > + ) > end > > end > diff --git a/server/support/cimi/address_template.xml > b/server/support/cimi/address_template.xml > new file mode 100644 > index 0000000..6c8d548 > --- /dev/null > +++ b/server/support/cimi/address_template.xml > @@ -0,0 +1,12 @@ > +<AddressTemplateCreate> > + <name>myAddressTemplate</name> > + <ip>10.0.0.1</ip> > + <allocation>static</allocation> > + <default_gateway>10.0.0.250</default_gateway> > + <dns>8.8.8.8</dns> > + <mask>255.255.255.0</mask> > + <protocol>ipv4</protocol> > + <description>A new awesome Address Template</description> > + <property key="address_template_test">value_address_template</property> > + <property key="foo">bar</property> > +</AddressTemplateCreate> >
