Skip to content

Commit

Permalink
cli-akeys-pools - add subscription to a key through CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
iNecas committed Oct 21, 2011
1 parent e221a94 commit 66c7d5a
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 6 deletions.
10 changes: 10 additions & 0 deletions cli/src/katello/client/api/activation_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ def update(self, keyId, environmentId, name, description, templateId):
path = "/api/activation_keys/%s/" % keyId
return self.server.PUT(path, {'activation_key': keyData})[1]

def add_pool(self, keyId, poolid):
path = "/api/activation_keys/%s/pools" % keyId
attrs = { "poolid": poolid }
return self.server.POST(path, attrs)[1]

def remove_pool(self, keyId, poolid):
path = "/api/activation_keys/%s/pools" % keyId
attrs = { "poolid": poolid }
return self.server.DELETE(path, attrs)[1]

def delete(self, keyId):
path = "/api/activation_keys/%s/" % keyId
return self.server.DELETE(path)[1]
12 changes: 10 additions & 2 deletions cli/src/katello/client/core/activation_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ def setup_parser(self):
self.parser.add_option('--template', dest='template',
help=_("new template name eg: servers"))

self.parser.add_option('--add_subscription', dest='add_poolid', action='append',
help=_("add a pool to the activation key"))
def check_options(self):
self.require_option('name')
self.require_option('org')
Expand All @@ -203,6 +205,7 @@ def run(self):
newKeyName = self.get_option('new_name')
keyDescription = self.get_option('description')
templateName = self.get_option('template')
add_poolids = self.get_option('add_poolid') or []

organization = get_organization(orgName)
if not organization: return os.EX_DATAERR
Expand All @@ -216,13 +219,18 @@ def run(self):
keys = self.api.activation_keys_by_organization(organization['cp_key'], keyName)
if len(keys) == 0:
return os.EX_DATAERR
key = keys[0]

try:
templateId = self.get_template_id(keys[0]['environment_id'], templateName)
templateId = self.get_template_id(key['environment_id'], templateName)
except OptionException:
print _("Could not find template [ %s ]") % (templateName)
return os.EX_DATAERR
key = self.api.update(keys[0]['id'], environment['id'] if environment != None else None, newKeyName, keyDescription, templateId)
key = self.api.update(key['id'], environment['id'] if environment != None else None, newKeyName, keyDescription, templateId)

for poolid in add_poolids:
self.api.add_pool(key['id'], poolid)

if key != None:
print _("Successfully updated activation key [ %s ]") % key['name']
return os.EX_OK
Expand Down
13 changes: 12 additions & 1 deletion src/app/controllers/api/activation_keys_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class Api::ActivationKeysController < Api::ApiController
before_filter :verify_presence_of_organization_or_environment, :only => [:index]
before_filter :find_environment, :only => [:index, :create]
before_filter :find_organization, :only => [:index]
before_filter :find_activation_key, :only => [:show, :update, :destroy]
before_filter :find_activation_key, :only => [:show, :update, :destroy, :add_pool]
before_filter :find_pool, :only => [:add_pool]
before_filter :authorize

def rules
Expand All @@ -27,6 +28,7 @@ def rules
:show => read_test,
:create => manage_test,
:update => manage_test,
:add_pool => manage_test,
:destroy => manage_test
}
end
Expand Down Expand Up @@ -56,6 +58,11 @@ def update
render :json => ActivationKey.find(@activation_key.id)
end

def add_pool
@activation_key.pools << @pool unless @activation_key.pools.include?(@pool)
render :json => @activation_key.id
end

def destroy
@activation_key.destroy
render :text => _("Deleted activation key '#{params[:id]}'"), :status => 204
Expand Down Expand Up @@ -83,6 +90,10 @@ def find_activation_key
@activation_key
end

def find_pool
@pool = KTPool.find_by_organization_and_id(@activation_key.organization, params[:poolid])
end

def verify_presence_of_organization_or_environment
return if params.has_key?(:organization_id) or params.has_key?(:environment_id)
raise HttpErrors::BadRequest, _("Either organization id or environment id needs to be specified")
Expand Down
17 changes: 16 additions & 1 deletion src/app/models/glue/candlepin/pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ module Glue::Candlepin::Pool
def self.included(base)
base.send :include, LazyAccessor
base.send :include, InstanceMethods
base.send :extend, ClassMethods

