Apipie: proper way to extend the attributes of a controller

Hi Ivan,

For Katello, we have a need to add attributes to the Foreman hostgroups
controller and have those attributes added as part of the apipie
documentation/bindings. For example, content_source_id, content_view_id
and lifecyle_environment_id.

I have tried a few different things, but have not had any luck. Can you
advise on what the proper way is to accomplish this?

The following are a couple of scenarios that I tried, but neither works:

file:
katello/app/controllers/katello/concerns/api/v2/hostgroups_controller_extensions.rb

Code scenario 1:

··· ---------------------- Issue: The attributes appear to get added to the apipie resource; however, this approach overwrites the create/update on the base controller, but we only want to add attributes to the interface and allow the base controller to be called.

module Katello
module Concerns
module Api::V2::HostgroupsControllerExtensions
extend ActiveSupport::Concern

   included do
     def_param_group :hostgroup_modified do
       param :hostgroup, Hash, :required => true, :action_aware => 

true do
param :name, String, :required => true
param :parent_id, :number
param :environment_id, :number
param :operatingsystem_id, :number
param :architecture_id, :number
param :medium_id, :number
param :ptable_id, :number
param :puppet_ca_proxy_id, :number
param :subnet_id, :number
param :domain_id, :number
param :realm_id, :number
param :puppet_proxy_id, :number
param :content_source_id, :number
param :content_view_id, :number
param :lifecycle_environment_id, :number
param_group :taxonomies, ::Api::V2::BaseController
end
end

     api :POST, "/hostgroups/", N_("Create a host group")
     param_group :hostgroup_modified, :as => :create
     def create
       super
     end

     api :PUT, "/hostgroups/:id/", N_("Update a host group")
     param :id, :identifier, :required => true
     param_group :hostgroup_modified
     def update
       super
     end
   end
 end

end
end

Code scenario 2:

Issue: The apipie resource does not seem to recognize/list the
additional attributes for the create/update actions.

module Katello
module Concerns
module Api::V2::HostgroupsControllerExtensions
extend ActiveSupport::Concern

   included do
     alias_method_chain :create, :katello_attributes
     alias_method_chain :update, :katello_attributes

     def_param_group :hostgroup_modified do
       param :hostgroup, Hash, :required => true, :action_aware => 

true do
param :name, String, :required => true
param :parent_id, :number
param :environment_id, :number
param :operatingsystem_id, :number
param :architecture_id, :number
param :medium_id, :number
param :ptable_id, :number
param :puppet_ca_proxy_id, :number
param :subnet_id, :number
param :domain_id, :number
param :realm_id, :number
param :puppet_proxy_id, :number
param :content_source_id, :number
param :content_view_id, :number
param :lifecycle_environment_id, :number
param_group :taxonomies, ::Api::V2::BaseController
end
end

     api :POST, "/hostgroups/", N_("Create a host group")
     param_group :hostgroup_modified, :as => :create

     api :PUT, "/hostgroups/:id/", N_("Update a host group")
     param :id, :identifier, :required => true
     param_group :hostgroup_modified
   end

   def create_with_katello_attributes
     super
   end

   def update_with_katello_attributes
     super
   end
 end

end
end

thanks in advance,
Brad

> Hi Ivan,
>
> For Katello, we have a need to add attributes to the Foreman hostgroups
> controller and have those attributes added as part of the apipie
> documentation/bindings. For example, content_source_id, content_view_id and
> lifecyle_environment_id.
>
> I have tried a few different things, but have not had any luck. Can you
> advise on what the proper way is to accomplish this?
>
> The following are a couple of scenarios that I tried, but neither works:

As you discovered on the PR, the only way to do this at the moment is
completely repeat the API, this is far too fragile and not DRY as
(1) we'll have to account for every single addition foreman makes to
host groups, and (2) only one installed plugin can do this, or it will
be up to the Rails Autoloading Demons to decide which plugin wins.

I would like the same in foreman_salt to have my extensions to hosts and host
groups documented AND in the RABL. We can't be alone.

After thinking about it, I thnik we could do something like this in the
Foreman controller, right?

  def_param_group :hostgroup do
    param :hostgroup, Hash, :required => true, :action_aware => true do
      # [blah blah foreman params here]
      Hostgroup.extended_attributes.each do |attribute|
        param attribute[:field], attribute[:options]
      end
    end
  end

Plugins would alias_method_chain extended_attributes, or maybe register their
extensions through the plugin service? I'm not sure exactly where the extension
point should go, but I think it could be possible, and if we act very quickly
we could manage this into 1.8.

