Skip to content
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
2 changes: 1 addition & 1 deletion pkg/app/api/grpcapi/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ go_library(
"//pkg/config:go_default_library",
"//pkg/crypto:go_default_library",
"//pkg/datastore:go_default_library",
"//pkg/insightstore:go_default_library",
"//pkg/git:go_default_library",
"//pkg/insightstore:go_default_library",
"//pkg/model:go_default_library",
"//pkg/rpc/rpcauth:go_default_library",
"@com_github_google_uuid//:go_default_library",
Expand Down
56 changes: 56 additions & 0 deletions pkg/app/api/grpcapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,62 @@ func (a *API) GetApplication(ctx context.Context, req *apiservice.GetApplication
}, nil
}

// ListApplications returns the application list of the project where the caller belongs to.
// Currently, the maximum number of returned applications is 10.
func (a *API) ListApplications(ctx context.Context, req *apiservice.ListApplicationsRequest) (*apiservice.ListApplicationsResponse, error) {
key, err := requireAPIKey(ctx, model.APIKey_READ_ONLY, a.logger)
if err != nil {
return nil, err
}

const limit = 10
filters := []datastore.ListFilter{
{
Field: "ProjectId",
Operator: "==",
Value: key.ProjectId,
},
}
if req.Name != "" {
filters = append(filters, datastore.ListFilter{
Field: "Name",
Operator: "==",
Value: req.Name,
})
}
if req.Kind != "" {
kind, ok := model.ApplicationKind_value[req.Kind]
if !ok {
return nil, status.Error(codes.InvalidArgument, "Invalid application kind")
}
filters = append(filters, datastore.ListFilter{
Field: "Kind",
Operator: "==",
Value: model.ApplicationKind(kind),
})
}
if req.EnvId != "" {
filters = append(filters, datastore.ListFilter{
Field: "EnvId",
Operator: "==",
Value: req.EnvId,
})
}
opts := datastore.ListOptions{
Filters: filters,
PageSize: limit,
}

apps, err := listApplications(ctx, a.applicationStore, opts, a.logger)
if err != nil {
return nil, err
}

return &apiservice.ListApplicationsResponse{
Applications: apps,
}, nil
}

func (a *API) GetDeployment(ctx context.Context, req *apiservice.GetDeploymentRequest) (*apiservice.GetDeploymentResponse, error) {
key, err := requireAPIKey(ctx, model.APIKey_READ_ONLY, a.logger)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions pkg/app/api/grpcapi/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ func getApplication(ctx context.Context, store datastore.ApplicationStore, id st
return app, nil
}

func listApplications(ctx context.Context, store datastore.ApplicationStore, opts datastore.ListOptions, logger *zap.Logger) ([]*model.Application, error) {
apps, err := store.ListApplications(ctx, opts)
if err != nil {
logger.Error("failed to list applications", zap.Error(err))
return nil, status.Error(codes.Internal, "Failed to list applications")
}

return apps, nil
}

func getDeployment(ctx context.Context, store datastore.DeploymentStore, id string, logger *zap.Logger) (*model.Deployment, error) {
deployment, err := store.GetDeployment(ctx, id)
if errors.Is(err, datastore.ErrNotFound) {
Expand Down
11 changes: 11 additions & 0 deletions pkg/app/api/service/apiservice/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ service APIService {
rpc AddApplication(AddApplicationRequest) returns (AddApplicationResponse) {}
rpc SyncApplication(SyncApplicationRequest) returns (SyncApplicationResponse) {}
rpc GetApplication(GetApplicationRequest) returns (GetApplicationResponse) {}
rpc ListApplications(ListApplicationsRequest) returns (ListApplicationsResponse) {}

rpc GetDeployment(GetDeploymentRequest) returns (GetDeploymentResponse) {}

Expand Down Expand Up @@ -64,6 +65,16 @@ message GetApplicationResponse {
pipe.model.Application application = 1;
}

message ListApplicationsRequest {
string name = 1;
string kind = 2;
string env_id = 3;
}

message ListApplicationsResponse {
repeated pipe.model.Application applications = 1;
}

message GetDeploymentRequest {
string deployment_id = 1;
}
Expand Down
1 change: 1 addition & 0 deletions pkg/app/pipectl/cmd/application/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ go_library(
"add.go",
"application.go",
"get.go",
"list.go",
"sync.go",
],
importpath = "github.com/pipe-cd/pipe/pkg/app/pipectl/cmd/application",
Expand Down
9 changes: 6 additions & 3 deletions pkg/app/pipectl/cmd/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ func NewCommand() *cobra.Command {
Short: "Manage application resources.",
}

cmd.AddCommand(newAddCommand(c))
cmd.AddCommand(newSyncCommand(c))
cmd.AddCommand(newGetCommand(c))
cmd.AddCommand(
newAddCommand(c),
newSyncCommand(c),
newGetCommand(c),
newListCommand(c),
)

c.clientOptions.RegisterPersistentFlags(cmd)

Expand Down
90 changes: 90 additions & 0 deletions pkg/app/pipectl/cmd/application/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2020 The PipeCD Authors.
//
// 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 application

import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"strings"

"github.com/spf13/cobra"

"github.com/pipe-cd/pipe/pkg/app/api/service/apiservice"
"github.com/pipe-cd/pipe/pkg/cli"
"github.com/pipe-cd/pipe/pkg/model"
)

type list struct {
root *command

appName string
envID string
appKind string
stdout io.Writer
}

func newListCommand(root *command) *cobra.Command {
c := &list{
root: root,
stdout: os.Stdout,
}
cmd := &cobra.Command{
Use: "list",
Short: "Show the list of applications. Currently, the maximum number of returned applications is 10.",
RunE: cli.WithContext(c.run),
}

cmd.Flags().StringVar(&c.appName, "app-name", c.appName, "The application name.")
cmd.Flags().StringVar(&c.envID, "env-id", c.envID, "The environment ID.")
cmd.Flags().StringVar(&c.appKind, "app-kind", c.appKind, fmt.Sprintf("The kind of application. (%s)", strings.Join(model.ApplicationKindStrings(), "|")))

return cmd
}

func (c *list) run(ctx context.Context, _ cli.Telemetry) error {
if c.appKind != "" {
if _, ok := model.ApplicationKind_value[c.appKind]; !ok {
return fmt.Errorf("invalid applicaiton kind")
}
}

cli, err := c.root.clientOptions.NewClient(ctx)
if err != nil {
return fmt.Errorf("failed to initialize client: %w", err)
}
defer cli.Close()

req := &apiservice.ListApplicationsRequest{
Name: c.appName,
EnvId: c.envID,
Kind: c.appKind,
}

resp, err := cli.ListApplications(ctx, req)
if err != nil {
return fmt.Errorf("failed to list application: %w", err)
}

bytes, err := json.Marshal(resp.Applications)
if err != nil {
return fmt.Errorf("failed to marshal applications: %w", err)
}

fmt.Fprintln(c.stdout, string(bytes))
return nil
}
1 change: 1 addition & 0 deletions pkg/model/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ go_library(
"application_live_state.go",
"cloudprovider.go",
"command.go",
"common.go",
"datastore.go",
"deployment.go",
"docs.go",
Expand Down
24 changes: 24 additions & 0 deletions pkg/model/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2020 The PipeCD Authors.
//
// 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 model

// ApplicationKindStrings returns a list of available deployment kinds in string.
func ApplicationKindStrings() []string {
out := make([]string, 0, len(ApplicationKind_value))
for k := range ApplicationKind_value {
out = append(out, k)
}
return out
}