foreman_puppet 0.1.0 → 1.0.0.rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/concerns/foreman_puppet/extensions/api_base_controller.rb +28 -0
  3. data/app/controllers/concerns/foreman_puppet/extensions/api_template_combinations_controller.rb +1 -1
  4. data/app/controllers/foreman_puppet/api/v2/config_groups_controller.rb +5 -1
  5. data/app/controllers/foreman_puppet/api/v2/environments_controller.rb +1 -2
  6. data/app/controllers/foreman_puppet/api/v2/host_classes_controller.rb +5 -1
  7. data/app/controllers/foreman_puppet/api/v2/hostgroup_classes_controller.rb +5 -1
  8. data/app/controllers/foreman_puppet/api/v2/override_values_controller.rb +1 -2
  9. data/app/controllers/foreman_puppet/api/v2/{base_controller.rb → puppet_base_controller.rb} +2 -3
  10. data/app/controllers/foreman_puppet/api/v2/{lookups_common_controller.rb → puppet_lookups_common_controller.rb} +6 -2
  11. data/app/controllers/foreman_puppet/api/v2/puppetclasses_controller.rb +5 -1
  12. data/app/controllers/foreman_puppet/api/v2/smart_class_parameters_controller.rb +1 -2
  13. data/app/controllers/foreman_puppet/puppetclasses_controller.rb +9 -2
  14. data/app/graphgl/concerns/foreman_puppet/mutations/hosts/create_extensions.rb +29 -0
  15. data/app/graphgl/concerns/foreman_puppet/types/host_extensions.rb +13 -0
  16. data/app/graphgl/concerns/foreman_puppet/types/hostgroup_extensions.rb +13 -0
  17. data/app/graphgl/concerns/foreman_puppet/types/location_extensions.rb +14 -0
  18. data/app/graphgl/concerns/foreman_puppet/types/organization_extensions.rb +14 -0
  19. data/app/graphgl/foreman_puppet/types/environment.rb +20 -0
  20. data/app/graphgl/foreman_puppet/types/puppetclass.rb +20 -0
  21. data/app/helpers/foreman_puppet/puppetclasses_and_environments_helper.rb +1 -1
  22. data/app/helpers/foreman_puppet/puppetclasses_helper.rb +2 -2
  23. data/app/models/concerns/foreman_puppet/extensions/host.rb +6 -4
  24. data/app/models/concerns/foreman_puppet/extensions/hostgroup.rb +6 -2
  25. data/app/models/concerns/foreman_puppet/extensions/operatingsystem.rb +1 -1
  26. data/app/models/concerns/foreman_puppet/extensions/provisioning_template.rb +16 -10
  27. data/app/models/concerns/foreman_puppet/extensions/taxonomy.rb +1 -1
  28. data/app/models/foreman_puppet/environment.rb +2 -0
  29. data/app/models/foreman_puppet/host_puppet_facet.rb +1 -1
  30. data/app/models/foreman_puppet/hostgroup_puppet_facet.rb +1 -1
  31. data/app/models/{concerns/foreman_puppet/host_common.rb → foreman_puppet/puppet_facet_common.rb} +1 -1
  32. data/app/models/foreman_puppet/puppetclass.rb +2 -0
  33. data/app/{views/api/v2/template_combinations/base.json.erb → prepend_views/api/v2/template_combinations/base.json.rabl} +0 -0
  34. data/app/services/foreman_puppet/puppet_class_importer.rb +3 -4
  35. data/app/views/foreman_puppet/config_groups/index.html.erb +2 -2
  36. data/app/views/foreman_puppet/config_groups/welcome.html.erb +12 -11
  37. data/app/views/foreman_puppet/environments/welcome.html.erb +3 -15
  38. data/app/views/foreman_puppet/puppetclass_lookup_keys/welcome.html.erb +10 -8
  39. data/config/initializers/api_reroute.rb +13 -0
  40. data/db/migrate/20200803113903_migrate_host_type_in_host_config_groups.foreman_puppet.rb +16 -10
  41. data/lib/foreman_puppet.rb +1 -1
  42. data/lib/foreman_puppet/engine.rb +48 -16
  43. data/lib/foreman_puppet/register.rb +5 -0
  44. data/lib/foreman_puppet/version.rb +1 -1
  45. data/locale/en/foreman_puppet.edit.po +927 -0
  46. data/locale/en/foreman_puppet.po.time_stamp +0 -0
  47. data/locale/en/foreman_puppet.pox +0 -0
  48. data/locale/messages.mo +0 -0
  49. data/test/controllers/foreman_puppet/api/v2/config_groups_controller_test.rb +7 -3
  50. data/test/controllers/foreman_puppet/api/v2/environments_controller_test.rb +17 -15
  51. data/test/controllers/foreman_puppet/api/v2/hosts_controller_test.rb +2 -1
  52. data/test/controllers/foreman_puppet/api/v2/lookups_common_controller_test.rb +1 -1
  53. data/test/controllers/foreman_puppet/api/v2/puppetclasses_controller_test.rb +3 -0
  54. data/test/controllers/foreman_puppet/api/v2/smart_class_parameters_controller_test.rb +7 -1
  55. data/test/controllers/foreman_puppet/api/v2/template_combinations_controller_test.rb +2 -2
  56. data/test/controllers/foreman_puppet/config_groups_controller_test.rb +7 -3
  57. data/test/controllers/foreman_puppet/environments_controller_test.rb +1 -1
  58. data/test/controllers/foreman_puppet/puppetclass_lookup_keys_controller_test.rb +2 -2
  59. data/test/controllers/foreman_puppet/puppetclasses_controller_test.rb +5 -6
  60. data/test/factories/host_puppet_enhancements.rb +6 -6
  61. data/test/graphql/mutations/hosts/create_mutation_test.rb +66 -0
  62. data/test/graphql/queries/environment_query_test.rb +54 -0
  63. data/test/graphql/queries/environments_query_test.rb +43 -0
  64. data/test/{qraphql → graphql}/queries/host_puppet_query_test.rb +3 -8
  65. data/test/{qraphql → graphql}/queries/hostgroup_puppet_query_test.rb +1 -6
  66. data/test/graphql/queries/location_query_test.rb +52 -0
  67. data/test/graphql/queries/organization_query_test.rb +52 -0
  68. data/test/graphql/queries/puppetclass_query_test.rb +68 -0
  69. data/test/graphql/queries/puppetclasses_query_test.rb +43 -0
  70. data/test/integration/foreman_puppet/environment_js_test.rb +4 -1
  71. data/test/integration/foreman_puppet/host_js_test.rb +1 -1
  72. data/test/integration/foreman_puppet/puppetclass_js_test.rb +1 -0
  73. data/test/models/foreman_puppet/config_group_class_test.rb +3 -1
  74. data/test/models/foreman_puppet/environment_test.rb +2 -0
  75. data/test/models/foreman_puppet/host_test.rb +3 -2
  76. data/test/models/foreman_puppet/hostgroup_puppet_facet_test.rb +2 -3
  77. data/test/models/foreman_puppet/provisioning_template_test.rb +23 -23
  78. data/test/models/foreman_puppet/smart_proxy_test.rb +1 -1
  79. data/test/models/foreman_puppet/user_test.rb +13 -5
  80. data/test/services/foreman_puppet/host_info_providers/puppet_info_test.rb +0 -36
  81. data/test/unit/foreman_puppet/puppet_class_importer_test.rb +3 -1
  82. data/webpack/index.js +3 -4
  83. data/webpack/src/Components/Environments/Welcome.js +37 -0
  84. metadata +78 -50
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 22506c7ebf919c047dded9e11b60b405e4a3e7c1456cd2d5125312ef156d3e7f
4
- data.tar.gz: 5249dd5ee17f4627b159fb10952ce36d0b2f0bcee212df9d5cd82ba18898259c
3
+ metadata.gz: f41d4919f84bff60f30a23f1e05ceae83e468bb4d005ed781f635ee39a33246c
4
+ data.tar.gz: 208fbb6587882e138874b60cf19d1c76d5470f3ffd1cf5c7487f5a48983a5e51
5
5
  SHA512:
