Skip to content
Open
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
45 changes: 45 additions & 0 deletions Sources/ContainerResource/Network/Attachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,49 @@ public struct Attachment: Codable, Sendable {
self.macAddress = macAddress
self.mtu = mtu
}

enum CodingKeys: String, CodingKey {
case network
case hostname
case ipv4Address
case ipv4Gateway
case ipv6Address
case macAddress
// TODO: retain for deserialization compatibility for now, remove later
case address
case gateway
}

/// Create a configuration from the supplied Decoder, initializing missing
/// values where possible to reasonable defaults.
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

network = try container.decode(String.self, forKey: .network)
hostname = try container.decode(String.self, forKey: .hostname)
if let address = try? container.decode(CIDRv4.self, forKey: .ipv4Address) {
ipv4Address = address
} else {
ipv4Address = try container.decode(CIDRv4.self, forKey: .address)
}
if let gateway = try? container.decode(IPv4Address.self, forKey: .ipv4Gateway) {
ipv4Gateway = gateway
} else {
ipv4Gateway = try container.decode(IPv4Address.self, forKey: .gateway)
}
ipv6Address = try container.decodeIfPresent(CIDRv6.self, forKey: .ipv6Address)
macAddress = try container.decodeIfPresent(MACAddress.self, forKey: .macAddress)
}

/// Encode the configuration to the supplied Encoder.
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(network, forKey: .network)
try container.encode(hostname, forKey: .hostname)
try container.encode(ipv4Address, forKey: .ipv4Address)
try container.encode(ipv4Gateway, forKey: .ipv4Gateway)
try container.encodeIfPresent(ipv6Address, forKey: .ipv6Address)
try container.encodeIfPresent(macAddress, forKey: .macAddress)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public struct NetworkConfiguration: Codable, Sendable, Identifiable {
case ipv6Subnet
case labels
case pluginInfo
// TODO: retain for deserialization compatability for now, remove later
// TODO: retain for deserialization compatibility for now, remove later
case subnet
}

Expand Down
37 changes: 37 additions & 0 deletions Sources/ContainerResource/Network/NetworkState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,43 @@ public struct NetworkStatus: Codable, Sendable {
self.ipv4Gateway = ipv4Gateway
self.ipv6Subnet = ipv6Subnet
}

enum CodingKeys: String, CodingKey {
case ipv4Subnet
case ipv4Gateway
case ipv6Subnet
// TODO: retain for deserialization compatibility for now, remove later
case address
case gateway
}

/// Create a configuration from the supplied Decoder, initializing missing
/// values where possible to reasonable defaults.
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

if let address = try? container.decode(CIDRv4.self, forKey: .ipv4Subnet) {
ipv4Subnet = address
} else {
ipv4Subnet = try container.decode(CIDRv4.self, forKey: .address)
}
if let gateway = try? container.decode(IPv4Address.self, forKey: .ipv4Gateway) {
ipv4Gateway = gateway
} else {
ipv4Gateway = try container.decode(IPv4Address.self, forKey: .gateway)
}
ipv6Subnet = try container.decodeIfPresent(String.self, forKey: .ipv6Subnet)
.map { try CIDRv6($0) }
}

/// Encode the configuration to the supplied Encoder.
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(ipv4Subnet, forKey: .ipv4Subnet)
try container.encode(ipv4Gateway, forKey: .ipv4Gateway)
try container.encodeIfPresent(ipv6Subnet, forKey: .ipv6Subnet)
}
}

/// The configuration and runtime attributes for a network.
Expand Down