foreman-katello-engine 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -26,24 +26,11 @@ Modified functionality
26
26
 
27
27
  ### Host Group forms
28
28
 
29
- New tab 'Katello' is added to the host groups form, exposing a select
30
- box for katello environment and activation keys.
29
+ New tab 'Activation Keys is added to the host groups form, exposing a select
30
+ box for activation keys.
31
31
 
32
- Katello Environment is modeled by Foreman organizations. For now, a
33
- naming convention makes it possible to identify Katello specific
34
- organizations like this:
35
-
36
- ```
37
- Foreman Organization | Katello Entity
38
- -----------------------------------------------------------------
39
- kt-[ACME_Corporation] | organization ACME_Corporation
40
- kt-[ACME_Corporation][Dev] | environment Dev in ACME_Corporation
41
- ```
42
-
43
- These organizations are hidden from the 'Organizations' tab and put
44
- into the 'Katello Environment' select box. Before submitting the form,
45
- the changed are reflected back to the Foreman organizations
46
- checkboxes.
32
+ Also environments select box is improved to group Foreman environments
33
+ by Katello organizations and environments.
47
34
 
48
35
  The activation keys are modeled similarly using foreman custom params.
49
36
  The value entered into 'Activation Keys' is saved as
@@ -54,24 +41,6 @@ The activation keys are autocompleted for better user experience and
54
41
  the list of products for each used activation key is displayed at the
55
42
  bottom of the Katello tab.
56
43
 
57
- ### Katello-friendly provisioning
58
-
59
- To register a provisioned system to katello a new provisioning
60
- template snippet needs to be crated, lets call it katello:
61
-
62
- ```
63
- <% if @host.params['kt_activation_keys'] %>
64
- rpm -ivh "http://katello.example.com/pub/candlepin-cert-consumer-katello.example.com-1.0-1.noarch.rpm"
65
- subscription-manager register --org "<%= @host.params['kt_org'] %>" --activationkey "<%= @host.params['kt_activation_keys'] %>"
66
- <% end %>
67
- ```
68
-
69
- and reference it from the kickstart template:
70
-
71
- ```
72
- <%= snippets "katello" %>
73
- ```
74
-
75
44
  Testing
76
45
  -------
77
46
 
@@ -0,0 +1,12 @@
1
+ module ForemanKatelloEngine
2
+ class ActivationKeysController < ApplicationController
3
+ def index
4
+ environment = ::Environment.find_by_id(params[:environment_id])
5
+ raise "Selected environment has now Katello connection" unless environment.katello_id
6
+ kt_org_label, kt_env_label, kt_cv_label = environment.katello_id.split('/')
7
+
8
+ ak_data = ForemanKatelloEngine::Bindings.activation_keys_to_subscriptions(kt_org_label, kt_env_label, kt_cv_label)
9
+ render :status => 200, :json => ak_data, :content_type => 'application/json'
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,35 @@
1
+ module ForemanKatelloEngine
2
+ module Api
3
+ class EnvironmentsController < ::Api::V2::BaseController
4
+
5
+ def show
6
+ @environment = ForemanKatelloEngine::Environment.find(params[:org], params[:env], params[:content_view])
7
+ if @environment
8
+ render :template => "api/v1/environments/show"
9
+ else
10
+ render_error 'not_found', :status => :not_found
11
+ end
12
+ end
13
+
14
+ def create
15
+ begin
16
+ organization = Organization.find(params[:org_id]) if params[:org_id].present?
17
+ @environment = ForemanKatelloEngine::Environment.create!(params[:org],
18
+ params[:env],
19
+ params[:content_view],
20
+ params[:content_view_id]) do |env|
21
+ env.organizations << organization if organization
22
+ end
23
+ rescue ForemanKatelloEngine::Environment::Conflict => e
24
+ render_error 'standard_error', :status => 409, :locals => { :exception => e }
25
+ return
26
+ rescue ArgumentError => e
27
+ render_error 'standard_error', :status => 422, :locals => { :exception => e }
28
+ return
29
+ end
30
+ render :template => "api/v1/environments/show"
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -15,5 +15,53 @@ module ForemanKatelloEngine
15
15
  "kt_activation_keys"
16
16
  end
17
17
 
