mirror of
https://github.com/apple/container.git
synced 2026-09-22 23:55:34 +00:00
[networks]: Add creationDate field (#791)
- Closes #665 - Existing containers that don't have `creationDate` default to the epoch date.
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
import ContainerizationError
|
||||
import ContainerizationExtras
|
||||
import Foundation
|
||||
|
||||
/// Configuration parameters for network creation.
|
||||
public struct NetworkConfiguration: Codable, Sendable, Identifiable {
|
||||
@@ -25,6 +26,9 @@ public struct NetworkConfiguration: Codable, Sendable, Identifiable {
|
||||
/// The network type
|
||||
public let mode: NetworkMode
|
||||
|
||||
/// When the network was created.
|
||||
public let creationDate: Date
|
||||
|
||||
/// The preferred CIDR address for the subnet, if specified
|
||||
public let subnet: String?
|
||||
|
||||
@@ -39,6 +43,7 @@ public struct NetworkConfiguration: Codable, Sendable, Identifiable {
|
||||
labels: [String: String] = [:]
|
||||
) throws {
|
||||
self.id = id
|
||||
self.creationDate = Date()
|
||||
self.mode = mode
|
||||
self.subnet = subnet
|
||||
self.labels = labels
|
||||
@@ -47,6 +52,7 @@ public struct NetworkConfiguration: Codable, Sendable, Identifiable {
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case id
|
||||
case creationDate
|
||||
case mode
|
||||
case subnet
|
||||
case labels
|
||||
@@ -58,6 +64,7 @@ public struct NetworkConfiguration: Codable, Sendable, Identifiable {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
|
||||
id = try container.decode(String.self, forKey: .id)
|
||||
creationDate = try container.decodeIfPresent(Date.self, forKey: .creationDate) ?? Date(timeIntervalSince1970: 0)
|
||||
mode = try container.decode(NetworkMode.self, forKey: .mode)
|
||||
subnet = try container.decodeIfPresent(String.self, forKey: .subnet)
|
||||
labels = try container.decodeIfPresent([String: String].self, forKey: .labels) ?? [:]
|
||||
|
||||
@@ -53,4 +53,11 @@ public enum NetworkState: Codable, Sendable {
|
||||
case .running(let configuration, _): configuration.id
|
||||
}
|
||||
}
|
||||
|
||||
public var creationDate: Date {
|
||||
switch self {
|
||||
case .created(let configuration): configuration.creationDate
|
||||
case .running(let configuration, _): configuration.creationDate
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user