Class: UffizziCore::DockerHubClient

Inherits:
Object
  • Object
show all
Defined in:
app/clients/uffizzi_core/docker_hub_client.rb,
app/clients/uffizzi_core/docker_hub_client/request_result.rb

Defined Under Namespace

Classes: RequestResult

Constant Summary collapse

BASE_URL =
'https://hub.docker.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credential = nil) ⇒ DockerHubClient

Returns a new instance of DockerHubClient.



8
9
10
11
12
13
14
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 8

def initialize(credential = nil)
  @connection = build_connection
  @credential = credential
  return unless credential

  @jwt = authentificate
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



4
5
6
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 4

def connection
  @connection
end

#credentialObject

Returns the value of attribute credential.



4
5
6
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 4

def credential
  @credential
end

#jwtObject

Returns the value of attribute jwt.



4
5
6
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 4

def jwt
  @jwt
end

Instance Method Details

#accountsObject

Raises:

  • (NotAuthorizedError)


74
75
76
77
78
79
80
81
82
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 74

def accounts
  raise NotAuthorizedError if !authentificated?

  url = "#{BASE_URL}/v2/repositories/namespaces/"
  response = connection.get(url) do |request|
    request.headers['Authorization'] = "JWT #{jwt}"
  end
  RequestResult.new(result: response.body)
end

#authentificateObject



16
17
18
19
20
21
22
23
24
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 16

def authentificate
  params = { username: credential.username, password: credential.password }
  url = "#{BASE_URL}/v2/users/login/"
  response = connection.post(url, params)
  request_result = RequestResult.new(result: response.body)
  request_result.result.token
rescue NoMethodError
  nil
end

#authentificated?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 128

def authentificated?
  jwt.present?
end

#create_webhook(slug:, name:, expect_final_callback:, webhooks:) ⇒ Object

Raises:

  • (NotAuthorizedError)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 56

def create_webhook(slug:, name:, expect_final_callback:, webhooks:)
  raise NotAuthorizedError if !authentificated?

  url = BASE_URL + "/v2/repositories/#{slug}/webhook_pipeline/"

  params = {
    name: name,
    expect_final_callback: expect_final_callback,
    webhooks: webhooks,
  }

  response = connection.post(url, params) do |request|
    request.headers['Authorization'] = "JWT #{jwt}"
  end

  RequestResult.new(status: response.status, result: response.body)
end

#digest(image:, tag:, token:) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 101

def digest(image:, tag:, token:)
  url = "https://index.docker.io/v2/#{image}/manifests/#{tag}"
  response = connection.get(url) do |request|
    request.headers['Accept'] = 'application/vnd.docker.distribution.manifest.v2+json'
    request.headers['Authorization'] = "Bearer #{token}"
  end

  RequestResult.quiet.new(result: response.body, headers: response.headers)
end

#get_token(repository) ⇒ Object



111
112
113
114
115
116
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 111

def get_token(repository)
  params = { username: credential.username, password: credential.password }
  url = "https://auth.docker.io/token?service=registry.docker.io&scope=repository:#{repository}:pull"
  response = connection.get(url, params)
  RequestResult.new(result: response.body)
end

#get_webhooks(slug:, registry:) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 46

def get_webhooks(slug:, registry:)
  url = BASE_URL + "/v2/repositories/#{slug}/webhook_pipeline/"

  response = connection.get(url, { registry: registry, page_size: 100 }) do |request|
    request.headers['Authorization'] = "JWT #{jwt}"
  end

  RequestResult.new(status: response.status, result: response.body)
end

#metadata(namespace:, image:) ⇒ Object



84
85
86
87
88
89
90
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 84

def (namespace:, image:)
  url = BASE_URL + "/v2/repositories/#{namespace}/#{image}/"
  response = connection.get(url) do |request|
    request.headers['Authorization'] = "JWT #{jwt}"
  end
  RequestResult.quiet.new(result: response.body)
end

#private_images(account:, page: 1, per_page: 25) ⇒ Object

Raises:

  • (NotAuthorizedError)


35
36
37
38
39
40
41
42
43
44
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 35

def private_images(account:, page: 1, per_page: 25)
  raise NotAuthorizedError if !authentificated? || .empty?

  url =  BASE_URL + "/v2/repositories/#{}/"
  params = { page_size: per_page, page: page }
  response = connection.get(url, params) do |request|
    request.headers['Authorization'] = "JWT #{jwt}"
  end
  RequestResult.new(result: response.body)
end

#public_images(q:, page: 1, per_page: 25) ⇒ Object



26
27
28
29
30
31
32
33
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 26

def public_images(q:, page: 1, per_page: 25)
  url = "#{BASE_URL}/api/content/v1/products/search"
  params = { page_size: per_page, q: q, type: :image, page: page }
  response = connection.get(url, params) do |request|
    request.headers['Search-Version'] = 'v3'
  end
  RequestResult.new(result: response.body)
end

#send_webhook_answer(url, params) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 118

def send_webhook_answer(url, params)
  conn = Faraday.new do |c|
    c.request(:json)
    c.adapter(Faraday.default_adapter)
  end
  response = conn.post(url, params)

  RequestResult.quiet.new(result: response.body)
end

#tags(namespace:, image:, q: '', page: 1, per_page: 10) ⇒ Object



92
93
94
95
96
97
98
99
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 92

def tags(namespace:, image:, q: '', page: 1, per_page: 10)
  url = BASE_URL + "/v2/repositories/#{namespace}/#{image}/tags"
  params = { page_size: per_page, page: page, name: q }
  response = connection.get(url, params) do |request|
    request.headers['Authorization'] = "JWT #{jwt}"
  end
  RequestResult.quiet.new(result: response.body)
end