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.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.md +28 -0
  3. data/README.md +587 -0
  4. data/lib/unikraft/cloud/api/controlplane/auth.gen.rb +83 -0
  5. data/lib/unikraft/cloud/api/controlplane/images.gen.rb +51 -0
  6. data/lib/unikraft/cloud/api/controlplane/metros.gen.rb +45 -0
  7. data/lib/unikraft/cloud/api/controlplane/models.gen.rb +865 -0
  8. data/lib/unikraft/cloud/api/controlplane/node_activation_service.gen.rb +79 -0
  9. data/lib/unikraft/cloud/api/controlplane/node_service.gen.rb +276 -0
  10. data/lib/unikraft/cloud/api/controlplane/resources.gen.rb +12 -0
  11. data/lib/unikraft/cloud/api/controlplane.rb +49 -0
  12. data/lib/unikraft/cloud/api/platform/audit.gen.rb +55 -0
  13. data/lib/unikraft/cloud/api/platform/autoscale.gen.rb +231 -0
  14. data/lib/unikraft/cloud/api/platform/certificates.gen.rb +168 -0
  15. data/lib/unikraft/cloud/api/platform/images.gen.rb +109 -0
  16. data/lib/unikraft/cloud/api/platform/instances.gen.rb +873 -0
  17. data/lib/unikraft/cloud/api/platform/models.gen.rb +6456 -0
  18. data/lib/unikraft/cloud/api/platform/node.gen.rb +45 -0
  19. data/lib/unikraft/cloud/api/platform/resources.gen.rb +16 -0
  20. data/lib/unikraft/cloud/api/platform/services.gen.rb +172 -0
  21. data/lib/unikraft/cloud/api/platform/users.gen.rb +134 -0
  22. data/lib/unikraft/cloud/api/platform/volumes.gen.rb +430 -0
  23. data/lib/unikraft/cloud/api/platform.rb +64 -0
  24. data/lib/unikraft/cloud/api.rb +45 -0
  25. data/lib/unikraft/cloud/client.rb +186 -0
  26. data/lib/unikraft/cloud/core/base64.rb +36 -0
  27. data/lib/unikraft/cloud/core/errors.rb +96 -0
  28. data/lib/unikraft/cloud/core/fanout.rb +150 -0
  29. data/lib/unikraft/cloud/core/handle.rb +214 -0
  30. data/lib/unikraft/cloud/core/handle_set.rb +105 -0
  31. data/lib/unikraft/cloud/core/http.rb +361 -0
  32. data/lib/unikraft/cloud/core/metro.rb +75 -0
  33. data/lib/unikraft/cloud/core/pagination.rb +43 -0
  34. data/lib/unikraft/cloud/core/patch.rb +113 -0
  35. data/lib/unikraft/cloud/core/pool.rb +97 -0
  36. data/lib/unikraft/cloud/core/ready.rb +145 -0
  37. data/lib/unikraft/cloud/core/ref.rb +95 -0
  38. data/lib/unikraft/cloud/core/resource.rb +399 -0
  39. data/lib/unikraft/cloud/core/response.rb +96 -0
  40. data/lib/unikraft/cloud/core/session.rb +182 -0
  41. data/lib/unikraft/cloud/core/url.rb +34 -0
  42. data/lib/unikraft/cloud/model.rb +194 -0
  43. data/lib/unikraft/cloud/resources/certificates.rb +57 -0
  44. data/lib/unikraft/cloud/resources/instances.rb +172 -0
  45. data/lib/unikraft/cloud/resources/service_groups.rb +36 -0
  46. data/lib/unikraft/cloud/resources/users.rb +39 -0
  47. data/lib/unikraft/cloud/resources/volumes.rb +93 -0
  48. data/lib/unikraft/cloud/version.rb +11 -0
  49. data/lib/unikraft/cloud.rb +29 -0
  50. metadata +89 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dda47375979809917658751935223fb4a12cbde8e5ed761be841524b601b8457
