-
Notifications
You must be signed in to change notification settings - Fork 59
Add ecctl project list command for serverless projects
#753
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
Open
Kushmaro
wants to merge
2
commits into
master
Choose a base branch
from
add-project-list-command
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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
This file contains hidden or 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,32 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you 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 cmdproject | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // Command is the top level project command. | ||
| var Command = &cobra.Command{ | ||
| Use: "project", | ||
| Short: "Manages serverless projects", | ||
| PreRunE: cobra.MaximumNArgs(0), | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| cmd.Help() | ||
| }, | ||
| } |
This file contains hidden or 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,54 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you 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 cmdproject | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/elastic/ecctl/pkg/ecctl" | ||
| "github.com/elastic/ecctl/pkg/project" | ||
| ) | ||
|
|
||
| var listCmd = &cobra.Command{ | ||
| Use: "list", | ||
| Short: "Lists serverless projects", | ||
| PreRunE: cobra.NoArgs, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| projectType, _ := cmd.Flags().GetString("type") | ||
|
|
||
| res, err := project.List(project.ListParams{ | ||
| API: ecctl.Get().API, | ||
| Host: ecctl.Get().Config.Host, | ||
| Type: projectType, | ||
| Client: ecctl.Get().Config.Client, | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return ecctl.Get().Formatter.Format(filepath.Join("project", "list"), res) | ||
| }, | ||
| } | ||
|
|
||
| func init() { | ||
| Command.AddCommand(listCmd) | ||
|
|
||
| listCmd.Flags().String("type", "", "Filters by project type (elasticsearch, observability, security)") | ||
| } | ||
This file contains hidden or 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,209 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you 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 cmdproject | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/elastic/cloud-sdk-go/pkg/api/mock" | ||
|
|
||
| "github.com/elastic/ecctl/cmd/util/testutils" | ||
| "github.com/elastic/ecctl/pkg/project" | ||
| ) | ||
|
|
||
| func newProjectListBody(projects []project.Project) mock.Response { | ||
| body := project.ListResponse{Items: projects} | ||
| b, _ := json.Marshal(body) | ||
| return mock.New200Response(mock.NewByteBody(b)) | ||
| } | ||
|
|
||
| func initListFlags() { | ||
| listCmd.ResetFlags() | ||
| listCmd.Flags().String("type", "", "Filters by project type (elasticsearch, observability, security)") | ||
| } | ||
|
|
||
| func Test_listCmd(t *testing.T) { | ||
| var esProjects = []project.Project{ | ||
| { | ||
| ID: "abc123", | ||
| Name: "my-es-project", | ||
| Type: "elasticsearch", | ||
| RegionID: "aws-us-east-1", | ||
| Alias: "my-es-project-abc123", | ||
| }, | ||
| } | ||
| var obsProjects = []project.Project{ | ||
| { | ||
| ID: "def456", | ||
| Name: "my-obs-project", | ||
| Type: "observability", | ||
| RegionID: "gcp-us-central1", | ||
| Alias: "my-obs-project-def456", | ||
| }, | ||
| } | ||
| var secProjects = []project.Project{ | ||
| { | ||
| ID: "ghi789", | ||
| Name: "my-sec-project", | ||
| Type: "security", | ||
| RegionID: "azure-eastus2", | ||
| Alias: "", | ||
| }, | ||
| } | ||
|
|
||
| var allProjects project.ListResult | ||
| allProjects.Projects = append(allProjects.Projects, esProjects...) | ||
| allProjects.Projects = append(allProjects.Projects, obsProjects...) | ||
| allProjects.Projects = append(allProjects.Projects, secProjects...) | ||
|
|
||
| allProjectsJSON, _ := json.MarshalIndent(allProjects, "", " ") | ||
|
|
||
| var esResult = project.ListResult{Projects: esProjects} | ||
| esResultJSON, _ := json.MarshalIndent(esResult, "", " ") | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| args testutils.Args | ||
| want testutils.Assertion | ||
| }{ | ||
| { | ||
| name: "fails due to arguments passed", | ||
| args: testutils.Args{ | ||
| Cmd: listCmd, | ||
| Args: []string{"list", "some-argument"}, | ||
| Cfg: testutils.MockCfg{Responses: []mock.Response{ | ||
| mock.SampleInternalError(), | ||
| }}, | ||
| }, | ||
| want: testutils.Assertion{ | ||
| Err: `unknown command "some-argument" for "project list"`, | ||
| }, | ||
| }, | ||
| { | ||
| name: "fails due to invalid type", | ||
| args: testutils.Args{ | ||
| Cmd: listCmd, | ||
| Args: []string{"list", "--type", "invalid"}, | ||
| Cfg: testutils.MockCfg{Responses: []mock.Response{ | ||
| mock.SampleInternalError(), | ||
| }}, | ||
| }, | ||
| want: testutils.Assertion{ | ||
| Err: `invalid project type "invalid", must be one of: elasticsearch, observability, security`, | ||
| }, | ||
| }, | ||
| { | ||
| name: "succeeds filtering by elasticsearch type with JSON format", | ||
| args: testutils.Args{ | ||
| Cmd: listCmd, | ||
| Args: []string{"list", "--type", "elasticsearch"}, | ||
| Cfg: testutils.MockCfg{ | ||
| OutputFormat: "json", | ||
| Responses: []mock.Response{ | ||
| newProjectListBody(esProjects), | ||
| }, | ||
| }, | ||
| }, | ||
| want: testutils.Assertion{ | ||
| Stdout: string(esResultJSON) + "\n", | ||
| }, | ||
| }, | ||
| { | ||
| name: "succeeds filtering by elasticsearch type with text format", | ||
| args: testutils.Args{ | ||
| Cmd: listCmd, | ||
| Args: []string{"list", "--type", "elasticsearch"}, | ||
| Cfg: testutils.MockCfg{ | ||
| OutputFormat: "text", | ||
| Responses: []mock.Response{ | ||
| newProjectListBody(esProjects), | ||
| }, | ||
| }, | ||
| }, | ||
| want: testutils.Assertion{ | ||
| Stdout: "ID NAME TYPE REGION ALIAS\n" + | ||
| "abc123 my-es-project elasticsearch aws-us-east-1 my-es-project-abc123\n", | ||
| }, | ||
| }, | ||
| { | ||
| name: "succeeds listing all project types with JSON format", | ||
| args: testutils.Args{ | ||
| Cmd: listCmd, | ||
| Args: []string{"list"}, | ||
| Cfg: testutils.MockCfg{ | ||
| OutputFormat: "json", | ||
| Responses: []mock.Response{ | ||
| newProjectListBody(esProjects), | ||
| newProjectListBody(obsProjects), | ||
| newProjectListBody(secProjects), | ||
| }, | ||
| }, | ||
| }, | ||
| want: testutils.Assertion{ | ||
| Stdout: string(allProjectsJSON) + "\n", | ||
| }, | ||
| }, | ||
| { | ||
| name: "succeeds listing all project types with text format", | ||
| args: testutils.Args{ | ||
| Cmd: listCmd, | ||
| Args: []string{"list"}, | ||
| Cfg: testutils.MockCfg{ | ||
| OutputFormat: "text", | ||
| Responses: []mock.Response{ | ||
| newProjectListBody(esProjects), | ||
| newProjectListBody(obsProjects), | ||
| newProjectListBody(secProjects), | ||
| }, | ||
| }, | ||
| }, | ||
| want: testutils.Assertion{ | ||
| Stdout: "ID NAME TYPE REGION ALIAS\n" + | ||
| "abc123 my-es-project elasticsearch aws-us-east-1 my-es-project-abc123\n" + | ||
| "def456 my-obs-project observability gcp-us-central1 my-obs-project-def456\n" + | ||
| "ghi789 my-sec-project security azure-eastus2 -\n", | ||
| }, | ||
| }, | ||
| { | ||
| name: "succeeds with empty project list", | ||
| args: testutils.Args{ | ||
| Cmd: listCmd, | ||
| Args: []string{"list", "--type", "security"}, | ||
| Cfg: testutils.MockCfg{ | ||
| OutputFormat: "json", | ||
| Responses: []mock.Response{ | ||
| newProjectListBody(nil), | ||
| }, | ||
| }, | ||
| }, | ||
| want: testutils.Assertion{ | ||
| Stdout: `{ | ||
| "projects": null | ||
| } | ||
| `, | ||
| }, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| testutils.RunCmdAssertion(t, tt.args, tt.want) | ||
| initListFlags() | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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,45 @@ | ||
| [#ecctl_project] | ||
| == ecctl project | ||
|
|
||
| Manages serverless projects | ||
|
|
||
| ---- | ||
| ecctl project [flags] | ||
| ---- | ||
|
|
||
| [float] | ||
| === Options | ||
|
|
||
| ---- | ||
| -h, --help help for project | ||
| ---- | ||
|
|
||
| [float] | ||
| === Options inherited from parent commands | ||
|
|
||
| ---- | ||
| --api-key string API key to use to authenticate (If empty will look for EC_API_KEY environment variable) | ||
| --config string Config name, used to have multiple configs in $HOME/.ecctl/<env> (default "config") | ||
| --force Do not ask for confirmation | ||
| --format string Formats the output using a Go template | ||
| --host string Base URL to use | ||
| --insecure Skips all TLS validation | ||
| --message string A message to set on cluster operation | ||
| --output string Output format [text|json] (default "text") | ||
| --pass string Password to use to authenticate (If empty will look for EC_PASS environment variable) | ||
| --pprof Enables pprofing and saves the profile to pprof-20060102150405 | ||
| -q, --quiet Suppresses the configuration file used for the run, if any | ||
| --region string Elastic Cloud Hosted region | ||
| --timeout duration Timeout to use on all HTTP calls (default 30s) | ||
| --trace Enables tracing saves the trace to trace-20060102150405 | ||
| --user string Username to use to authenticate (If empty will look for EC_USER environment variable) | ||
| --verbose Enable verbose mode | ||
| --verbose-credentials When set, Authorization headers on the request/response trail will be displayed as plain text | ||
| --verbose-file string When set, the verbose request/response trail will be written to the defined file | ||
| ---- | ||
|
|
||
| [float] | ||
| === SEE ALSO | ||
|
|
||
| * xref:ecctl[ecctl] - Elastic Cloud Control | ||
| * xref:ecctl_project_list[ecctl project list] - Lists serverless projects |
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.
Does it make sense to support multiple
typeflags?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.
Yes, but I can also add that as a follow up? didn't want to go deep with this PR since i'm totally vibing it hehe.