insights-api-common 3.6.0 → 3.7.0
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.
- checksums.yaml +4 -4
- data/lib/insights/api/common/application_controller_mixins/parameters.rb +4 -1
- data/lib/insights/api/common/application_controller_mixins/request_parameter_validation.rb +0 -16
- data/lib/insights/api/common/graphql/association_loader.rb +12 -5
- data/lib/insights/api/common/graphql/generator.rb +8 -0
- data/lib/insights/api/common/graphql/templates/model_type.erb +1 -1
- data/lib/insights/api/common/graphql/templates/query_type.erb +9 -3
- data/lib/insights/api/common/paginated_response.rb +12 -3
- data/lib/insights/api/common/paginated_response_v2.rb +38 -0
- data/lib/insights/api/common/version.rb +1 -1
- metadata +3 -3
- data/lib/insights/api/common/rbac/utilities.rb +0 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 81867b9d7ef11af5730e42563f88c156e7f51c2ef9e474990942f68535483fee
|
4
|
+
data.tar.gz: bba675752b9788c7fff6e4375b2301217a063f291713d32694a49227c8bed981
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40c3eab1a34663c42f8d85416d5b30d32d2066235dbe7bf86992860aa6a8ebd551b0c800ec80b0d8b82ef69e17707a79e2eeed4737205f678fe0c35fe41489ee
|
7
|
+
data.tar.gz: fc3a7e98ca9a116e94347ba8a09892628299c37571fa940063f5ddd95915ffc633b76170c4b738943c0ab3c4f4484f45f790e68a19494339b0876a3426232202
|
@@ -16,7 +16,10 @@ module Insights
|
|
16
16
|
def safe_params_for_list
|
17
17
|
check_if_openapi_enabled
|
18
18
|
# :limit & :offset can be passed in for pagination purposes, but shouldn't show up as params for filtering purposes
|
19
|
-
@safe_params_for_list ||=
|
19
|
+
@safe_params_for_list ||= begin
|
20
|
+
sort_by_default = (params[:sort_by].kind_of?(String) || params[:sort_by].kind_of?(Array)) ? [] : {}
|
21
|
+
params.merge(params_for_polymorphic_subcollection).permit(*permitted_params, :filter => {}, :sort_by => sort_by_default)
|
22
|
+
end
|
20
23
|
end
|
21
24
|
|
22
25
|
def permitted_params
|
@@ -5,22 +5,6 @@ module Insights
|
|
5
5
|
module RequestParameterValidation
|
6
6
|
def self.included(other)
|
7
7
|
other.include(OpenapiEnabled)
|
8
|
-
|
9
|
-
other.before_action(:validate_request_parameters)
|
10
|
-
end
|
11
|
-
|
12
|
-
private
|
13
|
-
|
14
|
-
def validate_request_parameters
|
15
|
-
api_version = self.class.send(:api_version)[1..-1].sub(/x/, ".")
|
16
|
-
|
17
|
-
api_doc.try(
|
18
|
-
:validate_parameters!,
|
19
|
-
request.method,
|
20
|
-
request.path,
|
21
|
-
api_version,
|
22
|
-
params.slice(:sort_by)
|
23
|
-
)
|
24
8
|
end
|
25
9
|
end
|
26
10
|
end
|
@@ -3,12 +3,13 @@ module Insights
|
|
3
3
|
module Common
|
4
4
|
module GraphQL
|
5
5
|
class AssociationLoader < ::GraphQL::Batch::Loader
|
6
|
-
attr_reader :args, :association_name, :model
|
6
|
+
attr_reader :args, :association_name, :graphql_options, :model
|
7
7
|
|
8
|
-
def initialize(model, association_name, args = {})
|
8
|
+
def initialize(model, association_name, args = {}, graphql_options = {})
|
9
9
|
@model = model
|
10
10
|
@association_name = association_name
|
11
11
|
@args = args
|
12
|
+
@graphql_options = graphql_options
|
12
13
|
end
|
13
14
|
|
14
15
|
def cache_key(record)
|
@@ -24,9 +25,15 @@ module Insights
|
|
24
25
|
def read_association(record)
|
25
26
|
recs = GraphQL::AssociatedRecords.new(record.public_send(association_name))
|
26
27
|
recs = GraphQL.search_options(recs, args)
|
27
|
-
|
28
|
-
|
29
|
-
|
28
|
+
if graphql_options[:use_pagination_v2] == true
|
29
|
+
PaginatedResponseV2.new(
|
30
|
+
:base_query => recs, :request => nil, :limit => args[:limit], :offset => args[:offset]
|
31
|
+
).records
|
32
|
+
else
|
33
|
+
PaginatedResponse.new(
|
34
|
+
:base_query => recs, :request => nil, :limit => args[:limit], :offset => args[:offset]
|
35
|
+
).records
|
36
|
+
end
|
30
37
|
end
|
31
38
|
end
|
32
39
|
end
|
@@ -79,6 +79,14 @@ module Insights
|
|
79
79
|
end
|
80
80
|
|
81
81
|
def self.init_schema(request, schema_overlay = {})
|
82
|
+
base_init_schema(request, { :use_pagination_v2 => false }, schema_overlay)
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.init_schema_v2(request, schema_overlay = {})
|
86
|
+
base_init_schema(request, { :use_pagination_v2 => true }, schema_overlay)
|
87
|
+
end
|
88
|
+
|
89
|
+
def self.base_init_schema(request, graphql_options, schema_overlay = {})
|
82
90
|
api_version = ::Insights::API::Common::GraphQL.version(request)
|
83
91
|
version_namespace = "V#{api_version.tr('.', 'x')}"
|
84
92
|
openapi_doc = ::Insights::API::Common::OpenApi::Docs.instance[api_version]
|
@@ -27,7 +27,7 @@
|
|
27
27
|
preload :<%= associations %>
|
28
28
|
|
29
29
|
resolve lambda { |obj, args, _ctx|
|
30
|
-
::Insights::API::Common::GraphQL::AssociationLoader.new(<%= klass_name.constantize %>, "<%= associations %>", args).load(obj)
|
30
|
+
::Insights::API::Common::GraphQL::AssociationLoader.new(<%= klass_name.constantize %>, "<%= associations %>", args, graphql_options).load(obj)
|
31
31
|
}
|
32
32
|
end
|
33
33
|
<% end %>
|
@@ -40,9 +40,15 @@ QueryType = ::GraphQL::ObjectType.define do
|
|
40
40
|
openapi_doc.definitions[openapi_schema_name]).apply
|
41
41
|
end
|
42
42
|
scope = ::Insights::API::Common::GraphQL.search_options(scope, args)
|
43
|
-
|
44
|
-
|
45
|
-
|
43
|
+
if <%= graphql_options[:use_pagination_v2] %> == true
|
44
|
+
::Insights::API::Common::PaginatedResponseV2.new(
|
45
|
+
base_query: scope, request: nil, limit: args[:limit], offset: args[:offset], sort_by: args[:sort_by]
|
46
|
+
).records
|
47
|
+
else
|
48
|
+
::Insights::API::Common::PaginatedResponse.new(
|
49
|
+
base_query: scope, request: nil, limit: args[:limit], offset: args[:offset], sort_by: args[:sort_by]
|
50
|
+
).records
|
51
|
+
end
|
46
52
|
}
|
47
53
|
end
|
48
54
|
end
|
@@ -10,6 +10,7 @@ module Insights
|
|
10
10
|
@limit = (limit || 100).to_i.clamp(1, 1000)
|
11
11
|
@offset = (offset || 0).to_i.clamp(0, Float::INFINITY)
|
12
12
|
@sort_by = sort_by
|
13
|
+
validate_sort_by
|
13
14
|
end
|
14
15
|
|
15
16
|
def records
|
@@ -96,12 +97,20 @@ module Insights
|
|
96
97
|
sort_attr, sort_order = selection.split(':')
|
97
98
|
sort_order ||= "asc"
|
98
99
|
arel = model.arel_attribute(sort_attr)
|
99
|
-
|
100
|
-
arel = arel.desc if sort_order == "desc"
|
101
|
-
arel
|
100
|
+
(sort_order == "desc") ? arel.desc : arel.asc
|
102
101
|
end
|
103
102
|
end
|
104
103
|
end
|
104
|
+
|
105
|
+
def validate_sort_by
|
106
|
+
return unless sort_by.present?
|
107
|
+
raise ArgumentError, "Invalid sort_by parameter specified \"#{sort_by}\"" unless sort_by.kind_of?(String) || sort_by.kind_of?(Array)
|
108
|
+
Array(sort_by).each { |key| validate_sort_by_directive(key) }
|
109
|
+
end
|
110
|
+
|
111
|
+
def validate_sort_by_directive(key)
|
112
|
+
raise ArgumentError, "Invalid sort_by directive specified \"#{key}\"" unless key.match?(/^[a-z\\-_]+(:asc|:desc)?$/)
|
113
|
+
end
|
105
114
|
end
|
106
115
|
end
|
107
116
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Insights
|
2
|
+
module API
|
3
|
+
module Common
|
4
|
+
class PaginatedResponseV2 < PaginatedResponse
|
5
|
+
attr_reader :limit, :offset, :sort_by
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
def sort_by_options(model)
|
10
|
+
@sort_by_options ||= begin
|
11
|
+
sort_options = []
|
12
|
+
return sort_options if sort_by.blank?
|
13
|
+
|
14
|
+
sort_by.each do |sort_attr, sort_order|
|
15
|
+
sort_order = "asc" if sort_order.blank?
|
16
|
+
arel = model.arel_attribute(sort_attr)
|
17
|
+
arel = (sort_order == "desc") ? arel.desc : arel.asc
|
18
|
+
sort_options << arel
|
19
|
+
end
|
20
|
+
sort_options
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def validate_sort_by
|
25
|
+
return unless sort_by.present?
|
26
|
+
raise ArgumentError, "Invalid sort_by parameter specified \"#{sort_by}\"" unless sort_by.kind_of?(ActionController::Parameters) || sort_by.kind_of?(Hash)
|
27
|
+
|
28
|
+
sort_by.each { |sort_attr, sort_order| validate_sort_by_directive(sort_attr, sort_order) }
|
29
|
+
end
|
30
|
+
|
31
|
+
def validate_sort_by_directive(sort_attr, sort_order)
|
32
|
+
order = sort_order.blank? ? "asc" : sort_order
|
33
|
+
raise ArgumentError, "Invalid sort_by directive specified \"#{sort_attr}=#{sort_order}\"" unless sort_attr.match?(/^[a-z\\-_]+$/) && order.match?(/^(asc|desc)$/)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: insights-api-common
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Insights Authors
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-03-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: acts_as_tenant
|
@@ -359,12 +359,12 @@ files:
|
|
359
359
|
- lib/insights/api/common/open_api/serializer.rb
|
360
360
|
- lib/insights/api/common/option_redirect_enhancements.rb
|
361
361
|
- lib/insights/api/common/paginated_response.rb
|
362
|
+
- lib/insights/api/common/paginated_response_v2.rb
|
362
363
|
- lib/insights/api/common/rbac/access.rb
|
363
364
|
- lib/insights/api/common/rbac/policies.rb
|
364
365
|
- lib/insights/api/common/rbac/roles.rb
|
365
366
|
- lib/insights/api/common/rbac/seed.rb
|
366
367
|
- lib/insights/api/common/rbac/service.rb
|
367
|
-
- lib/insights/api/common/rbac/utilities.rb
|
368
368
|
- lib/insights/api/common/rbac/validate_groups.rb
|
369
369
|
- lib/insights/api/common/request.rb
|
370
370
|
- lib/insights/api/common/routing.rb
|
@@ -1,30 +0,0 @@
|
|
1
|
-
module Insights
|
2
|
-
module API
|
3
|
-
module Common
|
4
|
-
module RBAC
|
5
|
-
module Utilities
|
6
|
-
def validate_groups
|
7
|
-
Service.call(RBACApiClient::GroupApi) do |api|
|
8
|
-
uuids = SortedSet.new
|
9
|
-
Service.paginate(api, :list_groups, {}).each { |group| uuids << group.uuid }
|
10
|
-
missing = @group_uuids - uuids
|
11
|
-
raise Insights::API::Common::InvalidParameter, "The following group uuids are missing #{missing.to_a.join(",")}" unless missing.empty?
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def unique_name(resource_id, group_id)
|
16
|
-
"#{@app_name}-#{@resource_name}-#{resource_id}-group-#{group_id}"
|
17
|
-
end
|
18
|
-
|
19
|
-
def parse_ids_from_name(name)
|
20
|
-
@regexp ||= Regexp.new("#{@app_name}-#{@resource_name}-(?<resource_id>.*)-group-(?<group_uuid>.*)")
|
21
|
-
result = @regexp.match(name)
|
22
|
-
if result
|
23
|
-
[result[:resource_id], result[:group_uuid]]
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|