base.class_eval do
lazy_accessor :productName, :startDate, :endDate, :consumed, :quantity, :attrs,
lazy_accessor :productName, :startDate, :endDate, :consumed, :quantity, :attrs, :owner,
:initializer => lambda {
json = Candlepin::Pool.get(cp_id)
# symbol "attributes" is reserved by Rails and cannot be used
Expand All @@ -33,6 +34,15 @@ def self.included(base)
end
end

module ClassMethods
def find_by_organization_and_id(organization, pool_id)
pool = KTPool.find_by_cp_id(pool_id) || KTPool.new(Candlepin::Pool.get(pool_id))
if pool.organization == organization
return pool
end
end
end

module InstanceMethods

def initialize(attrs = nil)
Expand All @@ -44,6 +54,7 @@ def initialize(attrs = nil)
@consumed = attrs["consumed"]
@quantity = attrs["quantity"]
@attrs = attrs["attributes"]
@owner = attrs["owner"]
super(:cp_id => attrs['id'])
else
super
Expand All @@ -58,5 +69,9 @@ def endDate_as_datetime
DateTime.parse(endDate)
end

def organization
Organization.find_by_name(owner["key"])
end

end
end
4 changes: 3 additions & 1 deletion src/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,9 @@ def matches?(request)
resources :templates, :only => [:index]
end

resources :activation_keys, :only => [:show, :update, :destroy]
resources :activation_keys do
post :pools, :action => :add_pool, :on => :member
end
resources :packages, :only => [:show]
resources :errata, :only => [:show]
resources :distributions, :only => [:show]
Expand Down
44 changes: 43 additions & 1 deletion src/spec/controllers/api/activation_keys_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
before(:each) do
login_user_api
@request.env["HTTP_ACCEPT"] = "application/json"
disable_org_orchestration

@organization = Organization.new do |o|
@organization = Organization.create! do |o|
o.id = 1234
o.name = "org-1234"
o.cp_key = "org-1234"
end

@environment = KTEnvironment.new(:organization => @organization)
Expand Down Expand Up @@ -155,6 +158,45 @@
end
end

context "pools in an activation key" do
before(:each) do
@environment = KTEnvironment.create!(:organization => @organization, :name => "Dev", :prior => @organization.locker)
@activation_key.organization = @organization
@activation_key.environment = @environment
@activation_key.save!
@pool_in_activation_key = KTPool.create!(:cp_id => "pool-123")
@pool_not_in_activation_key = KTPool.create!(:cp_id => "pool-456")
KeyPool.create!(:activation_key_id => @activation_key.id, :pool_id => @pool_in_activation_key.id, :allocated => 1)
ActivationKey.stub!(:find).and_return(@activation_key)
KTPool.stub(:find_by_organization_and_id => @pool_not_in_activation_key)
end

let(:action) {:add_pool }
let(:req) { post :add_pool, :id => 123, :poolid => "pool-456" }
let(:authorized_user) { user_with_manage_permissions }
let(:unauthorized_user) { user_without_manage_permissions }
it_should_behave_like "protected action"

it "should add pool to the activation key" do
req
@activation_key.pools.should include(@pool_in_activation_key)
@activation_key.pools.should include(@pool_not_in_activation_key)
@activation_key.pools.should have(2).pools
end

it "should not add a pool that is already in the activation key" do
KTPool.stub(:find_by_organization_and_id => @pool_in_activation_key)
req
@activation_key.pools.should include(@pool_in_activation_key)
@activation_key.pools.should have(1).pool
end

it "should return updated key" do
put :update, :id => 123, :activation_key => {:name => 'blah'}
response.body.should == @activation_key.to_json
end
end

context "delete an activation key" do
before(:each) do
ActivationKey.stub!(:find).and_return(@activation_key)
Expand Down
37 changes: 37 additions & 0 deletions src/spec/models/pool_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#
# Copyright 2011 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public
# License as published by the Free Software Foundation; either version
# 2 of the License (GPLv2) or (at your option) any later version.
# There is NO WARRANTY for this software, express or implied,
# including the implied warranties of MERCHANTABILITY,
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
# have received a copy of GPLv2 along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.

require 'spec_helper'

describe KTPool do

context "Find pool by organization and id" do
it "should return pool that is in the organization" do
@pool_id = ProductTestData::POOLS[:id]
disable_org_orchestration
cp_owner = ProductTestData::POOLS[:owner]
@organization = Organization.create!(:name => cp_owner[:displayName], :cp_key => cp_owner[:key])
Candlepin::Pool.should_receive(:get).with(@pool_id).and_return(ProductTestData::POOLS)
KTPool.find_by_organization_and_id(@organization, @pool_id).cp_id.should == ProductTestData::POOLS[:id]
end

it "should return nil if the pool doesn't belong to the organization" do
@pool_id = ProductTestData::POOLS[:id]
disable_org_orchestration
cp_owner = { :displayName => "Another Org", :key => "another_org" }
@organization = Organization.create!(:name => cp_owner[:displayName], :cp_key => cp_owner[:key])
Candlepin::Pool.should_receive(:get).with(@pool_id).and_return(ProductTestData::POOLS)
KTPool.find_by_organization_and_id(@organization, @pool_id).should be_nil
end
end

end

0 comments on commit 66c7d5a

Please sign in to comment.