|
| 1 | +// Copyright © 2023 |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cmd |
| 16 | + |
| 17 | +import ( |
| 18 | + "errors" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + "github.com/equinor/radix-cli/generated-client/client/application" |
| 22 | + "github.com/equinor/radix-cli/pkg/client" |
| 23 | + "github.com/equinor/radix-cli/pkg/config" |
| 24 | + "github.com/equinor/radix-cli/pkg/flagnames" |
| 25 | + "github.com/equinor/radix-cli/pkg/utils/completion" |
| 26 | + "github.com/equinor/radix-cli/pkg/utils/json" |
| 27 | + "github.com/equinor/radix-common/utils/pointers" |
| 28 | + "github.com/spf13/cobra" |
| 29 | +) |
| 30 | + |
| 31 | +// getResourcesCmd represents the getResourcesCmd command |
| 32 | +var getResourcesCmd = &cobra.Command{ |
| 33 | + Use: "resources", |
| 34 | + Short: "Gets resources used by the Radix application", |
| 35 | + Long: `Gets resources used by the Radix application or its environment or a component`, |
| 36 | + Example: ` |
| 37 | +# Get resources used by the application for the last 30 days |
| 38 | +rx get resources -a myapp |
| 39 | +
|
| 40 | +# Get resources used by the application in the environment for the last 30 days |
| 41 | +rx get resources -a myapp -e dev |
| 42 | +
|
| 43 | +# Get resources used by the application in the environment for a component for the last 30 days |
| 44 | +rx get resources -a myapp -e dev -n mycomponent |
| 45 | +
|
| 46 | +# Get resources used by the application in the environment for a component for the last 5 minutes |
| 47 | +rx get resources -a myapp -e dev -n mycomponent --duration 5m |
| 48 | +
|
| 49 | +# Get resources used by the application in the environment for a component for the last 12 hours starting with two days ago |
| 50 | +rx get resources -a myapp -e dev -n mycomponent --duration 12h --since 2d |
| 51 | +`, |
| 52 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 53 | + appName, err := config.GetAppNameFromConfigOrFromParameter(cmd, flagnames.Application) |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + if appName == "" { |
| 58 | + return errors.New("application name is required field") |
| 59 | + } |
| 60 | + envName, err := cmd.Flags().GetString(flagnames.Environment) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + componentName, err := cmd.Flags().GetString(flagnames.Component) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + since, err := cmd.Flags().GetString(flagnames.Since) |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + duration, err := cmd.Flags().GetString(flagnames.Duration) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + getResourcesParams := application.NewGetResourcesParams() |
| 78 | + getResourcesParams.SetAppName(appName) |
| 79 | + getResourcesParams.SetEnvironment(&envName) |
| 80 | + getResourcesParams.SetComponent(&componentName) |
| 81 | + if duration != "" { |
| 82 | + getResourcesParams.SetDuration(pointers.Ptr(duration)) |
| 83 | + } |
| 84 | + if since != "" { |
| 85 | + getResourcesParams.SetSince(pointers.Ptr(since)) |
| 86 | + } |
| 87 | + |
| 88 | + cmd.SilenceUsage = true |
| 89 | + |
| 90 | + apiClient, err := client.GetForCommand(cmd) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + resp, err := apiClient.Application.GetResources(getResourcesParams, nil) |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + prettyJSON, err := json.Pretty(resp.Payload) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + fmt.Println(*prettyJSON) |
| 103 | + return nil |
| 104 | + }, |
| 105 | +} |
| 106 | + |
| 107 | +func init() { |
| 108 | + getCmd.AddCommand(getResourcesCmd) |
| 109 | + getResourcesCmd.Flags().StringP(flagnames.Application, "a", "", "Name of the application") |
| 110 | + getResourcesCmd.Flags().StringP(flagnames.Environment, "e", "", "Optional, name of the environment") |
| 111 | + getResourcesCmd.Flags().StringP(flagnames.Component, "n", "", "Optional, name of the component") |
| 112 | + getResourcesCmd.Flags().String(flagnames.Duration, "", "If set, get resources during the specified period (default is 30 days), eg. 5m or 12h") |
| 113 | + getResourcesCmd.Flags().String(flagnames.Since, "", "If set, get resources starting from the specified time in the past, eg. 5m or 12h") |
| 114 | + _ = getResourcesCmd.RegisterFlagCompletionFunc(flagnames.Application, completion.ApplicationCompletion) |
| 115 | + _ = getResourcesCmd.RegisterFlagCompletionFunc(flagnames.Application, completion.EnvironmentCompletion) |
| 116 | + _ = getResourcesCmd.RegisterFlagCompletionFunc(flagnames.Application, completion.ComponentCompletion) |
| 117 | + setContextSpecificPersistentFlags(getResourcesCmd) |
| 118 | +} |
0 commit comments