Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function CheckID #157

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package proxmox

import (
"context"
"fmt"
"net/url"
"strconv"
"strings"
Expand Down Expand Up @@ -32,6 +33,17 @@ func (cl *Cluster) NextID(ctx context.Context) (int, error) {
return strconv.Atoi(ret)
}

func (cl *Cluster) IsVMIDTaken(ctx context.Context, vmid int) (bool, error) {
luthermonson marked this conversation as resolved.
Show resolved Hide resolved
var ret string
err := cl.client.Get(ctx, fmt.Sprintf("/cluster/nextid?vmid=%d", vmid), ret)
if err != nil && strings.Contains(err.Error(), fmt.Sprintf("VM %d already exists", vmid)) {
return true, nil
} else if err != nil {
return false, err
}
return false, nil
}

// Resources retrieves a summary list of all resources in the cluster.
// It calls /cluster/resources api v2 endpoint with an optional "type" parameter
// to filter searched values.
Expand Down
16 changes: 16 additions & 0 deletions cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ func TestNextID(t *testing.T) {
assert.Equal(t, 100, nextid)
}

func TestIsVMIDTaken(t *testing.T) {
mocks.On(mockConfig)
defer mocks.Off()
client := mockClient()
ctx := context.Background()

cluster, err := client.Cluster(ctx)
assert.Nil(t, err)
idtaken, err := cluster.IsVMIDTaken(ctx, 100)
assert.Nil(t, err)
assert.Equal(t, false, idtaken)
idtaken, err = cluster.IsVMIDTaken(ctx, 200)
assert.Nil(t, err)
assert.Equal(t, true, idtaken)
}

func TestCluster_Resources(t *testing.T) {
mocks.On(mockConfig)
defer mocks.Off()
Expand Down
14 changes: 13 additions & 1 deletion tests/mocks/pve7x/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@ import (

func cluster() {
gock.New(config.C.URI).
Get("/cluster/nextid").
Get("/cluster/nextid$").
Reply(200).
JSON(`{"data": "100"}`)

gock.New(config.C.URI).
Get("/cluster/nextid$").
MatchParam("vmid", "100").
Reply(200).
JSON(`{"data": "100"}`)

gock.New(config.C.URI).
Get("/cluster/nextid").
MatchParam("vmid", "200").
Reply(400).
JSON(`{"errors":{"vmid":"VM 200 already exists"},"data":null}`)

gock.New(config.C.URI).
Get("/cluster/status").
Reply(200).
Expand Down
Loading