|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// Copyright © 2026 Apple Inc. and the container project authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +import Foundation |
| 18 | + |
| 19 | +/// Common properties for all managed resources. |
| 20 | +public protocol ManagedResource: Identifiable, Sendable, Codable { |
| 21 | + /// A 64 byte hexadecimal string, assigned by the system, that uniquely |
| 22 | + /// identifies the resource. |
| 23 | + var id: String { get } |
| 24 | + |
| 25 | + /// A user assigned name that shall be unique within the namespace of |
| 26 | + /// the resource category. If the user does not assign a name, this value |
| 27 | + /// shall be the same as the system-assigned identifier. |
| 28 | + var name: String { get } |
| 29 | + |
| 30 | + /// The time at which the system created the resource. |
| 31 | + var creationDate: Date { get } |
| 32 | + |
| 33 | + /// Key-value properties for the resource. The user and system may both |
| 34 | + /// make use of labels to read and write annotations or other metadata. |
| 35 | + /// A good practice is to use |
| 36 | + var labels: [String: String] { get } |
| 37 | + |
| 38 | + /// Generates a unique resource ID value. |
| 39 | + static func generateId() -> String |
| 40 | + |
| 41 | + /// Returns true only if the specified resource name is syntactically valid. |
| 42 | + static func nameValid(_ name: String) -> Bool |
| 43 | +} |
| 44 | + |
| 45 | +extension ManagedResource { |
| 46 | + /// Generate a random identifier that has the format of an ASCII SHA-256 hash. |
| 47 | + public static func generateId() -> String { |
| 48 | + (0..<2) |
| 49 | + .map { _ in UInt128.random(in: 0...UInt128.max) } |
| 50 | + .map { String($0, radix: 16).padding(toLength: 32, withPad: "0", startingAt: 0) } |
| 51 | + .joined() |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// FIXME: This moves to ManagedResource and/or a ResourceLabels typealias eventually. |
| 56 | +extension [String: String] { |
| 57 | + public var isBuiltin: Bool { self.contains { $0 == ResourceLabelKeys.role && $1 == ResourceRoleValues.builtin } } |
| 58 | +} |
0 commit comments