func InterfaceInfoFromNetworkConfig(configs []NetworkConfig) network.InterfaceInfos {
	result := make(network.InterfaceInfos, len(configs))
	for i, v := range configs {
		var routes []network.Route
		if len(v.Routes) != 0 {
			routes = make([]network.Route, len(v.Routes))
			for j, route := range v.Routes {
				routes[j] = network.Route{
					DestinationCIDR: route.DestinationCIDR,
					GatewayIP:       route.GatewayIP,
					Metric:          route.Metric,
				}
			}
		}

		configType := network.AddressConfigType(v.ConfigType)

		result[i] = network.InterfaceInfo{
			DeviceIndex:         v.DeviceIndex,
			MACAddress:          network.NormalizeMACAddress(v.MACAddress),
			MTU:                 v.MTU,
			ProviderId:          network.Id(v.ProviderId),
			ProviderNetworkId:   network.Id(v.ProviderNetworkId),
			ProviderSubnetId:    network.Id(v.ProviderSubnetId),
			ProviderSpaceId:     network.Id(v.ProviderSpaceId),
			ProviderVLANId:      network.Id(v.ProviderVLANId),
			ProviderAddressId:   network.Id(v.ProviderAddressId),
			VLANTag:             v.VLANTag,
			InterfaceName:       v.InterfaceName,
			ParentInterfaceName: v.ParentInterfaceName,
			InterfaceType:       network.LinkLayerDeviceType(v.InterfaceType),
			Disabled:            v.Disabled,
			NoAutoStart:         v.NoAutoStart,
			ConfigType:          configType,
			Addresses:           ToProviderAddresses(v.Addresses...),
			ShadowAddresses:     ToProviderAddresses(v.ShadowAddresses...),
			DNSServers:          network.NewMachineAddresses(v.DNSServers).AsProviderAddresses(),
			DNSSearchDomains:    v.DNSSearchDomains,
			GatewayAddress:      network.NewMachineAddress(v.GatewayAddress).AsProviderAddress(),
			Routes:              routes,
			IsDefaultGateway:    v.IsDefaultGateway,
			VirtualPortType:     network.VirtualPortType(v.VirtualPortType),
			Origin:              network.Origin(v.NetworkOrigin),
		}

		// Compatibility accommodations follow.
		// TODO (manadart 2021-03-05): Juju 3/4 should require that only the
		// address collections are used, and the following fields removed from
		// the top-level interface:
		// - CIDR
		// - Address

		// 1) For clients that populate Addresses, but still set
		//    address-specific fields on the device.
		//    Note that the assumption must hold (as it does at the time of
		//    writing) that the collections are only populated with a single
		//    member, with repeated devices for each address.
		if len(result[i].Addresses) > 0 {
			if result[i].Addresses[0].CIDR == "" {
				result[i].Addresses[0].CIDR = v.CIDR
			}
			if result[i].Addresses[0].ConfigType == "" {
				result[i].Addresses[0].ConfigType = configType
			}
		} else {
			// 2) For even older clients that do not populate Addresses.
			if v.Address != "" {
				result[i].Addresses = network.ProviderAddresses{
					network.NewMachineAddress(
						v.Address,
						network.WithCIDR(v.CIDR),
						network.WithConfigType(configType),
					).AsProviderAddress(),
				}
			}
		}
	}

	return result
}
// InterfaceInfoFromNetworkConfig converts a slice of NetworkConfig into the
// equivalent network.InterfaceInfo slice.
InterfaceInfoFromNetworkConfig converts a slice of NetworkConfig into the
equivalent network.InterfaceInfo slice.
// InterfaceInfoFromNetworkConfig converts a slice of NetworkConfig into the // equivalent network . InterfaceInfo slice .
func InterfaceInfoFromNetworkConfig(configs []NetworkConfig) network.InterfaceInfos {
	result := make(network.InterfaceInfos, len(configs))
	for i, v := range configs {
		var routes []network.Route
		if len(v.Routes) != 0 {
			routes = make([]network.Route, len(v.Routes))
			for j, route := range v.Routes {
				routes[j] = network.Route{
					DestinationCIDR: route.DestinationCIDR,
					GatewayIP:       route.GatewayIP,
					Metric:          route.Metric,
				}
			}
		}
		configType := network.AddressConfigType(v.ConfigType)
		result[i] = network.InterfaceInfo{
			DeviceIndex:         v.DeviceIndex,
			MACAddress:          network.NormalizeMACAddress(v.MACAddress),
			MTU:                 v.MTU,
			ProviderId:          network.Id(v.ProviderId),
			ProviderNetworkId:   network.Id(v.ProviderNetworkId),
			ProviderSubnetId:    network.Id(v.ProviderSubnetId),
			ProviderSpaceId:     network.Id(v.ProviderSpaceId),
			ProviderVLANId:      network.Id(v.ProviderVLANId),
			ProviderAddressId:   network.Id(v.ProviderAddressId),
			VLANTag:             v.VLANTag,
			InterfaceName:       v.InterfaceName,
			ParentInterfaceName: v.ParentInterfaceName,
			InterfaceType:       network.LinkLayerDeviceType(v.InterfaceType),
			Disabled:            v.Disabled,
			NoAutoStart:         v.NoAutoStart,
			ConfigType:          configType,
			Addresses:           ToProviderAddresses(v.Addresses...),
			ShadowAddresses:     ToProviderAddresses(v.ShadowAddresses...),
			DNSServers:          network.NewMachineAddresses(v.DNSServers).AsProviderAddresses(),
			DNSSearchDomains:    v.DNSSearchDomains,
			GatewayAddress:      network.NewMachineAddress(v.GatewayAddress).AsProviderAddress(),
			Routes:              routes,
			IsDefaultGateway:    v.IsDefaultGateway,
			VirtualPortType:     network.VirtualPortType(v.VirtualPortType),
			Origin:              network.Origin(v.NetworkOrigin),
		}
		if len(result[i].Addresses) > 0 {
			if result[i].Addresses[0].CIDR == "" {
				result[i].Addresses[0].CIDR = v.CIDR
			}
			if result[i].Addresses[0].ConfigType == "" {
				result[i].Addresses[0].ConfigType = configType
			}
		} else {
			if v.Address != "" {
				result[i].Addresses = network.ProviderAddresses{
					network.NewMachineAddress(
						v.Address,
						network.WithCIDR(v.CIDR),
						network.WithConfigType(configType),
					).AsProviderAddress(),
				}
			}
		}
	}
	return result
}
func InterfaceInfoFromNetworkConfig ( configs [ ] NetworkConfig ) network . InterfaceInfos { result := make ( network . InterfaceInfos , len ( configs ) ) for i , v := range configs { var routes [ ] network . Route if len ( v . Routes ) != 0 { routes = make ( [ ] network . Route , len ( v . Routes ) ) for j , route := range v . Routes { routes [ j ] = network . Route { DestinationCIDR : route . DestinationCIDR , GatewayIP : route . GatewayIP , Metric : route . Metric , } } } configType := network . AddressConfigType ( v . ConfigType ) result [ i ] = network . InterfaceInfo { DeviceIndex : v . DeviceIndex , MACAddress : network . NormalizeMACAddress ( v . MACAddress ) , MTU : v . MTU , ProviderId : network . Id ( v . ProviderId ) , ProviderNetworkId : network . Id ( v . ProviderNetworkId ) , ProviderSubnetId : network . Id ( v . ProviderSubnetId ) , ProviderSpaceId : network . Id ( v . ProviderSpaceId ) , ProviderVLANId : network . Id ( v . ProviderVLANId ) , ProviderAddressId : network . Id ( v . ProviderAddressId ) , VLANTag : v . VLANTag , InterfaceName : v . InterfaceName , ParentInterfaceName : v . ParentInterfaceName , InterfaceType : network . LinkLayerDeviceType ( v . InterfaceType ) , Disabled : v . Disabled , NoAutoStart : v . NoAutoStart , ConfigType : configType , Addresses : ToProviderAddresses ( v . Addresses ... ) , ShadowAddresses : ToProviderAddresses ( v . ShadowAddresses ... ) , DNSServers : network . NewMachineAddresses ( v . DNSServers ) . AsProviderAddresses ( ) , DNSSearchDomains : v . DNSSearchDomains , GatewayAddress : network . NewMachineAddress ( v . GatewayAddress ) . AsProviderAddress ( ) , Routes : routes , IsDefaultGateway : v . IsDefaultGateway , VirtualPortType : network . VirtualPortType ( v . VirtualPortType ) , Origin : network . Origin ( v . NetworkOrigin ) , } if len ( result [ i ] . Addresses ) > 0 { if result [ i ] . Addresses [ 0 ] . CIDR == " " { result [ i ] . Addresses [ 0 ] . CIDR = v . CIDR } if result [ i ] . Addresses [ 0 ] . ConfigType == " " { result [ i ] . Addresses [ 0 ] . ConfigType = configType } } else { if v . Address != " " { result [ i ] . Addresses = network . ProviderAddresses { network . NewMachineAddress ( v . Address , network . WithCIDR ( v . CIDR ) , network . WithConfigType ( configType ) , ) . AsProviderAddress ( ) , } } } } return result }
InterfaceInfoFromNetworkConfig converts a slice of NetworkConfig into the
equivalent network.InterfaceInfo slice.
InterfaceInfoFromNetworkConfig converts a slice of NetworkConfig into the equivalent network . InterfaceInfo slice .
// Compatibility accommodations follow. // TODO (manadart 2021-03-05): Juju 3/4 should require that only the // address collections are used, and the following fields removed from // the top-level interface: // - CIDR // - Address // 1) For clients that populate Addresses, but still set //    address-specific fields on the device. //    Note that the assumption must hold (as it does at the time of //    writing) that the collections are only populated with a single //    member, with repeated devices for each address. // 2) For even older clients that do not populate Addresses.