Skip to content

Commit

Permalink
fix: adding container interfaces method (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
bl4ko authored Mar 26, 2024
1 parent 0a31b09 commit 91e6ecf
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
10 changes: 4 additions & 6 deletions containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,10 @@ func (c *Container) Feature(ctx context.Context) (hasFeature bool, err error) {
return feature.HasFeature, err
}

// This seems broken on the proxmox side: https://pve.proxmox.com/pve-docs/api-viewer/index.html#/nodes/{node}/lxc/{vmid}/interfaces
// I wasn't able to make it work with the API. Tested on {"release":"7.3","repoid":"c3928077","version":"7.3-3"}
// func (c *Container) Interfaces(ctx context.Context) (interfaces []string, err error) {
// err = c.client.Get(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/interfaces", c.Node, c.VMID), &interfaces)
// return interfaces, err
// }
func (c *Container) Interfaces(ctx context.Context) (interfaces ContainerInterfaces, err error) {
err = c.client.Get(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/interfaces", c.Node, c.VMID), &interfaces)
return interfaces, err
}

func (c *Container) Migrate(ctx context.Context, params *ContainerMigrateOptions) (task *Task, err error) {
var upid UPID
Expand Down
16 changes: 16 additions & 0 deletions containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,19 @@ func TestContainerRollbackSnapshot(t *testing.T) {
assert.Nil(t, err)
assert.NotEmpty(t, task)
}

func TestContainerInterfaces(t *testing.T) {
mocks.On(mockConfig)
defer mocks.Off()
client := mockClient()
ctx := context.Background()
container := Container{
client: client,
Node: "node1",
VMID: 101,
}
interfaces, err := container.Interfaces(ctx)
assert.Nil(t, err)
assert.NotEmpty(t, interfaces)
assert.Equal(t, interfaces, ContainerInterfaces{{HWAddr: "00:00:00:00:00:00", Inet: "127.0.0.1/8", Name: "lo", Inet6: "::1/128"}, {Inet6: "fe80::be24:11ff:fe89:6707/64", Name: "eth0", HWAddr: "bc:24:11:89:67:07", Inet: "192.168.3.95/22"}})
}
19 changes: 19 additions & 0 deletions tests/mocks/pve7x/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,25 @@ func nodes() {
}`)

// LXC
gock.New(config.C.URI).
Get("^/nodes/node1/lxc/101/interfaces").
Reply(200).
JSON(`{
"data": [
{
"inet":"127.0.0.1/8",
"hwaddr":"00:00:00:00:00:00",
"name":"lo",
"inet6":"::1/128"
},
{
"inet6":"fe80::be24:11ff:fe89:6707/64",
"name":"eth0",
"hwaddr":"bc:24:11:89:67:07",
"inet":"192.168.3.95/22"
}
]
}`)

gock.New(config.C.URI).
Get("^/nodes/node1/lxc").
Expand Down
9 changes: 9 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,15 @@ type Container struct {
Tags string
}

type ContainerInterfaces []*ContainerInterface

type ContainerInterface struct {
HWAddr string `json:"hwaddr,omitempty"`
Name string `json:"name,omitempty"`
Inet string `json:"inet,omitempty"`
Inet6 string `json:"inet6,omitempty"`
}

type ContainerCloneOptions struct {
NewID int `json:"newid"`
BWLimit uint64 `json:"bwlimit,omitempty"`
Expand Down

0 comments on commit 91e6ecf

Please sign in to comment.