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

get-resources #106

Merged
merged 8 commits into from
Oct 2, 2024
Merged
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
118 changes: 118 additions & 0 deletions cmd/getResources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright © 2023
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"errors"
"fmt"

"github.com/equinor/radix-cli/generated-client/client/application"
"github.com/equinor/radix-cli/pkg/client"
"github.com/equinor/radix-cli/pkg/config"
"github.com/equinor/radix-cli/pkg/flagnames"
"github.com/equinor/radix-cli/pkg/utils/completion"
"github.com/equinor/radix-cli/pkg/utils/json"
"github.com/equinor/radix-common/utils/pointers"
"github.com/spf13/cobra"
)

// getResourcesCmd represents the getResourcesCmd command
var getResourcesCmd = &cobra.Command{
Use: "resources",
Short: "Gets resources used by the Radix application",
Long: `Gets resources used by the Radix application or its environment or a component`,
Example: `
# Get resources used by the application for the last 30 days
rx get resources -a myapp

# Get resources used by the application in the environment for the last 30 days
rx get resources -a myapp -e dev

# Get resources used by the application in the environment for a component for the last 30 days
rx get resources -a myapp -e dev -n mycomponent

# Get resources used by the application in the environment for a component for the last 5 minutes
rx get resources -a myapp -e dev -n mycomponent --duration 5m

# Get resources used by the application in the environment for a component for the last 12 hours starting with two days ago
rx get resources -a myapp -e dev -n mycomponent --duration 12h --since 2d
`,
RunE: func(cmd *cobra.Command, args []string) error {
appName, err := config.GetAppNameFromConfigOrFromParameter(cmd, flagnames.Application)
if err != nil {
return err
}
if appName == "" {
return errors.New("application name is required field")
}
envName, err := cmd.Flags().GetString(flagnames.Environment)
if err != nil {
return err
}
componentName, err := cmd.Flags().GetString(flagnames.Component)
if err != nil {
return err
}
since, err := cmd.Flags().GetString(flagnames.Since)
if err != nil {
return err
}
duration, err := cmd.Flags().GetString(flagnames.Duration)
if err != nil {
return err
}

getResourcesParams := application.NewGetResourcesParams()
getResourcesParams.SetAppName(appName)
getResourcesParams.SetEnvironment(&envName)
getResourcesParams.SetComponent(&componentName)
if duration != "" {
getResourcesParams.SetDuration(pointers.Ptr(duration))
}
if since != "" {
getResourcesParams.SetSince(pointers.Ptr(since))
}

cmd.SilenceUsage = true

apiClient, err := client.GetForCommand(cmd)
if err != nil {
return err
}
resp, err := apiClient.Application.GetResources(getResourcesParams, nil)
if err != nil {
return err
}
prettyJSON, err := json.Pretty(resp.Payload)
if err != nil {
return err
}
fmt.Println(*prettyJSON)
return nil
},
}

func init() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include flag completion, see restartComponent.go.go for an example

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added flag

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing flag completion for environment and component

getCmd.AddCommand(getResourcesCmd)
getResourcesCmd.Flags().StringP(flagnames.Application, "a", "", "Name of the application")
getResourcesCmd.Flags().StringP(flagnames.Environment, "e", "", "Optional, name of the environment")
getResourcesCmd.Flags().StringP(flagnames.Component, "n", "", "Optional, name of the component")
getResourcesCmd.Flags().String(flagnames.Duration, "", "If set, get resources during the specified period (default is 30 days), eg. 5m or 12h")
getResourcesCmd.Flags().String(flagnames.Since, "", "If set, get resources starting from the specified time in the past, eg. 5m or 12h")
_ = getResourcesCmd.RegisterFlagCompletionFunc(flagnames.Application, completion.ApplicationCompletion)
_ = getResourcesCmd.RegisterFlagCompletionFunc(flagnames.Application, completion.EnvironmentCompletion)
_ = getResourcesCmd.RegisterFlagCompletionFunc(flagnames.Application, completion.ComponentCompletion)
setContextSpecificPersistentFlags(getResourcesCmd)
}
41 changes: 41 additions & 0 deletions generated-client/client/application/application_client.go

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

Loading
Loading