Delete data endpoints (#364)

This commit is contained in:
Dominic Wong
2022-02-07 16:45:27 +00:00
committed by GitHub
parent 7a826ed615
commit 9e252e9a30
6 changed files with 133 additions and 22 deletions

View File

@@ -19,6 +19,8 @@ import (
"github.com/micro/micro/v3/service/runtime/source/git"
"github.com/micro/micro/v3/service/store"
function "github.com/micro/services/function/proto"
pauth "github.com/micro/services/pkg/auth"
adminpb "github.com/micro/services/pkg/service/proto"
"github.com/micro/services/pkg/tenant"
"github.com/teris-io/shortid"
)
@@ -626,24 +628,26 @@ func (e *GoogleFunction) Delete(ctx context.Context, req *function.DeleteRequest
}
// async delete
go func() {
cmd := exec.Command("gcloud", "functions", "delete", "--quiet", "--project", e.project, "--region", fn.Region, fn.Id)
outp, err := cmd.CombinedOutput()
if err != nil && !strings.Contains(string(outp), "does not exist") {
log.Error(fmt.Errorf(string(outp)))
return
}
// delete the owner key
store.Delete(key)
// delete the global key
store.Delete(FunctionKey + fn.Id)
}()
go e.deleteFunction(fn, key)
return nil
}
func (e *GoogleFunction) deleteFunction(fn *function.Func, key string) {
cmd := exec.Command("gcloud", "functions", "delete", "--quiet", "--project", e.project, "--region", fn.Region, fn.Id)
outp, err := cmd.CombinedOutput()
if err != nil && !strings.Contains(string(outp), "does not exist") {
log.Error(fmt.Errorf(string(outp)))
return
}
// delete the owner key
store.Delete(key)
// delete the global key
store.Delete(FunctionKey + fn.Id)
}
func (e *GoogleFunction) List(ctx context.Context, req *function.ListRequest, rsp *function.ListResponse) error {
log.Info("Received Function.List request")
@@ -797,3 +801,32 @@ func (e *GoogleFunction) Regions(ctx context.Context, req *function.RegionsReque
rsp.Regions = GoogleRegions
return nil
}
func (e *GoogleFunction) DeleteData(ctx context.Context, request *adminpb.DeleteDataRequest, response *adminpb.DeleteDataResponse) error {
method := "admin.DeleteData"
_, err := pauth.VerifyMicroAdmin(ctx, method)
if err != nil {
return err
}
if len(request.TenantId) < 10 { // deliberate length check so we don't delete all the things
return errors.BadRequest(method, "Missing tenant ID")
}
prefix := OwnerKey + request.TenantId + "/"
recs, err := store.Read(prefix, store.ReadPrefix())
if err != nil {
return err
}
for _, rec := range recs {
var fn function.Func
if err := rec.Decode(&fn); err != nil {
return err
}
e.deleteFunction(&fn, rec.Key)
}
log.Infof("Deleted %d functions for %s", len(recs), request.TenantId)
return nil
}

View File

@@ -5,6 +5,7 @@ import (
"github.com/micro/micro/v3/service/logger"
"github.com/micro/services/function/handler"
pb "github.com/micro/services/function/proto"
admin "github.com/micro/services/pkg/service/proto"
)
func main() {
@@ -14,8 +15,10 @@ func main() {
service.Version("latest"),
)
h := handler.NewFunction()
// Register handler
pb.RegisterFunctionHandler(srv.Server(), handler.NewFunction())
pb.RegisterFunctionHandler(srv.Server(), h)
admin.RegisterAdminHandler(srv.Server(), h)
// Run service
if err := srv.Run(); err != nil {