6
- metadata.gz: 99998691aeb424b63ceca8f140049677b3ba2c8a280c21972425b5f3efa1f4b024e75b67e386735dbb6024dfcd6d8bc4dbc3c226cef6e6d87f23c6872fd29c75
7
- data.tar.gz: 5587134660de8b3a8bded2014307d5f515d7adcacfe6c32a90336289735ecf2123cc3b67f0231c6a1088bbe1ab11b56009cf0488449a61178e4be067bb330f0b
6
+ metadata.gz: 3d4c7c3a02a500d554a0c11fd35f3f47f37e3accb2503270f82d920c47b1bd6aa751a76b698105d508ef24c04757fc7cc5f08a412a0221cfc2e51a553523a567
7
+ data.tar.gz: bae382d27974df26cfebed71b5b89963229f97b962e75fecd53bf19a9b35edbff829b9d823fde79c05f8859341982cc24b4e3f3a3b35c58d1c81f5802cd49c01
@@ -0,0 +1,28 @@
1
+ module ForemanPuppet
2
+ module Extensions
3
+ module ApiBaseController
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ prepend PatchMethods
8
+
9
+ before_action :prepare_views
10
+ end
11
+
12
+ def prepare_views
13
+ prepend_view_path ForemanPuppet::Engine.root.join('app', 'prepend_views')
14
+ end
15
+
16
+ module PatchMethods
17
+ def resource_name(resource = controller_name)
18
+ case resource
19
+ when 'environment'
20
+ 'foreman_puppet/environment'
21
+ else
22
+ super
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -6,7 +6,7 @@ module ForemanPuppet
6
6
  included do
