mirror of
https://github.com/containers/podman.git
synced 2025-06-20 09:03:43 +08:00
Container rename bindings
Add bindings and podman-remote support for container rename. Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
This commit is contained in:
@ -32,14 +32,13 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// TODO: Once bindings are done, add this to TunnelMode
|
|
||||||
registry.Commands = append(registry.Commands, registry.CliCommand{
|
registry.Commands = append(registry.Commands, registry.CliCommand{
|
||||||
Mode: []entities.EngineMode{entities.ABIMode},
|
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
|
||||||
Command: renameCommand,
|
Command: renameCommand,
|
||||||
})
|
})
|
||||||
|
|
||||||
registry.Commands = append(registry.Commands, registry.CliCommand{
|
registry.Commands = append(registry.Commands, registry.CliCommand{
|
||||||
Mode: []entities.EngineMode{entities.ABIMode},
|
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
|
||||||
Command: containerRenameCommand,
|
Command: containerRenameCommand,
|
||||||
Parent: containerCmd,
|
Parent: containerCmd,
|
||||||
})
|
})
|
||||||
|
28
pkg/bindings/containers/rename.go
Normal file
28
pkg/bindings/containers/rename.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package containers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/containers/podman/v2/pkg/bindings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Rename an existing container.
|
||||||
|
func Rename(ctx context.Context, nameOrID string, options *RenameOptions) error {
|
||||||
|
if options == nil {
|
||||||
|
options = new(RenameOptions)
|
||||||
|
}
|
||||||
|
conn, err := bindings.GetClient(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
params, err := options.ToParams()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
response, err := conn.DoRequest(nil, http.MethodPost, "/containers/%s/rename", params, nil, nameOrID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return response.Process(nil)
|
||||||
|
}
|
@ -194,6 +194,13 @@ type InitOptions struct{}
|
|||||||
// ShouldRestartOptions
|
// ShouldRestartOptions
|
||||||
type ShouldRestartOptions struct{}
|
type ShouldRestartOptions struct{}
|
||||||
|
|
||||||
|
//go:generate go run ../generator/generator.go RenameOptions
|
||||||
|
// RenameOptions are options for renaming containers.
|
||||||
|
// The Name field is required.
|
||||||
|
type RenameOptions struct {
|
||||||
|
Name *string
|
||||||
|
}
|
||||||
|
|
||||||
//go:generate go run ../generator/generator.go ResizeTTYOptions
|
//go:generate go run ../generator/generator.go ResizeTTYOptions
|
||||||
// ResizeTTYOptions are optional options for resizing
|
// ResizeTTYOptions are optional options for resizing
|
||||||
// container TTYs
|
// container TTYs
|
||||||
|
104
pkg/bindings/containers/types_rename_options.go
Normal file
104
pkg/bindings/containers/types_rename_options.go
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
package containers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/url"
|
||||||
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
This file is generated automatically by go generate. Do not edit.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Changed
|
||||||
|
func (o *RenameOptions) Changed(fieldName string) bool {
|
||||||
|
r := reflect.ValueOf(o)
|
||||||
|
value := reflect.Indirect(r).FieldByName(fieldName)
|
||||||
|
return !value.IsNil()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToParams
|
||||||
|
func (o *RenameOptions) ToParams() (url.Values, error) {
|
||||||
|
params := url.Values{}
|
||||||
|
if o == nil {
|
||||||
|
return params, nil
|
||||||
|
}
|
||||||
|
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||||||
|
s := reflect.ValueOf(o)
|
||||||
|
if reflect.Ptr == s.Kind() {
|
||||||
|
s = s.Elem()
|
||||||
|
}
|
||||||
|
sType := s.Type()
|
||||||
|
for i := 0; i < s.NumField(); i++ {
|
||||||
|
fieldName := sType.Field(i).Name
|
||||||
|
if !o.Changed(fieldName) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fieldName = strings.ToLower(fieldName)
|
||||||
|
f := s.Field(i)
|
||||||
|
if reflect.Ptr == f.Kind() {
|
||||||
|
f = f.Elem()
|
||||||
|
}
|
||||||
|
switch f.Kind() {
|
||||||
|
case reflect.Bool:
|
||||||
|
params.Set(fieldName, strconv.FormatBool(f.Bool()))
|
||||||
|
case reflect.String:
|
||||||
|
params.Set(fieldName, f.String())
|
||||||
|
case reflect.Int, reflect.Int64:
|
||||||
|
// f.Int() is always an int64
|
||||||
|
params.Set(fieldName, strconv.FormatInt(f.Int(), 10))
|
||||||
|
case reflect.Uint, reflect.Uint64:
|
||||||
|
// f.Uint() is always an uint64
|
||||||
|
params.Set(fieldName, strconv.FormatUint(f.Uint(), 10))
|
||||||
|
case reflect.Slice:
|
||||||
|
typ := reflect.TypeOf(f.Interface()).Elem()
|
||||||
|
switch typ.Kind() {
|
||||||
|
case reflect.String:
|
||||||
|
sl := f.Slice(0, f.Len())
|
||||||
|
s, ok := sl.Interface().([]string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("failed to convert to string slice")
|
||||||
|
}
|
||||||
|
for _, val := range s {
|
||||||
|
params.Add(fieldName, val)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return nil, errors.Errorf("unknown slice type %s", f.Kind().String())
|
||||||
|
}
|
||||||
|
case reflect.Map:
|
||||||
|
lowerCaseKeys := make(map[string][]string)
|
||||||
|
iter := f.MapRange()
|
||||||
|
for iter.Next() {
|
||||||
|
lowerCaseKeys[iter.Key().Interface().(string)] = iter.Value().Interface().([]string)
|
||||||
|
|
||||||
|
}
|
||||||
|
s, err := json.MarshalToString(lowerCaseKeys)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
params.Set(fieldName, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return params, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithName
|
||||||
|
func (o *RenameOptions) WithName(value string) *RenameOptions {
|
||||||
|
v := &value
|
||||||
|
o.Name = v
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetName
|
||||||
|
func (o *RenameOptions) GetName() string {
|
||||||
|
var name string
|
||||||
|
if o.Name == nil {
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
return *o.Name
|
||||||
|
}
|
@ -823,5 +823,5 @@ func (ic *ContainerEngine) ShouldRestart(_ context.Context, id string) (bool, er
|
|||||||
|
|
||||||
// ContainerRename renames the given container.
|
// ContainerRename renames the given container.
|
||||||
func (ic *ContainerEngine) ContainerRename(ctx context.Context, nameOrID string, opts entities.ContainerRenameOptions) error {
|
func (ic *ContainerEngine) ContainerRename(ctx context.Context, nameOrID string, opts entities.ContainerRenameOptions) error {
|
||||||
return errors.Errorf("NOT YET IMPLEMENTED")
|
return containers.Rename(ic.ClientCtx, nameOrID, new(containers.RenameOptions).WithName(opts.NewName))
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,6 @@ var _ = Describe("podman rename", func() {
|
|||||||
)
|
)
|
||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
SkipIfRemote("Rename not yet implemented by podman-remote")
|
|
||||||
tempdir, err = CreateTempDirInTempDir()
|
tempdir, err = CreateTempDirInTempDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
Reference in New Issue
Block a user