diff --git a/proxmox.go b/proxmox.go index b2e1d3a..df43183 100644 --- a/proxmox.go +++ b/proxmox.go @@ -13,6 +13,7 @@ import ( "io" "log" "net/http" + "strconv" "github.com/platform-engineering-labs/formae/pkg/plugin" "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. // Called during discovery to find unmanaged resources. 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{ + 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: []string{}, + NativeIDs: nativeIds, NextPageToken: nil, - }, ErrNotImplemented + }, nil } diff --git a/proxmox_test.go b/proxmox_test.go index 5f0e5f8..0a7972a 100644 --- a/proxmox_test.go +++ b/proxmox_test.go @@ -153,6 +153,19 @@ func TestUpdate(t *testing.T) { // 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) { ctx := context.Background() plugin := &Plugin{} diff --git a/types.go b/types.go index 91cf4ea..367900b 100644 --- a/types.go +++ b/types.go @@ -34,6 +34,14 @@ type DeleteRequest struct { TargetConfig json.RawMessage } +type ListRequest struct { + ResourceType string + TargetConfig json.RawMessage + PageSize int32 + PageToken *string + AdditionalProperties map[string]string +} + type StatusLXCGeneral struct { Status string `json:"status"` NetIn int `json:"netin"`