unikraft-cloud 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.md +28 -0
- data/README.md +587 -0
- data/lib/unikraft/cloud/api/controlplane/auth.gen.rb +83 -0
- data/lib/unikraft/cloud/api/controlplane/images.gen.rb +51 -0
- data/lib/unikraft/cloud/api/controlplane/metros.gen.rb +45 -0
- data/lib/unikraft/cloud/api/controlplane/models.gen.rb +865 -0
- data/lib/unikraft/cloud/api/controlplane/node_activation_service.gen.rb +79 -0
- data/lib/unikraft/cloud/api/controlplane/node_service.gen.rb +276 -0
- data/lib/unikraft/cloud/api/controlplane/resources.gen.rb +12 -0
- data/lib/unikraft/cloud/api/controlplane.rb +49 -0
- data/lib/unikraft/cloud/api/platform/audit.gen.rb +55 -0
- data/lib/unikraft/cloud/api/platform/autoscale.gen.rb +231 -0
- data/lib/unikraft/cloud/api/platform/certificates.gen.rb +168 -0
- data/lib/unikraft/cloud/api/platform/images.gen.rb +109 -0
- data/lib/unikraft/cloud/api/platform/instances.gen.rb +873 -0
- data/lib/unikraft/cloud/api/platform/models.gen.rb +6456 -0
- data/lib/unikraft/cloud/api/platform/node.gen.rb +45 -0
- data/lib/unikraft/cloud/api/platform/resources.gen.rb +16 -0
- data/lib/unikraft/cloud/api/platform/services.gen.rb +172 -0
- data/lib/unikraft/cloud/api/platform/users.gen.rb +134 -0
- data/lib/unikraft/cloud/api/platform/volumes.gen.rb +430 -0
- data/lib/unikraft/cloud/api/platform.rb +64 -0
- data/lib/unikraft/cloud/api.rb +45 -0
- data/lib/unikraft/cloud/client.rb +186 -0
- data/lib/unikraft/cloud/core/base64.rb +36 -0
- data/lib/unikraft/cloud/core/errors.rb +96 -0
- data/lib/unikraft/cloud/core/fanout.rb +150 -0
- data/lib/unikraft/cloud/core/handle.rb +214 -0
- data/lib/unikraft/cloud/core/handle_set.rb +105 -0
- data/lib/unikraft/cloud/core/http.rb +361 -0
- data/lib/unikraft/cloud/core/metro.rb +75 -0
- data/lib/unikraft/cloud/core/pagination.rb +43 -0
- data/lib/unikraft/cloud/core/patch.rb +113 -0
- data/lib/unikraft/cloud/core/pool.rb +97 -0
- data/lib/unikraft/cloud/core/ready.rb +145 -0
- data/lib/unikraft/cloud/core/ref.rb +95 -0
- data/lib/unikraft/cloud/core/resource.rb +399 -0
- data/lib/unikraft/cloud/core/response.rb +96 -0
- data/lib/unikraft/cloud/core/session.rb +182 -0
- data/lib/unikraft/cloud/core/url.rb +34 -0
- data/lib/unikraft/cloud/model.rb +194 -0
- data/lib/unikraft/cloud/resources/certificates.rb +57 -0
- data/lib/unikraft/cloud/resources/instances.rb +172 -0
- data/lib/unikraft/cloud/resources/service_groups.rb +36 -0
- data/lib/unikraft/cloud/resources/users.rb +39 -0
- data/lib/unikraft/cloud/resources/volumes.rb +93 -0
- data/lib/unikraft/cloud/version.rb +11 -0
- data/lib/unikraft/cloud.rb +29 -0
- metadata +89 -0
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
4
|
+
# Copyright (c) 2026, Unikraft GmbH.
|
|
5
|
+
#
|
|
6
|
+
# The base every idiomatic ("porcelain") resource client is built on. It owns
|
|
7
|
+
# the metro scope, the plumbing client the operations go through, and the two
|
|
8
|
+
# hard problems of a metro-scoped API: finding which metro holds a named
|
|
9
|
+
# resource, and grouping a bulk operation by metro.
|
|
10
|
+
|
|
11
|
+
require_relative "errors"
|
|
12
|
+
require_relative "fanout"
|
|
13
|
+
require_relative "handle"
|
|
14
|
+
require_relative "handle_set"
|
|
15
|
+
require_relative "metro"
|
|
16
|
+
require_relative "pagination"
|
|
17
|
+
require_relative "ref"
|
|
18
|
+
require_relative "response"
|
|
19
|
+
|
|
20
|
+
module Unikraft
|
|
21
|
+
module Cloud
|
|
22
|
+
module Core
|
|
23
|
+
# A porcelain client bound to a metro scope. It holds, rather than
|
|
24
|
+
# inherits from, its plumbing client ({#api}).
|
|
25
|
+
class Scoped
|
|
26
|
+
# The per-call options every porcelain method takes besides its own:
|
|
27
|
+
# `metros:` overrides the scope, `base_url:` names one endpoint, and
|
|
28
|
+
# `headers:` and `timeout:` go to the transport.
|
|
29
|
+
OPTIONS = %i[metros base_url headers timeout].freeze
|
|
30
|
+
|
|
31
|
+
# @return [Object] the raw ("plumbing") client of this resource: every
|
|
32
|
+
# operation of the specification, returning the envelope untouched.
|
|
33
|
+
# Calls go to the scope's metro unless given `base_url:`.
|
|
34
|
+
attr_reader :api
|
|
35
|
+
# @return [:all, Array<String>] the metros this client covers
|
|
36
|
+
attr_reader :scope
|
|
37
|
+
|
|
38
|
+
# @api private
|
|
39
|
+
def initialize(session, scope, transport)
|
|
40
|
+
@session = session
|
|
41
|
+
@scope = scope
|
|
42
|
+
@api = self.class::API_CLASS.new(transport)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# The keywords of one plumbing call to an endpoint.
|
|
46
|
+
#
|
|
47
|
+
# @api private
|
|
48
|
+
# @param target [Endpoint, Location]
|
|
49
|
+
# @param call [Hash] `headers:`, `timeout:`; any other {OPTIONS} key
|
|
50
|
+
# is ignored, and an unknown one raises
|
|
51
|
+
def call_options(target, call)
|
|
52
|
+
{ base_url: target.base_url, **check_options!(call).slice(:headers, :timeout) }
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def inspect = "#<#{self.class.name} scope=#{@scope.inspect}>"
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
# The endpoints a call covers, honouring `metros:` or `base_url:`.
|
|
60
|
+
def endpoints(opts)
|
|
61
|
+
return [explicit(opts[:base_url])] if opts[:base_url]
|
|
62
|
+
|
|
63
|
+
@session.resolve(opts.key?(:metros) ? Metro.scope(opts[:metros]) : @scope)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# The single endpoint of an operation that must pick one metro.
|
|
67
|
+
def one_endpoint(operation, opts)
|
|
68
|
+
return explicit(opts[:base_url]) if opts[:base_url]
|
|
69
|
+
|
|
70
|
+
@session.resolve_one(opts.key?(:metros) ? Metro.scope(opts[:metros]) : @scope, operation)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def explicit(url)
|
|
74
|
+
url = Metro.base_url(url)
|
|
75
|
+
Endpoint.new(metro: url, base_url: url)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def check_options!(opts)
|
|
79
|
+
unknown = opts.keys - OPTIONS
|
|
80
|
+
raise ArgumentError, "unknown keyword(s): #{unknown.join(", ")}" unless unknown.empty?
|
|
81
|
+
|
|
82
|
+
opts
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# The collection operations every resource shares: create, get, where,
|
|
87
|
+
# list (and Enumerable), delete, update and edit.
|
|
88
|
+
class Resource < Scoped
|
|
89
|
+
include Enumerable
|
|
90
|
+
|
|
91
|
+
# What a concrete resource is called and which operations serve it.
|
|
92
|
+
Spec = Data.define(:noun, :key, :list, :create, :delete, :update)
|
|
93
|
+
|
|
94
|
+
# @api private
|
|
95
|
+
# @return [Spec]
|
|
96
|
+
def spec = self.class::SPEC
|
|
97
|
+
|
|
98
|
+
# Create a resource in one metro and return a handle on it. Creating
|
|
99
|
+
# never fans out: it uses the scope's metro, or the default metro
|
|
100
|
+
# when the scope is wider. `metro:` picks the metro, like
|
|
101
|
+
# `metros: [metro]`, and never reaches the body.
|
|
102
|
+
#
|
|
103
|
+
# @param attributes [Hash, nil] the request body; keywords are
|
|
104
|
+
# merged over it
|
|
105
|
+
# @return [Handle]
|
|
106
|
+
def create(attributes = nil, **kwargs)
|
|
107
|
+
body, opts = create_args(attributes, kwargs)
|
|
108
|
+
endpoint = one_endpoint("Creating #{article} #{spec.noun}", opts)
|
|
109
|
+
res = api.public_send(spec.create, body:, **call_options(endpoint, opts))
|
|
110
|
+
value = Response.first(res, spec.key, spec.noun).with_metro(endpoint.metro)
|
|
111
|
+
ref = Ref.new(uuid: value[:uuid], name: value[:uuid] ? nil : value[:name],
|
|
112
|
+
metro: endpoint.metro)
|
|
113
|
+
location = Location.new(ref:, metro: endpoint.metro, base_url: endpoint.base_url)
|
|
114
|
+
self.class::HANDLE.new(self, ref, opts, located: Located.new(location:, value:),
|
|
115
|
+
partial: true)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# A handle on one resource. Nothing is sent until it is used; then
|
|
119
|
+
# the resource is located (one concurrent read per metro, unless the
|
|
120
|
+
# ref or the scope already names the metro) and read once.
|
|
121
|
+
#
|
|
122
|
+
# @example
|
|
123
|
+
# ukc.instances.get(name: "web")
|
|
124
|
+
# ukc.instances.get(uuid: "550e8400-e29b-41d4-a716-446655440000")
|
|
125
|
+
# ukc.instances.get(name: "web", metro: :fra)
|
|
126
|
+
# ukc.instances.get({ name: "web" }, metros: %i[fra dal])
|
|
127
|
+
# @param ref [Hash, Ref, nil] or pass `name:`/`uuid:`/`metro:`
|
|
128
|
+
# @return [Handle]
|
|
129
|
+
# @raise [ArgumentError] for both or neither of name and uuid
|
|
130
|
+
def get(ref = nil, **kwargs)
|
|
131
|
+
ref, opts = split(ref, kwargs)
|
|
132
|
+
self.class::HANDLE.new(self, ref, opts) { |call = {}| locate(ref, opts.merge(call)) }
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# Every resource matching a ref, one per metro holding it: the
|
|
136
|
+
# deliberate plural of {#get}.
|
|
137
|
+
#
|
|
138
|
+
# @example
|
|
139
|
+
# ukc.instances.where(name: "web").suspend
|
|
140
|
+
# @return [HandleSet]
|
|
141
|
+
def where(ref = nil, **kwargs)
|
|
142
|
+
ref, opts = split(ref, kwargs)
|
|
143
|
+
self.class::SET.new(spec.noun) do |call = {}|
|
|
144
|
+
located = opts.merge(call)
|
|
145
|
+
locate_all(ref, located).map do |hit|
|
|
146
|
+
self.class::HANDLE.new(self, hit.location.ref, located, located: hit)
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# Every resource in scope, following each metro's pagination and
|
|
152
|
+
# interleaving the metros as their pages arrive. Each result is
|
|
153
|
+
# tagged with its `metro`. If some metros fail, the healthy ones are
|
|
154
|
+
# drained first, then a {MetroFanoutError} is raised.
|
|
155
|
+
#
|
|
156
|
+
# @param details [Boolean, nil] full resources rather than refs
|
|
157
|
+
# @param page_size [Integer, nil] the page size while paginating
|
|
158
|
+
# @param filters [Hash] e.g. `tags:` where the list operation takes
|
|
159
|
+
# it (instances, volumes), plus the call options
|
|
160
|
+
# @return [Enumerator, self] an Enumerator without a block
|
|
161
|
+
def list(details: nil, page_size: nil, **filters, &block)
|
|
162
|
+
opts = check_options!(takes_tags? ? filters.except(:tags) : filters)
|
|
163
|
+
query = { details:, tags: filters[:tags] }.compact
|
|
164
|
+
enum = Enumerator.new do |out|
|
|
165
|
+
Fanout.merge(endpoints(opts), out) do |endpoint, emit|
|
|
166
|
+
fetch = lambda do |count, from|
|
|
167
|
+
res = api.public_send(spec.list, count:, from:, **query,
|
|
168
|
+
**call_options(endpoint, opts))
|
|
169
|
+
Response.list(res, spec.key).map { |item| item.with_metro(endpoint.metro) }
|
|
170
|
+
end
|
|
171
|
+
Pagination.each(fetch:, page_size:, &emit)
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
return enum unless block
|
|
175
|
+
|
|
176
|
+
enum.each(&block)
|
|
177
|
+
self
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Enumerable over every resource in scope, with details.
|
|
181
|
+
#
|
|
182
|
+
# @yieldparam resource [Model]
|
|
183
|
+
def each(&)
|
|
184
|
+
return enum_for(:each) unless block_given?
|
|
185
|
+
|
|
186
|
+
list(details: true, &)
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# Delete one or more resources, in whichever metros hold them. Under
|
|
190
|
+
# a wide scope a name deletes every match in scope; qualify the ref
|
|
191
|
+
# or narrow the scope to delete one.
|
|
192
|
+
#
|
|
193
|
+
# @example
|
|
194
|
+
# ukc.instances.delete(name: "web", metro: :fra)
|
|
195
|
+
# ukc.instances.delete([{ name: "a" }, { uuid: "..." }])
|
|
196
|
+
# @param refs [Hash, Ref, Array, nil] or pass one ref as keywords
|
|
197
|
+
# @return [Array<Model>] what each metro reported, tagged
|
|
198
|
+
def delete(refs = nil, **kwargs) = bulk(spec.delete, refs, kwargs)
|
|
199
|
+
|
|
200
|
+
# Change one resource. Shorthand for `get(ref).update(...)`: the ref
|
|
201
|
+
# keys (and `metros:`/`base_url:`) locate it, `headers:` and
|
|
202
|
+
# `timeout:` go to the call, the rest is the patch.
|
|
203
|
+
#
|
|
204
|
+
# @example
|
|
205
|
+
# ukc.instances.update(name: "web", memory_mb: 512)
|
|
206
|
+
# ukc.instances.update({ name: "web" }, [{ prop: "vcpus", op: "set", value: 2 }])
|
|
207
|
+
# @return [Handle]
|
|
208
|
+
def update(ref = nil, changes = nil, **kwargs)
|
|
209
|
+
handle, rest = on(ref, kwargs)
|
|
210
|
+
handle.update(changes, **rest)
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
# Stage changes to one resource. Shorthand for `get(ref).edit`.
|
|
214
|
+
#
|
|
215
|
+
# @return [Patch::Editor, Handle]
|
|
216
|
+
def edit(ref = nil, **kwargs, &)
|
|
217
|
+
handle, call = on(ref, kwargs)
|
|
218
|
+
handle.edit(**call, &)
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
# Read one resource where it was located.
|
|
222
|
+
#
|
|
223
|
+
# @api private
|
|
224
|
+
# @return [Model]
|
|
225
|
+
def fetch(location, **call)
|
|
226
|
+
res = api.public_send(spec.list, **location.ref.query, details: true,
|
|
227
|
+
**call_options(location, call))
|
|
228
|
+
Response.first(res, spec.key, "#{spec.noun} #{location.ref}").with_metro(location.metro)
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
private
|
|
232
|
+
|
|
233
|
+
def article = spec.noun.match?(/\A[aeiou]/) ? "an" : "a"
|
|
234
|
+
|
|
235
|
+
# Whether the list operation filters by `tags:`.
|
|
236
|
+
def takes_tags? = api.method(spec.list).parameters.include?(%i[key tags])
|
|
237
|
+
|
|
238
|
+
# The body and call options of a create: keywords merge over the
|
|
239
|
+
# positional attributes, and `metro:` routes rather than sends.
|
|
240
|
+
def create_args(attributes, kwargs)
|
|
241
|
+
opts = kwargs.slice(*OPTIONS)
|
|
242
|
+
body = (attributes || {}).merge(kwargs.except(*OPTIONS))
|
|
243
|
+
if (metro = body.delete(:metro))
|
|
244
|
+
raise ArgumentError, "pass `metro:` or `metros:`, not both" if opts.key?(:metros)
|
|
245
|
+
|
|
246
|
+
opts[:metros] = metro
|
|
247
|
+
end
|
|
248
|
+
[body, opts]
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
# A ref from a positional Hash or keywords, and the call options.
|
|
252
|
+
def split(ref, kwargs)
|
|
253
|
+
ref_kw = kwargs.slice(*Ref.members)
|
|
254
|
+
opts = check_options!(kwargs.except(*Ref.members))
|
|
255
|
+
if ref && ref_kw.any?
|
|
256
|
+
raise ArgumentError,
|
|
257
|
+
"pass the ref positionally or as keywords, not both"
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
[ref ? Ref.coerce(ref) : Ref.new(**ref_kw), opts]
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
# A handle from the locating keywords, and the keywords left for the
|
|
264
|
+
# operation, for the shorthand forms. `headers:` and `timeout:` go to
|
|
265
|
+
# both: the lookup needs the same credentials as the operation.
|
|
266
|
+
def on(ref, kwargs)
|
|
267
|
+
locating = Ref.members + %i[metros base_url]
|
|
268
|
+
[get(ref, **kwargs.slice(*locating, :headers, :timeout)), kwargs.except(*locating)]
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
def bulk(operation, refs, kwargs)
|
|
272
|
+
check_options!(kwargs) if refs # positional refs leave only options
|
|
273
|
+
opts = kwargs.slice(*OPTIONS)
|
|
274
|
+
refs = Ref.list(refs || Ref.coerce(kwargs.except(*OPTIONS)))
|
|
275
|
+
groups = group_by_metro(refs, opts)
|
|
276
|
+
run = lambda do |endpoint|
|
|
277
|
+
body = groups[endpoint].map(&:wire)
|
|
278
|
+
res = api.public_send(operation, body:, **call_options(endpoint, opts))
|
|
279
|
+
Response.list(res, spec.key).map { |item| item.with_metro(endpoint.metro) }
|
|
280
|
+
end
|
|
281
|
+
return run.call(groups.keys.first) if groups.size == 1
|
|
282
|
+
|
|
283
|
+
Fanout.all(groups.keys, flat: true, &run)
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
# The endpoints to search for one ref. A ref that names its metro
|
|
287
|
+
# needs no search and no discovery.
|
|
288
|
+
def endpoints_for(ref, opts)
|
|
289
|
+
return [@session.endpoint_for(ref.metro)] if ref.metro && !opts[:base_url]
|
|
290
|
+
|
|
291
|
+
endpoints(opts)
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
# Find which metro holds one resource. Free when the ref or the scope
|
|
295
|
+
# already says; otherwise every metro is asked concurrently, and the
|
|
296
|
+
# resource found travels back so it is not read twice. A name in
|
|
297
|
+
# several metros raises rather than guessing, since the caller may be
|
|
298
|
+
# about to mutate it. A uuid should be unique, but if several metros
|
|
299
|
+
# report it, that is just as ambiguous.
|
|
300
|
+
def locate(ref, opts)
|
|
301
|
+
found = locate_all(ref, opts)
|
|
302
|
+
return found.first if found.size == 1
|
|
303
|
+
|
|
304
|
+
metros = found.map { |hit| hit.location.metro }
|
|
305
|
+
id = ref.uuid ? "uuid: #{ref.uuid.inspect}" : "name: #{ref.name.inspect}"
|
|
306
|
+
raise AmbiguousRefError.new(
|
|
307
|
+
"#{spec.noun} #{ref} exists in #{found.size} metros (#{metros.join(", ")}). Say " \
|
|
308
|
+
"which one with `get(#{id}, metro: :#{metros.first})`, or act on all of them " \
|
|
309
|
+
"with `where(#{id})`.",
|
|
310
|
+
matches: found.map(&:value)
|
|
311
|
+
)
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
# Locate every metro holding a match: the same name in five metros
|
|
315
|
+
# is five results, not an error. One endpoint is taken on trust,
|
|
316
|
+
# lazily, rather than spending a request to confirm it.
|
|
317
|
+
def locate_all(ref, opts)
|
|
318
|
+
endpoints = endpoints_for(ref, opts)
|
|
319
|
+
if endpoints.size == 1
|
|
320
|
+
return [Located.new(location: location_at(endpoints.first, ref),
|
|
321
|
+
value: nil)]
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
search(ref, endpoints, opts).map do |endpoint, value|
|
|
325
|
+
Located.new(location: location_at(endpoint, ref), value:)
|
|
326
|
+
end
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
def location_at(endpoint, ref)
|
|
330
|
+
Location.new(ref: ref.with(metro: endpoint.metro), metro: endpoint.metro,
|
|
331
|
+
base_url: endpoint.base_url)
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
# Ask every endpoint for the ref concurrently. Absent is not a
|
|
335
|
+
# failure, but absent everywhere is. Any metro that could not answer
|
|
336
|
+
# fails the search too, even with matches elsewhere: it might hold
|
|
337
|
+
# another match, and the caller may be about to mutate it.
|
|
338
|
+
def search(ref, endpoints, opts)
|
|
339
|
+
outcomes = Fanout.settled(endpoints) { |endpoint| lookup(endpoint, ref, opts) }
|
|
340
|
+
hits = outcomes.select { |o| o.ok? && o.value }
|
|
341
|
+
found = hits.map { |o| [o.item, o.value.with_metro(o.item.metro)] }
|
|
342
|
+
failures = outcomes.reject(&:ok?).map { |o| { metro: o.item.metro, error: o.error } }
|
|
343
|
+
return found if failures.empty? && found.any?
|
|
344
|
+
|
|
345
|
+
not_found(ref, endpoints.map(&:metro).join(", "), failures, found)
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
def not_found(ref, searched, failures, found)
|
|
349
|
+
unreachable = failures.map { |f| f[:metro] }.join(", ")
|
|
350
|
+
if found.any?
|
|
351
|
+
raise MetroFanoutError.new(
|
|
352
|
+
"#{spec.noun} #{ref} was found in #{found.map { |e, _| e.metro }.join(", ")}, but " \
|
|
353
|
+
"#{failures.size} metros could not be reached and might hold it too: #{unreachable}.",
|
|
354
|
+
failures:, results: found.map(&:last)
|
|
355
|
+
)
|
|
356
|
+
end
|
|
357
|
+
if failures.any?
|
|
358
|
+
raise MetroFanoutError.new(
|
|
359
|
+
"#{spec.noun} #{ref} was not found in #{searched}, and #{failures.size} of those " \
|
|
360
|
+
"metros could not be reached: #{unreachable}.",
|
|
361
|
+
failures:
|
|
362
|
+
)
|
|
363
|
+
end
|
|
364
|
+
raise Error.new("#{spec.noun} #{ref} not found in #{searched}", kind: :http, status: 404)
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
# Look for one resource in one metro; nil when it is not there.
|
|
368
|
+
def lookup(endpoint, ref, opts)
|
|
369
|
+
Response.absent do
|
|
370
|
+
res = api.public_send(spec.list, **ref.query, details: true,
|
|
371
|
+
**call_options(endpoint, opts))
|
|
372
|
+
Response.first(res, spec.key, spec.noun)
|
|
373
|
+
end
|
|
374
|
+
end
|
|
375
|
+
|
|
376
|
+
# Group refs by the metro holding them, so a bulk operation is one
|
|
377
|
+
# call per metro. Free when the scope is one metro and no ref names
|
|
378
|
+
# its own; otherwise refs are located concurrently, and a name
|
|
379
|
+
# matching in several metros counts in each. Refs that all name
|
|
380
|
+
# their metro skip resolving the scope, and so discovery.
|
|
381
|
+
def group_by_metro(refs, opts)
|
|
382
|
+
if refs.none?(&:metro)
|
|
383
|
+
endpoints = endpoints(opts)
|
|
384
|
+
return { endpoints.first => refs } if endpoints.size == 1
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
outcomes = Fanout.settled(refs) { |ref| locate_all(ref, opts) }
|
|
388
|
+
failed = outcomes.find { |o| !o.ok? }
|
|
389
|
+
raise failed.error if failed
|
|
390
|
+
|
|
391
|
+
outcomes.flat_map(&:value).each_with_object({}) do |hit, groups|
|
|
392
|
+
loc = hit.location
|
|
393
|
+
(groups[Endpoint.new(metro: loc.metro, base_url: loc.base_url)] ||= []) << loc.ref
|
|
394
|
+
end
|
|
395
|
+
end
|
|
396
|
+
end
|
|
397
|
+
end
|
|
398
|
+
end
|
|
399
|
+
end
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
4
|
+
# Copyright (c) 2026, Unikraft GmbH.
|
|
5
|
+
|
|
6
|
+
require_relative "errors"
|
|
7
|
+
|
|
8
|
+
module Unikraft
|
|
9
|
+
module Cloud
|
|
10
|
+
module Core
|
|
11
|
+
# Unwrapping the response envelope the plumbing returns.
|
|
12
|
+
module Response
|
|
13
|
+
# The platform's per-entry `error` codes the SDK acts on, as the HTTP
|
|
14
|
+
# status that says the same. Code 8 is "no such resource", answered
|
|
15
|
+
# with HTTP 200; the spec publishes no list, so this records observed
|
|
16
|
+
# behaviour. {absent} depends on it.
|
|
17
|
+
ENTRY_ERROR_STATUS = { 8 => 404 }.freeze
|
|
18
|
+
|
|
19
|
+
module_function
|
|
20
|
+
|
|
21
|
+
# The envelope's `data`, after checking it reports no failure.
|
|
22
|
+
#
|
|
23
|
+
# A whole-request failure fills the top-level `errors`; a per-resource
|
|
24
|
+
# one marks its entry inside `data` with `status: "error"`, which
|
|
25
|
+
# then supplies the message and status. A `partial_success` whose
|
|
26
|
+
# failures stay inside `data` is returned as is: the caller asked for
|
|
27
|
+
# several resources and the successes are in the same list.
|
|
28
|
+
#
|
|
29
|
+
# @param res [Model, nil] the envelope
|
|
30
|
+
# @return [Model, nil]
|
|
31
|
+
# @raise [Unikraft::Cloud::Error] kind `:http`
|
|
32
|
+
def unwrap(res)
|
|
33
|
+
return nil if res.nil?
|
|
34
|
+
|
|
35
|
+
reported = Array(res[:errors]).first
|
|
36
|
+
return res[:data] if res[:status] != "error" && reported.nil?
|
|
37
|
+
|
|
38
|
+
raise failure(res, reported.nil? ? first_failure(res[:data]) : nil, reported&.[](:status))
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# The `data[key]` list of an envelope (empty when absent).
|
|
42
|
+
#
|
|
43
|
+
# @return [Array<Model>]
|
|
44
|
+
def list(res, key)
|
|
45
|
+
Array(unwrap(res)&.[](key))
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# The first entry of `data[key]`. A single-resource caller has no
|
|
49
|
+
# use for a failed entry, so any failed one in a `partial_success`
|
|
50
|
+
# (e.g. one property of an update) raises.
|
|
51
|
+
#
|
|
52
|
+
# @param what [String] the subject of the not-found message
|
|
53
|
+
# @raise [Unikraft::Cloud::Error] status 404 when the list is empty
|
|
54
|
+
def first(res, key, what)
|
|
55
|
+
entries = list(res, key)
|
|
56
|
+
if entries.empty?
|
|
57
|
+
raise Error.new("#{what} not found", kind: :http, status: 404, body: res)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
failed = entries.find { |entry| entry[:status] == "error" }
|
|
61
|
+
raise failure(res, failed) if failed
|
|
62
|
+
|
|
63
|
+
entries.first
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# The block's value, or nil when it reports the resource absent (404).
|
|
67
|
+
# Searching several metros means most legitimately answer "not here".
|
|
68
|
+
def absent
|
|
69
|
+
yield
|
|
70
|
+
rescue Error => e
|
|
71
|
+
raise unless e.status == 404
|
|
72
|
+
|
|
73
|
+
nil
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def failure(res, entry, status = nil)
|
|
77
|
+
status ||= ENTRY_ERROR_STATUS[entry&.[](:error)]
|
|
78
|
+
message = entry&.[](:message) || res[:message] || "Unikraft Cloud API reported an error"
|
|
79
|
+
Error.new(message, kind: :http, status:, errors: res.to_h[:errors], body: res)
|
|
80
|
+
end
|
|
81
|
+
private_class_method :failure
|
|
82
|
+
|
|
83
|
+
def first_failure(data)
|
|
84
|
+
data.to_h.each_value do |value|
|
|
85
|
+
next unless value.is_a?(Array)
|
|
86
|
+
|
|
87
|
+
hit = value.find { |entry| entry.is_a?(Hash) && entry[:status] == "error" }
|
|
88
|
+
return hit if hit
|
|
89
|
+
end
|
|
90
|
+
nil
|
|
91
|
+
end
|
|
92
|
+
private_class_method :first_failure
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
4
|
+
# Copyright (c) 2026, Unikraft GmbH.
|
|
5
|
+
#
|
|
6
|
+
# The transport and metro knowledge shared by every resource client: one set
|
|
7
|
+
# of credentials, one memoised metro discovery, and the rules for turning a
|
|
8
|
+
# metro scope into concrete endpoints.
|
|
9
|
+
|
|
10
|
+
require_relative "errors"
|
|
11
|
+
require_relative "http"
|
|
12
|
+
require_relative "metro"
|
|
13
|
+
require_relative "response"
|
|
14
|
+
require_relative "../api"
|
|
15
|
+
|
|
16
|
+
module Unikraft
|
|
17
|
+
module Cloud
|
|
18
|
+
module Core
|
|
19
|
+
# Shared state behind one client. Scoped clients (`ukc.metro(:fra)`)
|
|
20
|
+
# reuse the same session, so discovery and transports are shared rather
|
|
21
|
+
# than duplicated. Safe to share across threads: discovery and the
|
|
22
|
+
# transport cache are guarded by a Mutex.
|
|
23
|
+
class Session
|
|
24
|
+
# @return [Endpoint] the endpoint of operations that must pick one
|
|
25
|
+
# metro, such as `create`
|
|
26
|
+
attr_reader :default_endpoint
|
|
27
|
+
# @return [Endpoint, nil] the only endpoint there is, when the caller
|
|
28
|
+
# named a URL
|
|
29
|
+
attr_reader :pinned
|
|
30
|
+
|
|
31
|
+
# @param config [Hash] {ApiClient#initialize} keywords other than
|
|
32
|
+
# `base_url:` (token, headers, timeout, ...)
|
|
33
|
+
# @param platform_url [String] the default metro's base URL
|
|
34
|
+
# @param controlplane_url [String]
|
|
35
|
+
# @param default_metro [String]
|
|
36
|
+
# @param pinned [Boolean] `platform_url` was named explicitly: never
|
|
37
|
+
# discover, never invent hostnames
|
|
38
|
+
# @param transport [#call, nil] builds a transport from the
|
|
39
|
+
# {ApiClient} keywords; defaults to `ApiClient.new`
|
|
40
|
+
def initialize(config:, platform_url:, controlplane_url:, default_metro:,
|
|
41
|
+
pinned: false, transport: nil)
|
|
42
|
+
@config = config
|
|
43
|
+
@build = transport || ->(**kw) { ApiClient.new(**kw) }
|
|
44
|
+
@platform_url = platform_url
|
|
45
|
+
@controlplane_url = controlplane_url
|
|
46
|
+
@pinned = Endpoint.new(metro: platform_url, base_url: platform_url) if pinned
|
|
47
|
+
@default_endpoint = @pinned || Endpoint.new(metro: default_metro, base_url: platform_url)
|
|
48
|
+
@mutex = Mutex.new
|
|
49
|
+
@discovery = Mutex.new
|
|
50
|
+
@transports = {}
|
|
51
|
+
@discovered = nil
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# The transport for a base URL, built once and shared.
|
|
55
|
+
#
|
|
56
|
+
# @param base_url [String]
|
|
57
|
+
# @return [#request, #bytes, #stream]
|
|
58
|
+
def transport(base_url = @platform_url)
|
|
59
|
+
@mutex.synchronize { @transports[base_url] ||= @build.call(base_url:, **@config) }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Both raw API surfaces, the platform one pointing at `base_url`.
|
|
63
|
+
#
|
|
64
|
+
# @return [API]
|
|
65
|
+
def api(base_url = @platform_url)
|
|
66
|
+
API.new(platform: transport(base_url), controlplane: transport(@controlplane_url))
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Every metro the account can reach, asked of the control plane once
|
|
70
|
+
# and cached. A failed discovery is not cached, so a transient outage
|
|
71
|
+
# does not poison the client.
|
|
72
|
+
#
|
|
73
|
+
# @return [Array<Endpoint>]
|
|
74
|
+
# @raise [Unikraft::Cloud::Error] kind `:fanout`
|
|
75
|
+
def discover
|
|
76
|
+
return [@pinned] if @pinned
|
|
77
|
+
|
|
78
|
+
# Held across the request, so concurrent callers share one discovery.
|
|
79
|
+
@discovery.synchronize { @discovered ||= list_metros }.values
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# The endpoint for a metro code, from what the session already knows.
|
|
83
|
+
# Sends no request. A discovered metro may report an endpoint its code
|
|
84
|
+
# does not build, so that wins over the URL derived from the code.
|
|
85
|
+
#
|
|
86
|
+
# @param metro [String, Symbol]
|
|
87
|
+
# @return [Endpoint]
|
|
88
|
+
def endpoint_for(metro)
|
|
89
|
+
return @pinned if @pinned
|
|
90
|
+
|
|
91
|
+
# @discovered is frozen and assigned once, so reading it needs no lock.
|
|
92
|
+
@discovered&.[](metro.to_s) || Metro.endpoint(metro)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# The endpoints a scope covers. A named scope never discovers.
|
|
96
|
+
#
|
|
97
|
+
# @param scope [:all, Array<String>]
|
|
98
|
+
# @return [Array<Endpoint>]
|
|
99
|
+
def resolve(scope)
|
|
100
|
+
return [@pinned] if @pinned
|
|
101
|
+
return discover if scope == :all
|
|
102
|
+
if scope.empty?
|
|
103
|
+
raise Error.new("An empty metro scope selects no metros; pass :all or a metro.",
|
|
104
|
+
kind: :fanout)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
scope.map { |metro| endpoint_for(metro) }
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# The one endpoint of an operation that must pick exactly one metro.
|
|
111
|
+
# A wider scope falls back to the default metro, since "create this
|
|
112
|
+
# in every metro" is not a thing a caller can mean; a named scope
|
|
113
|
+
# must hold it, or the call is refused rather than sent elsewhere.
|
|
114
|
+
#
|
|
115
|
+
# @param operation [String] e.g. "Creating an instance"
|
|
116
|
+
# @return [Endpoint]
|
|
117
|
+
# @raise [Unikraft::Cloud::Error] kind `:config` when a scope of
|
|
118
|
+
# several metros leaves out the default one
|
|
119
|
+
def resolve_one(scope, operation)
|
|
120
|
+
return @pinned if @pinned
|
|
121
|
+
|
|
122
|
+
default = @default_endpoint.metro
|
|
123
|
+
return endpoint_for(default) if scope == :all
|
|
124
|
+
|
|
125
|
+
endpoints = resolve(scope)
|
|
126
|
+
return endpoints.first if endpoints.size == 1
|
|
127
|
+
return endpoint_for(default) if scope.include?(default)
|
|
128
|
+
|
|
129
|
+
metros = endpoints.map(&:metro)
|
|
130
|
+
raise Error.new("#{operation} targets a single metro, but the scope spans " \
|
|
131
|
+
"#{metros.size} (#{metros.join(", ")}) and leaves out the default " \
|
|
132
|
+
"metro (#{default}). Pick one with `ukc.metro(:#{metros.first})` " \
|
|
133
|
+
"or `metros: :#{metros.first}`.",
|
|
134
|
+
kind: :config)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Close every transport this session built (idle connections only;
|
|
138
|
+
# a later call reopens them). Idempotent.
|
|
139
|
+
#
|
|
140
|
+
# @return [nil]
|
|
141
|
+
def close
|
|
142
|
+
transports = @mutex.synchronize { @transports.values }
|
|
143
|
+
# ApiClient#close closes the pooled idle connections. A custom
|
|
144
|
+
# `transport:` need only satisfy request/bytes/stream, so it may not
|
|
145
|
+
# have one.
|
|
146
|
+
transports.each { |t| t.close if t.respond_to?(:close) }
|
|
147
|
+
nil
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def inspect
|
|
151
|
+
"#<#{self.class.name} default=#{@default_endpoint.metro.inspect}" \
|
|
152
|
+
"#{" pinned" if @pinned}>"
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
private
|
|
156
|
+
|
|
157
|
+
def list_metros
|
|
158
|
+
metros = begin
|
|
159
|
+
Response.list(API::ControlPlane::MetrosApi.new(transport(@controlplane_url)).list_metros,
|
|
160
|
+
:metros)
|
|
161
|
+
rescue Error => e
|
|
162
|
+
raise Error.new("Could not discover the available metros from the control plane. " \
|
|
163
|
+
"Name the metros you want (`Unikraft::Cloud.new(metro: :fra)`, " \
|
|
164
|
+
"`ukc.metro(:fra)`, or `metros: [...]`) to skip discovery.",
|
|
165
|
+
kind: :fanout, cause: e)
|
|
166
|
+
end
|
|
167
|
+
endpoints = metros.filter_map do |m|
|
|
168
|
+
# The IATA code is what users type; the control plane's endpoint
|
|
169
|
+
# is trusted over one built from it, so new metros just work.
|
|
170
|
+
code = [m[:iata_code], m[:name]].find { |c| c.is_a?(String) && !c.empty? }
|
|
171
|
+
code && [code,
|
|
172
|
+
Endpoint.new(metro: code, base_url: Metro.base_url(m[:endpoint] || code))]
|
|
173
|
+
end.to_h
|
|
174
|
+
return endpoints.freeze unless endpoints.empty?
|
|
175
|
+
|
|
176
|
+
raise Error.new("The control plane reported no metros for this account, so there is " \
|
|
177
|
+
"nothing to query.", kind: :fanout)
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
end
|