Skip to content

Commit

Permalink
delete design code (#245)
Browse files Browse the repository at this point in the history
Co-authored-by: rakhanda <rakhanda@rakhandas-MBP.attlocal.net>
  • Loading branch information
RaviKhandavilli and rakhanda committed Oct 11, 2022
1 parent c8ae620 commit 900b9ea
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 30 deletions.
1 change: 1 addition & 0 deletions cmd/controller/app/database/db_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type DesignService interface {

CreateDesignCode(userId string, designId string, fileName string, fileVer string, fileData *os.File) error
GetDesignCode(userId string, designId string, version string) ([]byte, error)
DeleteDesignCode(userId string, designId string, version string) error
}

// JobService is an interface that defines a collection of APIs related to job
Expand Down
19 changes: 19 additions & 0 deletions cmd/controller/app/database/mongodb/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,22 @@ func (db *MongoService) GetDesignCode(userId string, designId string, fileVer st

return util.ZipFile(updatedDoc.DataSet)
}
func (db *MongoService) DeleteDesignCode(userId string, designId string, version string) error {
zap.S().Debugf("delete design code 1: %v, %v,%v", userId, designId, version)

if version == latestVersion {
version = db.GetLatestDesignCodeVersion(userId, designId)
}
updateRes, err := db.designCollection.UpdateOne(context.TODO(),
bson.M{util.DBFieldUserId: userId, util.DBFieldId: designId},
bson.M{"$pull": bson.M{util.DBFieldCodes: bson.M{"version": version}}})
if err != nil {
return fmt.Errorf("failed to delete design code deleted code error: %v", err)
}
if updateRes.ModifiedCount == 0 {
return fmt.Errorf("failed to delete design code, code version %s not found. deleted code count: %#v",
version, updateRes)
}
zap.S().Debugf("successfully deleted design code: %#v", updateRes)
return nil
}
33 changes: 33 additions & 0 deletions cmd/controller/app/database/mongodb/code_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package mongodb

import (
"testing"

"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
)

func TestMongoService_DeleteDesignCode(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("success", func(mt *mtest.T) {
db := &MongoService{
designCollection: mt.Coll,
}
mt.AddMockResponses(bson.D{{"ok", 1}, {"acknowledged", true},
{"n", 1}, {"nModified", 1}})
err := db.DeleteDesignCode("userid", "designid", "version")
assert.Nil(t, err)
})

mt.Run("no document deleted", func(mt *mtest.T) {
db := &MongoService{
designCollection: mt.Coll,
}
mt.AddMockResponses(bson.D{{"ok", 1}, {"acknowledged", true},
{"n", 0}, {"nModified", 0}})
err := db.DeleteDesignCode("userid", "designid", "version")
assert.NotNil(t, err)
})
}
2 changes: 1 addition & 1 deletion cmd/controller/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var (

instance, err := app.NewController(cfg, bInsecure, bPlain)
if err != nil {
fmt.Printf("Failed to create a new controller instance\n")
fmt.Printf("Failed to create a new controller instance: %v\n", err)
return nil
}
instance.Start()
Expand Down
55 changes: 55 additions & 0 deletions cmd/flamectl/cmd/delete_code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2022 Cisco Systems, Inc. and its affiliates
//
// 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.
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"github.com/spf13/cobra"

"github.com/cisco-open/flame/cmd/flamectl/resources/code"
)

var removeCodeCmd = &cobra.Command{
Use: "code <version> --design <design id>",
Short: "Remove a code with specific version from a design",
Long: "This command removes a code with specific version from a design",
Args: cobra.RangeArgs(1, 1),
RunE: func(cmd *cobra.Command, args []string) error {
checkInsecure(cmd)

codeVer := args[0]

flags := cmd.Flags()

designId, err := flags.GetString("design")
if err != nil {
return err
}

params := code.Params{}
params.Endpoint = config.ApiServer.Endpoint
params.User = config.User
params.DesignId = designId
params.CodeVer = codeVer

return code.Remove(params)
},
}

func init() {
removeCodeCmd.PersistentFlags().StringP("design", "d", "", "Design ID")
removeCodeCmd.MarkPersistentFlagRequired("design")
removeCmd.AddCommand(removeCodeCmd)
}
20 changes: 20 additions & 0 deletions cmd/flamectl/resources/code/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,23 @@ func mustOpen(f string) *os.File {

return r
}
func Remove(params Params) error {
// construct URL
uriMap := map[string]string{
"user": params.User,
"designId": params.DesignId,
"version": params.CodeVer,
}
url := restapi.CreateURL(params.Endpoint, restapi.DeleteDesignCodeEndPoint, uriMap)

statusCode, respBody, err := restapi.HTTPDelete(url, nil, "")
if err != nil || restapi.CheckStatusCode(statusCode) != nil {
fmt.Printf("Failed to delete %v code of version %s - statusCode: %d, error: %v, responsebody: %v\n",
params.CodePath, params.CodeVer, statusCode, err, string(respBody))
return nil
}

fmt.Printf("Deleted %s successfully\n", respBody)

return nil
}
30 changes: 17 additions & 13 deletions pkg/openapi/apiserver/api_design_codes_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,27 @@ func (s *DesignCodesApiService) CreateDesignCode(ctx context.Context, user strin
// DeleteDesignCode - Delete a zipped design code file owned by user
func (s *DesignCodesApiService) DeleteDesignCode(ctx context.Context, user string, designId string,
version string) (openapi.ImplResponse, error) {
// TODO - update DeleteDesignCode with the required logic for this service method.
// Add api_design_codes_service.go to the .openapi-generator-ignore
// to avoid overwriting this service implementation when updating open api generation.

//TODO: Uncomment the next line to return response Response(200, {}) or use other options such as http.Ok ...
//return openapi.Response(200, nil),nil
//create controller request
uriMap := map[string]string{
"user": user,
"designId": designId,
"version": version,
}
url := restapi.CreateURL(HostEndpoint, restapi.DeleteDesignCodeEndPoint, uriMap)

//TODO: Uncomment the next line to return response Response(404, {}) or use other options such as http.Ok ...
//return openapi.Response(404, nil),nil
// send Delete request
code, respBody, err := restapi.HTTPDelete(url, "", "application/json")

//TODO: Uncomment the next line to return response Response(401, {}) or use other options such as http.Ok ...
//return openapi.Response(401, nil),nil
// response to the user
if err != nil {
return openapi.Response(http.StatusInternalServerError, nil), fmt.Errorf("delete design code request failed:%v", err)
}

//TODO: Uncomment the next line to return response Response(0, Error{}) or use other options such as http.Ok ...
//return openapi.Response(0, Error{}), nil
if err = restapi.CheckStatusCode(code); err != nil {
return openapi.Response(code, nil), err
}

return openapi.Response(http.StatusNotImplemented, nil), errors.New("DeleteDesignCode method not implemented")
return openapi.Response(http.StatusOK, respBody), nil
}

// GetDesignCode - Get a zipped design code file owned by user
Expand Down
23 changes: 7 additions & 16 deletions pkg/openapi/controller/api_design_codes_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,14 @@ func (s *DesignCodesApiService) CreateDesignCode(ctx context.Context, user strin
// DeleteDesignCode - Delete a zipped design code file owned by user
func (s *DesignCodesApiService) DeleteDesignCode(ctx context.Context, user string, designId string,
version string) (openapi.ImplResponse, error) {
// TODO - update DeleteDesignCode with the required logic for this service method.
// Add api_design_codes_service.go to the .openapi-generator-ignore
// to avoid overwriting this service implementation when updating open api generation.
zap.S().Debugf("Received DeleteDesignCode Delete request: %s | %s | %s", user, designId, version)

//TODO: Uncomment the next line to return response Response(200, {}) or use other options such as http.Ok ...
//return openapi.Response(200, nil),nil

//TODO: Uncomment the next line to return response Response(404, {}) or use other options such as http.Ok ...
//return openapi.Response(404, nil),nil

//TODO: Uncomment the next line to return response Response(401, {}) or use other options such as http.Ok ...
//return openapi.Response(401, nil),nil

//TODO: Uncomment the next line to return response Response(0, Error{}) or use other options such as http.Ok ...
//return openapi.Response(0, Error{}), nil

return openapi.Response(http.StatusNotImplemented, nil), errors.New("DeleteDesignCode method not implemented")
err := s.dbService.DeleteDesignCode(user, designId, version)
if err != nil {
return openapi.Response(http.StatusInternalServerError, fmt.Errorf("failed to delete design code: %v", err)),
fmt.Errorf("failed to delete design code: %v", err)
}
return openapi.Response(http.StatusOK, nil), nil
}

// GetDesignCode - Get a zipped design code file owned by user
Expand Down
2 changes: 2 additions & 0 deletions pkg/restapi/restapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const (
CreateDesignCodeEndPoint = "CREATE_DESIGN_CODE"
GetDesignCodeEndPoint = "GET_DESIGN_CODE"
UpdateDesignCodeEndPoint = "UPDATE_DESIGN_CODE"
DeleteDesignCodeEndPoint = "DELETE_DESIGN_CODE"

// Keys for job endpoints
CreateJobEndpoint = "CREATE_JOB"
Expand Down Expand Up @@ -105,6 +106,7 @@ var URI = map[string]string{
CreateDesignCodeEndPoint: "/users/{{.user}}/designs/{{.designId}}/codes",
GetDesignCodeEndPoint: "/users/{{.user}}/designs/{{.designId}}/codes/{{.version}}",
UpdateDesignCodeEndPoint: "/users/{{.user}}/designs/{{.designId}}/codes/{{.version}}",
DeleteDesignCodeEndPoint: "/users/{{.user}}/designs/{{.designId}}/codes/{{.version}}",

// Job
CreateJobEndpoint: "/users/{{.user}}/jobs",
Expand Down
1 change: 1 addition & 0 deletions pkg/util/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
DBFieldUserId = "userid"
DBFieldId = "id"
DBFieldDesignId = "designid"
DBFieldCodes = "codes"
DBFieldSchemaId = "schemaid"
DBFieldJobId = "jobid"
DBFieldTaskId = "taskid"
Expand Down

0 comments on commit 900b9ea

Please sign in to comment.