github.com/containers/podman/v4@v4.9.4/pkg/bindings/network/network.go (about)

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