Ivan, do you have any thoughts on this?

··· On Tue, Feb 03, 2015 at 07:33:38AM -0500, Brad Buckingham wrote:

file: katello/app/controllers/katello/concerns/api/v2/hostgroups_controller_extensions.rb

Code scenario 1:

Issue: The attributes appear to get added to the apipie resource; however,
this approach overwrites the create/update on the base controller, but we
only want to add attributes to the interface and allow the base controller
to be called.

module Katello
module Concerns
module Api::V2::HostgroupsControllerExtensions
extend ActiveSupport::Concern

  included do
    def_param_group :hostgroup_modified do
      param :hostgroup, Hash, :required => true, :action_aware => true

do
param :name, String, :required => true
param :parent_id, :number
param :environment_id, :number
param :operatingsystem_id, :number
param :architecture_id, :number
param :medium_id, :number
param :ptable_id, :number
param :puppet_ca_proxy_id, :number
param :subnet_id, :number
param :domain_id, :number
param :realm_id, :number
param :puppet_proxy_id, :number
param :content_source_id, :number
param :content_view_id, :number
param :lifecycle_environment_id, :number
param_group :taxonomies, ::Api::V2::BaseController
end
end

    api :POST, "/hostgroups/", N_("Create a host group")
    param_group :hostgroup_modified, :as => :create
    def create
      super
    end

    api :PUT, "/hostgroups/:id/", N_("Update a host group")
    param :id, :identifier, :required => true
    param_group :hostgroup_modified
    def update
      super
    end
  end
end

end
end

Code scenario 2:

Issue: The apipie resource does not seem to recognize/list the additional
attributes for the create/update actions.

module Katello
module Concerns
module Api::V2::HostgroupsControllerExtensions
extend ActiveSupport::Concern

  included do
    alias_method_chain :create, :katello_attributes
    alias_method_chain :update, :katello_attributes

    def_param_group :hostgroup_modified do
      param :hostgroup, Hash, :required => true, :action_aware => true

do
param :name, String, :required => true
param :parent_id, :number
param :environment_id, :number
param :operatingsystem_id, :number
param :architecture_id, :number
param :medium_id, :number
param :ptable_id, :number
param :puppet_ca_proxy_id, :number
param :subnet_id, :number
param :domain_id, :number
param :realm_id, :number
param :puppet_proxy_id, :number
param :content_source_id, :number
param :content_view_id, :number
param :lifecycle_environment_id, :number
param_group :taxonomies, ::Api::V2::BaseController
end
end

    api :POST, "/hostgroups/", N_("Create a host group")
    param_group :hostgroup_modified, :as => :create

    api :PUT, "/hostgroups/:id/", N_("Update a host group")
    param :id, :identifier, :required => true
    param_group :hostgroup_modified
  end

  def create_with_katello_attributes
    super
  end

  def update_with_katello_attributes
    super
  end
end

end
end

thanks in advance,
Brad


You received this message because you are subscribed to the Google Groups “foreman-dev” group.
To unsubscribe from this group and stop receiving emails from it, send an email to foreman-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Best Regards,

Stephen Benjamin
Red Hat Engineering

https://github.com/Katello/katello/pull/4976 is the PR in question - I
left my thoughts there, hopefully inecas can tell us about how
feasible it is to extend apipie-rails to do this in a reasonable
amount of time.

Best,

