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
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ # SPDX-License-Identifier: BSD-3-Clause
4
+ # Copyright (c) 2026, Unikraft GmbH.
5
+
6
+ require "uri"
7
+
8
+ module Unikraft
9
+ module Cloud
10
+ module Core
11
+ # Small URL helpers shared by the transport and the metro resolution.
12
+ module URL
13
+ module_function
14
+
15
+ # Remove every trailing "/" from a URL.
16
+ #
17
+ # @param url [String]
18
+ # @return [String]
19
+ def strip_trailing_slashes(url)
20
+ url.to_s.sub(%r{/+\z}, "")
21
+ end
22
+
23
+ # Percent-encode one path segment, so a value with a `/`, `?` or `#`
24
+ # in it cannot change the path or start a query.
25
+ #
26
+ # @param value [#to_s]
27
+ # @return [String]
28
+ def escape_segment(value)
29
+ URI.encode_www_form_component(value.to_s).gsub("+", "%20")
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,194 @@
1
+ # frozen_string_literal: true
2
+
3
+ # SPDX-License-Identifier: BSD-3-Clause
4
+ # Copyright (c) 2026, Unikraft GmbH.
5
+
6
+ require "json"
7
+
8
+ module Unikraft
9
+ module Cloud
10
+ # Base class of every generated model: a read-only view over a JSON
11
+ # object with one reader per declared property.
12
+ #
13
+ # A model is built from a Hash with symbol or string keys. Every field is
14
+ # optional (a reader returns nil for a missing key) and unknown keys are
15
+ # kept, so a newer server never breaks an older SDK: they stay reachable
16
+ # through {#[]} and {#to_h}. Nested objects become their declared model
17
+ # class, recursively.
18
+ #
19
+ # @example
20
+ # class Instance < Unikraft::Cloud::Model
21
+ # attribute :name
22
+ # end
23
+ # inst = Instance.new(name: "web", extra: 1)
24
+ # inst.name # => "web"
25
+ # inst[:extra] # => 1
26
+ # inst.to_h # => { name: "web", extra: 1 }
27
+ class Model
28
+ # Fields {#inspect} masks: keys, tokens and credentials the API returns.
29
+ REDACTED = %i[authorization credentials pkey secret token].freeze
30
+ MASK = Object.new.tap { |o| o.define_singleton_method(:inspect) { "[REDACTED]" } }.freeze
31
+ private_constant :MASK
32
+
33
+ class << self
34
+ # @return [Hash{Symbol => Array}] the declared properties, by wire key
35
+ def attributes
36
+ @attributes ||= superclass <= Model ? superclass.attributes.dup : {}
37
+ end
38
+
39
+ # Declare a property and define its reader.
40
+ #
41
+ # @param name [Symbol] the reader, and the wire key unless `key:` is given
42
+ # @param type [String, Class, nil] the model class of the value, by
43
+ # constant name relative to this class's namespace; nil for scalars
44
+ # @param array [Boolean] the value is an array of `type`
45
+ # @param map [Boolean] the value is an object whose values are `type`
46
+ # @param key [Symbol] the wire key, when it is no valid reader name
47
+ def attribute(name, type = nil, array: false, map: false, key: name)
48
+ attributes[key.to_sym] = [type, array, map].freeze
49
+ # A reader the base class already defines (`metro`) keeps its own.
50
+ return if Model.method_defined?(name)
51
+
52
+ define_method(name) { @values[key.to_sym] }
53
+ end
54
+
55
+ # Build a model from a Hash, pass a model or nil through.
56
+ #
57
+ # @param value [Hash, Model, nil]
58
+ # @return [Model, nil]
59
+ def from(value)
60
+ value.nil? || value.is_a?(self) ? value : new(value)
61
+ end
62
+ end
63
+
64
+ # @param values [Hash, Model] the JSON object
65
+ def initialize(values = {})
66
+ values = values.to_h unless values.is_a?(Hash)
67
+ @values = values.to_h do |k, v|
68
+ key = k.to_sym
69
+ [key, cast(self.class.attributes[key], v)]
70
+ end.freeze
71
+ @metro = nil
72
+ end
73
+
74
+ # Read a field by its wire key, declared or not.
75
+ #
76
+ # @param key [Symbol, String]
77
+ def [](key)
78
+ @values[key.to_sym]
79
+ end
80
+
81
+ # The metro this model was read from, when the porcelain tagged it (see
82
+ # {#with_metro}), or else its own `metro` field.
83
+ #
84
+ # @return [String, nil]
85
+ def metro
86
+ @metro || @values[:metro]
87
+ end
88
+
89
+ # A copy tagged with the metro it came from. The tag is not part of
90
+ # {#to_h}, so it never reaches the wire.
91
+ #
92
+ # @param metro [String]
93
+ # @return [Model]
94
+ def with_metro(metro)
95
+ copy = dup
96
+ copy.instance_variable_set(:@metro, metro)
97
+ copy
98
+ end
99
+
100
+ # The JSON object, nested models included, with symbol keys.
101
+ #
102
+ # @return [Hash]
103
+ def to_h
104
+ @values.transform_values { |v| plain(v) }
105
+ end
106
+
107
+ # Pattern matching on the fields, and on the `metro` tag when set.
108
+ #
109
+ # @example
110
+ # case inst
111
+ # in { state: "running", metro: "fra" } then ...
112
+ # end
113
+ def deconstruct_keys(_keys)
114
+ @metro ? @values.merge(metro: @metro) : @values
115
+ end
116
+
117
+ # @return [String]
118
+ def to_json(*)
119
+ to_h.to_json(*)
120
+ end
121
+
122
+ def ==(other)
123
+ other.instance_of?(self.class) && other.to_h == to_h && other.metro == metro
124
+ end
125
+ alias eql? ==
126
+
127
+ def hash
128
+ [self.class, to_h, metro].hash
129
+ end
130
+
131
+ def inspect = "#<#{self.class.name || "Model"} #{inspect_fields}>"
132
+ alias to_s inspect
133
+
134
+ # The fields as {#inspect} shows them, secrets ({REDACTED}) masked at
135
+ # any depth, untyped Hashes included.
136
+ #
137
+ # @api private
138
+ # @return [String]
139
+ def inspect_fields
140
+ fields = redact(@values).map { |k, v| "#{k}=#{v.inspect}" }
141
+ fields << "metro=#{@metro.inspect}" if @metro
142
+ fields.join(", ")
143
+ end
144
+
145
+ private
146
+
147
+ def cast(spec, value)
148
+ type, array, map = spec
149
+ return value if type.nil? || value.nil?
150
+
151
+ klass = resolve(type)
152
+ if array && value.is_a?(Array)
153
+ value.map { |v| cast_one(klass, v) }
154
+ elsif map && value.is_a?(Hash)
155
+ value.transform_values { |v| cast_one(klass, v) }
156
+ else
157
+ cast_one(klass, value)
158
+ end
159
+ end
160
+
161
+ # Anything other than an object (a newer server's shape) is kept as is.
162
+ def cast_one(klass, value)
163
+ value.is_a?(Hash) ? klass.new(value) : value
164
+ end
165
+
166
+ def resolve(type)
167
+ return type if type.is_a?(Class)
168
+
169
+ namespace = self.class.name.to_s.rpartition("::").first
170
+ Object.const_get(namespace.empty? ? type : "#{namespace}::#{type}")
171
+ end
172
+
173
+ def redact(value)
174
+ case value
175
+ when Hash then value.to_h { |k, v| [k, secret?(k) ? MASK : redact(v)] }
176
+ when Array then value.map { |v| redact(v) }
177
+ else value
178
+ end
179
+ end
180
+
181
+ # HTTP headers come in any case, e.g. `Authorization`.
182
+ def secret?(key) = REDACTED.include?(key.to_s.downcase.to_sym)
183
+
184
+ def plain(value)
185
+ case value
186
+ when Model then value.to_h
187
+ when Array then value.map { |v| plain(v) }
188
+ when Hash then value.transform_values { |v| plain(v) }
189
+ else value
190
+ end
191
+ end
192
+ end
193
+ end
194
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ # SPDX-License-Identifier: BSD-3-Clause
4
+ # Copyright (c) 2026, Unikraft GmbH.
5
+
6
+ require_relative "../core/resource"
7
+ require_relative "../api/platform"
8
+
9
+ module Unikraft
10
+ module Cloud
11
+ module Resources
12
+ # A handle on one certificate (see {Core::Handle}). A certificate is
13
+ # replaced rather than patched, so `update` takes the new `chain:` and
14
+ # `pkey:`, and there is no `edit`.
15
+ class CertificateHandle < Core::Handle
16
+ undef_method :edit
17
+
18
+ # Replace the certificate chain and private key.
19
+ #
20
+ # @example
21
+ # ukc.certificates.get(name: "star").update(chain: pem, pkey: key)
22
+ # @param spec [Hash] `chain:`, `pkey:`; plus `headers:`/`timeout:`
23
+ # @return [CertificateHandle]
24
+ def update(**spec)
25
+ call = spec.slice(:headers, :timeout)
26
+ mutate(@resource.spec.update, [wire(call).merge(spec.except(*call.keys))], **call)
27
+ end
28
+
29
+ attributes_of API::Platform::Models::Certificate
30
+ end
31
+
32
+ # Every certificate matching one ref, one per metro.
33
+ class CertificateSet < Core::HandleSet
34
+ undef_method :edit
35
+ end
36
+
37
+ # TLS certificates.
38
+ class Certificates < Core::Resource
39
+ API_CLASS = API::Platform::CertificatesApi
40
+ HANDLE = CertificateHandle
41
+ SET = CertificateSet
42
+ SPEC = Spec.new(noun: "certificate", key: :certificates, list: :get_certificates,
43
+ create: :create_certificate, delete: :delete_certificates,
44
+ update: :update_certificates)
45
+
46
+ undef_method :edit
47
+
48
+ # Shorthand for `get(ref).update(chain:, pkey:)`.
49
+ # @return [CertificateHandle]
50
+ def update(ref = nil, **kwargs)
51
+ handle, spec = on(ref, kwargs)
52
+ handle.update(**spec)
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,172 @@
1
+ # frozen_string_literal: true
2
+
3
+ # SPDX-License-Identifier: BSD-3-Clause
4
+ # Copyright (c) 2026, Unikraft GmbH.
5
+
6
+ require_relative "../core/resource"
7
+ require_relative "../api/platform"
8
+
9
+ module Unikraft
10
+ module Cloud
11
+ # The idiomatic ("porcelain") resource clients.
12
+ module Resources
13
+ # A handle on one instance (see {Core::Handle}). Readers such as
14
+ # `state` and `memory_mb` resolve it once; `running?`, `stopped?` and
15
+ # the other predicates compare `state` with each API state.
16
+ #
17
+ # @example
18
+ # web = ukc.instances.get(name: "web")
19
+ # web.running? # one read, memoized
20
+ # web.suspend.state # => "standby"
21
+ # fra.instances.create(image: "nginx:latest")
22
+ # .wait(state: :running, within: 30)
23
+ # .logs(offset: -4096)
24
+ class InstanceHandle < Core::Handle
25
+ # Start the instance.
26
+ # @return [InstanceHandle] carrying what the endpoint reported
27
+ def start(**call) = mutate(:start_instances, [wire(call)], **call)
28
+
29
+ # Stop the instance.
30
+ # @return [InstanceHandle]
31
+ def stop(**call) = mutate(:stop_instances, [wire(call)], **call)
32
+
33
+ # Suspend the instance.
34
+ # @return [InstanceHandle]
35
+ def suspend(**call) = mutate(:suspend_instances, [wire(call)], **call)
36
+
37
+ # Block until the instance reaches a state. The platform holds the
38
+ # request open until then, and fails it when `within` passes first,
39
+ # so this raises rather than returning.
40
+ #
41
+ # @param state [Symbol, String, nil] e.g. `:running` (the API's
42
+ # default)
43
+ # @param within [Integer, nil] seconds the platform waits; -1 waits
44
+ # indefinitely. The read timeout of the request is stretched to
45
+ # cover it, unless `timeout:` sets it.
46
+ # @param call [Hash] `headers:`, `timeout:` (the HTTP timeout, as
47
+ # on every call)
48
+ # @return [InstanceHandle] carrying the observed state
49
+ def wait(state: nil, within: nil, **call)
50
+ http = call
51
+ # The connection must outlive the server-side wait.
52
+ http = { timeout: within.negative? ? nil : within + 10 }.merge(call) if within
53
+ http = effective(http)
54
+ loc = location(**http)
55
+ res = @resource.api.wait_instances(**loc.ref.query, state: state && [state.to_s],
56
+ timeout_s: within && [within],
57
+ **@resource.call_options(loc, http))
58
+ following(loc, one(res), effective(call))
59
+ end
60
+
61
+ # The console log. `output` is base64, as the API sends it.
62
+ #
63
+ # @param offset [Integer, nil] byte offset; negative counts from the end
64
+ # @param limit [Integer, nil] at most this many bytes
65
+ # @return [Model] tagged with the metro
66
+ def logs(offset: nil, limit: nil, **call)
67
+ call = effective(call)
68
+ loc = location(**call)
69
+ one(@resource.api.get_instance_logs(**loc.ref.query, offset: offset && [offset],
70
+ limit: limit && [limit],
71
+ **@resource.call_options(loc, call)))
72
+ end
73
+
74
+ # Resource-usage metrics.
75
+ # @return [Model]
76
+ def metrics(**call)
77
+ call = effective(call)
78
+ loc = location(**call)
79
+ one(@resource.api.get_instance_metrics(**loc.ref.query,
80
+ **@resource.call_options(loc, call)))
81
+ end
82
+
83
+ # The state-change history: this instance's entries, each tagged
84
+ # with the metro. A failed per-instance result raises.
85
+ # @return [Array<Model>]
86
+ def history(**call)
87
+ call = effective(call)
88
+ loc = location(**call)
89
+ res = @resource.api.get_instance_history(**loc.ref.query,
90
+ **@resource.call_options(loc, call))
91
+ Array(one(res, :instances)[:history]).map { |entry| entry.with_metro(loc.metro) }
92
+ end
93
+
94
+ API::Platform::Models::InstanceState::ALL.each do |value|
95
+ define_method(:"#{value}?") { state == value }
96
+ end
97
+
98
+ attributes_of API::Platform::Models::Instance
99
+ end
100
+
101
+ # Every instance matching one ref, one per metro (see
102
+ # {Core::HandleSet}).
103
+ #
104
+ # @example
105
+ # ukc.instances.where(name: "web").suspend
106
+ class InstanceSet < Core::HandleSet
107
+ forward :start, :stop, :suspend, :wait, :logs, :metrics, :history
108
+ end
109
+
110
+ # Instances (microVMs). Enumerable over every instance in scope.
111
+ #
112
+ # @example
113
+ # ukc.instances.list(details: true).each { |i| puts "#{i.metro} #{i.name}" }
114
+ # ukc.instances.select { |i| i.state == "running" }
115
+ # ukc.instances.get(name: "web").suspend
116
+ # ukc.instances.stop([{ name: "a" }, { name: "b" }])
117
+ class Instances < Core::Resource
118
+ API_CLASS = API::Platform::InstancesApi
119
+ HANDLE = InstanceHandle
120
+ SET = InstanceSet
121
+ SPEC = Spec.new(noun: "instance", key: :instances, list: :get_instances,
122
+ create: :create_instance, delete: :delete_instances,
123
+ update: :update_instances)
124
+
125
+ # Start one or more instances, in whichever metros hold them.
126
+ # @param (see Core::Resource#delete)
127
+ # @return [Array<Model>]
128
+ def start(refs = nil, **kwargs) = bulk(:start_instances, refs, kwargs)
129
+
130
+ # Stop one or more instances.
131
+ # @return [Array<Model>]
132
+ def stop(refs = nil, **kwargs) = bulk(:stop_instances, refs, kwargs)
133
+
134
+ # Suspend one or more instances.
135
+ # @return [Array<Model>]
136
+ def suspend(refs = nil, **kwargs) = bulk(:suspend_instances, refs, kwargs)
137
+
138
+ # Shorthand for `get(ref).wait(...)`: the ref keys (and `metros:`)
139
+ # locate the instance, the rest go to the operation.
140
+ #
141
+ # @example
142
+ # ukc.instances.wait(name: "web", state: :running, within: 30)
143
+ # @return [InstanceHandle]
144
+ def wait(ref = nil, **kwargs)
145
+ handle, rest = on(ref, kwargs)
146
+ handle.wait(**rest)
147
+ end
148
+
149
+ # Shorthand for `get(ref).logs(...)`.
150
+ # @return [Model]
151
+ def logs(ref = nil, **kwargs)
152
+ handle, rest = on(ref, kwargs)
153
+ handle.logs(**rest)
154
+ end
155
+
156
+ # Shorthand for `get(ref).metrics`.
157
+ # @return [Model]
158
+ def metrics(ref = nil, **kwargs)
159
+ handle, rest = on(ref, kwargs)
160
+ handle.metrics(**rest)
161
+ end
162
+
163
+ # Shorthand for `get(ref).history`.
164
+ # @return [Array<Model>]
165
+ def history(ref = nil, **kwargs)
166
+ handle, rest = on(ref, kwargs)
167
+ handle.history(**rest)
168
+ end
169
+ end
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ # SPDX-License-Identifier: BSD-3-Clause
4
+ # Copyright (c) 2026, Unikraft GmbH.
5
+
6
+ require_relative "../core/resource"
7
+ require_relative "../api/platform"
8
+
9
+ module Unikraft
10
+ module Cloud
11
+ module Resources
12
+ # A handle on one service group (see {Core::Handle}): `update`, `edit`
13
+ # and `delete`.
14
+ #
15
+ # @example
16
+ # ukc.services.get(name: "web").update(soft_limit: 5, hard_limit: 20)
17
+ # ukc.services.edit(name: "web") { |e| e.remove(domains: nil) }
18
+ class ServiceGroupHandle < Core::Handle
19
+ attributes_of API::Platform::Models::ServiceGroup
20
+ end
21
+
22
+ # Every service group matching one ref, one per metro.
23
+ class ServiceGroupSet < Core::HandleSet; end
24
+
25
+ # Service groups (load-balanced networking), as `ukc.services`.
26
+ class ServiceGroups < Core::Resource
27
+ API_CLASS = API::Platform::ServicesApi
28
+ HANDLE = ServiceGroupHandle
29
+ SET = ServiceGroupSet
30
+ SPEC = Spec.new(noun: "service group", key: :service_groups, list: :get_service_groups,
31
+ create: :create_service_group, delete: :delete_service_groups,
32
+ update: :update_service_groups)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ # SPDX-License-Identifier: BSD-3-Clause
4
+ # Copyright (c) 2026, Unikraft GmbH.
5
+
6
+ require_relative "../core/resource"
7
+ require_relative "../api/platform"
8
+
9
+ module Unikraft
10
+ module Cloud
11
+ module Resources
12
+ # Users and quotas. Quotas are per metro, so reading them fans out
13
+ # across the scope and tags each entry with its metro.
14
+ class Users < Core::Scoped
15
+ API_CLASS = API::Platform::UsersApi
16
+
17
+ # The quotas of the authenticated user, or of the user `uuid`, in
18
+ # every metro in scope.
19
+ #
20
+ # @example
21
+ # ukc.users.quotas.each { |q| puts "#{q.metro} #{q.used.inspect}" }
22
+ # @param uuid [String, nil]
23
+ # @param opts [Hash] `metros:`, `base_url:`, `headers:`, `timeout:`
24
+ # @return [Array<Model>]
25
+ def quotas(uuid: nil, **opts)
26
+ endpoints = endpoints(check_options!(opts))
27
+ read = lambda do |endpoint|
28
+ call = call_options(endpoint, opts)
29
+ res = uuid ? api.get_user_by_uuid(uuid:, **call) : api.get_user(**call)
30
+ Core::Response.list(res, :quotas).map { |q| q.with_metro(endpoint.metro) }
31
+ end
32
+ return read.call(endpoints.first) if endpoints.size == 1
33
+
34
+ Core::Fanout.all(endpoints, flat: true, &read)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ # SPDX-License-Identifier: BSD-3-Clause
4
+ # Copyright (c) 2026, Unikraft GmbH.
5
+
6
+ require_relative "../core/resource"
7
+ require_relative "../api/platform"
8
+
9
+ module Unikraft
10
+ module Cloud
11
+ module Resources
12
+ # A handle on one volume (see {Core::Handle}).
13
+ #
14
+ # @example
15
+ # ukc.volumes.get(name: "data").attach(attach_to: { name: "web" }, at: "/data")
16
+ class VolumeHandle < Core::Handle
17
+ # Attach the volume to an instance.
18
+ #
19
+ # @param spec [Hash] the attachment, with the API's keys:
20
+ # `attach_to:` (an instance ref), `at:` (the mount point),
21
+ # `readonly:`; plus `headers:`/`timeout:` for the call
22
+ # @return [VolumeHandle] carrying what the endpoint reported
23
+ def attach(**spec) = volume_op(:attach_volumes, :attach_to, spec)
24
+
25
+ # Detach the volume, from every instance or, with `from:` (an
26
+ # instance ref), one.
27
+ #
28
+ # @return [VolumeHandle]
29
+ def detach(**spec) = volume_op(:detach_volumes, :from, spec)
30
+
31
+ attributes_of API::Platform::Models::Volume
32
+
33
+ private
34
+
35
+ # Send the operation with its instance ref as the wire carries it:
36
+ # the identifier alone, whatever form the ref was given in.
37
+ def volume_op(operation, key, spec)
38
+ call = spec.slice(:headers, :timeout)
39
+ body = wire(call).merge(spec.except(*call.keys))
40
+ body[key] = same_metro(Core::Ref.coerce(body[key])).wire if body[key]
41
+ mutate(operation, [body], **call)
42
+ end
43
+
44
+ # The wire drops the instance's metro, so one elsewhere would name
45
+ # a different instance in this volume's metro. A URL metro matches
46
+ # by its normalized base URL (`/v1` or not).
47
+ def same_metro(ref)
48
+ return ref if ref.metro.nil? || ref.metro == metro
49
+ return ref if Core::Metro.url?(ref.metro) &&
50
+ Core::Metro.base_url(ref.metro) == location.base_url
51
+
52
+ raise ArgumentError, "instance #{ref} is not in this volume's metro (#{metro})"
53
+ end
54
+ end
55
+
56
+ # Every volume matching one ref, one per metro.
57
+ class VolumeSet < Core::HandleSet
58
+ forward :attach, :detach
59
+ end
60
+
61
+ # Persistent volumes. Enumerable over every volume in scope.
62
+ #
63
+ # @example
64
+ # ukc.metro(:fra).volumes.create(name: "data", size_mb: 1024)
65
+ # ukc.volumes.update(name: "data", size_mb: 2048)
66
+ class Volumes < Core::Resource
67
+ API_CLASS = API::Platform::VolumesApi
68
+ HANDLE = VolumeHandle
69
+ SET = VolumeSet
70
+ SPEC = Spec.new(noun: "volume", key: :volumes, list: :get_volumes,
71
+ create: :create_volume, delete: :delete_volumes,
72
+ update: :update_volumes)
73
+
74
+ # Shorthand for `get(ref).attach(...)`.
75
+ #
76
+ # @example
77
+ # ukc.volumes.attach(name: "data", attach_to: { name: "web" }, at: "/data")
78
+ # @return [VolumeHandle]
79
+ def attach(ref = nil, **kwargs)
80
+ handle, spec = on(ref, kwargs)
81
+ handle.attach(**spec)
82
+ end
83
+
84
+ # Shorthand for `get(ref).detach(...)`.
85
+ # @return [VolumeHandle]
86
+ def detach(ref = nil, **kwargs)
87
+ handle, spec = on(ref, kwargs)
88
+ handle.detach(**spec)
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ # SPDX-License-Identifier: BSD-3-Clause
4
+ # Copyright (c) 2026, Unikraft GmbH.
5
+
6
+ module Unikraft
7
+ module Cloud
8
+ # Placeholder: the release workflow stamps the real version.
9
+ VERSION = "0.1.0"
10
+ end
11
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ # SPDX-License-Identifier: BSD-3-Clause
4
+ # Copyright (c) 2026, Unikraft GmbH.
5
+
6
+ require_relative "cloud/version"
7
+ require_relative "cloud/core/errors"
8
+ require_relative "cloud/core/url"
9
+ require_relative "cloud/core/base64"
10
+ require_relative "cloud/core/http"
11
+ require_relative "cloud/core/ready"
12
+ require_relative "cloud/model"
13
+ require_relative "cloud/api"
14
+ require_relative "cloud/core/metro"
15
+ require_relative "cloud/core/ref"
16
+ require_relative "cloud/core/response"
17
+ require_relative "cloud/core/fanout"
18
+ require_relative "cloud/core/pagination"
19
+ require_relative "cloud/core/session"
20
+ require_relative "cloud/core/patch"
21
+ require_relative "cloud/core/handle"
22
+ require_relative "cloud/core/handle_set"
23
+ require_relative "cloud/core/resource"
24
+ require_relative "cloud/resources/instances"
25
+ require_relative "cloud/resources/volumes"
26
+ require_relative "cloud/resources/service_groups"
27
+ require_relative "cloud/resources/certificates"
28
+ require_relative "cloud/resources/users"
29
+ require_relative "cloud/client"