Skip to content

Commit

Permalink
Add a web RPC to update the description of application (#1842)
Browse files Browse the repository at this point in the history
**What this PR does / why we need it**:

**Which issue(s) this PR fixes**:

Fixes #

**Does this PR introduce a user-facing change?**:
<!--
If no, just write "NONE" in the release-note block below.
-->
```release-note
NONE
```

This PR was merged by Kapetanios.
  • Loading branch information
nghialv authored Apr 9, 2021
1 parent 676cb08 commit 3cbe27e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
59 changes: 41 additions & 18 deletions pkg/app/api/grpcapi/web_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,36 +430,59 @@ func (a *WebAPI) AddApplication(ctx context.Context, req *webservice.AddApplicat
}

func (a *WebAPI) UpdateApplication(ctx context.Context, req *webservice.UpdateApplicationRequest) (*webservice.UpdateApplicationResponse, error) {
claims, err := rpcauth.ExtractClaims(ctx)
if err != nil {
a.logger.Error("failed to authenticate the current user", zap.Error(err))
updater := func(app *model.Application) error {
app.Name = req.Name
app.EnvId = req.EnvId
app.PipedId = req.PipedId
app.Kind = req.Kind
app.CloudProvider = req.CloudProvider
return nil
}

if err := a.updateApplication(ctx, req.ApplicationId, req.PipedId, updater); err != nil {
return nil, err
}
return &webservice.UpdateApplicationResponse{}, nil
}

piped, err := getPiped(ctx, a.pipedStore, req.PipedId, a.logger)
if err != nil {
func (a *WebAPI) UpdateApplicationDescription(ctx context.Context, req *webservice.UpdateApplicationDescriptionRequest) (*webservice.UpdateApplicationDescriptionResponse, error) {
updater := func(app *model.Application) error {
app.Description = req.Description
return nil
}

if err := a.updateApplication(ctx, req.ApplicationId, "", updater); err != nil {
return nil, err
}
return &webservice.UpdateApplicationDescriptionResponse{}, nil
}

if piped.ProjectId != claims.Role.ProjectId {
return nil, status.Error(codes.InvalidArgument, "Requested piped does not belong to your project")
func (a *WebAPI) updateApplication(ctx context.Context, id, pipedID string, updater func(app *model.Application) error) error {
claims, err := rpcauth.ExtractClaims(ctx)
if err != nil {
a.logger.Error("failed to authenticate the current user", zap.Error(err))
return err
}

err = a.applicationStore.UpdateApplication(ctx, req.ApplicationId, func(app *model.Application) error {
app.Name = req.Name
app.EnvId = req.EnvId
app.PipedId = req.PipedId
app.Kind = req.Kind
app.CloudProvider = req.CloudProvider
app.Description = req.Description
return nil
})
// Ensure that the specified piped is assignable for this application.
if pipedID != "" {
piped, err := getPiped(ctx, a.pipedStore, pipedID, a.logger)
if err != nil {
return err
}

if piped.ProjectId != claims.Role.ProjectId {
return status.Error(codes.InvalidArgument, "Requested piped does not belong to your project")
}
}

err = a.applicationStore.UpdateApplication(ctx, id, updater)
if err != nil {
a.logger.Error("failed to update application", zap.Error(err))
return nil, status.Error(codes.Internal, "Failed to update application")
return status.Error(codes.Internal, "Failed to update application")
}

return &webservice.UpdateApplicationResponse{}, nil
return nil
}

func (a *WebAPI) EnableApplication(ctx context.Context, req *webservice.EnableApplicationRequest) (*webservice.EnableApplicationResponse, error) {
Expand Down
10 changes: 9 additions & 1 deletion pkg/app/api/service/webservice/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ service WebService {
// Application
rpc AddApplication(AddApplicationRequest) returns (AddApplicationResponse) {}
rpc UpdateApplication(UpdateApplicationRequest) returns (UpdateApplicationResponse) {}
rpc UpdateApplicationDescription(UpdateApplicationDescriptionRequest) returns (UpdateApplicationDescriptionResponse) {}
rpc EnableApplication(EnableApplicationRequest) returns (EnableApplicationResponse) {}
rpc DisableApplication(DisableApplicationRequest) returns (DisableApplicationResponse) {}
rpc DeleteApplication(DeleteApplicationRequest) returns (DeleteApplicationResponse) {}
Expand Down Expand Up @@ -201,12 +202,19 @@ message UpdateApplicationRequest {
string piped_id = 4 [(validate.rules).string.min_len = 1];
model.ApplicationKind kind = 6 [(validate.rules).enum.defined_only = true];
string cloud_provider = 7 [(validate.rules).string.min_len = 1];
string description = 8;
}

message UpdateApplicationResponse {
}

message UpdateApplicationDescriptionRequest {
string application_id = 1 [(validate.rules).string.min_len = 1];
string description = 2;
}

message UpdateApplicationDescriptionResponse {
}

message EnableApplicationRequest {
string application_id = 1 [(validate.rules).string.min_len = 1];
}
Expand Down
1 change: 0 additions & 1 deletion pkg/app/web/src/modules/update-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export const updateApplication = createAsyncThunk<
pipedId: values.pipedId,
cloudProvider: values.cloudProvider,
kind: values.kind,
description: "",
});
});

Expand Down

0 comments on commit 3cbe27e

Please sign in to comment.