lxd/db/networks.go
func (c *ClusterTx) NetworkCreatePending(node, name string, conf map[string]string) error {
	// First check if a network with the given name exists, and, if
	// so, that it's in the pending state.
	network := struct {
		id    int64
		state int
	}{}

	var errConsistency error
	dest := func(i int) []interface{} {
		// Sanity check that there is at most one pool with the given name.
		if i != 0 {
			errConsistency = fmt.Errorf("more than one network exists with the given name")
		}
		return []interface{}{&network.id, &network.state}
	}
	stmt, err := c.tx.Prepare("SELECT id, state FROM networks WHERE name=?")
	if err != nil {
		return err
	}
	defer stmt.Close()
	err = query.SelectObjects(stmt, dest, name)
	if err != nil {
		return err
	}
	if errConsistency != nil {
		return errConsistency
	}

	var networkID = network.id
	if networkID == 0 {
		// No existing network with the given name was found, let's create
		// one.
		columns := []string{"name"}
		values := []interface{}{name}
		networkID, err = query.UpsertObject(c.tx, "networks", columns, values)
		if err != nil {
			return err
		}
	} else {
		// Check that the existing network  is in the pending state.
		if network.state != networkPending {
			return fmt.Errorf("network is not in pending state")
		}
	}

	// Get the ID of the node with the given name.
	nodeInfo, err := c.NodeByName(node)
	if err != nil {
		return err
	}

	// Check that no network entry for this node and network exists yet.
	count, err := query.Count(
		c.tx, "networks_nodes", "network_id=? AND node_id=?", networkID, nodeInfo.ID)
	if err != nil {
		return err
	}
	if count != 0 {
		return ErrAlreadyDefined
	}

	// Insert the node-specific configuration.
	columns := []string{"network_id", "node_id"}
	values := []interface{}{networkID, nodeInfo.ID}
	_, err = query.UpsertObject(c.tx, "networks_nodes", columns, values)
	if err != nil {
		return err
	}
	err = c.NetworkConfigAdd(networkID, nodeInfo.ID, conf)
	if err != nil {
		return err
	}

	return nil
}
func (c *ClusterTx) NetworkCreatePending(node, name string, conf map[string]string) error {
	// First check if a network with the given name exists, and, if
	// so, that it's in the pending state.
	network := struct {
		id    int64
		state int
	}{}

	var errConsistency error
	dest := func(i int) []interface{} {
		// Sanity check that there is at most one pool with the given name.
		if i != 0 {
			errConsistency = fmt.Errorf("more than one network exists with the given name")
		}
		return []interface{}{&network.id, &network.state}
	}
	stmt, err := c.tx.Prepare("SELECT id, state FROM networks WHERE name=?")
	if err != nil {
		return err
	}
	defer stmt.Close()
	err = query.SelectObjects(stmt, dest, name)
	if err != nil {
		return err
	}
	if errConsistency != nil {
		return errConsistency
	}

	var networkID = network.id
	if networkID == 0 {
		// No existing network with the given name was found, let's create
		// one.
		columns := []string{"name"}
		values := []interface{}{name}
		networkID, err = query.UpsertObject(c.tx, "networks", columns, values)
		if err != nil {
			return err
		}
	} else {
		// Check that the existing network  is in the pending state.
		if network.state != networkPending {
			return fmt.Errorf("network is not in pending state")
		}
	}

	// Get the ID of the node with the given name.
	nodeInfo, err := c.NodeByName(node)
	if err != nil {
		return err
	}

	// Check that no network entry for this node and network exists yet.
	count, err := query.Count(
		c.tx, "networks_nodes", "network_id=? AND node_id=?", networkID, nodeInfo.ID)
	if err != nil {
		return err
	}
	if count != 0 {
		return ErrAlreadyDefined
	}

	// Insert the node-specific configuration.
	columns := []string{"network_id", "node_id"}
	values := []interface{}{networkID, nodeInfo.ID}
	_, err = query.UpsertObject(c.tx, "networks_nodes", columns, values)
	if err != nil {
		return err
	}
	err = c.NetworkConfigAdd(networkID, nodeInfo.ID, conf)
	if err != nil {
		return err
	}

	return nil
}
// NetworkCreatePending creates a new pending network on the node with
// the given name.