Skip to content

Commit 7d3a628

Browse files
Merge pull request containers#8982 from Luap99/container-rename-bindings
Container rename bindings
2 parents 5a166b2 + 0688f08 commit 7d3a628

File tree

7 files changed

+143
-5
lines changed

7 files changed

+143
-5
lines changed

cmd/podman/containers/rename.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@ var (
3232
)
3333

3434
func init() {
35-
// TODO: Once bindings are done, add this to TunnelMode
3635
registry.Commands = append(registry.Commands, registry.CliCommand{
37-
Mode: []entities.EngineMode{entities.ABIMode},
36+
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
3837
Command: renameCommand,
3938
})
4039

4140
registry.Commands = append(registry.Commands, registry.CliCommand{
42-
Mode: []entities.EngineMode{entities.ABIMode},
41+
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
4342
Command: containerRenameCommand,
4443
Parent: containerCmd,
4544
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.so man1/podman-rename.1

pkg/bindings/containers/rename.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package containers
2+
3+
import (
4+
"context"
5+
"net/http"
6+
7+
"github.com/containers/podman/v2/pkg/bindings"
8+
)
9+
10+
// Rename an existing container.
11+
func Rename(ctx context.Context, nameOrID string, options *RenameOptions) error {
12+
if options == nil {
13+
options = new(RenameOptions)
14+
}
15+
conn, err := bindings.GetClient(ctx)
16+
if err != nil {
17+
return err
18+
}
19+
params, err := options.ToParams()
20+
if err != nil {
21+
return err
22+
}
23+
response, err := conn.DoRequest(nil, http.MethodPost, "/containers/%s/rename", params, nil, nameOrID)
24+
if err != nil {
25+
return err
26+
}
27+
return response.Process(nil)
28+
}

pkg/bindings/containers/types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ type InitOptions struct{}
194194
// ShouldRestartOptions
195195
type ShouldRestartOptions struct{}
196196

197+
//go:generate go run ../generator/generator.go RenameOptions
198+
// RenameOptions are options for renaming containers.
199+
// The Name field is required.
200+
type RenameOptions struct {
201+
Name *string
202+
}
203+
197204
//go:generate go run ../generator/generator.go ResizeTTYOptions
198205
// ResizeTTYOptions are optional options for resizing
199206
// container TTYs
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package containers
2+
3+
import (
4+
"net/url"
5+
"reflect"
6+
"strconv"
7+
"strings"
8+
9+
jsoniter "github.com/json-iterator/go"
10+
"github.com/pkg/errors"
11+
)
12+
13+
/*
14+
This file is generated automatically by go generate. Do not edit.
15+
*/
16+
17+
// Changed
18+
func (o *RenameOptions) Changed(fieldName string) bool {
19+
r := reflect.ValueOf(o)
20+
value := reflect.Indirect(r).FieldByName(fieldName)
21+
return !value.IsNil()
22+
}
23+
24+
// ToParams
25+
func (o *RenameOptions) ToParams() (url.Values, error) {
26+
params := url.Values{}
27+
if o == nil {
28+
return params, nil
29+
}
30+
json := jsoniter.ConfigCompatibleWithStandardLibrary
31+
s := reflect.ValueOf(o)
32+
if reflect.Ptr == s.Kind() {
33+
s = s.Elem()
34+
}
35+
sType := s.Type()
36+
for i := 0; i < s.NumField(); i++ {
37+
fieldName := sType.Field(i).Name
38+
if !o.Changed(fieldName) {
39+
continue
40+
}
41+
fieldName = strings.ToLower(fieldName)
42+
f := s.Field(i)
43+
if reflect.Ptr == f.Kind() {
44+
f = f.Elem()
45+
}
46+
switch f.Kind() {
47+
case reflect.Bool:
48+
params.Set(fieldName, strconv.FormatBool(f.Bool()))
49+
case reflect.String:
50+
params.Set(fieldName, f.String())
51+
case reflect.Int, reflect.Int64:
52+
// f.Int() is always an int64
53+
params.Set(fieldName, strconv.FormatInt(f.Int(), 10))
54+
case reflect.Uint, reflect.Uint64:
55+
// f.Uint() is always an uint64
56+
params.Set(fieldName, strconv.FormatUint(f.Uint(), 10))
57+
case reflect.Slice:
58+
typ := reflect.TypeOf(f.Interface()).Elem()
59+
switch typ.Kind() {
60+
case reflect.String:
61+
sl := f.Slice(0, f.Len())
62+
s, ok := sl.Interface().([]string)
63+
if !ok {
64+
return nil, errors.New("failed to convert to string slice")
65+
}
66+
for _, val := range s {
67+
params.Add(fieldName, val)
68+
}
69+
default:
70+
return nil, errors.Errorf("unknown slice type %s", f.Kind().String())
71+
}
72+
case reflect.Map:
73+
lowerCaseKeys := make(map[string][]string)
74+
iter := f.MapRange()
75+
for iter.Next() {
76+
lowerCaseKeys[iter.Key().Interface().(string)] = iter.Value().Interface().([]string)
77+
78+
}
79+
s, err := json.MarshalToString(lowerCaseKeys)
80+
if err != nil {
81+
return nil, err
82+
}
83+
84+
params.Set(fieldName, s)
85+
}
86+
}
87+
return params, nil
88+
}
89+
90+
// WithName
91+
func (o *RenameOptions) WithName(value string) *RenameOptions {
92+
v := &value
93+
o.Name = v
94+
return o
95+
}
96+
97+
// GetName
98+
func (o *RenameOptions) GetName() string {
99+
var name string
100+
if o.Name == nil {
101+
return name
102+
}
103+
return *o.Name
104+
}

pkg/domain/infra/tunnel/containers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,5 +823,5 @@ func (ic *ContainerEngine) ShouldRestart(_ context.Context, id string) (bool, er
823823

824824
// ContainerRename renames the given container.
825825
func (ic *ContainerEngine) ContainerRename(ctx context.Context, nameOrID string, opts entities.ContainerRenameOptions) error {
826-
return errors.Errorf("NOT YET IMPLEMENTED")
826+
return containers.Rename(ic.ClientCtx, nameOrID, new(containers.RenameOptions).WithName(opts.NewName))
827827
}

test/e2e/rename_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ var _ = Describe("podman rename", func() {
1717
)
1818

1919
BeforeEach(func() {
20-
SkipIfRemote("Rename not yet implemented by podman-remote")
2120
tempdir, err = CreateTempDirInTempDir()
2221
if err != nil {
2322
os.Exit(1)

0 commit comments

Comments
 (0)