Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/ContainerCommands/Network/NetworkList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,14 @@ extension NetworkState {

public struct PrintableNetwork: Codable {
let id: String
let createdAt: Date
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this in PrintableNetwork if we're not printing it for now, right?

Let's leave it out here and not print the create date in tabular output, to start with.

let state: String
let config: NetworkConfiguration
let status: NetworkStatus?

public init(_ network: NetworkState) {
self.id = network.id
self.createdAt = network.createdAt
self.state = network.state
switch network {
case .created(let config):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import ContainerizationError
import ContainerizationExtras
import Foundation

/// Configuration parameters for network creation.
public struct NetworkConfiguration: Codable, Sendable, Identifiable {
Expand All @@ -24,6 +25,9 @@ public struct NetworkConfiguration: Codable, Sendable, Identifiable {

/// The network type
public let mode: NetworkMode

/// When the network was created.
public let createdAt: Date

/// The preferred CIDR address for the subnet, if specified
public let subnet: String?
Expand All @@ -39,6 +43,7 @@ public struct NetworkConfiguration: Codable, Sendable, Identifiable {
labels: [String: String] = [:]
) throws {
self.id = id
self.createdAt = Date()
self.mode = mode
self.subnet = subnet
self.labels = labels
Expand All @@ -47,6 +52,7 @@ public struct NetworkConfiguration: Codable, Sendable, Identifiable {

enum CodingKeys: String, CodingKey {
case id
case createdAt
case mode
case subnet
case labels
Expand All @@ -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)
createdAt = try container.decode(Date.self, forKey: .createdAt)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) ?? [:]
Expand Down
7 changes: 7 additions & 0 deletions Sources/Services/ContainerNetworkService/NetworkState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,11 @@ public enum NetworkState: Codable, Sendable {
case .running(let configuration, _): configuration.id
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern could get out of hand as we add more configuration attributes.

Perhaps we could have a public var configuration: NetworkConfiguration? that provides configuration if the state has that associated data (always would for now but it would give us leeway for states that don't)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I understand what you kinda mean? Do we want to update id as well?

public var configuration: NetworkConfiguration? {
   switch self {
      case .created(let configuration): configuration
      case .running(let configuration, _): configuration
   }
}

public var createdAt: Date? {
   configuration?.createdAt
}

public var createdAt: Date {
switch self {
case .created(let configuration): configuration.createdAt
case .running(let configuration, _): configuration.createdAt
}
}
}
1 change: 1 addition & 0 deletions Tests/CLITests/Utilities/CLITest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class CLITest {

struct NetworkInspectOutput: Codable {
let id: String
let createdAt: Date
let state: String
let config: NetworkConfiguration
let status: NetworkStatus?
Expand Down