Skip to content
Open
Show file tree
Hide file tree
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
36 changes: 34 additions & 2 deletions rover-ctl/pkg/commands/get-info/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ func NewCommand() *cobra.Command {
baseCmd := base.NewFileCommand(
"get-info",
"Get information about a resource",
"Get detailed information about a resource using its metadata",
`Get detailed information about a specific resource by name or multiple resources from the server.
If no name or file is provided, information about all resources of the specified type will be retrieved.`,
)
cmd := &Command{
FileCommand: baseCmd,
}

cmd.Cmd.Flags().StringVarP(&cmd.Name, "name", "n", "", "Name of the resource to get information about")
cmd.Cmd.MarkFlagsOneRequired("name", "file")
cmd.Cmd.MarkFlagsMutuallyExclusive("name", "file")

cmd.Cmd.Flags().BoolVarP(&cmd.Shallow, "shallow", "s", false, "Get only basic information without details")
Expand Down Expand Up @@ -75,10 +75,42 @@ func (c *Command) Run(cmd *cobra.Command, args []string) error {
}
}

if c.Name == "" && c.FilePath == "" {
return c.getInfoMany()
}

c.Logger().V(1).Info("Completed get-info command")
return nil
}

func (c *Command) getInfoMany() error {
roverHandler, err := handlers.GetHandler(kind, apiVersion)
if err != nil {
return errors.Wrap(err, "failed to get rover handler")
}

c.Logger().V(1).Info("Getting info for multiple resources")

infoList, err := roverHandler.InfoMany(c.Cmd.Context(), nil)
if err != nil {
return c.HandleError(err, "get info for Rovers")
}

prettyString, err := util.FormatOutput(infoList, viper.GetString("output.format"))
if err != nil {
return errors.Wrap(err, "failed to format output")
}

_, err = c.Cmd.OutOrStdout().Write([]byte(prettyString))
if err != nil {
return errors.Wrap(err, "failed to write output")
}

c.Logger().V(1).Info("Successfully retrieved info for multiple resources")

return nil
}

func (c *Command) getInfoFor(name string) error {
roverHandler, err := handlers.GetHandler(kind, apiVersion)
if err != nil {
Expand Down
44 changes: 30 additions & 14 deletions rover-ctl/pkg/handlers/common/base_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ func (h *BaseHandler) Get(ctx context.Context, name string) (any, error) {
token := h.Setup(ctx)
url := h.GetRequestUrl(token.Group, token.Team, name)

// Send the request (no obj, so no hooks will be executed)
resp, err := h.SendRequest(ctx, nil, http.MethodGet, url)
if err != nil {
return nil, err
Expand Down Expand Up @@ -233,7 +232,6 @@ func (h *BaseHandler) ListWithCursor(ctx context.Context, cursor string) (*ListR
url += "?cursor=" + cursor
}

// Send the request (no obj, so no hooks will be executed)
resp, err := h.SendRequest(ctx, nil, http.MethodGet, url)
if err != nil {
return nil, err
Expand All @@ -259,7 +257,6 @@ func (h *BaseHandler) Status(ctx context.Context, name string) (types.ObjectStat
token := h.Setup(ctx)
url := h.GetRequestUrl(token.Group, token.Team, name, "status")

// Send the request (no obj, so no hooks will be executed)
resp, err := h.SendRequest(ctx, nil, http.MethodGet, url)
if err != nil {
return nil, err
Expand Down Expand Up @@ -296,7 +293,32 @@ func (h *BaseHandler) Info(ctx context.Context, name string) (any, error) {
token := h.Setup(ctx)
url := h.GetRequestUrl(token.Group, token.Team, name, "info")

// Send the request (no obj, so no hooks will be executed)
return h.execInfoRequest(ctx, url)
}

func (h *BaseHandler) InfoMany(ctx context.Context, names []string) (any, error) {
if !h.SupportsInfo {
return nil, errors.Errorf("info operation is not supported for %s", h.Resource)
}
token := h.Setup(ctx)
reqUrl := h.GetRequestUrl(token.Group, token.Team, "", "info")
queryParams := ""
for _, name := range names {
if queryParams == "" {
queryParams = "?name=" + name
} else {
queryParams += "&name=" + name
}
}
reqUrl += queryParams

return h.execInfoRequest(ctx, reqUrl)
}

func (h *BaseHandler) execInfoRequest(ctx context.Context, url string) (any, error) {

h.logger.V(1).Info("Executing info request", "url", url)

resp, err := h.SendRequest(ctx, nil, http.MethodGet, url)
if err != nil {
return nil, err
Expand Down Expand Up @@ -339,11 +361,8 @@ func (h *BaseHandler) RunHooks(stage HandlerHookStage, ctx context.Context, obj
// SendRequest handles common request operations including running hooks
func (h *BaseHandler) SendRequest(ctx context.Context, obj types.Object, method, url string) (*http.Response, error) {

// Run pre-request hooks if object is provided
if obj != nil {
if err := h.RunHooks(PreRequestHook, ctx, obj); err != nil {
return nil, err
}
if err := h.RunHooks(PreRequestHook, ctx, obj); err != nil {
return nil, err
}

var body io.ReadWriter
Expand Down Expand Up @@ -382,11 +401,8 @@ func (h *BaseHandler) SendRequest(ctx context.Context, obj types.Object, method,

h.logger.V(1).Info("Received response", "status", resp.Status)

// Run post-request hooks if object is provided
if obj != nil {
if err := h.RunHooks(PostRequestHook, ctx, obj); err != nil {
return nil, err
}
if err := h.RunHooks(PostRequestHook, ctx, obj); err != nil {
return nil, err
}

return resp, nil
Expand Down
5 changes: 5 additions & 0 deletions rover-ctl/pkg/handlers/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ type ResourceHandler interface {
// List retrieves all resources of this type from the server
List(ctx context.Context) ([]any, error)

// Info retrieves detailed information about a resource by name from the server
Info(ctx context.Context, name string) (any, error)

// InfoMany retrieves detailed information about multiple resources by their names from the server
// If names is empty, all resources of this type should be returned
InfoMany(ctx context.Context, names []string) (any, error)
}

type Waiter interface {
Expand Down
3 changes: 3 additions & 0 deletions rover-ctl/pkg/handlers/v0/apispec.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func NewApiSpecHandlerInstance() *ApiSpecHandler {
}

func PatchApiSpecificationRequest(ctx context.Context, obj types.Object) error {
if obj == nil {
return nil
}
content := map[string]any{
"specification": obj.GetContent(),
}
Expand Down
4 changes: 4 additions & 0 deletions rover-ctl/pkg/handlers/v0/rover.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func NewRoverHandlerInstance() *RoverHandler {
}

func PatchRoverRequest(ctx context.Context, obj types.Object) error {
if obj == nil {
return nil
}

content := obj.GetContent()
spec, ok := content["spec"].(map[string]any)
if !ok {
Expand Down
61 changes: 60 additions & 1 deletion rover-ctl/test/mocks/mock_ResetSecretHandler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 60 additions & 1 deletion rover-ctl/test/mocks/mock_ResourceHandler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading