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

     1  package network
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"strings"
     7  
     8  	"github.com/containers/common/libnetwork/types"
     9  	"github.com/hanks177/podman/v4/pkg/bindings"
    10  	"github.com/hanks177/podman/v4/pkg/domain/entities"
    11  	jsoniter "github.com/json-iterator/go"
    12  )
    13  
    14  // Create makes a new CNI network configuration
    15  func Create(ctx context.Context, network *types.Network) (types.Network, error) {
    16  	var report types.Network
    17  	conn, err := bindings.GetClient(ctx)
    18  	if err != nil {
    19  		return report, err
    20  	}
    21  	// create empty network if the caller did not provide one
    22  	if network == nil {
    23  		network = &types.Network{}
    24  	}
    25  	networkConfig, err := jsoniter.MarshalToString(*network)
    26  	if err != nil {
    27  		return report, err
    28  	}
    29  	reader := strings.NewReader(networkConfig)
    30  	response, err := conn.DoRequest(ctx, reader, http.MethodPost, "/networks/create", nil, nil)
    31  	if err != nil {
    32  		return report, err
    33  	}
    34  	defer response.Body.Close()
    35  
    36  	return report, response.Process(&report)
    37  }
    38  
    39  // Inspect returns low level information about a CNI network configuration
    40  func Inspect(ctx context.Context, nameOrID string, _ *InspectOptions) (types.Network, error) {
    41  	var net types.Network
    42  	conn, err := bindings.GetClient(ctx)
    43  	if err != nil {
    44  		return net, err
    45  	}
    46  	response, err := conn.DoRequest(ctx, nil, http.MethodGet, "/networks/%s/json", nil, nil, nameOrID)
    47  	if err != nil {
    48  		return net, err
    49  	}
    50  	defer response.Body.Close()
    51  
    52  	return net, response.Process(&net)
    53  }
    54  
    55  // Remove deletes a defined CNI network configuration by name.  The optional force boolean
    56  // will remove all containers associated with the network when set to true.  A slice
    57  // of NetworkRemoveReports are returned.
    58  func Remove(ctx context.Context, nameOrID string, options *RemoveOptions) ([]*entities.NetworkRmReport, error) {
    59  	var reports []*entities.NetworkRmReport
    60  	if options == nil {
    61  		options = new(RemoveOptions)
    62  	}
    63  	conn, err := bindings.GetClient(ctx)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  	params, err := options.ToParams()
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	response, err := conn.DoRequest(ctx, nil, http.MethodDelete, "/networks/%s", params, nil, nameOrID)
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  	defer response.Body.Close()
    76  
    77  	return reports, response.Process(&reports)
    78  }
    79  
    80  // List returns a summary of all CNI network configurations
    81  func List(ctx context.Context, options *ListOptions) ([]types.Network, error) {
    82  	var netList []types.Network
    83  	if options == nil {
    84  		options = new(ListOptions)
    85  	}
    86  	conn, err := bindings.GetClient(ctx)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  	params, err := options.ToParams()
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  	response, err := conn.DoRequest(ctx, nil, http.MethodGet, "/networks/json", params, nil)
    95  	if err != nil {
    96  		return netList, err
    97  	}
    98  	defer response.Body.Close()
    99  
   100  	return netList, response.Process(&netList)
   101  }
   102  
   103  // Disconnect removes a container from a given network
   104  func Disconnect(ctx context.Context, networkName string, containerNameOrID string, options *DisconnectOptions) error {
   105  	if options == nil {
   106  		options = new(DisconnectOptions)
   107  	}
   108  	conn, err := bindings.GetClient(ctx)
   109  	if err != nil {
   110  		return err
   111  	}
   112  	// Disconnect sends everything in body
   113  	disconnect := struct {
   114  		Container string
   115  		Force     bool
   116  	}{
   117  		Container: containerNameOrID,
   118  	}
   119  	if force := options.GetForce(); options.Changed("Force") {
   120  		disconnect.Force = force
   121  	}
   122  
   123  	body, err := jsoniter.MarshalToString(disconnect)
   124  	if err != nil {
   125  		return err
   126  	}
   127  	stringReader := strings.NewReader(body)
   128  	response, err := conn.DoRequest(ctx, stringReader, http.MethodPost, "/networks/%s/disconnect", nil, nil, networkName)
   129  	if err != nil {
   130  		return err
   131  	}
   132  	defer response.Body.Close()
   133  
   134  	return response.Process(nil)
   135  }
   136  
   137  // Connect adds a container to a network
   138  func Connect(ctx context.Context, networkName string, containerNameOrID string, options *types.PerNetworkOptions) error {
   139  	if options == nil {
   140  		options = new(types.PerNetworkOptions)
   141  	}
   142  	conn, err := bindings.GetClient(ctx)
   143  	if err != nil {
   144  		return err
   145  	}
   146  	// Connect sends everything in body
   147  	connect := entities.NetworkConnectOptions{
   148  		Container:         containerNameOrID,
   149  		PerNetworkOptions: *options,
   150  	}
   151  
   152  	body, err := jsoniter.MarshalToString(connect)
   153  	if err != nil {
   154  		return err
   155  	}
   156  	stringReader := strings.NewReader(body)
   157  	response, err := conn.DoRequest(ctx, stringReader, http.MethodPost, "/networks/%s/connect", nil, nil, networkName)
   158  	if err != nil {
   159  		return err
   160  	}
   161  	defer response.Body.Close()
   162  
   163  	return response.Process(nil)
   164  }
   165  
   166  // Exists returns true if a given network exists
   167  func Exists(ctx context.Context, nameOrID string, options *ExistsOptions) (bool, error) {
   168  	conn, err := bindings.GetClient(ctx)
   169  	if err != nil {
   170  		return false, err
   171  	}
   172  	response, err := conn.DoRequest(ctx, nil, http.MethodGet, "/networks/%s/exists", nil, nil, nameOrID)
   173  	if err != nil {
   174  		return false, err
   175  	}
   176  	defer response.Body.Close()
   177  
   178  	return response.IsSuccess(), nil
   179  }
   180  
   181  // Prune removes unused CNI networks
   182  func Prune(ctx context.Context, options *PruneOptions) ([]*entities.NetworkPruneReport, error) {
   183  	if options == nil {
   184  		options = new(PruneOptions)
   185  	}
   186  	params, err := options.ToParams()
   187  	if err != nil {
   188  		return nil, err
   189  	}
   190  	var (
   191  		prunedNetworks []*entities.NetworkPruneReport
   192  	)
   193  	conn, err := bindings.GetClient(ctx)
   194  	if err != nil {
   195  		return nil, err
   196  	}
   197  
   198  	response, err := conn.DoRequest(ctx, nil, http.MethodPost, "/networks/prune", params, nil)
   199  	if err != nil {
   200  		return nil, err
   201  	}
   202  	defer response.Body.Close()
   203  
   204  	return prunedNetworks, response.Process(&prunedNetworks)
   205  }