Class: Kontena::StacksClient

Inherits:
Client
  • Object
show all
Defined in:
lib/kontena/stacks_client.rb

Constant Summary collapse

ACCEPT_JSON =
{ 'Accept' => 'application/json' }
ACCEPT_YAML =
{ 'Accept' => 'application/yaml' }
ACCEPT_JSONAPI =
{ 'Accept' => 'application/vnd.api+json' }
CT_YAML =
{ 'Content-Type' => 'application/yaml' }
CT_JSONAPI =
{ 'Content-Type' => 'application/vnd.api+json' }

Constants inherited from Client

Client::ACCEPT, Client::ACCEPT_ENCODING, Client::AUTHORIZATION, Client::CLIENT_ID, Client::CLIENT_SECRET, Client::CONTENT_JSON, Client::CONTENT_TYPE, Client::CONTENT_URLENCODED, Client::GZIP, Client::JSON_REGEX, Client::X_KONTENA_VERSION

Instance Attribute Summary

Attributes inherited from Client

#api_url, #default_headers, #host, #http_client, #last_response, #logger, #options, #path_prefix, #token

Instance Method Summary collapse

Methods inherited from Client

#authentication_ok?, #basic_auth_header, #bearer_authorization_header, #client_id, #client_secret, #debug, #delete, #error, #exchange_code, #get, #get_stream, #initialize, #patch, #post, #put, #refresh_request_params, #refresh_token, #request, #server_version, #token_account, #token_expired?

Constructor Details

This class inherits a constructor from Kontena::Client

Instance Method Details

#create(stack_name, is_private: true) ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/kontena/stacks_client.rb', line 130

def create(stack_name, is_private: true)
  post(
    '/v2/stacks',
    stack_data(stack_name, is_private: is_private),
    {},
    CT_JSONAPI.merge(ACCEPT_JSONAPI)
  )
end

#destroy(stack_name) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/kontena/stacks_client.rb', line 105

def destroy(stack_name)
  raise_unless_token
  if stack_name.version
    id = stack_version_id(stack_name)
    if id.nil?
      raise Kontena::Errors::StandardError.new(404, 'Not found')
    end
    delete('/v2/stack-versions/%s' % id, nil, {}, ACCEPT_JSONAPI)
  else
    id = stack_id(stack_name)
    if id.nil?
      raise Kontena::Errors::StandardError.new(404, 'Not found')
    end
    delete('/v2/stacks/%s' % id, nil, {}, ACCEPT_JSONAPI)
  end
end

#full_uri(stack_name) ⇒ Object



25
26
27
# File 'lib/kontena/stacks_client.rb', line 25

def full_uri(stack_name)
  URI.join(api_url, path_to_version(stack_name)).to_s
end

#make_private(stack_name) ⇒ Object



122
123
124
# File 'lib/kontena/stacks_client.rb', line 122

def make_private(stack_name)
  change_visibility(stack_name, is_private: true)
end

#make_public(stack_name) ⇒ Object



126
127
128
# File 'lib/kontena/stacks_client.rb', line 126

def make_public(stack_name)
  change_visibility(stack_name, is_private: false)
end

#path_to_stack(stack_name) ⇒ Object



33
34
35
# File 'lib/kontena/stacks_client.rb', line 33

def path_to_stack(stack_name)
  "/v2/organizations/%s/stacks/%s" % [stack_name.user, stack_name.stack]
end

#path_to_version(stack_name) ⇒ Object



29
30
31
# File 'lib/kontena/stacks_client.rb', line 29

def path_to_version(stack_name)
  path_to_stack(stack_name) + "/stack-versions/%s" % (stack_name.version || 'latest')
end

#pull(stack_name) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/kontena/stacks_client.rb', line 72

def pull(stack_name)
  raise_unless_read_token
  get(path_to_version(stack_name) + '/yaml', nil, ACCEPT_YAML)
rescue StandardError => ex
  ex.message << " : #{path_to_version(stack_name)}"
  raise ex, ex.message
end

#push(stack_name, data) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kontena/stacks_client.rb', line 37

def push(stack_name, data)
  raise_unless_token
  post(
    '/v2/stack-files',
    {
      'data' => {
        'type' => 'stack-files',
        'attributes' => { 'content' => data }
      }
    },
    {},
    CT_JSONAPI,
    true
  )
end

#raise_unless_read_tokenObject



18
19
20
21
22
23
# File 'lib/kontena/stacks_client.rb', line 18

def raise_unless_read_token
  return false unless options[:read_requires_token]
  unless token && token['access_token']
    raise Kontena::Errors::StandardError.new(401, "Stack registry requires authentication")
  end
end

#raise_unless_tokenObject



12
13
14
15
16
# File 'lib/kontena/stacks_client.rb', line 12

def raise_unless_token
  unless token && token['access_token']
    raise Kontena::Errors::StandardError.new(401, "Stack registry write operations require authentication")
  end
end

#search(query, tags: [], include_prerelease: true, include_private: true, include_versionless: true) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/kontena/stacks_client.rb', line 80

def search(query, tags: [], include_prerelease: true, include_private: true, include_versionless: true)
  raise_unless_read_token
  if tags.empty?
    result = get('/v2/stacks', { 'query' => query, 'include' => 'latest-version', 'include-prerelease' => include_prerelease, 'include-private' => include_private, 'include-versionless' => include_versionless }, ACCEPT_JSONAPI)
  else
    result = get('/v2/tags/%s/stacks' % tags.join(','), { 'query' => query, 'include' => 'latest-version', 'include-prerelease' => include_prerelease, 'include-private' => include_private }, ACCEPT_JSONAPI)
  end

  data = result.dig('data')
  included = result.dig('included')
  if included
    data.each do |row|
      name = '%s/%s' % [row.fetch('attributes', {}).fetch('organization-id'), row.fetch('attributes', {}).fetch('name')]
      next if name.nil?
      included_version = included.find { |i| i.fetch('attributes', {}).fetch('name') == name }
      if included_version
        row['attributes']['latest-version'] = {}
        row['attributes']['latest-version']['version'] = included_version['attributes']['version']
        row['attributes']['latest-version']['description'] = included_version['attributes']['description']
      end
    end
  end
  data
end

#show(stack_name, include_prerelease: true) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/kontena/stacks_client.rb', line 53

def show(stack_name, include_prerelease: true)
  raise_unless_read_token
  result = get("#{path_to_stack(stack_name)}", { 'include' => 'latest-version', 'include-prerelease' => include_prerelease }, ACCEPT_JSONAPI)
  if result['included']
    latest = result['included'].find { |i| i['type'] == 'stack-versions' }
    return result unless latest
    result['data']['attributes']['latest-version'] = {}
    result['data']['attributes']['latest-version']['version'] = latest['attributes']['version']
    result['data']['attributes']['latest-version']['description'] = latest['attributes']['description']
    result['data']['attributes']['latest-version']['meta'] = latest['meta']
  end
  result
end

#versions(stack_name, include_prerelease: true, include_deleted: false) ⇒ Object



67
68
69
70
# File 'lib/kontena/stacks_client.rb', line 67

def versions(stack_name, include_prerelease: true, include_deleted: false)
  raise_unless_read_token
  get("#{path_to_stack(stack_name)}/stack-versions", { 'include-prerelease' => include_prerelease, 'include-deleted' => include_deleted}, ACCEPT_JSONAPI).dig('data')
end