Skip to content
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

Add a pipectl command to disable an application #4119

Merged
merged 1 commit into from
Jan 11, 2023
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
1 change: 1 addition & 0 deletions pkg/app/pipectl/cmd/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func NewCommand() *cobra.Command {
newGetCommand(c),
newListCommand(c),
newDeleteCommand(c),
newDisableCommand(c),
)

c.clientOptions.RegisterPersistentFlags(cmd)
Expand Down
67 changes: 67 additions & 0 deletions pkg/app/pipectl/cmd/application/disable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2022 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"
"fmt"

"github.com/spf13/cobra"

"github.com/pipe-cd/pipecd/pkg/app/server/service/apiservice"
"github.com/pipe-cd/pipecd/pkg/cli"
)

type disable struct {
root *command

appID string
}

func newDisableCommand(root *command) *cobra.Command {
c := &disable{
root: root,
}
cmd := &cobra.Command{
Use: "disable",
Short: "Disable an application.",
RunE: cli.WithContext(c.run),
}

cmd.Flags().StringVar(&c.appID, "app-id", c.appID, "The application ID.")
cmd.MarkFlagRequired("app-id")

return cmd
}

func (c *disable) run(ctx context.Context, input cli.Input) error {
cli, err := c.root.clientOptions.NewClient(ctx)
if err != nil {
return fmt.Errorf("failed to initialize client: %w", err)
}
defer cli.Close()

req := &apiservice.DisableApplicationRequest{
ApplicationId: c.appID,
}

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

input.Logger.Info(fmt.Sprintf("Successfully disable application id = %s", resp.ApplicationId))
return nil
}
25 changes: 25 additions & 0 deletions pkg/app/server/grpcapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type apiApplicationStore interface {
Get(ctx context.Context, id string) (*model.Application, error)
List(ctx context.Context, opts datastore.ListOptions) ([]*model.Application, string, error)
Delete(ctx context.Context, id string) error
Disable(ctx context.Context, id string) error
UpdateConfigFilename(ctx context.Context, id, filename string) error
}

Expand Down Expand Up @@ -308,6 +309,30 @@ func (a *API) DeleteApplication(ctx context.Context, req *apiservice.DeleteAppli
}, nil
}

func (a *API) DisableApplication(ctx context.Context, req *apiservice.DisableApplicationRequest) (*apiservice.DisableApplicationResponse, error) {
key, err := requireAPIKey(ctx, model.APIKey_READ_WRITE, a.logger)
if err != nil {
return nil, err
}

app, err := getApplication(ctx, a.applicationStore, req.ApplicationId, a.logger)
if err != nil {
return nil, err
}

if app.ProjectId != key.ProjectId {
return nil, status.Error(codes.InvalidArgument, "Requested application does not belong to your project")
}

if err := a.applicationStore.Disable(ctx, req.ApplicationId); err != nil {
return nil, gRPCStoreError(err, fmt.Sprintf("disable application %s", req.ApplicationId))
}

return &apiservice.DisableApplicationResponse{
ApplicationId: app.Id,
}, nil
}

func (a *API) RenameApplicationConfigFile(ctx context.Context, req *apiservice.RenameApplicationConfigFileRequest) (*apiservice.RenameApplicationConfigFileResponse, error) {
key, err := requireAPIKey(ctx, model.APIKey_READ_WRITE, a.logger)
if err != nil {
Expand Down
Loading