Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: data structures #279

Merged
merged 6 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 0 additions & 45 deletions cmd/relation/get.go

This file was deleted.

10 changes: 5 additions & 5 deletions cmd/relation/create.go → cmd/relationtuple/create.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package relation
package relationtuple

import (
"context"
Expand All @@ -17,7 +17,7 @@ import (

func newCreateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create <relation.json>",
Use: "create <relation-tuple.json>",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
conn, err := client.GetGRPCConn(cmd)
Expand All @@ -36,16 +36,16 @@ func newCreateCmd() *cobra.Command {
}
}

var r models.Relation
var r models.InternalRelationTuple
err = json.NewDecoder(f).Decode(&r)
if err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not decode: %s\n", err)
return cmdx.FailSilently(cmd)
}

cl := models.NewGRPCRelationWriterClient(conn)
cl := models.NewRelationTupleServiceClient(conn)

_, err = cl.WriteRelation(context.Background(), (*models.GRPCRelation)(&r))
_, err = cl.WriteRelationTuple(context.Background(), &models.WriteRelationTupleRequest{Tuple: (&models.RelationTuple{}).FromInternal(&r)})
if err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Error doing the request: %s\n", err)
return cmdx.FailSilently(cmd)
Expand Down
101 changes: 101 additions & 0 deletions cmd/relationtuple/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package relationtuple

import (
"context"
"fmt"
"strings"

"github.com/spf13/pflag"

"github.com/ory/x/cmdx"

"github.com/spf13/cobra"

"github.com/ory/keto/cmd/client"
"github.com/ory/keto/models"
)

const (
FlagSubject = "subject"
FlagRelation = "relation"
FlagObject = "object"
)

func registerRelationTupleFlags(flags *pflag.FlagSet) {
flags.String(FlagSubject, "", "Set the requested subject")
flags.String(FlagRelation, "", "Set the requested relation")
flags.String(FlagObject, "", "Set the requested object")
}

func readQueryFromFlags(cmd *cobra.Command) (*models.ReadRelationTuplesRequest_Query, error) {
subject, err := cmd.Flags().GetString(FlagSubject)
if err != nil {
return nil, err
}
relation, err := cmd.Flags().GetString(FlagRelation)
if err != nil {
return nil, err
}
object, err := cmd.Flags().GetString(FlagObject)
if err != nil {
return nil, err
}

query := &models.ReadRelationTuplesRequest_Query{
Relation: relation,
Object: (&models.RelationObject{}).FromString(object),
}

subjectParts := strings.Split(subject, "#")
if len(subjectParts) == 2 {
query.Subject = &models.ReadRelationTuplesRequest_Query_UserSet{
UserSet: &models.RelationUserSet{
Object: (&models.RelationObject{}).FromString(subjectParts[0]),
Relation: subjectParts[1],
},
}
} else {
query.Subject = &models.ReadRelationTuplesRequest_Query_UserId{
UserId: subject,
}
}

return query, nil
}

func newGetCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "get",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
conn, err := client.GetGRPCConn(cmd)
if err != nil {
return err
}
defer conn.Close()

cl := models.NewRelationTupleServiceClient(conn)
query, err := readQueryFromFlags(cmd)
if err != nil {
return err
}
resp, err := cl.ReadRelationTuples(context.Background(), &models.ReadRelationTuplesRequest{
TupleSets: []*models.ReadRelationTuplesRequest_Query{query},
Page: 0,
PerPage: 100,
})
if err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not make request: %s\n", err)
return err
}

cmdx.PrintCollection(cmd, models.NewGRPCRelationCollection(resp.Tuples))
return nil
},
}

cmd.Flags().AddFlagSet(packageFlags)
registerRelationTupleFlags(cmd.Flags())

return cmd
}
4 changes: 2 additions & 2 deletions cmd/relation/root.go → cmd/relationtuple/root.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package relation
package relationtuple

import (
"github.com/spf13/cobra"
Expand All @@ -10,7 +10,7 @@ import (
)

var relationCmd = &cobra.Command{
Use: "relation",
Use: "relation-tuple",
}

var packageFlags = pflag.NewFlagSet("relation package flags", pflag.ContinueOnError)
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"runtime"
"strings"

"github.com/ory/keto/cmd/relation"
"github.com/ory/keto/cmd/relationtuple"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -66,7 +66,7 @@ func init() {
// when this action is called directly.
RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

relation.RegisterCommandRecursive(RootCmd)
relationtuple.RegisterCommandRecursive(RootCmd)
}

// initConfig reads in config file and ENV variables if set.
Expand Down
9 changes: 4 additions & 5 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

"github.com/ory/keto/driver"
"github.com/ory/keto/models"
"github.com/ory/keto/relation"
"github.com/ory/keto/relationtuple"

"github.com/ory/x/viperx"

Expand Down Expand Up @@ -65,9 +65,8 @@ on configuration options, open the configuration documentation:
defer wg.Done()

s := grpc.NewServer()
relS := relation.NewServer(reg)
models.RegisterGRPCRelationReaderServer(s, relS)
models.RegisterGRPCRelationWriterServer(s, relS)
relS := relationtuple.NewServer(reg)
models.RegisterRelationTupleServiceServer(s, relS)
fmt.Println("going to serve GRPC on", lis.Addr().String())
if err := s.Serve(lis); err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "%+v\n", err)
Expand All @@ -78,7 +77,7 @@ on configuration options, open the configuration documentation:
defer wg.Done()

