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
2 changes: 2 additions & 0 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
cmdcomment "github.com/elastic/ecctl/cmd/comment"
cmddeployment "github.com/elastic/ecctl/cmd/deployment"
cmdplatform "github.com/elastic/ecctl/cmd/platform"
cmdproject "github.com/elastic/ecctl/cmd/project"
cmdstack "github.com/elastic/ecctl/cmd/stack"
cmduser "github.com/elastic/ecctl/cmd/user"
)
Expand All @@ -32,6 +33,7 @@ func init() {
cmdcomment.Command,
cmddeployment.Command,
cmdplatform.Command,
cmdproject.Command,
cmduser.Command,
cmdstack.Command,
)
Expand Down
32 changes: 32 additions & 0 deletions cmd/project/command.go
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()
},
}
54 changes: 54 additions & 0 deletions cmd/project/list.go
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)")
Copy link
Member

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 type flags?

project list --type elasticsearch --type security

Copy link
Contributor Author

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.

}
209 changes: 209 additions & 0 deletions cmd/project/list_test.go
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()
})
}
}
45 changes: 45 additions & 0 deletions docs/ecctl_project.adoc
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
Loading
Loading