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

     1  package tunnel
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/hanks177/podman/v4/pkg/bindings/volumes"
     7  	"github.com/hanks177/podman/v4/pkg/domain/entities"
     8  	"github.com/hanks177/podman/v4/pkg/domain/entities/reports"
     9  	"github.com/hanks177/podman/v4/pkg/errorhandling"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  func (ic *ContainerEngine) VolumeCreate(ctx context.Context, opts entities.VolumeCreateOptions) (*entities.IDOrNameResponse, error) {
    14  	response, err := volumes.Create(ic.ClientCtx, opts, nil)
    15  	if err != nil {
    16  		return nil, err
    17  	}
    18  	return &entities.IDOrNameResponse{IDOrName: response.Name}, nil
    19  }
    20  
    21  func (ic *ContainerEngine) VolumeRm(ctx context.Context, namesOrIds []string, opts entities.VolumeRmOptions) ([]*entities.VolumeRmReport, error) {
    22  	if opts.All {
    23  		vols, err := volumes.List(ic.ClientCtx, nil)
    24  		if err != nil {
    25  			return nil, err
    26  		}
    27  		for _, v := range vols {
    28  			namesOrIds = append(namesOrIds, v.Name)
    29  		}
    30  	}
    31  	reports := make([]*entities.VolumeRmReport, 0, len(namesOrIds))
    32  	for _, id := range namesOrIds {
    33  		options := new(volumes.RemoveOptions).WithForce(opts.Force)
    34  		if opts.Timeout != nil {
    35  			options = options.WithTimeout(*opts.Timeout)
    36  		}
    37  		reports = append(reports, &entities.VolumeRmReport{
    38  			Err: volumes.Remove(ic.ClientCtx, id, options),
    39  			Id:  id,
    40  		})
    41  	}
    42  	return reports, nil
    43  }
    44  
    45  func (ic *ContainerEngine) VolumeInspect(ctx context.Context, namesOrIds []string, opts entities.InspectOptions) ([]*entities.VolumeInspectReport, []error, error) {
    46  	var (
    47  		reports = make([]*entities.VolumeInspectReport, 0, len(namesOrIds))
    48  		errs    = []error{}
    49  	)
    50  	if opts.All {
    51  		vols, err := volumes.List(ic.ClientCtx, nil)
    52  		if err != nil {
    53  			return nil, nil, err
    54  		}
    55  		for _, v := range vols {
    56  			namesOrIds = append(namesOrIds, v.Name)
    57  		}
    58  	}
    59  	for _, id := range namesOrIds {
    60  		data, err := volumes.Inspect(ic.ClientCtx, id, nil)
    61  		if err != nil {
    62  			errModel, ok := err.(*errorhandling.ErrorModel)
    63  			if !ok {
    64  				return nil, nil, err
    65  			}
    66  			if errModel.ResponseCode == 404 {
    67  				errs = append(errs, errors.Errorf("no such volume %q", id))
    68  				continue
    69  			}
    70  			return nil, nil, err
    71  		}
    72  		reports = append(reports, &entities.VolumeInspectReport{VolumeConfigResponse: data})
    73  	}
    74  	return reports, errs, nil
    75  }
    76  
    77  func (ic *ContainerEngine) VolumePrune(ctx context.Context, opts entities.VolumePruneOptions) ([]*reports.PruneReport, error) {
    78  	options := new(volumes.PruneOptions).WithFilters(opts.Filters)
    79  	return volumes.Prune(ic.ClientCtx, options)
    80  }
    81  
    82  func (ic *ContainerEngine) VolumeList(ctx context.Context, opts entities.VolumeListOptions) ([]*entities.VolumeListReport, error) {
    83  	options := new(volumes.ListOptions).WithFilters(opts.Filter)
    84  	return volumes.List(ic.ClientCtx, options)
    85  }
    86  
    87  // VolumeExists checks if the given volume exists
    88  func (ic *ContainerEngine) VolumeExists(ctx context.Context, nameOrID string) (*entities.BoolReport, error) {
    89  	exists, err := volumes.Exists(ic.ClientCtx, nameOrID, nil)
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  	return &entities.BoolReport{
    94  		Value: exists,
    95  	}, nil
    96  }
    97  
    98  // Volumemounted check if a given volume using plugin or filesystem is mounted or not.
    99  // TODO: Not used and exposed to tunnel. Will be used by `export` command which is unavailable to `podman-remote`
   100  func (ic *ContainerEngine) VolumeMounted(ctx context.Context, nameOrID string) (*entities.BoolReport, error) {
   101  	return nil, errors.New("not implemented")
   102  }
   103  
   104  func (ic *ContainerEngine) VolumeMount(ctx context.Context, nameOrIDs []string) ([]*entities.VolumeMountReport, error) {
   105  	return nil, errors.New("mounting volumes is not supported for remote clients")
   106  }
   107  
   108  func (ic *ContainerEngine) VolumeUnmount(ctx context.Context, nameOrIDs []string) ([]*entities.VolumeUnmountReport, error) {
   109  	return nil, errors.New("unmounting volumes is not supported for remote clients")
   110  }