Class: Artifactory::Client
- Inherits:
-
Object
- Object
- Artifactory::Client
- Defined in:
- lib/artifactory/client.rb
Instance Attribute Summary collapse
-
#basic_auth ⇒ Object
readonly
Returns the value of attribute basic_auth.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#http ⇒ Object
readonly
Returns the value of attribute http.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
-
#docker_images(repo_key:, recurse: false) ⇒ Hash, Array<String>
Lists all Docker repositories hosted in under an Artifactory Docker repository.
-
#docker_manifest(repo_key:, image_name:, image_tag:) ⇒ Hash
Retrieve a docker image tag manifest.
-
#docker_tags(repo_key:, image_name:) ⇒ Array<String>
Retrieve all tags for a docker image.
-
#file_delete(repo_key:, path:) ⇒ Object
Deletes a file or a folder from the specified destination.
-
#file_info(repo_key:, path:) ⇒ Hash
Get file information like last modification time, creation time etc.
-
#file_list(repo_key:, folder_path: '/', deep: false, depth: 0, list_folders: false, md_timestamps: false, include_root_path: false) ⇒ Hash
Get a flat (the default) or deep listing of the files and folders (not included by default) within a folder.
-
#file_stat(repo_key:, path:) ⇒ Hash
Get file statistics like the number of times an item was downloaded, last download date and last downloader.
-
#get_repo(key:) ⇒ Hash
Retrieves the current configuration of a repository.
-
#initialize(endpoint:, username: nil, password: nil, api_key: nil, ssl_verify: true) ⇒ Client
constructor
Initialize an Artifactory client instance.
-
#repos(type: nil, recurse: false) ⇒ Hash
Returns a list of minimal repository details (unless recurse is enabled) for all repositories of the specified type.
-
#search_creation(repo_key:, from_date: nil, to_date: Time.now) ⇒ Hash
Get all artifacts created in date range.
-
#search_dates(repo_key:, from_date: nil, to_date: Time.now, date_fields:) ⇒ Hash
Get all artifacts with specified dates within the given range.
-
#search_pattern(repo_key:, pattern:) ⇒ Hash
Get all artifacts matching the given path pattern.
-
#search_usage(repo_key:, not_used_since:, created_before: nil) ⇒ Hash
Retrieve all artifacts not downloaded since the specified Java epoch in milliseconds.
Constructor Details
#initialize(endpoint:, username: nil, password: nil, api_key: nil, ssl_verify: true) ⇒ Client
Initialize an Artifactory client instance
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/artifactory/client.rb', line 23 def initialize(endpoint:, username: nil, password: nil, api_key: nil, ssl_verify: true) basic_auth = {} uri = URI.parse(endpoint) http = Net::HTTP.new(uri.host, uri.port) if (username and api_key) or (username.nil? and api_key.nil?) raise RuntimeError, "Either HTTP basic or API key are allowed as authentication methods" end headers = { 'Content-type' => 'application/json', 'Accept' => 'application/json', } if username basic_auth = {'username' => username, 'password' => password} else headers['X-JFrog-Art-Api'] = api_key end if uri.scheme == 'https' http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless ssl_verify end @uri = uri @http = http @basic_auth = basic_auth @headers = headers end |
Instance Attribute Details
#basic_auth ⇒ Object (readonly)
Returns the value of attribute basic_auth.
13 14 15 |
# File 'lib/artifactory/client.rb', line 13 def basic_auth @basic_auth end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
13 14 15 |
# File 'lib/artifactory/client.rb', line 13 def headers @headers end |
#http ⇒ Object (readonly)
Returns the value of attribute http.
13 14 15 |
# File 'lib/artifactory/client.rb', line 13 def http @http end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
13 14 15 |
# File 'lib/artifactory/client.rb', line 13 def uri @uri end |
Instance Method Details
#docker_images(repo_key:, recurse: false) ⇒ Hash, Array<String>
Lists all Docker repositories hosted in under an Artifactory Docker repository
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/artifactory/client.rb', line 87 def docker_images(repo_key:, recurse: false) ret = {} repolist = api_get("/docker/#{repo_key}/v2/_catalog")['repositories'] if recurse api_get("/docker/#{repo_key}/v2/_catalog")['repositories'].each do |name| ret[name] = self.(repo_key: repo_key, image_name: name) end else ret = repolist end images end |
#docker_manifest(repo_key:, image_name:, image_tag:) ⇒ Hash
Retrieve a docker image tag manifest
119 120 121 |
# File 'lib/artifactory/client.rb', line 119 def docker_manifest(repo_key:, image_name:, image_tag:) api_get("/docker/#{repo_key}/v2/#{image_name}/manifests/#{image_tag}") end |
#docker_tags(repo_key:, image_name:) ⇒ Array<String>
Retrieve all tags for a docker image
108 109 110 |
# File 'lib/artifactory/client.rb', line 108 def (repo_key:, image_name:) api_get("/docker/#{repo_key}/v2/#{image_name}/tags/list")['tags'] end |
#file_delete(repo_key:, path:) ⇒ Object
Deletes a file or a folder from the specified destination
201 202 203 |
# File 'lib/artifactory/client.rb', line 201 def file_delete(repo_key:, path:) api_delete(File.join(repo_key, path)) end |
#file_info(repo_key:, path:) ⇒ Hash
Get file information like last modification time, creation time etc.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/artifactory/client.rb', line 155 def file_info(repo_key:, path:) ret = {} api_get(File.join("/storage", repo_key, path).chomp('/')).each do |k, v| case k when "created", "lastModified", "lastUpdated" ret[k] = Time.parse(v) else ret[k] = v end end ret end |
#file_list(repo_key:, folder_path: '/', deep: false, depth: 0, list_folders: false, md_timestamps: false, include_root_path: false) ⇒ Hash
Get a flat (the default) or deep listing of the files and folders (not included by default) within a folder
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/artifactory/client.rb', line 130 def file_list(repo_key:, folder_path: '/', deep: false, depth: 0, list_folders: false, md_timestamps: false, include_root_path: false) path = ["/storage", repo_key, folder_path].join('/').chomp('/') params = ['list'] params << "deep=#{deep ? 1 : 0}" params << "depth=#{depth}" if depth > 0 params << "listFolders=#{list_folders ? 1 : 0}" params << "mdTimestamps=#{ ? 1 : 0}" params << "includeRootPath=#{include_root_path ? 1 : 0}" files = {} api_get([path, params.join('&')].join('?'))['files'].each do |file| name = file['uri'] files[name] = file.tap { |h| h.delete('uri') } files[name]['lastModified'] = Time.parse(files[name]['lastModified']) end files end |
#file_stat(repo_key:, path:) ⇒ Hash
Get file statistics like the number of times an item was downloaded, last download date and last downloader.
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/artifactory/client.rb', line 177 def file_stat(repo_key:, path:) ret = {} p = File.join("/storage", repo_key, path).chomp('/') params = ['stats'] api_get([p, params.join('&')].join('?')).tap { |h| h.delete('uri') }.each do |k, v| case k when "lastDownloaded", "remoteLastDownloaded" ret[k] = Time.at(v/1000) if v > 0 else ret[k] = v end end ret end |
#get_repo(key:) ⇒ Hash
Retrieves the current configuration of a repository. Supported by local, remote and virtual repositories.
59 60 61 |
# File 'lib/artifactory/client.rb', line 59 def get_repo(key:) api_get("/repositories/#{key}").tap { |h| h.delete('key') } end |
#repos(type: nil, recurse: false) ⇒ Hash
Returns a list of minimal repository details (unless recurse is enabled) for all repositories of the specified type.
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/artifactory/client.rb', line 69 def repos(type: nil, recurse: false) ret = {} params = [] params << "type=#{type}" if type api_get(["/repositories", params.join('&')].join('?')).each do |repo| ret[repo['key']] = recurse ? self.get_repo(key: repo['key']) : repo.tap { |h| h.delete('key') } end ret end |
#search_creation(repo_key:, from_date: nil, to_date: Time.now) ⇒ Hash
Get all artifacts created in date range
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/artifactory/client.rb', line 302 def search_creation(repo_key:, from_date: nil, to_date: Time.now) ret = [] path = File.join("/search", "creation") params = [] params << "from=#{(from_date.to_f.round(3) * 1000).to_i}" if from_date params << "to=#{(to_date.to_f.round(3) * 1000).to_i}" params << "repos=#{repo_key.is_a?(Array) ? repo_key.join(',') : repo_key}" api_get([path, params.join('&')].join('?'))['results'].each do |result| path = result['uri'].scan(/\/storage\/(.+?)(\/.*)/).flatten file = { "path" => path.join, "repo_key" => path[0], "name" => path[1] } result.each do |k, v| case k when "created" file[k] = Time.parse(v) else file[k] = v end end ret << file end ret end |
#search_dates(repo_key:, from_date: nil, to_date: Time.now, date_fields:) ⇒ Hash
Get all artifacts with specified dates within the given range
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/artifactory/client.rb', line 254 def search_dates(repo_key:, from_date: nil, to_date: Time.now, date_fields:) ret = [] valid_date_fields = ["created", "lastModified", "lastDownloaded"] date_fields.each do |date_field| raise ValueError, "Not a valid date field '#{date_field}'" unless valid_date_fields.include?(date_field) end path = File.join("/search", "dates") params = [] params << "from=#{(from_date.to_f.round(3) * 1000).to_i}" if from_date params << "to=#{(to_date.to_f.round(3) * 1000).to_i}" params << "repos=#{repo_key.is_a?(Array) ? repo_key.join(',') : repo_key}" params << "dateFields=#{date_fields.join(',')}" api_get([path, params.join('&')].join('?'))['results'].each do |result| path = result['uri'].scan(/\/storage\/(.+?)(\/.*)/).flatten file = { "path" => path.join, "repo_key" => path[0], "name" => path[1] } result.each do |k, v| case k when *valid_date_fields file[k] = Time.parse(v) else file[k] = v end end ret << file end ret end |
#search_pattern(repo_key:, pattern:) ⇒ Hash
Get all artifacts matching the given path pattern
342 343 344 345 346 347 |
# File 'lib/artifactory/client.rb', line 342 def search_pattern(repo_key:, pattern:) path = File.join("/search", "pattern") params = ["pattern=#{repo_key}:#{pattern}"] api_get([path, params].join('?'))['files'] end |
#search_usage(repo_key:, not_used_since:, created_before: nil) ⇒ Hash
Retrieve all artifacts not downloaded since the specified Java epoch in milliseconds
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/artifactory/client.rb', line 212 def search_usage(repo_key:, not_used_since:, created_before: nil) ret = [] path = File.join("/search", "usage") params = [] params << "notUsedSince=#{(not_used_since.to_f.round(3) * 1000).to_i}" params << "createdBefore=#{(created_before.to_f.round(3) * 1000).to_i}" if created_before params << "repos=#{repo_key.is_a?(Array) ? repo_key.join(',') : repo_key}" api_get([path, params.join('&')].join('?'))['results'].each do |result| path = result['uri'].scan(/\/storage\/(.+?)(\/.*)/).flatten file = { "path" => path.join, "repo_key" => path[0], "name" => path[1] } result.each do |k, v| case k when "lastDownloaded", "remoteLastDownloaded" file[k] = Time.parse(v) else file[k] = v end end ret << file end ret end |