4
+ data.tar.gz: 39c23e76e1f9cdb99e55aacadd3e8bf6510e2490bd21a379ac475088edafeb93
5
+ SHA512:
6
+ metadata.gz: 587ed1ba7084a3304737bdd912fadd892c6f82188d110bb34625550e83323288726a1316e5305bfe6abfd9bbc886eee2433161506b8fa5b0e3cd5a07d74ae68e
7
+ data.tar.gz: be46db9d83a23f04cfa908d84f4baa112d1456fc6ec06512c84e4d1ed385440dd3b4590e7925bd97a990701badaf32401771b89fffe1cba667c3c76e02cbd3a3
data/LICENSE.md ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2026, Unikraft GmbH. All rights reserved.
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,587 @@
1
+ # `unikraft-cloud`
2
+
3
+ The official Ruby SDK for the [Unikraft Cloud][ukc] API.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ gem install unikraft-cloud
9
+ ```
10
+
11
+ or, with Bundler, in your `Gemfile`:
12
+
13
+ ```ruby
14
+ gem "unikraft-cloud"
15
+ ```
16
+
17
+ ## Quickstart
18
+
19
+ ```ruby
20
+ require "unikraft/cloud"
21
+
22
+ ukc = Unikraft::Cloud.new(token: ENV["UKC_TOKEN"])
23
+
24
+ # By default the client is account-wide: every metro is asked in parallel and
25
+ # the results are merged, each tagged with the metro it came from.
26
+ ukc.instances.list(details: true) do |inst|
27
+ puts "#{inst.metro} #{inst.name} #{inst.state}"
28
+ end
29
+
30
+ # Creating a resource happens in one metro, so name it.
31
+ fra = ukc.metro(:fra)
32
+ instance = fra.instances.create(
33
+ image: "nginx:latest",
34
+ autostart: true,
35
+ memory_mb: 256,
36
+ service_group: {
37
+ services: [{ port: 443, handlers: %w[tls http], destination_port: 80 }]
38
+ }
39
+ )
40
+
41
+ # Operations chain off a handle, so a ref is written once.
42
+ instance.wait(state: :running)
43
+ ukc.instances.get(name: instance.name).suspend
44
+ fra.instances.delete(uuid: instance.uuid)
45
+ ```
46
+
47
+ ## Authentication
48
+
49
+ Pass a bearer `token:` to `Unikraft::Cloud.new`, or set the `UKC_TOKEN`
50
+ environment variable. Create a token in the
51
+ [Unikraft Cloud dashboard][dashboard].
52
+
53
+ ## The client
54
+
55
+ `Unikraft::Cloud.new(**options)` returns a `Unikraft::Cloud::Client`. Every
56
+ option is optional:
57
+
58
+ | Option | Default | Meaning |
59
+ | -------------------- | ------------------------ | -------------------------------------------------------------------------- |
60
+ | `token:` | `UKC_TOKEN` | Bearer token. |
61
+ | `metro:` | `UKC_METRO` | One metro (`:fra`) or a full `http(s)://` URL. The target of `create`. |
62
+ | `metros:` | `:all`, or `[metro]` | The default scope of reads and bulk operations: `:all`, a metro, or a list. |
63
+ | `base_url:` | | An explicit platform API URL; overrides `metro:`. |
64
+ | `control_plane_url:` | the global control plane | Where metro discovery and `ukc.api.controlplane` go. |
65
+ | `headers:` | `{}` | Extra headers sent with every request. |
66
+ | `timeout:` | `60` | Seconds per connect and read; `nil` waits without limit. |
67
+ | `proxy_from_env:` | `true` | Honour `http_proxy`/`https_proxy`/`no_proxy`. |
68
+ | `user_agent:` | `unikraft-cloud-ruby/…` | The `User-Agent` header. |
69
+ | `transport:` | | Builds the transport from the transport keywords; see [Runtime support](#runtime-support). |
70
+
71
+ The client is thread-safe and meant to be long-lived: build one and share it
72
+ across threads, rather than one per request. Metro discovery and connections
73
+ are shared by everything that comes from it. `close` closes its connections;
74
+ a later call reopens what it needs. For a script, the block form closes the
75
+ client for you and returns the block's value:
76
+
77
+ ```ruby
78
+ count = Unikraft::Cloud.open(metro: :fra) do |ukc|
79
+ ukc.instances.count
80
+ end
81
+ ```
82
+
83
+ ## The two layers: porcelain and plumbing
84
+
85
+ The SDK is explicitly two layers, and you choose per call which one you are in:
86
+
87
+ | Layer | Where | What you get |
88
+ | ------------- | ------------------------------------------------------------- | -------------------------------------------------------------------------------- |
89
+ | **Porcelain** | `ukc.instances`, `ukc.volumes`, … | Short verbs, no envelope, auto-pagination, metro fan-out, chainable handles |
90
+ | **Plumbing** | `Unikraft::Cloud::API::Platform`, `Unikraft::Cloud::API::ControlPlane` | The OpenAPI specification as written: `operationId` methods, raw envelope, one metro per call |
91
+
92
+ The porcelain layer **holds** a plumbing client rather than inheriting from
93
+ one, so the two never blur together, and the raw client is always one method
94
+ away:
95
+
96
+ ```ruby
97
+ # Porcelain: unwrapped, metro-aware.
98
+ inst = ukc.instances.get(name: "web")
99
+
100
+ # Plumbing, from the same client and credentials.
101
+ res = ukc.api.platform.instances.get_instances(count: 10, details: true)
102
+ res.status # => "success"
103
+ res.data.instances
104
+
105
+ # Plumbing, per resource (the escape hatch for anything not wrapped yet).
106
+ ukc.instances.api.get_instance_logs(name: ["web"])
107
+
108
+ # Plumbing, standalone: no porcelain involved.
109
+ api = Unikraft::Cloud::API::Platform.new(
110
+ base_url: "https://api.fra.unikraft.cloud", token: ENV["UKC_TOKEN"]
111
+ )
112
+ api.instances.get_instances(count: 10)
113
+ ```
114
+
115
+ Plumbing clients are metro-scoped by construction: they talk to whatever
116
+ `base_url:` names, and a single call can be redirected with `base_url:`.
117
+ Fanning out across metros is the porcelain layer's job.
118
+
119
+ ### Two APIs: platform and control plane
120
+
121
+ Both Unikraft Cloud APIs defined by our [OpenAPI][openapi] specification are
122
+ wrapped:
123
+
124
+ - **Platform**: metro-scoped resources (instances, volumes, services,
125
+ certificates, autoscale, images). Wrapped idiomatically at the top level;
126
+ raw under `Unikraft::Cloud::API::Platform` and `ukc.api.platform`.
127
+ - **Control plane**: the global (non-metro) API for the account, metros,
128
+ images and self-hosted nodes. Raw only, under
129
+ `Unikraft::Cloud::API::ControlPlane` and `ukc.api.controlplane`.
130
+
131
+ ```ruby
132
+ ukc.api.controlplane.metros.list_metros.data.metros.each do |metro|
133
+ puts "#{metro.iata_code} #{metro.endpoint}"
134
+ end
135
+ ```
136
+
137
+ ## Metros: one account, many regions
138
+
139
+ The platform API is per metro, but an account's instances are spread across
140
+ them. The porcelain layer treats **the metros in scope** as one namespace:
141
+ reads fan out concurrently and are merged, and every result carries the
142
+ `metro` it came from.
143
+
144
+ ```ruby
145
+ ukc = Unikraft::Cloud.new(token: ENV["UKC_TOKEN"])
146
+
147
+ # Every metro the account can reach (discovered once, then cached).
148
+ ukc.instances.each { |inst| puts "#{inst.metro} #{inst.name}" }
149
+
150
+ # Explicit scopes; these also skip metro discovery entirely.
151
+ ukc.metro(:fra).instances.to_a # one metro
152
+ ukc.metros(:fra, :dal).instances.to_a # several
153
+ ukc.instances.list(metros: %i[fra dal]).to_a # just this call
154
+ ukc.instances.list(metros: :all).to_a # back to everything
155
+
156
+ # What the account can reach.
157
+ ukc.available_metros.each { |m| puts "#{m.metro} #{m.base_url}" }
158
+ ```
159
+
160
+ Metros are Symbols or Strings, whichever you have. Set the default scope at
161
+ construction, too:
162
+
163
+ ```ruby
164
+ Unikraft::Cloud.new # all metros (default)
165
+ Unikraft::Cloud.new(metro: :fra) # one metro
166
+ Unikraft::Cloud.new(metros: %i[fra dal]) # several
167
+ ```
168
+
169
+ `UKC_METRO` acts like `metro:`: setting it pins the client to that metro.
170
+ With both `metro:` and `metros:`, `metros:` is the scope and `metro:` stays
171
+ the target of `create`.
172
+
173
+ | Code | Location |
174
+ | ----- | ------------------- |
175
+ | `fra` | Frankfurt, DE |
176
+ | `dal` | Dallas, TX, USA |
177
+ | `sin` | Singapore |
178
+ | `was` | Washington, DC, USA |
179
+ | `sfo` | San Francisco, USA |
180
+
181
+ Metro discovery asks the control plane and trusts the endpoint it reports, so
182
+ new metros work without an SDK upgrade. `Unikraft::Cloud::KNOWN_METROS` lists
183
+ the ones known when this version was published.
184
+
185
+ ### Rules the fan-out follows
186
+
187
+ - **Reads** cover the whole scope. Pages are interleaved in arrival order, so
188
+ a slow metro never holds up a fast one, and each metro fetches its next
189
+ page only as the last one is used up. Leaving early (`first`, `break`)
190
+ stops the metros still in flight. An external enumerator (`next`) left
191
+ midway cannot stop them; they stay parked, sending nothing, so prefer
192
+ `each`, `first` or `lazy`.
193
+ - **A name identifies a resource within a metro.** The same name can exist in
194
+ several metros at once, usually because you deployed the same thing
195
+ everywhere, so a name plus a wide scope may match more than one resource.
196
+ See [Names across metros](#names-across-metros).
197
+ - **`create` never fans out.** It needs one metro: `create(metro: :dal, ...)`,
198
+ the scope's, or the default metro (`metro:` / `UKC_METRO` / `fra`) when
199
+ the scope is wider. A scope of several named metros must include the
200
+ default; otherwise `create` raises `kind: :config` rather than guess.
201
+ - **Bulk operations are bounded by the scope.** Refs are located first and one
202
+ call goes to each metro that matched, so `delete(name: "web")` under a wide
203
+ scope deletes every `web` in scope. Narrow the scope or qualify the ref to
204
+ act on one.
205
+ - **The control plane is global**, so it is unaffected by scope.
206
+
207
+ ### Names across metros
208
+
209
+ Names are scoped to a metro, so the same name can name a different resource
210
+ in every metro. Three ways to say what you mean:
211
+
212
+ ```ruby
213
+ # 1. Qualify the ref. No search, no discovery: one request.
214
+ ukc.instances.get(name: "web", metro: :fra).suspend
215
+
216
+ # 2. Narrow the scope, which qualifies every ref through it.
217
+ ukc.metro(:fra).instances.get(name: "web").suspend
218
+
219
+ # 3. Address every metro holding it, on purpose.
220
+ ukc.instances.where(name: "web").suspend # one result per metro
221
+ ```
222
+
223
+ `uuid:` refs rarely need qualifying: a UUID identifies one resource wherever
224
+ it lives.
225
+
226
+ `get` insists on exactly one match, because the next thing you write might be
227
+ a mutation. When a ref matches in several metros it raises an
228
+ `AmbiguousRefError` carrying the matches, so recovering costs no further
229
+ requests:
230
+
231
+ ```ruby
232
+ begin
233
+ ukc.instances.get(name: "web").suspend
234
+ rescue Unikraft::Cloud::AmbiguousRefError => e
235
+ e.metros # => ["fra", "dal", "sin"]
236
+ e.matches # the instances themselves, each tagged with #metro
237
+ end
238
+ ```
239
+
240
+ `where` is the deliberate plural. It locates the matches once, is Enumerable
241
+ over them, and runs each operation in the metro that holds each one:
242
+
243
+ ```ruby
244
+ web = ukc.instances.where(name: "web")
245
+
246
+ web.metros # => ["fra", "dal", "sin"]
247
+ web.size # => 3
248
+ web.each { |inst| puts inst.state } # the instances
249
+ web.suspend # one result per metro
250
+ web.edit { |e| e.set(memory_mb: 512) }
251
+ ```
252
+
253
+ Set operations return Arrays and follow the same partial-failure rule as
254
+ reads: the successes ride on the raised `MetroFanoutError` as `results`.
255
+ `where` exists on instances, volumes, services and certificates.
256
+
257
+ ### Partial failure
258
+
259
+ A metro that is unreachable does not throw away the rest of the answer.
260
+ Healthy metros are drained first, then a `MetroFanoutError` naming the
261
+ failures is raised:
262
+
263
+ ```ruby
264
+ begin
265
+ ukc.instances.each { |inst| puts inst.name } # fra, dal, was delivered
266
+ rescue Unikraft::Cloud::MetroFanoutError => e
267
+ e.message # => "1 of 4 metros failed: sin (503)"
268
+ e.failures # => [{ metro: "sin", error: #<Unikraft::Cloud::Error ...> }]
269
+ end
270
+ ```
271
+
272
+ For a bulk operation, which cannot yield as it goes, the results that did
273
+ succeed are attached to the raised error as `results`.
274
+
275
+ ## Lists are lazy Enumerables
276
+
277
+ `list` returns an `Enumerator` that pages through every metro in scope on
278
+ demand, or takes a block (and then returns the client, like `each`).
279
+ `tags:` filters instances and volumes, which the API filters by tag. Each
280
+ resource client is itself `Enumerable` over
281
+ every resource in scope (with details), so the usual methods just work:
282
+
283
+ ```ruby
284
+ ukc.instances.list(details: true, tags: ["prod"]).each { |i| puts i.name }
285
+ ukc.instances.list(page_size: 50).first(10)
286
+
287
+ ukc.instances.select { |i| i.state == "running" }
288
+ ukc.instances.count
289
+ ukc.volumes.map(&:size_mb).sum
290
+ ```
291
+
292
+ Results are models: one reader per field, `#[]` for any key, `#to_h`, and
293
+ pattern matching, including the `metro` tag:
294
+
295
+ ```ruby
296
+ case ukc.instances.first
297
+ in { state: "running", metro: "fra" } then puts "running in Frankfurt"
298
+ in { name: } then puts "#{name} is elsewhere"
299
+ end
300
+ ```
301
+
302
+ ## Chainable handles
303
+
304
+ Single-resource operations return a **handle**: a reference to one resource in
305
+ one metro. `get` sends nothing; reading an attribute resolves the handle once
306
+ and memoizes the resource:
307
+
308
+ ```ruby
309
+ web = ukc.instances.get(name: "web") # no request yet
310
+ web.state # => "running" (located and read once)
311
+ web.running? # => true (no further request)
312
+ web.reload # read again, in place
313
+ ```
314
+
315
+ Ruby has no `await`, and a lazy mutation that nobody forced would silently
316
+ never run. So mutations run **immediately** and return a new handle carrying
317
+ what that endpoint reported, at the same location, so calls still chain:
318
+
319
+ ```ruby
320
+ ukc.instances.get(name: "web").suspend.state # => "standby"
321
+ ukc.instances.get(name: "web").update(memory_mb: 512)
322
+
323
+ ukc.metro(:fra).instances
324
+ .create(image: "nginx:latest")
325
+ .wait(state: :running, within: 30)
326
+ .logs(offset: -4096)
327
+
328
+ ukc.volumes.get(name: "data").attach(attach_to: { name: "web" }, at: "/data")
329
+ ```
330
+
331
+ Each step runs once. What locating costs depends on the scope:
332
+
333
+ | Ref and scope | `get(...).suspend` |
334
+ | --------------------------------- | ----------------------------------------------------------------- |
335
+ | `name:` and `metro:`, any scope | 1 request: the suspend. The ref says where. |
336
+ | `name:`, one metro in scope | 1 request: the scope says where. |
337
+ | `name:`, many metros | Locate first (one concurrent read per metro), then suspend where it lives, or raise `AmbiguousRefError` if several match. |
338
+
339
+ Handles also answer where they landed, and a mutating step carries what its
340
+ endpoint reports (`suspend` reports `uuid`, `name`, `state` and
341
+ `previous_state`, not a full instance; read it with `handle[:previous_state]`,
342
+ and a declared attribute the reply leaves out, such as `memory_mb`, is read
343
+ once from the platform on first use):
344
+
345
+ ```ruby
346
+ web = ukc.instances.get(name: "web")
347
+ web.metro # => "dal"
348
+ web.location # => #<data Location ref=..., metro="dal", base_url="https://api.dal.unikraft.cloud">
349
+ web.to_h # the resource as a Hash
350
+ ```
351
+
352
+ A handle is meant for one thread; the client behind it is shared.
353
+
354
+ Every idiomatic method is also available in non-chained form, the ref keys
355
+ (`name:`, `uuid:`, `metro:`) locating the resource and the rest going to the
356
+ operation: `ukc.instances.logs(name: "web", offset: -4096)` is exactly
357
+ `get(name: "web").logs(offset: -4096)`. `start`, `stop`, `suspend` and
358
+ `delete` on the collection are bulk operations instead; see [Refs](#refs).
359
+
360
+ ## Updating a resource
361
+
362
+ The API models an update as a list of `{ prop:, op:, value: }` triples. That
363
+ is the plumbing. Idiomatically you pass **keyword arguments** and the op is
364
+ worked out for you: a value sets the property, `nil` removes it, and an
365
+ omitted property is left alone, following JSON Merge Patch:
366
+
367
+ ```ruby
368
+ ukc.instances.get(name: "web").update(memory_mb: 512, vcpus: 2)
369
+ ukc.instances.get(name: "web").update(env: { LOG_LEVEL: "debug" })
370
+ ukc.instances.get(name: "web").update(autokill: nil) # remove it
371
+ ukc.volumes.get(name: "data").update(size_mb: 2048)
372
+ ukc.services.get(name: "web").update(soft_limit: 5, hard_limit: 20)
373
+ ```
374
+
375
+ `image` takes the same string shorthand as `create`. `headers:` and
376
+ `timeout:` are always call options, never properties:
377
+ `update(memory_mb: 512, timeout: 10)`.
378
+
379
+ When `set` is not what you mean (merging into a property, or removing
380
+ individual members), stage the operations with `edit` and send them as one
381
+ request, from a block or chained:
382
+
383
+ ```ruby
384
+ ukc.instances.get(name: "web").edit do |e|
385
+ e.set(memory_mb: 512)
386
+ e.add(env: { LOG_LEVEL: "debug" }, tags: ["prod"])
387
+ e.remove(env: ["OLD_FLAG"], tags: ["staging"])
388
+ end
389
+
390
+ ukc.instances.get(name: "web").edit
391
+ .set(memory_mb: 512)
392
+ .remove(env: ["OLD_FLAG"])
393
+ .apply
394
+
395
+ # `nil` in remove removes the property outright, not just some members.
396
+ ukc.services.edit(name: "web") { |e| e.remove(domains: nil) }
397
+ ```
398
+
399
+ `remove` is the API's `del`. The block form applies when the block returns;
400
+ the chained form applies on `apply`. Both return a handle like any other
401
+ operation, so chaining continues:
402
+
403
+ ```ruby
404
+ ukc.metro(:fra).instances
405
+ .edit(name: "web")
406
+ .set(memory_mb: 1024)
407
+ .apply
408
+ .wait(state: :running)
409
+ ```
410
+
411
+ Both forms are one request, and both are available without a handle
412
+ (`ukc.instances.update(name: "web", memory_mb: 512)`,
413
+ `ukc.instances.edit(name: "web")`). The raw triples still work as an escape
414
+ hatch:
415
+ `ukc.instances.update({ name: "web" }, [{ prop: "memory_mb", op: "set", value: 512 }])`.
416
+
417
+ ## Refs
418
+
419
+ Every operation on an existing resource takes a **ref**: either `name:` or
420
+ `uuid:`, never both (passing both raises `ArgumentError`). The API validates
421
+ each identifier it is given, so a name sent in the `uuid` filter fails with
422
+ `Invalid uuid '<name>'`: the ref makes you state which kind you hold, and only
423
+ that field is sent.
424
+
425
+ ```ruby
426
+ ukc.instances.get(name: "web")
427
+ ukc.instances.get(uuid: "550e8400-e29b-41d4-a716-446655440000")
428
+
429
+ # A name belongs to a metro, so it can be qualified; see "Names across metros".
430
+ ukc.instances.get(name: "web", metro: :fra)
431
+
432
+ # Bulk operations take one ref, as keywords or a Hash, or an Array of them.
433
+ ukc.instances.stop(name: "web")
434
+ ukc.instances.stop([{ name: "web" }, { uuid: "550e8400-e29b-41d4-a716-446655440000" }])
435
+ ```
436
+
437
+ ## Idiomatic methods
438
+
439
+ | Resource | Methods |
440
+ | ------------------ | --------------------------------------------------------------------------------------------------------- |
441
+ | `ukc.instances` | `create`, `get`, `where`, `list`, `each` (Enumerable), `update`, `edit`, `delete`, `start`, `stop`, `suspend`, `wait`, `logs`, `metrics`, `history` |
442
+ | `ukc.volumes` | `create`, `get`, `where`, `list`, `each`, `update`, `edit`, `delete`, `attach`, `detach` |
443
+ | `ukc.services` | `create`, `get`, `where`, `list`, `each`, `update`, `edit`, `delete` |
444
+ | `ukc.certificates` | `create`, `get`, `where`, `list`, `each`, `update`, `delete` |
445
+ | `ukc.users` | `quotas`, `quotas(uuid:)` |
446
+
447
+ Handles add the per-resource operations: `reload`, `start`, `stop`,
448
+ `suspend`, `delete`, `update`, `edit`, `wait`, `logs`, `metrics`, `history`
449
+ and the state predicates (`running?`, `stopped?`, `standby?`, …) on an
450
+ instance; `attach`, `detach`, `update`, `edit`, `delete` on a volume;
451
+ `update`, `edit` and `delete` on a service group; `update` (with `chain:` and
452
+ `pkey:`) and `delete` on a certificate. Every handle has `metro`, `location`,
453
+ `model`, `to_h`, `[]` and a reader per attribute.
454
+
455
+ Anything not listed (autoscale, the image registry, node information, the
456
+ whole control plane) is reachable raw via `ukc.api.platform` and
457
+ `ukc.api.controlplane`:
458
+
459
+ ```ruby
460
+ ukc.api.platform.autoscale.get_autoscale_configurations(name: ["web"])
461
+ ukc.api.platform.images.get_images
462
+ ```
463
+
464
+ See [`examples/`](./examples) for complete programs.
465
+
466
+ ## Errors
467
+
468
+ Any network failure or non-2xx response raises a `Unikraft::Cloud::Error`:
469
+
470
+ ```ruby
471
+ begin
472
+ ukc.metro(:fra).instances.get(name: "does-not-exist").state
473
+ rescue Unikraft::Cloud::Error => e
474
+ warn [e.kind, e.status, e.message, e.errors].inspect
475
+ end
476
+ ```
477
+
478
+ `e.kind` says which layer failed:
479
+
480
+ | `kind` | Meaning |
481
+ | ---------- | -------------------------------------------------------------------------------------------------------------------- |
482
+ | `:http` | The server answered with a non-2xx status. `e.status` and `e.errors` carry the detail. |
483
+ | `:network` | The request never got an answer: a refused connection, a reset, a DNS miss, an unreachable proxy. |
484
+ | `:parse` | The server answered, but the body is not the JSON the operation expects. |
485
+ | `:fanout` | A multi-metro operation that partly failed, or an unusable scope; see `MetroFanoutError` above. |
486
+ | `:config` | The call could never be sent as configured: two options that contradict each other, or a body on a `GET`. |
487
+ | `:timeout` | A wait ran out of time. It carries no `status`, because no single request failed; the last failure is in `e.cause`. |
488
+
489
+ `AmbiguousRefError` and `MetroFanoutError` are subclasses, so one
490
+ `rescue Unikraft::Cloud::Error` covers everything the SDK raises. An invalid
491
+ argument, such as a ref with both `name:` and `uuid:`, raises `ArgumentError`
492
+ instead.
493
+
494
+ ### Waiting
495
+
496
+ The transport sends each request exactly once. It has no retries, and nothing
497
+ in the SDK retries a call you made.
498
+
499
+ Waiting is a separate, explicit step, and it takes one of two shapes:
500
+
501
+ - **The server waits.** `instance.wait(state: :running, within: 30)`
502
+ forwards the state and the deadline (in seconds; `-1` waits indefinitely)
503
+ to the platform, which holds the connection open. One request; the read
504
+ timeout of that request is stretched to outlive the wait. `timeout:` is
505
+ the HTTP timeout, as on every other call.
506
+ - **The SDK polls.** Where the platform has nothing to hold open, the SDK
507
+ asks again on a schedule. `Unikraft::Cloud::Core::Ready.wait_until_ready`
508
+ owns that loop: the delay doubles from 100 ms to 2 s with jitter, and the
509
+ deadline is 60 s. Only a failure that can still change is worth another
510
+ probe: a network fault, or a `404`/`502`/`503`/`504`. A `401` or a `403` is
511
+ raised at once, so a rejected token reports itself instead of timing out.
512
+
513
+ Both raise on failure rather than returning. A poll that runs out of time
514
+ raises `kind: :timeout`.
515
+
516
+ ## Self-hosted and staging deployments
517
+
518
+ `metro:` (and `UKC_METRO`) also accepts a full `http(s)://` base URL, which is
519
+ used verbatim instead of being expanded into
520
+ `https://api.<metro>.unikraft.cloud`. A trailing `/v1` is dropped, since every
521
+ operation path already carries it:
522
+
523
+ ```sh
524
+ export UKC_METRO=https://api.staging.example.internal
525
+ ```
526
+
527
+ A named endpoint is the only endpoint there is: no metro discovery is
528
+ attempted and no hostnames are invented, whatever the scope says. Point the
529
+ control plane at a matching deployment with `control_plane_url:`:
530
+
531
+ ```ruby
532
+ ukc = Unikraft::Cloud.new(
533
+ token: ENV["UKC_TOKEN"],
534
+ metro: "https://api.staging.example.internal",
535
+ control_plane_url: "https://controlplane.staging.example.internal"
536
+ )
537
+ ```
538
+
539
+ ## Runtime support
540
+
541
+ The SDK needs Ruby 3.3 or later (CI covers 3.3, 3.4 and 4.0) and has no
542
+ runtime dependencies: it uses `net/http`, `json` and `uri` from the standard
543
+ library.
544
+
545
+ To customise the transport (a different HTTP stack, instrumentation, or a fake
546
+ in tests), pass `transport:`, a callable that receives the transport keywords
547
+ (`base_url:`, `token:`, `headers:`, `timeout:`, `proxy_from_env:`,
548
+ `user_agent:`) and returns an object with `request`, `bytes` and `stream`, as
549
+ `Unikraft::Cloud::Core::ApiClient` has:
550
+
551
+ ```ruby
552
+ ukc = Unikraft::Cloud.new(
553
+ transport: ->(**config) { Unikraft::Cloud::Core::ApiClient.new(**config) }
554
+ )
555
+ ```
556
+
557
+ ## Debugging with a proxy (MITM)
558
+
559
+ The client honours the standard proxy environment variables through
560
+ `Net::HTTP`, so you can route traffic through a man-in-the-middle proxy such
561
+ as [mitmproxy](https://mitmproxy.org), Charles, or Proxyman without any code
562
+ change:
563
+
564
+ ```sh
565
+ export https_proxy=http://127.0.0.1:8080
566
+ export SSL_CERT_FILE=~/.mitmproxy/mitmproxy-ca-cert.pem # trust the proxy CA
567
+ ruby your_script.rb
568
+ ```
569
+
570
+ `http_proxy`, `https_proxy` and `no_proxy` are recognised, in lower- or
571
+ upper-case. `SSL_CERT_FILE` replaces OpenSSL's default trust store for the
572
+ process, which is what lets it trust the proxy's certificate; unset it when
573
+ you are done. Opt out of the proxy variables per client with
574
+ `Unikraft::Cloud.new(proxy_from_env: false)`.
575
+
576
+ ## Contributing
577
+
578
+ The plumbing layer is generated; see [CONTRIBUTING.md](./CONTRIBUTING.md) for
579
+ how to regenerate it and work on the SDK.
580
+
581
+ ## License
582
+
583
+ BSD-3-Clause. See [LICENSE.md](./LICENSE.md).
584
+
585
+ [ukc]: https://unikraft.com
586
+ [openapi]: https://github.com/unikraft-cloud/openapi
587
+ [dashboard]: https://console.unikraft.cloud