7
7
  if ForemanPuppet.extracted_from_core?
8
8
  apipie_update_methods(%i[index create show update]) do
9
- param :environment_id, String, desc: N_('ID of environment')
9
+ param :environment_id, nil, desc: N_('ID of environment')
10
10
  end
11
11
  end
12
12
 
@@ -1,9 +1,13 @@
1
1
  module ForemanPuppet
2
2
  module Api
3
3
  module V2
4
- class ConfigGroupsController < BaseController
4
+ class ConfigGroupsController < ::ForemanPuppet::Api::V2::PuppetBaseController
5
5
  include ForemanPuppet::Parameters::ConfigGroup
6
6
 
7
+ resource_description do
8
+ api_base_url '/foreman_puppet/api'
9
+ end
10
+
7
11
  wrap_parameters ConfigGroup, include: config_group_params_filter.accessible_attributes(parameter_filter_context)
8
12
 
9
13
  before_action :find_resource, only: %i[show update destroy]
@@ -1,12 +1,11 @@
1
1
  module ForemanPuppet
2
2
  module Api
3
3
  module V2
4
- class EnvironmentsController < BaseController
4
+ class EnvironmentsController < ::ForemanPuppet::Api::V2::PuppetBaseController
5
5
  include ForemanPuppet::Api::ImportPuppetclassesCommonController
6
6
  include ForemanPuppet::Parameters::Environment
7
7
 
8
8
  resource_description do
9
- api_version '2'
10
9
  api_base_url '/foreman_puppet/api'
11
10
  end
12
11
 
@@ -1,7 +1,11 @@
1
1
  module ForemanPuppet
2
2
  module Api
3
3
  module V2
4
- class HostClassesController < V2::BaseController
4
+ class HostClassesController < ::ForemanPuppet::Api::V2::PuppetBaseController
5
+ resource_description do
6
+ api_base_url '/foreman_puppet/api'
7
+ end
8
+
5
9
  before_action :find_host, only: %i[index create destroy]
6
10
  before_action :find_puppetclass, only: %i[create destroy]
7
11
 
@@ -1,7 +1,11 @@
1
1
  module ForemanPuppet
2
2
  module Api
3
3
  module V2
4
- class HostgroupClassesController < V2::BaseController
4
+ class HostgroupClassesController < ::ForemanPuppet::Api::V2::PuppetBaseController
5
+ resource_description do
6
+ api_base_url '/foreman_puppet/api'
7
+ end
8
+
5
9
  before_action :find_hostgroup, only: %i[index create destroy]
6
10
 
7
11
  api :GET, '/hostgroups/:hostgroup_id/puppetclass_ids/', N_('List all Puppet class IDs for host group')
@@ -1,11 +1,10 @@
1
1
  module ForemanPuppet
2
2
  module Api
3
3
  module V2
4
- class OverrideValuesController < LookupsCommonController
4
+ class OverrideValuesController < ::ForemanPuppet::Api::V2::PuppetLookupsCommonController
5
5
  include Foreman::Controller::Parameters::LookupValue
6
6
 
7
7
  resource_description do
8
- api_version '2'
9
8
  api_base_url '/foreman_puppet/api'
10
9
  end
11
10
 
@@ -1,10 +1,9 @@
1
1
  module ForemanPuppet
2
2
  module Api
3
3
  module V2
4
- class BaseController < ::Api::V2::BaseController
4
+ class PuppetBaseController < ::Api::V2::BaseController
5
5
  resource_description do
6
- api_version '2'
7
- api_base_url '/foreman_puppet/api'
6
+ api_version 'v2'
8
7
  end
