-
Notifications
You must be signed in to change notification settings - Fork 1
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
get-resources #106
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fc24c4f
Added resources endpoint
satr 4beda5c
Added resources endpoint
satr c4a3a29
Merge remote-tracking branch 'origin/master' into get-resources
satr 9bdc13f
Corrected resources command arguments
satr 6b34a74
Removed outdated const
satr 2c91530
Added flag completion
satr 4d9cdf8
Added flag completion
satr e23eaf3
Added flag completion
satr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
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) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added flag
There was a problem hiding this comment.
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