mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-11 19:04:35 +00:00
Delete data endpoints (#364)
This commit is contained in:
@@ -19,6 +19,8 @@ import (
|
|||||||
"github.com/micro/micro/v3/service/store"
|
"github.com/micro/micro/v3/service/store"
|
||||||
"github.com/micro/services/app/domain"
|
"github.com/micro/services/app/domain"
|
||||||
pb "github.com/micro/services/app/proto"
|
pb "github.com/micro/services/app/proto"
|
||||||
|
pauth "github.com/micro/services/pkg/auth"
|
||||||
|
adminpb "github.com/micro/services/pkg/service/proto"
|
||||||
"github.com/micro/services/pkg/tenant"
|
"github.com/micro/services/pkg/tenant"
|
||||||
"github.com/teris-io/shortid"
|
"github.com/teris-io/shortid"
|
||||||
)
|
)
|
||||||
@@ -555,7 +557,6 @@ func (e *GoogleApp) Delete(ctx context.Context, req *pb.DeleteRequest, rsp *pb.D
|
|||||||
if !ok {
|
if !ok {
|
||||||
id = "micro"
|
id = "micro"
|
||||||
}
|
}
|
||||||
|
|
||||||
// read the app for the owner
|
// read the app for the owner
|
||||||
key := OwnerKey + id + "/" + req.Name
|
key := OwnerKey + id + "/" + req.Name
|
||||||
recs, err := store.Read(key)
|
recs, err := store.Read(key)
|
||||||
@@ -575,11 +576,16 @@ func (e *GoogleApp) Delete(ctx context.Context, req *pb.DeleteRequest, rsp *pb.D
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return e.deleteApp(ctx, id, srv)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *GoogleApp) deleteApp(ctx context.Context, tenantID string, srv *pb.Service) error {
|
||||||
|
|
||||||
// check the status
|
// check the status
|
||||||
switch srv.Status {
|
switch srv.Status {
|
||||||
case domain.StatusUpdating, domain.StatusDeploying, domain.StatusDeleting:
|
case domain.StatusUpdating, domain.StatusDeploying, domain.StatusDeleting:
|
||||||
log.Errorf("Won't delete: % is %s", req.Name, srv.Status)
|
log.Errorf("Won't delete: % is %s", srv.Name, srv.Status)
|
||||||
return errors.BadRequest("app.delete", "% status: %s", req.Name, srv.Status)
|
return errors.BadRequest("app.delete", "% status: %s", srv.Name, srv.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete from the db
|
// delete from the db
|
||||||
@@ -587,7 +593,7 @@ func (e *GoogleApp) Delete(ctx context.Context, req *pb.DeleteRequest, rsp *pb.D
|
|||||||
// service key
|
// service key
|
||||||
ServiceKey + srv.Id,
|
ServiceKey + srv.Id,
|
||||||
// owner key
|
// owner key
|
||||||
OwnerKey + id + "/" + req.Name,
|
OwnerKey + tenantID + "/" + srv.Name,
|
||||||
}
|
}
|
||||||
|
|
||||||
// set the delete status
|
// set the delete status
|
||||||
@@ -623,6 +629,35 @@ func (e *GoogleApp) Delete(ctx context.Context, req *pb.DeleteRequest, rsp *pb.D
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *GoogleApp) 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 srv pb.Service
|
||||||
|
if err := rec.Decode(&srv); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
e.deleteApp(ctx, request.TenantId, &srv)
|
||||||
|
}
|
||||||
|
log.Infof("Deleted %d functions for %s", len(recs), request.TenantId)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (e *GoogleApp) List(ctx context.Context, req *pb.ListRequest, rsp *pb.ListResponse) error {
|
func (e *GoogleApp) List(ctx context.Context, req *pb.ListRequest, rsp *pb.ListResponse) error {
|
||||||
log.Info("Received App.List request")
|
log.Info("Received App.List request")
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"github.com/micro/micro/v3/service/logger"
|
"github.com/micro/micro/v3/service/logger"
|
||||||
"github.com/micro/services/app/handler"
|
"github.com/micro/services/app/handler"
|
||||||
pb "github.com/micro/services/app/proto"
|
pb "github.com/micro/services/app/proto"
|
||||||
|
admin "github.com/micro/services/pkg/service/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -14,8 +15,10 @@ func main() {
|
|||||||
service.Version("latest"),
|
service.Version("latest"),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
h := handler.New()
|
||||||
// Register handler
|
// Register handler
|
||||||
pb.RegisterAppHandler(srv.Server(), handler.New())
|
pb.RegisterAppHandler(srv.Server(), h)
|
||||||
|
admin.RegisterAdminHandler(srv.Server(), h)
|
||||||
|
|
||||||
// Run service
|
// Run service
|
||||||
if err := srv.Run(); err != nil {
|
if err := srv.Run(); err != nil {
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ import (
|
|||||||
"github.com/micro/micro/v3/service/runtime/source/git"
|
"github.com/micro/micro/v3/service/runtime/source/git"
|
||||||
"github.com/micro/micro/v3/service/store"
|
"github.com/micro/micro/v3/service/store"
|
||||||
function "github.com/micro/services/function/proto"
|
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/micro/services/pkg/tenant"
|
||||||
"github.com/teris-io/shortid"
|
"github.com/teris-io/shortid"
|
||||||
)
|
)
|
||||||
@@ -626,24 +628,26 @@ func (e *GoogleFunction) Delete(ctx context.Context, req *function.DeleteRequest
|
|||||||
}
|
}
|
||||||
|
|
||||||
// async delete
|
// async delete
|
||||||
go func() {
|
go e.deleteFunction(fn, key)
|
||||||
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)
|
|
||||||
}()
|
|
||||||
|
|
||||||
return nil
|
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 {
|
func (e *GoogleFunction) List(ctx context.Context, req *function.ListRequest, rsp *function.ListResponse) error {
|
||||||
log.Info("Received Function.List request")
|
log.Info("Received Function.List request")
|
||||||
|
|
||||||
@@ -797,3 +801,32 @@ func (e *GoogleFunction) Regions(ctx context.Context, req *function.RegionsReque
|
|||||||
rsp.Regions = GoogleRegions
|
rsp.Regions = GoogleRegions
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"github.com/micro/micro/v3/service/logger"
|
"github.com/micro/micro/v3/service/logger"
|
||||||
"github.com/micro/services/function/handler"
|
"github.com/micro/services/function/handler"
|
||||||
pb "github.com/micro/services/function/proto"
|
pb "github.com/micro/services/function/proto"
|
||||||
|
admin "github.com/micro/services/pkg/service/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -14,8 +15,10 @@ func main() {
|
|||||||
service.Version("latest"),
|
service.Version("latest"),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
h := handler.NewFunction()
|
||||||
// Register handler
|
// Register handler
|
||||||
pb.RegisterFunctionHandler(srv.Server(), handler.NewFunction())
|
pb.RegisterFunctionHandler(srv.Server(), h)
|
||||||
|
admin.RegisterAdminHandler(srv.Server(), h)
|
||||||
|
|
||||||
// Run service
|
// Run service
|
||||||
if err := srv.Run(); err != nil {
|
if err := srv.Run(); err != nil {
|
||||||
|
|||||||
@@ -7,7 +7,10 @@ import (
|
|||||||
|
|
||||||
"github.com/micro/micro/v3/service/config"
|
"github.com/micro/micro/v3/service/config"
|
||||||
"github.com/micro/micro/v3/service/errors"
|
"github.com/micro/micro/v3/service/errors"
|
||||||
|
"github.com/micro/micro/v3/service/logger"
|
||||||
"github.com/micro/micro/v3/service/store"
|
"github.com/micro/micro/v3/service/store"
|
||||||
|
pauth "github.com/micro/services/pkg/auth"
|
||||||
|
adminpb "github.com/micro/services/pkg/service/proto"
|
||||||
"github.com/micro/services/pkg/tenant"
|
"github.com/micro/services/pkg/tenant"
|
||||||
url "github.com/micro/services/url/proto"
|
url "github.com/micro/services/url/proto"
|
||||||
cache "github.com/patrickmn/go-cache"
|
cache "github.com/patrickmn/go-cache"
|
||||||
@@ -135,3 +138,35 @@ func (e *Url) Proxy(ctx context.Context, req *url.ProxyRequest, rsp *url.ProxyRe
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *Url) 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 := "urlOwner/" + request.TenantId + "/"
|
||||||
|
|
||||||
|
keys, err := store.List(store.ListPrefix(prefix))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, key := range keys {
|
||||||
|
id := strings.TrimPrefix(key, prefix)
|
||||||
|
if err := store.Delete("url/" + id); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := store.Delete(key); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logger.Infof("Deleted %d objects from S3 for %s", len(keys), request.TenantId)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
admin "github.com/micro/services/pkg/service/proto"
|
||||||
"github.com/micro/services/pkg/tracing"
|
"github.com/micro/services/pkg/tracing"
|
||||||
"github.com/micro/services/url/handler"
|
"github.com/micro/services/url/handler"
|
||||||
pb "github.com/micro/services/url/proto"
|
pb "github.com/micro/services/url/proto"
|
||||||
@@ -15,9 +16,10 @@ func main() {
|
|||||||
service.Name("url"),
|
service.Name("url"),
|
||||||
service.Version("latest"),
|
service.Version("latest"),
|
||||||
)
|
)
|
||||||
|
h := handler.NewUrl()
|
||||||
// Register handler
|
// Register handler
|
||||||
pb.RegisterUrlHandler(srv.Server(), handler.NewUrl())
|
pb.RegisterUrlHandler(srv.Server(), h)
|
||||||
|
admin.RegisterAdminHandler(srv.Server(), h)
|
||||||
|
|
||||||
traceCloser := tracing.SetupOpentracing("url")
|
traceCloser := tracing.SetupOpentracing("url")
|
||||||
defer traceCloser.Close()
|
defer traceCloser.Close()
|
||||||
|
|||||||
Reference in New Issue
Block a user