9
8
 
10
9
  before_action :show_deprecation_for_core_routes
@@ -1,7 +1,11 @@
1
1
  module ForemanPuppet
2
2
  module Api
3
3
  module V2
4
- class LookupsCommonController < ::Api::V2::BaseController
4
+ class PuppetLookupsCommonController < ::Api::V2::BaseController
5
+ resource_description do
6
+ api_version 'v2'
7
+ end
8
+
5
9
  before_action :find_environment, if: :environment_id?
6
10
  before_action :find_puppetclass, if: :puppetclass_id?
7
11
  before_action :find_host, if: :host_id?
@@ -45,7 +49,7 @@ module ForemanPuppet
45
49
 
46
50
  def find_smart_class_parameter
47
51
  id = params.key?('smart_class_parameter_id') ? params['smart_class_parameter_id'] : params['id']
48
- @smart_class_parameter = PuppetclassLookupKey.authorized(:view_external_parameters).smart_class_parameters.find_by(id: id.to_i) if id.to_i > 0
52
+ @smart_class_parameter = PuppetclassLookupKey.authorized(:view_external_parameters).smart_class_parameters.find_by(id: id.to_i) if id.to_i.positive?
49
53
  @smart_class_parameter ||= begin
50
54
  puppet_cond = { 'environment_classes.puppetclass_id' => @puppetclass.id } if @puppetclass
51
55
  env_cond = { 'environment_classes.environment_id' => @environment.id } if @environment
@@ -1,9 +1,13 @@
1
1
  module ForemanPuppet
2
2
  module Api
3
3
  module V2
4
- class PuppetclassesController < BaseController
4
+ class PuppetclassesController < ::ForemanPuppet::Api::V2::PuppetBaseController
5
5
  include ForemanPuppet::Parameters::Puppetclass
6
6
 
7
+ resource_description do
8
+ api_base_url '/foreman_puppet/api'
9
+ end
10
+
7
11
  before_action :find_optional_nested_object
8
12
  before_action :find_resource, only: %w[show update destroy]
9
13
 
@@ -1,11 +1,10 @@
1
1
  module ForemanPuppet
2
2
  module Api
3
3
  module V2
4
- class SmartClassParametersController < ForemanPuppet::Api::V2::LookupsCommonController
4
+ class SmartClassParametersController < ::ForemanPuppet::Api::V2::PuppetLookupsCommonController
5
5
  include ForemanPuppet::Parameters::PuppetclassLookupKey
6
6
 
7
7
  resource_description do
8
- api_version '2'
9
8
  api_base_url '/foreman_puppet/api'
10
9
  end
11
10
 
@@ -3,6 +3,8 @@ module ForemanPuppet
3
3
  include Foreman::Controller::AutoCompleteSearch
4
4
  include Foreman::Controller::Parameters::Host
5
5
  include Foreman::Controller::Parameters::Hostgroup
6
+ include ForemanPuppet::Extensions::ParametersHost
7
+ include ForemanPuppet::Extensions::ParametersHostgroup
6
8
  include ForemanPuppet::Parameters::Environment
7
9
  include ForemanPuppet::Parameters::Puppetclass
8
10
  include ForemanPuppet::EnvironmentsImport
@@ -93,11 +95,11 @@ module ForemanPuppet
93
95
  @obj.type = 'Host::Managed'
94
96
  end
95
97
  # puppetclass_ids and config_group_ids need to be removed so they don't cause automatic insertsgroup
96
- @obj.attributes = host_params('host')
98
+ @obj.attributes = host_params('host').tap { |params| strip_relation_ids(params) }
97
99
  elsif params['hostgroup']
98
100
  # hostgroup.id is assigned to params['host_id'] by host_edit.js#load_puppet_class_parameters
99
101
  @obj = Hostgroup.find(host_id)
100
- @obj.attributes = hostgroup_params('hostgroup')
102
+ @obj.attributes = hostgroup_params('hostgroup').tap { |params| strip_relation_ids(params) }
101
103
  end
102
104
  @obj
103
105
  end
@@ -110,5 +112,10 @@ module ForemanPuppet
110
112
  super
111
113
  end
112
114
  end
115
+
116
+ def strip_relation_ids(params)
117
+ return unless params[:puppet_attributes]
118
+ params[:puppet_attributes].except!(:puppetclass_ids, :config_group_ids)
119
+ end
113
120
  end
