Class: ContainerRegistry::GitlabApiClient

Inherits:
BaseClient
  • Object
show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
lib/container_registry/gitlab_api_client.rb

Constant Summary collapse

JSON_TYPE =
'application/json'
CANCEL_RESPONSE_STATUS_HEADER =
'status'
IMPORT_RESPONSES =
{
  200 => :already_imported,
  202 => :ok,
  400 => :bad_request,
  401 => :unauthorized,
  404 => :not_found,
  409 => :already_being_imported,
  424 => :pre_import_failed,
  425 => :already_being_imported,
  429 => :too_many_imports
}.freeze
REGISTRY_GITLAB_V1_API_FEATURE =
'gitlab_v1_api'

Constants inherited from BaseClient

BaseClient::ACCEPTED_TYPES, BaseClient::ACCEPTED_TYPES_RAW, BaseClient::CONTAINER_IMAGE_V1_TYPE, BaseClient::DOCKER_DISTRIBUTION_MANIFEST_LIST_V2_TYPE, BaseClient::DOCKER_DISTRIBUTION_MANIFEST_V2_TYPE, BaseClient::ERROR_CALLBACK_OPTIONS, BaseClient::OCI_DISTRIBUTION_INDEX_TYPE, BaseClient::OCI_MANIFEST_V1_TYPE, BaseClient::RETRY_EXCEPTIONS, BaseClient::RETRY_OPTIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods inherited from BaseClient

#initialize

Constructor Details

This class inherits a constructor from ContainerRegistry::BaseClient

Class Method Details

.deduplicated_size(path) ⇒ Object


30
31
32
33
34
# File 'lib/container_registry/gitlab_api_client.rb', line 30

def self.deduplicated_size(path)
  with_dummy_client(token_config: { type: :nested_repositories_token, path: path }) do |client|
    client.repository_details(path, sizing: :self_with_descendants)['size_bytes']
  end
end

.supports_gitlab_api?Boolean

Returns:

  • (Boolean)

24
25
26
27
28
# File 'lib/container_registry/gitlab_api_client.rb', line 24

def self.supports_gitlab_api?
  with_dummy_client(return_value_if_disabled: false) do |client|
    client.supports_gitlab_api?
  end
end

Instance Method Details

#cancel_repository_import(path, force: false) ⇒ Object


62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/container_registry/gitlab_api_client.rb', line 62

def cancel_repository_import(path, force: false)
  response = with_import_token_faraday do |faraday_client|
    faraday_client.delete(import_url_for(path)) do |req|
      req.params['force'] = true if force
    end
  end

  status = IMPORT_RESPONSES.fetch(response.status, :error)
  actual_state = response.body[CANCEL_RESPONSE_STATUS_HEADER]

  { status: status, migration_state: actual_state }
end

#import_repository(path) ⇒ Object


56
57
58
59
# File 'lib/container_registry/gitlab_api_client.rb', line 56

def import_repository(path)
  response = start_import_for(path, pre: false)
  IMPORT_RESPONSES.fetch(response.status, :error)
end

#import_status(path) ⇒ Object


76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/container_registry/gitlab_api_client.rb', line 76

def import_status(path)
  with_import_token_faraday do |faraday_client|
    response = faraday_client.get(import_url_for(path))

    # Temporary solution for https://gitlab.com/gitlab-org/gitlab/-/issues/356085#solutions
    # this will trigger a `retry_pre_import`
    break 'pre_import_failed' unless response.success?

    body_hash = response_body(response)
    body_hash&.fetch('status') || 'error'
  end
end

#pre_import_repository(path) ⇒ Object


50
51
52
53
# File 'lib/container_registry/gitlab_api_client.rb', line 50

def pre_import_repository(path)
  response = start_import_for(path, pre: true)
  IMPORT_RESPONSES.fetch(response.status, :error)
end

#repository_details(path, sizing: nil) ⇒ Object


89
90
91
92
93
94
95
96
97
98
99
# File 'lib/container_registry/gitlab_api_client.rb', line 89

def repository_details(path, sizing: nil)
  with_token_faraday do |faraday_client|
    req = faraday_client.get("/gitlab/v1/repositories/#{path}/") do |req|
      req.params['size'] = sizing if sizing
    end

    break {} unless req.success?

    response_body(req)
  end
end

#supports_gitlab_api?Boolean

Returns:

  • (Boolean)

37
38
39
40
41
42
43
44
45
46
47
# File 'lib/container_registry/gitlab_api_client.rb', line 37

def supports_gitlab_api?
  strong_memoize(:supports_gitlab_api) do
    registry_features = Gitlab::CurrentSettings.container_registry_features || []
    next true if ::Gitlab.com? && registry_features.include?(REGISTRY_GITLAB_V1_API_FEATURE)

    with_token_faraday do |faraday_client|
      response = faraday_client.get('/gitlab/v1/')
      response.success? || response.status == 401
    end
  end
end