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 inventory download URL to agent #120

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Changes from all commits
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
39 changes: 39 additions & 0 deletions internal/agent/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"fmt"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"

"github.com/go-chi/chi"
"github.com/go-chi/render"
liberr "github.com/konveyor/forklift-controller/pkg/lib/error"
api "github.com/kubev2v/migration-planner/api/v1alpha1"
"github.com/kubev2v/migration-planner/internal/agent/config"
"github.com/kubev2v/migration-planner/internal/agent/fileio"
"github.com/kubev2v/migration-planner/internal/agent/service"
Expand All @@ -37,6 +39,19 @@ func RegisterApi(router *chi.Mux, statusUpdater *service.StatusUpdater, configur
router.Put("/api/v1/credentials", func(w http.ResponseWriter, r *http.Request) {
credentialHandler(configuration.DataDir, w, r)
})
router.Get("/api/v1/inventory", func(w http.ResponseWriter, r *http.Request) {
data, err := getInventory(configuration.DataDir)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if data == nil {
http.Error(w, "Inventory not found", http.StatusNotFound)
return
}
w.Header().Add("Content-Disposition", "attachment")
_ = render.Render(w, r, InventoryReply{Inventory: data.Inventory})
})
}

type StatusReply struct {
Expand All @@ -52,6 +67,10 @@ type ServiceUIReply struct {
URL string `json:"url"`
}

type InventoryReply struct {
Inventory api.Inventory `json:"inventory"`
}

func (s ServiceUIReply) Render(w http.ResponseWriter, r *http.Request) error {
return nil
}
Expand All @@ -64,6 +83,26 @@ func (v VersionReply) Render(w http.ResponseWriter, r *http.Request) error {
return nil
}

func (v InventoryReply) Render(w http.ResponseWriter, r *http.Request) error {
return nil
}

func getInventory(dataDir string) (*service.InventoryData, error) {
filename := filepath.Join(dataDir, config.InventoryFile)
contents, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("error reading inventory file: %v", err)
}

var inventory service.InventoryData
err = json.Unmarshal(contents, &inventory)
if err != nil {
return nil, fmt.Errorf("error parsing inventory file: %v", err)
}

return &inventory, nil
}

func credentialHandler(dataDir string, w http.ResponseWriter, r *http.Request) {
credentials := &config.Credentials{}

Expand Down
Loading