router := httprouter.New()
h := relation.NewHandler(reg)
h := relationtuple.NewHandler(reg)
h.RegisterPublicRoutes(router)

server := graceful.WithDefaults(&http.Server{
Expand Down
6 changes: 3 additions & 3 deletions driver/registry_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"github.com/ory/x/logrusx"

"github.com/ory/keto/persistence/memory"
"github.com/ory/keto/relation"
"github.com/ory/keto/relationtuple"
"github.com/ory/keto/x"
)

var _ relation.ManagerProvider = &RegistryDefault{}
var _ relationtuple.ManagerProvider = &RegistryDefault{}
var _ x.WriterProvider = &RegistryDefault{}
var _ x.LoggerProvider = &RegistryDefault{}

Expand All @@ -33,7 +33,7 @@ func (r *RegistryDefault) Writer() herodot.Writer {
return r.w
}

func (r *RegistryDefault) RelationManager() relation.Manager {
func (r *RegistryDefault) RelationTupleManager() relationtuple.Manager {
if r.p == nil {
r.p = memory.NewPersister()
}
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ require (
github.com/go-openapi/validate v0.19.3
github.com/go-swagger/go-swagger v0.21.1-0.20200107003254-1c98855b472d
github.com/gobuffalo/packr v1.24.1
github.com/gofrs/uuid v3.2.0+incompatible
github.com/golang/protobuf v1.4.2
github.com/gorilla/sessions v1.1.3
github.com/gorilla/websocket v1.4.2
Expand All @@ -23,14 +22,14 @@ require (
github.com/ory/viper v1.7.5
github.com/ory/x v0.0.154
github.com/pkg/errors v0.9.1
github.com/pkg/profile v1.3.0 // indirect
github.com/rs/cors v1.6.0
github.com/rubenv/sql-migrate v0.0.0-20190327083759-54bad0a9b051 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.7.0 // indirect
github.com/sqs/goreturns v0.0.0-20181028201513-538ac6014518
github.com/tidwall/gjson v1.6.0
github.com/tidwall/sjson v1.1.1 // indirect
github.com/urfave/negroni v1.0.0
go.mongodb.org/mongo-driver v1.3.4 // indirect
Expand Down
9 changes: 3 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand Down Expand Up @@ -592,6 +593,7 @@ github.com/gorilla/handlers v1.4.2/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/
github.com/gorilla/mux v1.6.2 h1:Pgr17XVTNXAk3q/r4CpKzC5xBM/qW1uVLV+IhRZpIIk=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/pat v0.0.0-20180118222023-199c85a7f6d1/go.mod h1:YeAe0gNeiNT5hoiZRI4yiOky6jVdNvfO2N6Kav/HmxY=
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
Expand Down Expand Up @@ -944,8 +946,6 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/profile v1.2.1 h1:F++O52m40owAmADcojzM+9gyjmMOY/T4oYJkgFDH8RE=
github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA=
github.com/pkg/profile v1.3.0 h1:OQIvuDgm00gWVWGTf4m4mCt6W1/0YqU7Ntg0mySWgaI=
github.com/pkg/profile v1.3.0/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
Expand Down Expand Up @@ -1084,6 +1084,7 @@ github.com/sqs/goreturns v0.0.0-20181028201513-538ac6014518/go.mod h1:CKI4AZ4XmG
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
Expand Down Expand Up @@ -1488,8 +1489,6 @@ google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQ
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
google.golang.org/grpc v1.27.1 h1:zvIju4sqAGvwKspUQOhwnpcqSbzi7/H6QomNNjTL4sk=
google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
google.golang.org/grpc v1.29.1 h1:EC2SB8S04d2r73uptxphDSUG+kTKVgjRPF+N3xpxRB4=
google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk=
google.golang.org/grpc v1.33.0 h1:IBKSUNL2uBS2DkJBncPP+TwT0sp9tgA8A75NjHt6umg=
google.golang.org/grpc v1.33.0/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
Expand All @@ -1501,8 +1500,6 @@ google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2
google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.24.0 h1:UhZDfRO8JRQru4/+LlLE0BRKGF8L+PICnvYZmx/fEGA=
google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4=
google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
gopkg.in/DataDog/dd-trace-go.v1 v1.27.0 h1:WGVt9dwn9vNeWZVdDYzjGQbEW8CghAkJlrC8w80jFVY=
Expand Down
9 changes: 9 additions & 0 deletions models/REQUIREMENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Relation Tuple API

## Read

Reads tuples paginated, can be filtered by including queries.

## Write

Write one or more tuples.
Loading