18
+ def envs_by_kt_org
19
+ ::Environment.all.find_all(&:katello_id).group_by do |env|
20
+ if env.katello_id
21
+ env.katello_id.split('/').first
22
+ end
23
+ end
24
+ end
25
+
26
+ def envs_by_kt_env(envs)
27
+ envs.group_by do |env|
28
+ env.katello_id.split('/')[1]
29
+ end
30
+ end
31
+
32
+ def envs_without_kt_org
33
+ envs_without_kt_org = ::Environment.all.reject(&:katello_id)
34
+ end
35
+
36
+ def envs_without_kt_org_options
37
+ options_from_collection_for_select(envs_without_kt_org,
38
+ :id,
39
+ :to_label,
40
+ (@host || @hostgroup).environment_id)
41
+ end
42
+
43
+ def grouped_env_options
44
+ envs_by_org = envs_by_kt_org.reduce({}) do |hash, (org, envs)|
45
+ hash.update(org => envs_by_kt_env(envs))
46
+ end
47
+ grouped_options = envs_by_org.sort_by(&:first).map do |org, envs_by_env|
48
+ optgroup = %[<optgroup label="#{org}">]
49
+ opts = envs_by_env.sort_by(&:first).map do |kt_env, envs|
50
+ envs.sort_by(&:katello_id).map do |env|
51
+ selected = env.id == (@host || @hostgroup).environment_id ? "selected" : ""
52
+ if cv = env.katello_id.split('/')[2]
53
+ %[<option value="#{env.id}" class="kt-cv" #{selected}>#{cv}</option>]
54
+ else
55
+ %[<option value="#{env.id}" class="kt-env" #{selected}>#{kt_env}</option>]
56
+ end
57
+ end.join
58
+ end.join
59
+ optgroup << opts
60
+ optgroup << '</optgroup>'
61
+ end.join
62
+
63
+ return grouped_options.html_safe + envs_without_kt_org_options
64
+ end
65
+
18
66
  end
19
67
  end
@@ -0,0 +1,39 @@
1
+ module ForemanKatelloEngine
2
+ class Environment
3
+
4
+ class Conflict < StandardError; end
5
+
6
+ def self.find(org_label, env_label, content_view_label)
7
+ katello_id = generate_katello_id(org_label, env_label, content_view_label)
8
+ ::Environment.where(:katello_id => katello_id).first
9
+ end
10
+
11
+ def self.create!(org_label, env_label, content_view_label, content_view_id)
12
+ katello_id = generate_katello_id(org_label, env_label, content_view_label)
13
+ ::Environment.transaction do
14
+ if existing_env = ::Environment.where(:katello_id => katello_id).first
15
+ raise Conflict, "environment already exists: #{existing_env.id} - #{existing_env.name}"
16
+ end
17
+ ::Environment.create! do |env|
18
+ env.name = generate_name(org_label, env_label, content_view_label, content_view_id)
19
+ env.katello_id = katello_id
20
+ yield env if block_given?
21
+ end
22
+ end
23
+ end
24
+
25
+ def self.generate_katello_id(org_label, env_label, content_view_label)
26
+ raise ArgumentError, "org_label has to be specified" if org_label.blank?
27
+ raise ArgumentError, "env_label has to be specified" if env_label.blank?
28
+ [org_label, env_label, content_view_label].reject(&:blank?).join('/')
29
+ end
30
+
31
+ # content_view_id provides the uniqueness of the name
32
+ def self.generate_name(org_label, env_label, content_view_label, content_view_id)
33
+ name = ["KT", org_label, env_label, content_view_label, content_view_id].reject(&:blank?).join('_')
34
+ return name.gsub('-','_')
35
+ end
36
+
37
+
38
+ end
39
+ end
@@ -0,0 +1,14 @@
1
+ class Setting::Katello < ::Setting
2
+
3
+ def self.load_defaults
4
+ return unless (self.table_exists? rescue false)
5
+ BLANK_ATTRS << "katello_url"
6
+ Setting.transaction do
7
+ [
8
+ self.set('katello_url', 'url of a Katello instance', 'https://localhost/katello'),
9
+ ].compact.each { |s| self.create s.update(:category => "Setting::Katello")}
10
+ end
11
+ true
12
+ end
13
+
14
+ end
@@ -1,9 +1,17 @@
1
1
  Deface::Override.new(:virtual_path => "hostgroups/_form",
2
2
  :name => "add_activation_keys_tab",
3
3
  :insert_after => 'ul.nav > code[erb-silent]:contains("show_organization_tab?") ~ code[erb-silent]:contains("end")',
4
- :partial => 'activation_keys/host_tab')
4
+ :partial => 'foreman_katello_engine/activation_keys/host_tab')
5
5
 
6
6
  Deface::Override.new(:virtual_path => "hostgroups/_form",
7
7
  :name => "add_activation_keys_tab_pane",
8
8
  :insert_after => 'div.tab-content > code[erb-silent]:contains("show_organization_tab?") ~ code[erb-silent]:contains("end")',
9
- :partial => 'activation_keys/host_tab_pane')
9
+ :partial => 'foreman_katello_engine/activation_keys/host_tab_pane')
10
+ Deface::Override.new(:virtual_path => "hostgroups/_form",
11
+ :name => "hostgroups_update_environments_select",
12
+ :replace => 'code[erb-loud]:contains("select_f"):contains(":environment_id")',
13
+ :partial => 'foreman_katello_engine/activation_keys/host_environment_select')
14
+ Deface::Override.new(:virtual_path => "hosts/_form",
15
+ :name => "hosts_update_environments_select",
16
+ :replace => 'code[erb-loud]:contains("select_f"):contains(":environment_id")',
17
+ :partial => 'foreman_katello_engine/activation_keys/host_environment_select')
@@ -0,0 +1,13 @@
1
+ <style>
2
+ option.kt-env { margin-left: 0em; }
3
+ option.kt-cv { margin-left: 1em; }
4
+ </style>
5
+
6
+ <%= field(f, :environment_id, {:include_blank => true}) do
7
+ f.select :environment_id,
8
+ grouped_env_options,
9
+ {:include_blank =>true},
10
+ {:onchange => 'update_puppetclasses(this);',
11
+ :"data-url" => environment_selected_hostgroups_path,
12
+ :'data-host-id' => (@host && @host.id)}
13
+ end %>
@@ -0,0 +1,3 @@
1
+ <li id="activation_keys_tab">
2
+ <a href="#activation_keys" data-toggle="tab">Activation Keys</a>
3
+ </li>
@@ -5,10 +5,17 @@ $(function() {
5
5
  var availableActivationKeys = {};
6
6
 
7
7
  function ktLoadActivationKeys() {
8
+ if(ktSelectedOrgEnvCv()[0]) {
9
+ ktAkTab().show();
10
+ } else {
11
+ ktAkTab().hide();
12
+ return; //no Katello-specific env selected
13
+ }
14
+
8
15
  $("#ak-subscriptions-info").hide();
9
16
  $("#ak-subscriptions-spinner").show();
10
- var url = foreman_url('/activation_keys');
11
- var environmentId = $("#kt_environment_id").val();
17
+ var url = foreman_url('/foreman_katello_engine/activation_keys');
18
+ var environmentId = $("#hostgroup_environment_id").val();
12
19
  if(environmentId) {
13
20
  $.ajax({
14
21
  type: 'get',
@@ -43,8 +50,8 @@ $(function() {
43
50
  }
44
51
 
45
52
  function ktOrganizations() {
46
- return $.grep($("#organizations label.organization"), function (el) {
47
- return $(el).text().trim().match(/^kt-/)
53
+ return $.grep($("#organizations #hostgroup_organization_ids option"), function (el) {
54
+ return $(el).text().trim().match(/^KT-/)
48
55
  });
49
56
  }
50
57
 
@@ -63,54 +70,27 @@ $(function() {
63
70
  });
64
71
  }
65
72
 
66
- function ktOrganizationsToKtEnv() {
67
- var ktOrgs = [], ktOrgsToEnvs = [], selectedEnvId;
68
- var ktEnvironmentSelect = $("#kt_environment_id");
69
- $.each(ktOrganizations(), function (i, label) {
70
- var input = $(label).find("input[ type = 'checkbox' ]");
71
- var frOrgName = $(label).text().trim();
72
- var frOrgId = input.val();
73
- var match = ktParseOrgName(frOrgName);
74
- if(match) {
75
- if(input.attr('checked')) {
76
- selectedEnvId = frOrgId;
77
- }
78
- var ktOrg = match[1]; ktEnv = match[2];
79
- if(ktOrgs.indexOf(ktOrg) == -1) {
80
- ktOrgs.push(ktOrg);
81
- ktOrgsToEnvs[ktOrg] = [];
82
- }
83
- ktOrgsToEnvs[ktOrg].push([frOrgId, ktEnv]);
84
- }
85
- $(this).parent().hide();
86
- });
73
+ function ktSelectedOrgEnvCv() {
74
+ var selectedEnvId = $("#hostgroup_environment_id").val();
75
+ var selectedOption = $("#hostgroup_environment_id option[ value ='" + selectedEnvId + "' ]");
87
76
 
88
- ktEnvironmentSelect.empty();
89
- var content = '<option value=""></option>';
90
- $.each(ktOrgs, function (i, org) {
91
- content += '<optgroup label="' + org + '">';
92
- $.each(ktOrgsToEnvs[org], function (j, env) {
93
- // we don't quotre the value beacuse of deface bug
94
- content += '<option value=';
95
- content += env[0];
96
-
97
- if(env[0] == selectedEnvId) {
98
- content += " selected";
99
- }
100
- content += '>' + env[1] + '</option>';
101
- });
102
- content += '</optgroup>';
103
- });
104
- ktEnvironmentSelect.append(content);
77
+ var ktEnvOption, ktCvOption, ktOrgLabel, ktEnvLabel, ktCvLabel;
78
+ if(selectedOption.hasClass('kt-cv')) {
79
+ ktCvOption = selectedOption;
80
+ ktEnvOption = ktCvOption.prevAll('option.kt-env').first();
81
+ } else if(selectedOption.hasClass('kt-env')) {
82
+ ktEnvOption = selectedOption;
83
+ }
105
84
 
106
- }
85
+ if(ktEnvOption) {
86
+ ktEnvLabel = ktEnvOption.text();
87
+ ktOrgLabel = ktEnvOption.parent().attr('label');
88
+ }
107
89
 
108
- function ktSelectedOrgAndEnv() {
109
- var selectedEnvId = $("#kt_environment_id").val();
110
- var orgCheckbox = $("#organizations label.organization input[ type = 'checkbox' ][ value = '" + selectedEnvId + "' ]");
111
- if(orgCheckbox) {
112
- return ktParseOrgName(orgCheckbox.parent().text().trim());
90
+ if(ktCvOption) {
91
+ ktCvLabel = ktCvOption.text();
113
92
  }
93
+ return [ktOrgLabel, ktEnvLabel, ktCvLabel];
114
94
  }
115
95
 
116
96
  function ktSetParam(name, value) {
@@ -130,19 +110,25 @@ $(function() {
130
110
  }
131
111
 
132
112
  function ktEnvToParam() {
133
- var orgAndEnv = ktSelectedOrgAndEnv() || [];
134
- ktSetParam('kt_org', orgAndEnv[1]);
135
- ktSetParam('kt_env', orgAndEnv[2]);
113
+ var orgEnvCv = ktSelectedOrgEnvCv() || [];
114
+ ktSetParam('kt_org', orgEnvCv[0]);
115
+ ktSetParam('kt_env', orgEnvCv[1]);
116
+ ktSetParam('kt_cv', orgEnvCv[2]);
136
117
  }
137
118
 
138
119
  function ktEnvToFrOrganizations() {
139
- var selectedEnvId = $("#kt_environment_id").val();
140
- $.each(ktOrganizations(), function (i, label) {
141
- var input = $(label).find("input[ type = 'checkbox' ]");
142
- if(selectedEnvId == input.val()) {
143
- input.prop("checked", true);
120
+ var orgEnvCv = ktSelectedOrgEnvCv() || [];
121
+
122
+ var orgName = "";
123
+ if(orgEnvCv[0]) {
124
+ orgName = 'KT-[' + orgEnvCv[0] + ']';
125
+ }
126
+
127
+ $.each(ktOrganizations(), function (i, option) {
128
+ if(orgName == $(option).text().trim()) {
129
+ $("#hostgroup_organization_ids").multiSelect('select', $(option).val());
144
130
  } else {
145
- input.prop("checked", false);
131
+ $("#hostgroup_organization_ids").multiSelect('deselect', $(option).val());
146
132
  }
147
133
  });
148
134
  }
@@ -181,10 +167,13 @@ $(function() {
181
167
  $("#ak-subscriptions-spinner").hide();
182
168
  }
183
169
 
170
+ function ktAkTab() {
171
+ return $('li#activation_keys_tab');
172
+ }
173
+
184
174
  function ktOnLoad() {
185
175
  ktHideParams();
186
176
  ktParamToAkInput();
187
- ktOrganizationsToKtEnv();
188
177
  ktLoadActivationKeys();
189
178
  };
190
179
 
@@ -232,7 +221,7 @@ $(function() {
232
221
  $(this).autocomplete( "search" );
233
222
  }});
234
223
 
235
- $("#kt_environment_id").change(ktLoadActivationKeys);
224
+ $("#hostgroup_environment_id").change(ktLoadActivationKeys);
236
225
 
237
226
  $("#ak_refresh_subscriptions").click(function () {
238
227
  ktLoadActivationKeys();
@@ -242,11 +231,8 @@ $(function() {
242
231
  });
243
232
 
244
233
  <% end %>
245
- <div class="tab-pane" id="katello">
246
- <%= field(nil, "Katello Environment", :help_inline => "the names of Katello organization and environment will be available in templates as @host.params['kt_org'] and @host.params['kt_env']") do
247
- select_tag(:kt_environment_id, [])
248
- end %>
249
- <%= field(nil, "Activation Keys", :help_inline => "comma separated values. The value will be available in templates as @host.params['#{kt_ak_label}']") do
234
+ <div class="tab-pane" id="activation_keys">
235
+ <%= field(f, "Activation Keys", :help_inline => "comma separated values. The value will be available in templates as @host.params['#{kt_ak_label}']") do
250
236
  text_field_tag("kt_activation_keys", "", :style => 'width: 98%')
251
237
  end %>
252
238
  <div class="alert alert-info">
data/config/routes.rb CHANGED
@@ -1,3 +1,9 @@
1
1
  Rails.application.routes.draw do
2
- resources :activation_keys, :only => [:index]
2
+
3
+ namespace :foreman_katello_engine do
4
+ resources :activation_keys, :only => [:index]
5
+ namespace :api do
6
+ resources :environments, :only => [:show, :create]
7
+ end
8
+ end
3
9
  end
@@ -0,0 +1,10 @@
1
+ class UpdateEnvironmentsAddKatelloId < ActiveRecord::Migration
2
+ def up
3
+ add_column :environments, :katello_id, :string
4
+ add_index :environments, :katello_id
5
+ end
6
+
7
+ def down
8
+ remote_column :environment, :katello_id
9
+ end
10
+ end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "foreman-katello-engine"
3
- s.version = "0.0.5"
3
+ s.version = "0.0.6"
4
4
 
5
5
  s.authors = ["Katello"]
6
6
  s.date = "2013-03-04"
@@ -20,14 +20,29 @@ module ForemanKatelloEngine
20
20
  KatelloApi::Resources::Environment.new(client_config)
21
21
  end
22
22
 
23
+ def content_view
24
+ KatelloApi::Resources::ContentView.new(client_config)
25
+ end
26
+
23
27
  def activation_key
24
28
  KatelloApi::Resources::ActivationKey.new(client_config)
25
29
  end
26
30
 
27
- def activation_keys_to_subscriptions(org_name, env_name)
28
- environments, _ = self.environment.index('organization_id' => org_name, 'name' => env_name)
31
+ def activation_keys_to_subscriptions(org_label, env_label, content_view_label = nil)
32
+ ak_query = {}
33
+ if content_view_label
34
+ content_views, _ = self.content_view.index('organization_id' => org_label,
35
+ 'label' => content_view_label)
36
+ if content_view = content_views.first
37
+ ak_query['content_view_id'] = content_view['id']
38
+ end
39
+ end
40
+ environments, _ = self.environment.index('organization_id' => org_label, 'name' => env_label)
29
41
  if environment = environments.first
30
- activation_keys, _ = self.activation_key.index('environment_id' => environment['id'])
42
+ ak_query['environment_id'] = environment['id']
43
+ end
44
+ if ak_query.any?
45
+ activation_keys, _ = self.activation_key.index(ak_query)
31
46
  return activation_keys.reduce({}) do |h, ak|
32
47
  h.update(ak['name'] => ak['pools'].map { |pool| pool['productName'] })
33
48
  end
@@ -7,10 +7,11 @@ module ForemanKatelloEngine
7
7
  end
8
8
 
9
9
  config.after_initialize do
10
+ if (Setting::Katello.table_exists? rescue false)
11
+ Setting::Katello.load_defaults
12
+ end
10
13
  require 'foreman_katello_engine/bindings'
11
- require 'foreman_katello_engine/settings'
12
14
  require 'foreman_katello_engine/renderer'
13
- ForemanKatelloEngine::Settings.initialize_settings
14
15
  end
15
16
 
16
17
  initializer 'foreman_katello_engine.helper' do |app|
@@ -1,13 +0,0 @@
1
- module ForemanKatelloEngine
2
- class Settings
3
-
4
- def self.initialize_settings
5
- # proceed only if db set up correctly
6
- Setting.first rescue return
7
- [
8
- Foreman::DefaultSettings::Loader.set('katello_url', 'url of a Katello instance', 'https://localhost/katello')
9
- ].each { |s| Foreman::DefaultSettings::Loader.create(s.update(:category => "General")) }
10
- end
11
-
12
- end
13
- end
@@ -1,17 +1,35 @@
1
1
  require 'test_helper'
2
2
  require 'mocha/setup'
3
3
 
4
- class ActivationKeysControllerTest < ActionController::TestCase
4
+ module ForemanKatelloEngine
5
+ class ActivationKeysControllerTest < ActionController::TestCase
5
6
 
6
- def aks_to_subscriptions_data
7
- {"ak1" => ["prod1", "prod2"], "ak2" => ["prod3", "prod4"]}
8
- end
7
+ before do
8
+ setup_users
9
+ end
9
10
 
10
- def test_index
11
- ForemanKatelloEngine::Bindings.stubs(:activation_keys_to_subscriptions).returns(aks_to_subscriptions_data)
12
- get :index, {}, set_session_user
13
- assert_response :success
14
- assert_equal aks_to_subscriptions_data, JSON.parse(response.body)
15
- end
11
+ let :environment do
12
+ ::Environment.create! do |env|
13
+ env.name = 'DevEnv'
14
+ env.katello_id = 'ACME/Dev/CV1'
15
+ end
16
+ end
17
+
18
+ let :aks_to_subscriptions_data do
19
+ {"ak1" => ["prod1", "prod2"], "ak2" => ["prod3", "prod4"]}
20
+ end
21
+
22
+ describe "#index" do
16
23
 
24
+ it "loads activation keys from Katello" do
25
+ ForemanKatelloEngine::Bindings.expects(:activation_keys_to_subscriptions).
26
+ with('ACME', 'Dev', 'CV1').returns(aks_to_subscriptions_data)
27
+ get :index, { :environment_id => environment.id }, set_session_user
28
+ assert_response :success
29
+ assert_equal aks_to_subscriptions_data, JSON.parse(response.body)
30
+ end
31
+
32
+ end
33
+
34
+ end
17
35
  end
@@ -0,0 +1,96 @@
1
+ require 'test_helper'
2
+ require 'mocha/setup'
3
+
4
+ module ForemanKatelloEngine
5
+ module Api
6
+ class EnvironmentsControllerTest < ActionController::TestCase
7
+
8
+ before do
9
+ setup_users
10
+ environment
11
+ end
12
+
13
+ let :environment do
14
+ ::Environment.create! do |env|
15
+ env.name = 'DevEnv'
16
+ env.katello_id = 'ACME/Dev/CV1'
17
+ end
18
+ end
19
+
20
+ let :organization do
21
+ Organization.create! do |org|
22
+ org.name = 'ACME'
23
+ end
24
+ end
25
+
26
+ describe "#show" do
27
+
28
+ it "show an environment based on Katello org, env and CV label" do
29
+ # id => show means id is not really used
30
+ get :show, { :id => "show", :org => 'ACME', :env => 'Dev', :content_view => 'CV1' }, set_session_user
31
+ assert_response :success
32
+ response_env = JSON.parse(response.body)["environment"]
33
+ response_env["id"].must_equal environment.id
34
+ end
35
+
36
+ end
37
+
38
+
39
+ describe "#create" do
40
+
41
+ it "creates an environment based on Katello org and env" do
42
+ # id => show means id is not really used
43
+ post :create, { :org => 'ACME', :env => 'Dev', :content_view_id => 'env'}, set_session_user
44
+ assert_response :success
45
+ response_env = JSON.parse(response.body)["environment"]
46
+ env = ::Environment.find(response_env["id"])
47
+ env.name.must_equal "KT_ACME_Dev_env"
48
+ env.katello_id.must_equal "ACME/Dev"
49
+ end
50
+
51
+ it "assigns the environment to org when org_id provided" do
52
+ # id => show means id is not really used
53
+ create_params = {
54
+ :org => 'ACME',
55
+ :env => 'Dev',
56
+ :content_view_id => 'env',
57
+ :org_id => organization.id
58
+ }
59
+ post :create, create_params, set_session_user
60
+ assert_response :success
61
+ response_env = JSON.parse(response.body)["environment"]
62
+ env = ::Environment.find(response_env["id"])
63
+ env.name.must_equal "KT_ACME_Dev_env"
64
+ env.katello_id.must_equal "ACME/Dev"
65
+ env.organizations.must_include organization
66
+ end
67
+
68
+ it "creates an environment based on Katello org, env and CV label" do
69
+ post :create, { :org => 'ACME', :env => 'Dev', :content_view => 'CV2', :content_view_id => 2 }, set_session_user
70
+ assert_response :success
71
+ response_env = JSON.parse(response.body)["environment"]
72
+ env = ::Environment.find(response_env["id"])
73
+ env.name.must_equal "KT_ACME_Dev_CV2_2"
74
+ env.katello_id.must_equal "ACME/Dev/CV2"
75
+ end
76
+
77
+ it "requires env to be set" do
78
+ post :create, { :org => 'ACME'}, set_session_user
79
+ assert_response 422
80
+ end
81
+
82
+ it "requires org to be set" do
83
+ post :create, {}, set_session_user
84
+ assert_response 422
85
+ end
86
+
87
+ it "refused to create the same env twice" do
88
+ post :create, { :org => 'ACME', :env => 'Dev', :content_view => 'CV1', :content_view_id => 2 }, set_session_user
89
+ assert_response 409
90
+ end
91
+
92
+ end
93
+
94
+ end
95
+ end
96
+ end
@@ -4,14 +4,14 @@ require 'mocha/setup'
4
4
  class BindingsTest < ActiveSupport::TestCase
5
5
 
6
6
  def setup
7
- Foreman::DefaultSettings::Loader.load
8
- ForemanKatelloEngine::Settings.initialize_settings
7
+ Setting::Auth.load_defaults
8
+ Setting::Katello.load_defaults
9
9
  end
10
10
 
11
11
  test 'client lib setting' do
12
- Setting['katello_url'] = 'https://example.com/katello'
13
- Setting['oauth_consumer_key'] = 'key'
14
- Setting['oauth_consumer_secret'] = 'secret'
12
+ Setting::Katello['katello_url'] = 'https://example.com/katello'
13
+ Setting::Auth['oauth_consumer_key'] = 'key'
14
+ Setting::Auth['oauth_consumer_secret'] = 'secret'
15
15
  config = ForemanKatelloEngine::Bindings.environment.config
16
16
  assert_equal 'https://example.com/katello', config[:base_url]
17
17
  assert_equal 'key', config[:oauth][:consumer_key]
@@ -2,12 +2,9 @@ require 'test_helper'
2
2
 
3
3
  class SettingsTest < ActiveSupport::TestCase
4
4
 
5
- def setup
6
- #Setting.where(:name => 'katello_url').delete_all
7
- end
8
-
9
5
  test "katello specific settings" do
10
- ForemanKatelloEngine::Settings.initialize_settings
11
- assert_equal 'https://localhost/katello', Setting['katello_url']
6
+ Setting::Katello.load_defaults
7
+ assert_equal 'https://localhost/katello', Setting::Katello['katello_url']
12
8
  end
9
+
13
10
  end
data/test/test_helper.rb CHANGED
@@ -5,6 +5,7 @@ ENV["RAILS_ENV"] ||= 'test'
5
5
  require File.join("foreman_app/config/environment.rb")
6
6
 
7
7
  require 'test/unit'
8
+ require 'minitest/spec'
8
9
  require 'foreman-katello-engine'
9
10
  require 'rails/test_help'
10
11
 
@@ -26,3 +27,12 @@ end
26
27
  def set_session_user
27
28
  SETTINGS[:login] ? {:user => User.admin.id, :expires_at => 5.minutes.from_now} : {}
28
29
  end
30
+
31
+ def setup_users
32
+ User.current = users :admin
33
+ user = User.find_by_login("one")
34
+ @request.session[:user] = user.id
35
+ @request.session[:expires_at] = 5.minutes.from_now
36
+ user.roles = [Role.find_by_name('Anonymous'), Role.find_by_name('Viewer')]
37
+ user.save!
38
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman-katello-engine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -55,18 +55,22 @@ files:
55
55
  - LICENSE
56
56
  - README.md
57
57
  - Rakefile
58
- - app/controllers/activation_keys_controller.rb
58
+ - app/controllers/foreman_katello_engine/activation_keys_controller.rb
59
+ - app/controllers/foreman_katello_engine/api/environments_controller.rb
59
60
  - app/helpers/foreman_katello_engine/hosts_and_hostgroups_helper.rb
60
61
  - app/helpers/foreman_katello_engine/katello_urls_helper.rb
61
- - app/models/activation_key.rb
62
+ - app/models/foreman_katello_engine/environment.rb
63
+ - app/models/setting/katello.rb
62
64
  - app/overrides/add_activation_keys_input.rb
63
- - app/views/activation_keys/_host_tab.html.erb
64
- - app/views/activation_keys/_host_tab_pane.html.erb
65
+ - app/views/foreman_katello_engine/activation_keys/_host_environment_select.html.erb
66
+ - app/views/foreman_katello_engine/activation_keys/_host_tab.html.erb
67
+ - app/views/foreman_katello_engine/activation_keys/_host_tab_pane.html.erb
65
68
  - app/views/unattended/kickstart-katello.erb
66
69
  - app/views/unattended/snippets/_katello_registration.erb
67
70
  - config/routes.rb
68
71
  - db/migrate/20130416152224_add_katello_templates.rb
69
72
  - db/migrate/20130418105901_add_katello_ptables.rb
73
+ - db/migrate/20130419110332_update_environments_add_katello_id.rb
70
74
  - foreman-katello-engine.gemspec
71
75
  - lib/foreman-katello-engine.rb
72
76
  - lib/foreman_katello_engine.rb
@@ -76,6 +80,7 @@ files:
76
80
  - lib/foreman_katello_engine/settings.rb
77
81
  - script/rails
78
82
  - test/functional/activation_keys_controller_test.rb
83
+ - test/functional/api/environments_controller_test.rb
79
84
  - test/lib/bindings_test.rb
80
85
  - test/lib/settings_test.rb
81
86
  - test/test_helper.rb
@@ -1,12 +0,0 @@
1
- class ActivationKeysController < ApplicationController
2
- def index
3
- if org = Organization.find_by_id(params[:environment_id])
4
- if match = org.name.match(/\Akt-\[(.*)\]\[(.*)\]\Z/)
5
- org_name, env_name = match[1], match[2]
6
- end
7
- end
8
-
9
- ak_data = ForemanKatelloEngine::Bindings.activation_keys_to_subscriptions(org_name, env_name)
10
- render :status => 200, :json => ak_data, :content_type => 'application/json'
11
- end
12
- end
@@ -1,5 +0,0 @@
1
- class ActivationKey
2
- def self.complete_for(params)
3
- ["key_one", "key_two", "key_three"]
4
- end
5
- end
@@ -1,3 +0,0 @@
1
- <li>
2
- <a href="#katello" data-toggle="tab">Katello</a>
3
- </li>