Skip to content

Commit

Permalink
Merge pull request #65 from napptive/feature/PG-1308_upload_catalog_app
Browse files Browse the repository at this point in the history
Implement upload application
  • Loading branch information
Carmendelope authored Mar 31, 2023
2 parents 850363d + 0b7558b commit 77dc4ca
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 15 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2
github.com/napptive/analytics v1.1.0
github.com/napptive/grpc-catalog-common-go v0.2.0
github.com/napptive/grpc-catalog-go v0.27.0
github.com/napptive/grpc-catalog-go v0.28.0
github.com/napptive/grpc-jwt-go v0.1.0
github.com/napptive/grpc-playground-apps-go v0.33.0
github.com/napptive/mock-extensions v1.0.0
Expand All @@ -19,7 +19,7 @@ require (
github.com/napptive/oam-utils v1.0.0
github.com/napptive/rdbms/v2 v2.4.0
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.27.4
github.com/onsi/gomega v1.27.5
github.com/rs/xid v1.4.0
github.com/rs/zerolog v1.29.0
github.com/spf13/cobra v1.6.1
Expand All @@ -43,7 +43,7 @@ require (
github.com/envoyproxy/protoc-gen-validate v0.10.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gofrs/uuid v4.2.0+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
Expand Down
13 changes: 7 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ github.com/go-openapi/jsonreference v0.19.3/go.mod h1:rjx6GuL8TTa9VaixXglHmQmIL9
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
Expand Down Expand Up @@ -397,8 +398,8 @@ github.com/napptive/analytics v1.1.0 h1:UuiJ6ZrBuT9D+JCC+1ukjkuF6vJG5yRZGM3PneNS
github.com/napptive/analytics v1.1.0/go.mod h1:WJoXJvxO0rHHVYFB9EU2HSqsHxexLNAgEqbtqtCd2hI=
github.com/napptive/grpc-catalog-common-go v0.2.0 h1:BdIYVOEmzIZbzbVvjKnd9/8T8nQfNqZeEzzVCXjWhvI=
github.com/napptive/grpc-catalog-common-go v0.2.0/go.mod h1:bnedZvTuFnhO2q1u66EUseAy3k0/N5MW/BFvb8S23bA=
github.com/napptive/grpc-catalog-go v0.27.0 h1:xzFgu8a1pHPVOS1hmFmXe0uZnoWVk8iFHkFkApAegMQ=
github.com/napptive/grpc-catalog-go v0.27.0/go.mod h1:BoOdDpZZlW8rWNlSe2/mjG9JC6+UCLlZkUNovwMFxY0=
github.com/napptive/grpc-catalog-go v0.28.0 h1:1sD5pUcJED3uj+HIf3CZyatIA+lNsYmyhe08Gl6SCR8=
github.com/napptive/grpc-catalog-go v0.28.0/go.mod h1:BoOdDpZZlW8rWNlSe2/mjG9JC6+UCLlZkUNovwMFxY0=
github.com/napptive/grpc-common-go v0.2.0/go.mod h1:Q896cZY+yIkted9zYw3jtguVDdfL1bqTHjjiirBTjnw=
github.com/napptive/grpc-common-go v0.4.0/go.mod h1:Q896cZY+yIkted9zYw3jtguVDdfL1bqTHjjiirBTjnw=
github.com/napptive/grpc-common-go v0.8.0 h1:iRGbhQyHiLYm7EVKprEMiFixAvcLroajAtA+I7n2K1M=
Expand Down Expand Up @@ -438,14 +439,14 @@ github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/ginkgo/v2 v2.1.4/go.mod h1:um6tUpWM/cxCK3/FK8BXqEiUMUwRgSM4JXG47RKZmLU=
github.com/onsi/ginkgo/v2 v2.9.1 h1:zie5Ly042PD3bsCvsSOPvRnFwyo3rKe64TJlD6nu0mk=
github.com/onsi/ginkgo/v2 v2.9.2 h1:BA2GMJOtfGAfagzYtrAlufIP0lq6QERkFmHLMLPwFSU=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48=
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
github.com/onsi/gomega v1.27.4 h1:Z2AnStgsdSayCMDiCU42qIz+HLqEPcgiOCXjAU/w+8E=
github.com/onsi/gomega v1.27.4/go.mod h1:riYq/GJKh8hhoM01HN6Vmuy93AarCXCBGpvFDK3q3fQ=
github.com/onsi/gomega v1.27.5 h1:T/X6I0RNFw/kTqgfkZPcQ5KU6vCnWNBGdtrIx2dpGeQ=
github.com/onsi/gomega v1.27.5/go.mod h1:PIQNjfQwkP3aQAH7lf7j87O/5FiNr+ZR8+ipb+qQlhg=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/pierrec/lz4/v4 v4.1.17 h1:kV4Ip+/hUBC+8T6+2EgburRtkE9ef4nbY3f4dFhGjMc=
github.com/pierrec/lz4/v4 v4.1.17/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
Expand Down
3 changes: 3 additions & 0 deletions internal/pkg/entities/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ type FileInfo struct {

// NewFileInfo creates FileInfo from *grpc_catalog_go.FileInfo
func NewFileInfo(info *grpc_catalog_go.FileInfo) *FileInfo {
if info == nil {
return nil
}
return &FileInfo{
Path: info.Path,
Data: info.Data,
Expand Down
24 changes: 19 additions & 5 deletions internal/pkg/provider/metadata/elastic_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const (
// PrivateField with the name of the field where we store the application scope
PrivateField = "Private"
// CacheRefreshTime ick duration to update cache
CacheRefreshTime = time.Second * 30
CacheRefreshTime = time.Minute * 5
)

// mapping with the elastic-schema
Expand Down Expand Up @@ -90,6 +90,7 @@ type responseWrapper struct {

type ElasticFilter interface {
ToElasticQuery() map[string]interface{}
ToString() string
}

// ToElasticQuery returns the search query for a ListFilter. Required to implement ElasticFilter interface
Expand Down Expand Up @@ -153,6 +154,18 @@ func (f *ListFilter) ToElasticQuery() map[string]interface{} {
return query
}

func (f *ListFilter) ToString() string {
namespace := ""
private := ""
if f.Private != nil {
private = fmt.Sprintf("%v", *f.Private)
}
if f.Namespace != nil {
namespace = *f.Namespace
}
return fmt.Sprintf("ListFilter. Namespace [%s] - Private [%s]", namespace, private)
}

// ApplicationFilter struct to filter by namespace and applicationName
type ApplicationFilter struct {
namespace string
Expand Down Expand Up @@ -193,6 +206,10 @@ func (af *ApplicationFilter) ToElasticQuery() map[string]interface{} {
}
}

func (af *ApplicationFilter) ToString() string {
return fmt.Sprintf("ApplicationFilter. Namespace [%s] - Application [%s]", af.namespace, af.application)
}

// ElasticProvider a struct to manage elastic storage
type ElasticProvider struct {
client *elasticsearch.Client
Expand Down Expand Up @@ -547,7 +564,6 @@ func (e *ElasticProvider) GetSummary() (*entities.Summary, error) {

// listFromWithFilter search applications in elastic with pagination
func (e *ElasticProvider) listFromWithFilter(filter ElasticFilter, lastReceived int, getFields ...string) (*responseWrapper, error) {

sortedBy := []string{NamespaceField, ApplicationField, TagField}
searchFunctions := []func(*esapi.SearchRequest){
e.client.Search.WithContext(context.Background()),
Expand Down Expand Up @@ -589,7 +605,7 @@ func (e *ElasticProvider) listFromWithFilter(filter ElasticFilter, lastReceived
return nil, nerrors.FromError(err)
}
// Print the response status, number of results, and request duration.
log.Debug().Str("Status", res.Status()).Int("total", r.Hits.Total.Value).Int("took(ms)", r.Took).Msg("List operation")
log.Debug().Str("filter", filter.ToString()).Str("Status", res.Status()).Int("total", r.Hits.Total.Value).Int("took(ms)", r.Took).Msg("List operation")

return &r, nil
}
Expand All @@ -598,7 +614,6 @@ func (e *ElasticProvider) listFromWithFilter(filter ElasticFilter, lastReceived
func (e *ElasticProvider) ListSummaryWithFilter(filter *ListFilter) ([]*entities.AppSummary, *entities.Summary, error) {
// if filtering == (public applications for all namespaces) -> return cache
if filter != nil && (filter.Namespace == nil || *filter.Namespace == "") && (filter.Private == nil || !*filter.Private) {
log.Debug().Msg("returning public apps")
e.Lock()
defer e.Unlock()
return e.appCache, e.summaryCache, nil
Expand Down Expand Up @@ -797,7 +812,6 @@ type updateVisibilityStruct struct {

// UpdateApplicationVisibility changes the application visibility
func (e *ElasticProvider) UpdateApplicationVisibility(namespace string, applicationName string, isPrivate bool) error {

// Get all the catalogIDs for namespace, applicationName
// Foreach:
// update the data
Expand Down
60 changes: 59 additions & 1 deletion internal/pkg/server/catalog-manager/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package catalog_manager

import (
"context"
b64 "encoding/base64"
"fmt"
"io"

Expand Down Expand Up @@ -118,8 +119,65 @@ func (h *Handler) Add(server grpc_catalog_go.Catalog_AddServer) error {
return nerrors.FromError(sErr).ToGRPC()
}
// Append the files
applicationFiles = append(applicationFiles, entities.NewFileInfo(request.File))
fileInfo := entities.NewFileInfo(request.File)
if fileInfo != nil {
applicationFiles = append(applicationFiles, fileInfo)
}
}
}

func (h *Handler) Upload(ctx context.Context, request *grpc_catalog_go.UploadApplicationRequest) (*grpc_catalog_common_go.OpResponse, error) {
if err := request.Validate(); err != nil {
return nil, nerrors.FromError(err).ToGRPC()
}

// check the user (check if after validation yto be sure the ApplicationId is filled
if err := h.validateUser(ctx, request.ApplicationId, "remove", true); err != nil {
return nil, err
}

accountName := ""
// if authentication is enabled -> Get the account name to filter all private apps by namespace
if h.authEnabled {
accountNameFromCtx, err := h.getAccountNameFromContext(ctx)
if err != nil {
log.Error().Err(err).Msg("error getting username from context")
return nil, err
}
accountName = *accountNameFromCtx
}

files := make([]*entities.FileInfo, 0)
for _, file := range request.Files {
sDec, err := b64.StdEncoding.DecodeString(file.Data)
if err != nil {
log.Error().Err(err).Str("file", file.Path).Msg("error uploading catalog application. Error decoding application file")
return nil, nerrors.NewInternalErrorFrom(err, "Error uploading catalog application, error decoding application file [%s]", file.Path)
}
files = append(files, &entities.FileInfo{
Path: file.Path,
Data: sDec,
})
}
isPrivate, err := h.manager.Add(request.ApplicationId, files, request.Private, accountName)
if err != nil {
log.Error().Err(err).Str("applicationID", request.ApplicationId).Msg("error uploading application")
return nil, nerrors.FromGRPC(err)
}

message := ""

if isPrivate {
message = fmt.Sprintf("Private application %s added.", request.ApplicationId)
} else {
message = fmt.Sprintf("Public application %s added.", request.ApplicationId)
}

return &grpc_catalog_common_go.OpResponse{
Status: grpc_catalog_common_go.OpStatus_SUCCESS,
StatusName: grpc_catalog_common_go.OpStatus_SUCCESS.String(),
UserInfo: message,
}, nil
}

// Download an application from catalog
Expand Down

0 comments on commit 77dc4ca

Please sign in to comment.