··· On Wed, Feb 4, 2015 at 10:38 PM, Stephen Benjamin wrote: > On Tue, Feb 03, 2015 at 07:33:38AM -0500, Brad Buckingham wrote: >> Hi Ivan, >> >> For Katello, we have a need to add attributes to the Foreman hostgroups >> controller and have those attributes added as part of the apipie >> documentation/bindings. For example, content_source_id, content_view_id and >> lifecyle_environment_id. >> >> I have tried a few different things, but have not had any luck. Can you >> advise on what the proper way is to accomplish this? >> >> The following are a couple of scenarios that I tried, but neither works: > > As you discovered on the PR, the only way to do this at the moment is > completely repeat the API, this is far too fragile and not DRY as > (1) we'll have to account for every single addition foreman makes to > host groups, and (2) only one installed plugin can do this, or it will > be up to the Rails Autoloading Demons to decide which plugin wins. > > I would like the same in foreman_salt to have my extensions to hosts and host > groups documented AND in the RABL. We can't be alone. > > After thinking about it, I thnik we could do something like this in the > Foreman controller, right? > > > ``` > def_param_group :hostgroup do > param :hostgroup, Hash, :required => true, :action_aware => true do > # [blah blah foreman params here] > Hostgroup.extended_attributes.each do |attribute| > param attribute[:field], attribute[:options] > end > end > end > ``` > > Plugins would alias_method_chain extended_attributes, or maybe register their > extensions through the plugin service? I'm not sure exactly where the extension > point should go, but I think it could be possible, and if we act very quickly > we could manage this into 1.8. > > Ivan, do you have any thoughts on this? > > > >> >> file: katello/app/controllers/katello/concerns/api/v2/hostgroups_controller_extensions.rb >> >> Code scenario 1: >> ---------------------- >> Issue: The attributes appear to get added to the apipie resource; however, >> this approach overwrites the create/update on the base controller, but we >> only want to add attributes to the interface and allow the base controller >> to be called. >> >> module Katello >> module Concerns >> module Api::V2::HostgroupsControllerExtensions >> extend ActiveSupport::Concern >> >> included do >> def_param_group :hostgroup_modified do >> param :hostgroup, Hash, :required => true, :action_aware => true >> do >> param :name, String, :required => true >> param :parent_id, :number >> param :environment_id, :number >> param :operatingsystem_id, :number >> param :architecture_id, :number >> param :medium_id, :number >> param :ptable_id, :number >> param :puppet_ca_proxy_id, :number >> param :subnet_id, :number >> param :domain_id, :number >> param :realm_id, :number >> param :puppet_proxy_id, :number >> param :content_source_id, :number >> param :content_view_id, :number >> param :lifecycle_environment_id, :number >> param_group :taxonomies, ::Api::V2::BaseController >> end >> end >> >> api :POST, "/hostgroups/", N_("Create a host group") >> param_group :hostgroup_modified, :as => :create >> def create >> super >> end >> >> api :PUT, "/hostgroups/:id/", N_("Update a host group") >> param :id, :identifier, :required => true >> param_group :hostgroup_modified >> def update >> super >> end >> end >> end >> end >> end >> >> Code scenario 2: >> --------------------- >> >> Issue: The apipie resource does not seem to recognize/list the additional >> attributes for the create/update actions. >> >> module Katello >> module Concerns >> module Api::V2::HostgroupsControllerExtensions >> extend ActiveSupport::Concern >> >> included do >> alias_method_chain :create, :katello_attributes >> alias_method_chain :update, :katello_attributes >> >> def_param_group :hostgroup_modified do >> param :hostgroup, Hash, :required => true, :action_aware => true >> do >> param :name, String, :required => true >> param :parent_id, :number >> param :environment_id, :number >> param :operatingsystem_id, :number >> param :architecture_id, :number >> param :medium_id, :number >> param :ptable_id, :number >> param :puppet_ca_proxy_id, :number >> param :subnet_id, :number >> param :domain_id, :number >> param :realm_id, :number >> param :puppet_proxy_id, :number >> param :content_source_id, :number >> param :content_view_id, :number >> param :lifecycle_environment_id, :number >> param_group :taxonomies, ::Api::V2::BaseController >> end >> end >> >> api :POST, "/hostgroups/", N_("Create a host group") >> param_group :hostgroup_modified, :as => :create >> >> api :PUT, "/hostgroups/:id/", N_("Update a host group") >> param :id, :identifier, :required => true >> param_group :hostgroup_modified >> end >> >> def create_with_katello_attributes >> super >> end >> >> def update_with_katello_attributes >> super >> end >> end >> end >> end >> >> >> thanks in advance, >> Brad >> >> -- >> You received this message because you are subscribed to the Google Groups "foreman-dev" group. >> To unsubscribe from this group and stop receiving emails from it, send an email to foreman-dev+unsubscribe@googlegroups.com. >> For more options, visit https://groups.google.com/d/optout. > > -- > Best Regards, > > Stephen Benjamin > Red Hat Engineering > > -- > You received this message because you are subscribed to the Google Groups "foreman-dev" group. > To unsubscribe from this group and stop receiving emails from it, send an email to foreman-dev+unsubscribe@googlegroups.com. > For more options, visit https://groups.google.com/d/optout.


Daniel Lobato

@elobatoss
blog.daniellobato.me
daniellobato.me

GPG: http://keys.gnupg.net/pks/lookup?op=get&search=0x7A92D6DD38D6DE30