Skip to content

Commit

Permalink
add grpc API for creating/deleting/rename/listing namespaces (#9252)
Browse files Browse the repository at this point in the history
  • Loading branch information
mangalaman93 authored Feb 18, 2025
1 parent 921446e commit e666f13
Show file tree
Hide file tree
Showing 16 changed files with 816 additions and 47 deletions.
2 changes: 2 additions & 0 deletions dgraph/cmd/alpha/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (

"github.com/dgraph-io/badger/v4"
"github.com/dgraph-io/dgo/v240/protos/api"
apiv25 "github.com/dgraph-io/dgo/v240/protos/api.v25"
_ "github.com/dgraph-io/gqlparser/v2/validator/rules" // make gql validator init() all rules
"github.com/dgraph-io/ristretto/v2/z"
"github.com/hypermodeinc/dgraph/v24/edgraph"
Expand Down Expand Up @@ -461,6 +462,7 @@ func serveGRPC(l net.Listener, tlsCfg *tls.Config, closer *z.Closer) {

s := grpc.NewServer(opt...)
api.RegisterDgraphServer(s, &edgraph.Server{})
apiv25.RegisterDgraphHMServer(s, &edgraph.Server{})
hapi.RegisterHealthServer(s, health.NewServer())
worker.RegisterZeroProxyServer(s)

Expand Down
15 changes: 2 additions & 13 deletions dgraphapi/schema.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
/*
* Copyright 2025 Dgraph Labs, Inc. and Contributors
*
* 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-FileCopyrightText: © Hypermode Inc. <[email protected]>
* SPDX-License-Identifier: Apache-2.0
*/

package dgraphapi
Expand Down
9 changes: 6 additions & 3 deletions dgraphtest/compose_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ package dgraphtest

import (
"github.com/dgraph-io/dgo/v240"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/hypermodeinc/dgraph/v24/dgraphapi"
"github.com/hypermodeinc/dgraph/v24/testutil"
)
Expand All @@ -18,12 +21,12 @@ func NewComposeCluster() *ComposeCluster {
}

func (c *ComposeCluster) Client() (*dgraphapi.GrpcClient, func(), error) {
client, err := testutil.DgraphClient(testutil.SockAddr)
dg, err := dgo.NewClient(testutil.SockAddr,
dgo.WithGrpcOption(grpc.WithTransportCredentials(insecure.NewCredentials())))
if err != nil {
return nil, nil, err
}

return &dgraphapi.GrpcClient{Dgraph: client}, func() {}, nil
return &dgraphapi.GrpcClient{Dgraph: dg}, func() { dg.Close() }, nil
}

// HTTPClient creates an HTTP client
Expand Down
35 changes: 24 additions & 11 deletions dgraphtest/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,25 @@ import (
"os/exec"
"path/filepath"
"strings"
"sync"

"github.com/pkg/errors"
"golang.org/x/mod/modfile"
)

var (
cloneOnce sync.Once
)

func (c *LocalCluster) dgraphImage() string {
return "dgraph/dgraph:local"
}

func (c *LocalCluster) setupBinary() error {
if err := ensureDgraphClone(); err != nil {
panic(err)
}

if c.conf.customPlugins {
race := false // Explicit var declaration to avoid confusion on the next line
if err := c.GeneratePlugins(race); err != nil {
Expand Down Expand Up @@ -52,21 +61,25 @@ func (c *LocalCluster) setupBinary() error {
}

func ensureDgraphClone() error {
if _, err := os.Stat(repoDir); err != nil {
return runGitClone()
}
f := func() error {
if _, err := os.Stat(repoDir); err != nil {
return runGitClone()
}

if err := runGitStatus(); err != nil {
if ierr := cleanupRepo(); ierr != nil {
return ierr
if err := runGitStatus(); err != nil {
if err := os.RemoveAll(repoDir); err != nil {
return err
}
return runGitClone()
}
return runGitClone()
return nil
}
return nil
}

func cleanupRepo() error {
return os.RemoveAll(repoDir)
var err error
cloneOnce.Do(func() {
err = f()
})
return err
}

func runGitClone() error {
Expand Down
3 changes: 0 additions & 3 deletions dgraphtest/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ func init() {
if err != nil {
panic(err)
}
if err := ensureDgraphClone(); err != nil {
panic(err)
}

log.Printf("[INFO] baseRepoDir: %v", baseRepoDir)
log.Printf("[INFO] repoDir: %v", repoDir)
Expand Down
3 changes: 1 addition & 2 deletions edgraph/access_ee.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ func (s *Server) Login(ctx context.Context,
// authenticateLogin authenticates the login request using either the refresh token if present, or
// the <userId, password> pair. If authentication passes, it queries the user's uid and associated
// groups from DB and returns the user object
func (s *Server) authenticateLogin(ctx context.Context, request *api.LoginRequest) (*acl.User,
error) {
func (s *Server) authenticateLogin(ctx context.Context, request *api.LoginRequest) (*acl.User, error) {
if err := validateLoginRequest(request); err != nil {
return nil, errors.Wrapf(err, "invalid login request")
}
Expand Down
2 changes: 1 addition & 1 deletion edgraph/multi_tenancy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type ResetPasswordInput struct {
Namespace uint64
}

func (s *Server) CreateNamespace(ctx context.Context, passwd string) (uint64, error) {
func (s *Server) CreateNamespaceInternal(ctx context.Context, passwd string) (uint64, error) {
return 0, nil
}

Expand Down
4 changes: 2 additions & 2 deletions edgraph/multi_tenancy_ee.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ func (s *Server) ResetPassword(ctx context.Context, inp *ResetPasswordInput) err
return nil
}

// CreateNamespace creates a new namespace. Only guardian of galaxy is authorized to do so.
// CreateNamespaceInternal creates a new namespace. Only guardian of galaxy is authorized to do so.
// Authorization is handled by middlewares.
func (s *Server) CreateNamespace(ctx context.Context, passwd string) (uint64, error) {
func (s *Server) CreateNamespaceInternal(ctx context.Context, passwd string) (uint64, error) {
glog.V(2).Info("Got create namespace request.")

num := &pb.Num{Val: 1, Type: pb.Num_NS_ID}
Expand Down
Loading

0 comments on commit e666f13

Please sign in to comment.