114
121
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanPuppet
4
+ module Mutations
5
+ module Hosts
6
+ module CreateExtensions
7
+ PUPPET_PARAMS = %i[
8
+ environment
9
+ puppetclasses
10
+ ].freeze
11
+
12
+ extend ActiveSupport::Concern
13
+
14
+ included do
15
+ argument :environment_id, GraphQL::Types::ID, loads: ForemanPuppet::Types::Environment
16
+ argument :puppetclass_ids, [GraphQL::Types::ID], loads: ForemanPuppet::Types::Puppetclass, as: :puppetclasses
17
+
18
+ private
19
+
20
+ def initialize_object(params)
21
+ host = super(params.except(*PUPPET_PARAMS))
22
+ host.build_puppet(params.slice(*PUPPET_PARAMS))
23
+ host
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanPuppet
4
+ module Types
5
+ module HostExtensions
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ belongs_to :environment, ForemanPuppet::Types::Environment
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanPuppet
4
+ module Types
5
+ module HostgroupExtensions
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ belongs_to :environment, ForemanPuppet::Types::Environment
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanPuppet
4
+ module Types
5
+ module LocationExtensions
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ has_many :environments, ForemanPuppet::Types::Environment
10
+ has_many :puppetclasses, ForemanPuppet::Types::Puppetclass
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanPuppet
4
+ module Types
5
+ module OrganizationExtensions
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ has_many :environments, ForemanPuppet::Types::Environment
10
+ has_many :puppetclasses, ForemanPuppet::Types::Puppetclass
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ module ForemanPuppet
2
+ module Types
3
+ class Environment < ::Types::BaseObject
4
+ model_class ForemanPuppet::Environment
5
+ description 'An Environment'
6
+
7
+ global_id_field :id
8
+ timestamps
9
+ field :name, String
10
+
11
+ has_many :locations, ::Types::Location
12
+ has_many :organizations, ::Types::Organization
13
+ has_many :puppetclasses, ForemanPuppet::Types::Puppetclass
14
+
15
+ def self.graphql_definition
16
+ super.tap { |type| type.instance_variable_set(:@name, 'ForemanPuppet::Environment') }
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ module ForemanPuppet
2
+ module Types
3
+ class Puppetclass < ::Types::BaseObject
4
+ model_class ForemanPuppet::Puppetclass
5
+ description 'A Puppetclass'
6
+
7
+ global_id_field :id
8
+ timestamps
9
+ field :name, String
10
+
11
+ has_many :environments, ForemanPuppet::Types::Environment
12
+ has_many :locations, ::Types::Location
13
+ has_many :organizations, ::Types::Organization
14
+
15
+ def self.graphql_definition
16
+ super.tap { |type| type.instance_variable_set(:@name, 'ForemanPuppet::Puppetclass') }
17
+ end
18
+ end
19
+ end
20
+ end
@@ -42,7 +42,7 @@ module ForemanPuppet
42
42
  mod_name,
43
43
  rel: 'popover',
44
44
  data: { content: mod_classes[mod_name].sort.join('<br>').html_safe,
45
- "original-title": format(n_('%{name} has %{num_tag} class', '%{name} has %{num_tag} classes', num), name: mod_name, num_tag: num_tag),
45
+ 'original-title': format(n_('%{name} has %{num_tag} class', '%{name} has %{num_tag} classes', num), name: mod_name, num_tag: num_tag),
46
46
  trigger: 'focus',
47
47
  container: 'body',
48
48
  html: true },
@@ -30,7 +30,7 @@ module ForemanPuppet
30
30
 
31
31
  def link_to_remove_function(text, options)
32
32
  options.delete_if { |key, _value| !options[key].to_s } # otherwise error during template render
33
- title = (_('Click to remove %s') % options[:"data-class-name"])
33
+ title = (_('Click to remove %s') % options[:'data-class-name'])
34
34
  link_to_function(text, 'tfm.classEditor.removePuppetClass(this)', options.merge!('data-original-title': title))
35
35
  end
36
36
 
@@ -43,7 +43,7 @@ module ForemanPuppet
43
43
 
44
44
  def link_to_add_function(text, options)
45
45
  link_to_function(text, 'tfm.classEditor.addPuppetClass(this)',
46
- options.merge('data-original-title': _('Click to add %s') % options[:"data-class-name"]))
46
+ options.merge('data-original-title': _('Click to add %s') % options[:'data-class-name']))
47
47
  end
48
48
 
49
49
  def options_for_puppetclass_selection(klass, type)
@@ -6,7 +6,9 @@ module ForemanPuppet
6
6
  included do
