Skip to content

Commit 6f55360

Browse files
authored
[networks]: Add creationDate field (apple#791)
- Closes apple#665 - Existing containers that don't have `creationDate` default to the epoch date.
1 parent a5fd902 commit 6f55360

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

Sources/Services/ContainerNetworkService/NetworkConfiguration.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import ContainerizationError
1818
import ContainerizationExtras
19+
import Foundation
1920

2021
/// Configuration parameters for network creation.
2122
public struct NetworkConfiguration: Codable, Sendable, Identifiable {
@@ -25,6 +26,9 @@ public struct NetworkConfiguration: Codable, Sendable, Identifiable {
2526
/// The network type
2627
public let mode: NetworkMode
2728

29+
/// When the network was created.
30+
public let creationDate: Date
31+
2832
/// The preferred CIDR address for the subnet, if specified
2933
public let subnet: String?
3034

@@ -39,6 +43,7 @@ public struct NetworkConfiguration: Codable, Sendable, Identifiable {
3943
labels: [String: String] = [:]
4044
) throws {
4145
self.id = id
46+
self.creationDate = Date()
4247
self.mode = mode
4348
self.subnet = subnet
4449
self.labels = labels
@@ -47,6 +52,7 @@ public struct NetworkConfiguration: Codable, Sendable, Identifiable {
4752

4853
enum CodingKeys: String, CodingKey {
4954
case id
55+
case creationDate
5056
case mode
5157
case subnet
5258
case labels
@@ -58,6 +64,7 @@ public struct NetworkConfiguration: Codable, Sendable, Identifiable {
5864
let container = try decoder.container(keyedBy: CodingKeys.self)
5965

6066
id = try container.decode(String.self, forKey: .id)
67+
creationDate = try container.decodeIfPresent(Date.self, forKey: .creationDate) ?? Date(timeIntervalSince1970: 0)
6168
mode = try container.decode(NetworkMode.self, forKey: .mode)
6269
subnet = try container.decodeIfPresent(String.self, forKey: .subnet)
6370
labels = try container.decodeIfPresent([String: String].self, forKey: .labels) ?? [:]

Sources/Services/ContainerNetworkService/NetworkState.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,11 @@ public enum NetworkState: Codable, Sendable {
5353
case .running(let configuration, _): configuration.id
5454
}
5555
}
56+
57+
public var creationDate: Date {
58+
switch self {
59+
case .created(let configuration): configuration.creationDate
60+
case .running(let configuration, _): configuration.creationDate
61+
}
62+
}
5663
}

0 commit comments

Comments
 (0)