<commit_before>package network

import (
	"github.com/lxc/lxd/lxd/state"
)

var drivers = map[string]func() Network{
	"bridge": func() Network { return &bridge{} },
}

// LoadByName loads the network info from the database by name.
func LoadByName(s *state.State, name string) (Network, error) {
	id, netInfo, err := s.Cluster.GetNetwork(name)
	if err != nil {
		return nil, err
	}

	driverFunc, ok := drivers[netInfo.Type]
	if !ok {
		return nil, ErrUnknownDriver
	}

	n := driverFunc()
	n.init(s, id, name, netInfo.Type, netInfo.Description, netInfo.Config)

	return n, nil
}

// Validate validates the supplied network configuration for the specified network type.
func Validate(name string, netType string, config map[string]string) error {
	driverFunc, ok := drivers[netType]
	if !ok {
		return ErrUnknownDriver
	}

	err := ValidNetworkName(name)
	if err != nil {
		return err
	}

	n := driverFunc()
	n.init(nil, 0, name, netType, "", config)
	return n.Validate(config)
}<commit_msg>lxd/network/network/load: Adds per-driver FillConfig wrapper<commit_after>package network

import (
	"github.com/lxc/lxd/lxd/state"
	"github.com/lxc/lxd/shared/api"
)

var drivers = map[string]func() Network{
	"bridge": func() Network { return &bridge{} },
}

// LoadByName loads the network info from the database by name.
func LoadByName(s *state.State, name string) (Network, error) {
	id, netInfo, err := s.Cluster.GetNetwork(name)
	if err != nil {
		return nil, err
	}

	driverFunc, ok := drivers[netInfo.Type]
	if !ok {
		return nil, ErrUnknownDriver
	}

	n := driverFunc()
	n.init(s, id, name, netInfo.Type, netInfo.Description, netInfo.Config)

	return n, nil
}

// Validate validates the supplied network configuration for the specified network type.
func Validate(name string, netType string, config map[string]string) error {
	driverFunc, ok := drivers[netType]
	if !ok {
		return ErrUnknownDriver
	}

	err := ValidNetworkName(name)
	if err != nil {
		return err
	}

	n := driverFunc()
	n.init(nil, 0, name, netType, "", config)
	return n.Validate(config)
}

// FillConfig populates the supplied api.NetworkPost with automatically populated values.
func FillConfig(req *api.NetworksPost) error {
	driverFunc, ok := drivers[req.Type]
	if !ok {
		return ErrUnknownDriver
	}

	n := driverFunc()
	n.init(nil, 0, req.Name, req.Type, req.Description, req.Config)

	err := n.fillConfig(req)
	if err != nil {
		return err
	}

	// Replace "auto" by actual values.
	err = fillAuto(req.Config)
	if err != nil {
		return err
	}

	return nil
}
{'fraction_non_alphanumeric': 0.1269276393831554, 'fraction_numerical': 0.0011862396204033216, 'mean_word_length': 4.111111111111111, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4863491', 'n_tokens_mistral': 881, 'n_tokens_neox': 859, 'n_words': 256}