github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/domain/infra/tunnel/network.go (about)

     1  package tunnel
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/containers/common/libnetwork/types"
     7  	"github.com/hanks177/podman/v4/libpod/define"
     8  	"github.com/hanks177/podman/v4/pkg/bindings/network"
     9  	"github.com/hanks177/podman/v4/pkg/domain/entities"
    10  	"github.com/hanks177/podman/v4/pkg/errorhandling"
    11  	"github.com/pkg/errors"
    12  )
    13  
    14  func (ic *ContainerEngine) NetworkList(ctx context.Context, opts entities.NetworkListOptions) ([]types.Network, error) {
    15  	options := new(network.ListOptions).WithFilters(opts.Filters)
    16  	return network.List(ic.ClientCtx, options)
    17  }
    18  
    19  func (ic *ContainerEngine) NetworkInspect(ctx context.Context, namesOrIds []string, opts entities.InspectOptions) ([]types.Network, []error, error) {
    20  	var (
    21  		reports = make([]types.Network, 0, len(namesOrIds))
    22  		errs    = []error{}
    23  	)
    24  	options := new(network.InspectOptions)
    25  	for _, name := range namesOrIds {
    26  		report, err := network.Inspect(ic.ClientCtx, name, options)
    27  		if err != nil {
    28  			errModel, ok := err.(*errorhandling.ErrorModel)
    29  			if !ok {
    30  				return nil, nil, err
    31  			}
    32  			if errModel.ResponseCode == 404 {
    33  				errs = append(errs, errors.Wrapf(define.ErrNoSuchNetwork, "network %s", name))
    34  				continue
    35  			}
    36  			return nil, nil, err
    37  		}
    38  		reports = append(reports, report)
    39  	}
    40  	return reports, errs, nil
    41  }
    42  
    43  func (ic *ContainerEngine) NetworkReload(ctx context.Context, names []string, opts entities.NetworkReloadOptions) ([]*entities.NetworkReloadReport, error) {
    44  	return nil, errors.New("not implemented")
    45  }
    46  
    47  func (ic *ContainerEngine) NetworkRm(ctx context.Context, namesOrIds []string, opts entities.NetworkRmOptions) ([]*entities.NetworkRmReport, error) {
    48  	reports := make([]*entities.NetworkRmReport, 0, len(namesOrIds))
    49  	options := new(network.RemoveOptions).WithForce(opts.Force)
    50  	if opts.Timeout != nil {
    51  		options = options.WithTimeout(*opts.Timeout)
    52  	}
    53  	for _, name := range namesOrIds {
    54  		response, err := network.Remove(ic.ClientCtx, name, options)
    55  		if err != nil {
    56  			report := &entities.NetworkRmReport{
    57  				Name: name,
    58  				Err:  err,
    59  			}
    60  			reports = append(reports, report)
    61  		} else {
    62  			reports = append(reports, response...)
    63  		}
    64  	}
    65  	return reports, nil
    66  }
    67  
    68  func (ic *ContainerEngine) NetworkCreate(ctx context.Context, net types.Network) (*types.Network, error) {
    69  	net, err := network.Create(ic.ClientCtx, &net)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	return &net, nil
    74  }
    75  
    76  // NetworkDisconnect removes a container from a given network
    77  func (ic *ContainerEngine) NetworkDisconnect(ctx context.Context, networkname string, opts entities.NetworkDisconnectOptions) error {
    78  	options := new(network.DisconnectOptions).WithForce(opts.Force)
    79  	return network.Disconnect(ic.ClientCtx, networkname, opts.Container, options)
    80  }
    81  
    82  // NetworkConnect removes a container from a given network
    83  func (ic *ContainerEngine) NetworkConnect(ctx context.Context, networkname string, opts entities.NetworkConnectOptions) error {
    84  	return network.Connect(ic.ClientCtx, networkname, opts.Container, &opts.PerNetworkOptions)
    85  }
    86  
    87  // NetworkExists checks if the given network exists
    88  func (ic *ContainerEngine) NetworkExists(ctx context.Context, networkname string) (*entities.BoolReport, error) {
    89  	exists, err := network.Exists(ic.ClientCtx, networkname, nil)
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  	return &entities.BoolReport{
    94  		Value: exists,
    95  	}, nil
    96  }
    97  
    98  // Network prune removes unused cni networks
    99  func (ic *ContainerEngine) NetworkPrune(ctx context.Context, options entities.NetworkPruneOptions) ([]*entities.NetworkPruneReport, error) {
   100  	opts := new(network.PruneOptions).WithFilters(options.Filters)
   101  	return network.Prune(ic.ClientCtx, opts)
   102  }