feat: working list
This commit is contained in:
48
proxmox.go
48
proxmox.go
@@ -13,6 +13,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/platform-engineering-labs/formae/pkg/plugin"
|
"github.com/platform-engineering-labs/formae/pkg/plugin"
|
||||||
"github.com/platform-engineering-labs/formae/pkg/plugin/resource"
|
"github.com/platform-engineering-labs/formae/pkg/plugin/resource"
|
||||||
@@ -435,16 +436,47 @@ func (p *Plugin) Status(ctx context.Context, req *resource.StatusRequest) (*reso
|
|||||||
// List returns all resource identifiers of a given type.
|
// List returns all resource identifiers of a given type.
|
||||||
// Called during discovery to find unmanaged resources.
|
// Called during discovery to find unmanaged resources.
|
||||||
func (p *Plugin) List(ctx context.Context, req *resource.ListRequest) (*resource.ListResult, error) {
|
func (p *Plugin) List(ctx context.Context, req *resource.ListRequest) (*resource.ListResult, error) {
|
||||||
// TODO: Implement resource listing for discovery
|
|
||||||
//
|
|
||||||
// 1. Use req.ResourceType to determine what to list
|
|
||||||
// 2. Parse req.TargetConfig for provider credentials
|
|
||||||
// 3. Use req.PageToken/PageSize for pagination
|
|
||||||
// 4. Call your provider's API to list resources
|
|
||||||
// 5. Return NativeIDs and NextPageToken (if more pages)
|
|
||||||
|
|
||||||
|
username, token, err := getCredentials()
|
||||||
|
if err != nil {
|
||||||
return &resource.ListResult{
|
return &resource.ListResult{
|
||||||
NativeIDs: []string{},
|
NativeIDs: []string{},
|
||||||
|
}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
config, err := parseTargetConfig(req.TargetConfig)
|
||||||
|
if err != nil {
|
||||||
|
return &resource.ListResult{
|
||||||
|
NativeIDs: []string{},
|
||||||
|
}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
|
||||||
|
var props StatusGeneralResponse
|
||||||
|
|
||||||
|
request, err := http.NewRequest("GET", config.URL+"/api2/json/nodes/"+config.NODE+"/lxc", nil)
|
||||||
|
if err != nil {
|
||||||
|
return &resource.ListResult{
|
||||||
|
NativeIDs: []string{},
|
||||||
|
}, err
|
||||||
|
}
|
||||||
|
request.Header.Set("Authorization", "PVEAPIToken="+username+"="+token)
|
||||||
|
|
||||||
|
resp, err := client.Do(request)
|
||||||
|
|
||||||
|
data, err := io.ReadAll(resp.Body)
|
||||||
|
|
||||||
|
json.Unmarshal(data, &props)
|
||||||
|
|
||||||
|
nativeIds := make([]string, 0, len(props.Data))
|
||||||
|
|
||||||
|
for _, value := range props.Data {
|
||||||
|
nativeIds = append(nativeIds, strconv.Itoa(value.VMID))
|
||||||
|
}
|
||||||
|
|
||||||
|
return &resource.ListResult{
|
||||||
|
NativeIDs: nativeIds,
|
||||||
NextPageToken: nil,
|
NextPageToken: nil,
|
||||||
}, ErrNotImplemented
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -153,6 +153,19 @@ func TestUpdate(t *testing.T) {
|
|||||||
// test if update has happened
|
// test if update has happened
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestList(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
plugin := &Plugin{}
|
||||||
|
|
||||||
|
result, err := plugin.List(ctx, &resource.ListRequest{
|
||||||
|
ResourceType: "PROXMOX::Service::LXC",
|
||||||
|
TargetConfig: testTargetConfig(),
|
||||||
|
})
|
||||||
|
require.NoError(t, err, "ListRequest should not return an error")
|
||||||
|
|
||||||
|
require.Contains(t, result.NativeIDs, "200", "List should include created LXC")
|
||||||
|
}
|
||||||
|
|
||||||
func TestDelete(t *testing.T) {
|
func TestDelete(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
plugin := &Plugin{}
|
plugin := &Plugin{}
|
||||||
|
|||||||
8
types.go
8
types.go
@@ -34,6 +34,14 @@ type DeleteRequest struct {
|
|||||||
TargetConfig json.RawMessage
|
TargetConfig json.RawMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ListRequest struct {
|
||||||
|
ResourceType string
|
||||||
|
TargetConfig json.RawMessage
|
||||||
|
PageSize int32
|
||||||
|
PageToken *string
|
||||||
|
AdditionalProperties map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
type StatusLXCGeneral struct {
|
type StatusLXCGeneral struct {
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
NetIn int `json:"netin"`
|
NetIn int `json:"netin"`
|
||||||
|
|||||||
Reference in New Issue
Block a user