7
7
  prepend PrependedMethods
8
8
 
9
- unless ForemanPuppet.extracted_from_core?
9
+ if ForemanPuppet.extracted_from_core?
10
+ has_one :environment, through: :puppet, class_name: 'ForemanPuppet::Environment'
11
+ else
10
12
  env_assoc = reflect_on_association(:environment)
11
13
  env_assoc&.instance_variable_set(:@class_name, 'ForemanPuppet::Environment')
12
14
 
@@ -81,9 +83,9 @@ module ForemanPuppet
81
83
  kinds = template_kinds(provisioning)
82
84
  kinds.map do |kind|
83
85
  ProvisioningTemplate.find_template({ kind: kind.name,
84
- operatingsystem_id: operatingsystem_id,
85
- hostgroup_id: hostgroup_id,
86
- environment_id: puppet&.environment_id })
86
+ operatingsystem_id: operatingsystem_id,
87
+ hostgroup_id: hostgroup_id,
88
+ environment_id: puppet&.environment_id })
87
89
  end.compact
88
90
  end
89
91
 
@@ -8,8 +8,12 @@ module ForemanPuppet
8
8
  prepend PatchedClassMethods
9
9
  end
10
10
 
11
- env_assoc = reflect_on_association(:environment)
12
- env_assoc.instance_variable_set(:@class_name, 'ForemanPuppet::Environment')
11
+ if ForemanPuppet.extracted_from_core?
12
+ has_one :environment, through: :puppet, class_name: 'ForemanPuppet::Environment'
13
+ else
14
+ env_assoc = reflect_on_association(:environment)
15
+ env_assoc&.instance_variable_set(:@class_name, 'ForemanPuppet::Environment')
16
+ end
13
17
 
14
18
  include_in_clone puppet: %i[host_config_groups config_groups hostgroup_classes]
15
19
 
@@ -4,7 +4,7 @@ module ForemanPuppet
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  included do
7
- has_and_belongs_to_many :puppetclasses
7
+ has_and_belongs_to_many :puppetclasses, class_name: 'ForemanPuppet::Puppetclass'
8
8
  end
9
9
  end
10
10
  end
@@ -24,18 +24,24 @@ module ForemanPuppet
24
24
  module PrependedClassMethods
25
25
  def templates_by_template_combinations(templates, hosts_or_conditions)
26
26
  if hosts_or_conditions.is_a?(Hash)
27
- conditions = hosts_or_conditions
28
- conditions[:hostgroup_id] = Array.wrap(conditions[:hostgroup_id]) | [nil]
29
- conditions[:environment_id] = Array.wrap(conditions[:environment_id]) | [nil]
27
+ conditions = []
28
+ if hosts_or_conditions[:hostgroup_id] && hosts_or_conditions[:environment_id]
29
+ conditions << { hostgroup_id: Array.wrap(hosts_or_conditions[:hostgroup_id]), environment_id: Array.wrap(hosts_or_conditions[:environment_id]) }
30
+ end
31
+ conditions << { hostgroup_id: Array.wrap(hosts_or_conditions[:hostgroup_id]), environment_id: [nil] } if hosts_or_conditions[:hostgroup_id]
32
+ conditions << { hostgroup_id: [nil], environment_id: Array.wrap(hosts_or_conditions[:environment_id]) } if hosts_or_conditions[:environment_id]
30
33
  else
31
- conditions = {}
32
- conditions[:hostgroup_id] = hosts_or_conditions.pluck(:hostgroup_id) | [nil]
33
- conditions[:environment_id] = hosts_or_conditions.pluck(:environment_id) | [nil]
34
+ conditions = [{
35
+ hostgroup_id: hosts_or_conditions.pluck(:hostgroup_id) | [nil],
36
+ environment_id: hosts_or_conditions.joins(:puppet).pluck('host_puppet_facets.environment_id') | [nil],
37
+ }]
34
38
  end
35
- at = TemplateCombination.arel_table
36
- arel = at[:hostgroup_id].in(conditions[:hostgroup_id])
37
- arel = arel.and(at[:environment_id].in(conditions[:environment_id]))
38
- templates.joins(:template_combinations).where(arel).distinct
39
+ tpls = templates.where('1=0')
40
+ conditions.each do |cond|
41
+ tpls = templates.joins(:template_combinations).where(template_combinations: cond).distinct
42
+ return tpls if tpls.any?
43
+ end
44
+ tpls
39
45
  end
40
46
 
41
47
  def template_includes