2020-09-30 08:09:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module API
|
2020-10-14 20:08:42 -04:00
|
|
|
class Unleash < ::API::Base
|
2020-09-30 08:09:53 -04:00
|
|
|
include PaginationParams
|
|
|
|
|
2020-10-30 14:08:56 -04:00
|
|
|
feature_category :feature_flags
|
|
|
|
|
2020-09-30 08:09:53 -04:00
|
|
|
namespace :feature_flags do
|
|
|
|
resource :unleash, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
|
|
|
params do
|
|
|
|
requires :project_id, type: String, desc: 'The ID of a project'
|
|
|
|
optional :instance_id, type: String, desc: 'The Instance ID of Unleash Client'
|
|
|
|
optional :app_name, type: String, desc: 'The Application Name of Unleash Client'
|
|
|
|
end
|
|
|
|
route_param :project_id do
|
|
|
|
before do
|
|
|
|
authorize_by_unleash_instance_id!
|
|
|
|
end
|
|
|
|
|
|
|
|
get do
|
|
|
|
# not supported yet
|
|
|
|
status :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Get a list of features (deprecated, v2 client support)'
|
|
|
|
get 'features' do
|
2022-07-13 08:09:54 -04:00
|
|
|
if ::Feature.enabled?(:cache_unleash_client_api, project)
|
|
|
|
present_feature_flags
|
|
|
|
else
|
|
|
|
present :version, 1
|
|
|
|
present :features, feature_flags, with: ::API::Entities::UnleashFeature
|
|
|
|
end
|
2020-09-30 08:09:53 -04:00
|
|
|
end
|
|
|
|
|
2022-07-28 05:10:54 -04:00
|
|
|
# We decrease the urgency of this endpoint until the maxmemory issue of redis-cache has been resolved.
|
|
|
|
# See https://gitlab.com/gitlab-org/gitlab/-/issues/365575#note_1033611872 for more information.
|
2020-09-30 08:09:53 -04:00
|
|
|
desc 'Get a list of features'
|
2022-07-28 05:10:54 -04:00
|
|
|
get 'client/features', urgency: :low do
|
2022-07-13 08:09:54 -04:00
|
|
|
if ::Feature.enabled?(:cache_unleash_client_api, project)
|
|
|
|
present_feature_flags
|
|
|
|
else
|
|
|
|
present :version, 1
|
|
|
|
present :features, feature_flags, with: ::API::Entities::UnleashFeature
|
|
|
|
end
|
2020-09-30 08:09:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
post 'client/register' do
|
|
|
|
# not supported yet
|
|
|
|
status :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
post 'client/metrics' do
|
|
|
|
# not supported yet
|
|
|
|
status :ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
helpers do
|
2022-07-13 08:09:54 -04:00
|
|
|
def present_feature_flags
|
|
|
|
present_cached feature_flags_client,
|
|
|
|
with: ::API::Entities::Unleash::ClientFeatureFlags,
|
|
|
|
cache_context: -> (client) { client.unleash_api_cache_key }
|
|
|
|
end
|
|
|
|
|
2020-09-30 08:09:53 -04:00
|
|
|
def project
|
|
|
|
@project ||= find_project(params[:project_id])
|
|
|
|
end
|
|
|
|
|
2022-07-13 08:09:54 -04:00
|
|
|
def feature_flags_client
|
|
|
|
strong_memoize(:feature_flags_client) do
|
|
|
|
client = Operations::FeatureFlagsClient.find_for_project_and_token(project, unleash_instance_id)
|
|
|
|
client.unleash_app_name = unleash_app_name if client
|
|
|
|
client
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-30 08:09:53 -04:00
|
|
|
def unleash_instance_id
|
|
|
|
env['HTTP_UNLEASH_INSTANCEID'] || params[:instance_id]
|
|
|
|
end
|
|
|
|
|
|
|
|
def unleash_app_name
|
|
|
|
env['HTTP_UNLEASH_APPNAME'] || params[:app_name]
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_by_unleash_instance_id!
|
2022-07-13 08:09:54 -04:00
|
|
|
unauthorized! unless feature_flags_client
|
2020-09-30 08:09:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def feature_flags
|
|
|
|
return [] unless unleash_app_name.present?
|
|
|
|
|
2021-06-15 08:10:11 -04:00
|
|
|
Operations::FeatureFlag.for_unleash_client(project, unleash_app_name)
|
2021-06-01 05:09:36 -04:00
|
|
|
end
|
2020-09-30 08:09:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|