From 6cde2493abf41f9fc53277c9b6d436c1cbd08eb3 Mon Sep 17 00:00:00 2001 From: Aaron Stein Date: Tue, 24 Jul 2018 16:14:21 -0700 Subject: [PATCH] query: scope to tenant fixes: #304 fixes: #285 fixes: #286 --- _integration/cmp/deployment.go | 6 +-- app/deployment/app.go | 12 +++++- app/lease/app.go | 18 +++++++-- app/market/mocks/client.go | 2 +- app/market/mocks/engine.go | 2 +- app/market/mocks/facilitator.go | 2 +- cmd/akash/query/account.go | 13 +++++- cmd/akash/query/deployment.go | 11 ++++- cmd/akash/query/lease.go | 11 ++++- cmd/akash/session/flags.go | 4 ++ marketplace/mocks/handler.go | 2 +- provider/cluster/kube/mocks/client.go | 2 +- provider/cluster/mocks/client.go | 2 +- provider/cluster/mocks/cluster.go | 2 +- provider/cluster/mocks/deployment.go | 2 +- provider/cluster/mocks/reservation.go | 2 +- provider/manifest/mocks/handler.go | 2 +- query/client.go | 54 +++++++++++++++++-------- query/mocks/client.go | 58 ++++++++++++++++++++++++--- txutil/mocks/client.go | 2 +- 20 files changed, 161 insertions(+), 48 deletions(-) diff --git a/_integration/cmp/deployment.go b/_integration/cmp/deployment.go index ad3fff54c9..18dbdbd592 100644 --- a/_integration/cmp/deployment.go +++ b/_integration/cmp/deployment.go @@ -57,12 +57,12 @@ func orderQuery(daddr vars.Ref) gestalt.Component { WithMeta(g.Require(daddr.Name())) } -func leaseQuery(daddr vars.Ref) gestalt.Component { +func leaseQuery(key vars.Ref, daddr vars.Ref) gestalt.Component { parse := js.Do( js.Str(daddr.Var(), "items", "[0]", "id", "deployment"), ) - return akash("lease-query", "query", "lease"). + return akash("lease-query", "query", "lease", "-k", key.Name()). FN(parse). WithMeta(g.Require(daddr.Name())) } @@ -73,7 +73,7 @@ func groupDeploy(key vars.Ref, daddr vars.Ref) gestalt.Component { Run(g.Retry(5). Run(orderQuery(daddr))). Run(g.Retry(15). - Run(leaseQuery(daddr))). + Run(leaseQuery(key, daddr))). Run(deployClose(key, daddr)). WithMeta(g.Export(daddr.Name())) } diff --git a/app/deployment/app.go b/app/deployment/app.go index 4877703a28..031906e2cd 100644 --- a/app/deployment/app.go +++ b/app/deployment/app.go @@ -60,7 +60,7 @@ func (a *app) Query(state appstate.State, req tmtypes.RequestQuery) tmtypes.Resp id := strings.TrimPrefix(req.Path, appstate.DeploymentPath) if len(id) == 0 { - return a.doRangeQuery(state) + return a.doRangeQuery(state, req.Data) } key, err := keys.ParseDeploymentPath(id) @@ -142,7 +142,7 @@ func (a *app) doQuery(state appstate.State, key keys.Deployment) tmtypes.Respons } } -func (a *app) doRangeQuery(state appstate.State) tmtypes.ResponseQuery { +func (a *app) doRangeQuery(state appstate.State, tenant []byte) tmtypes.ResponseQuery { deps, err := state.Deployment().GetMaxRange() if err != nil { return tmtypes.ResponseQuery{ @@ -151,6 +151,14 @@ func (a *app) doRangeQuery(state appstate.State) tmtypes.ResponseQuery { } } + tenantDeps := []types.Deployment{} + for _, deployment := range deps.Items { + if bytes.Equal(deployment.Tenant, tenant) { + tenantDeps = append(tenantDeps, deployment) + } + } + + deps.Items = tenantDeps bytes, err := proto.Marshal(deps) if err != nil { return tmtypes.ResponseQuery{ diff --git a/app/lease/app.go b/app/lease/app.go index 08bbb2a8af..d2da8c1f32 100644 --- a/app/lease/app.go +++ b/app/lease/app.go @@ -1,6 +1,7 @@ package lease import ( + "bytes" "errors" "fmt" "strings" @@ -83,7 +84,7 @@ func (a *app) Query(state appstate.State, req tmtypes.RequestQuery) tmtypes.Resp id := strings.TrimPrefix(req.Path, appstate.LeasePath) if len(id) == 0 { - return a.doRangeQuery(state) + return a.doRangeQuery(state, req.Data) } { @@ -411,8 +412,8 @@ func (a *app) doQuery(state appstate.State, key keys.Lease) tmtypes.ResponseQuer } } -func (a *app) doRangeQuery(state appstate.State) tmtypes.ResponseQuery { - items, err := state.Lease().All() +func (a *app) doRangeQuery(state appstate.State, tenant []byte) tmtypes.ResponseQuery { + leases, err := state.Lease().All() if err != nil { return tmtypes.ResponseQuery{ Code: code.ERROR, @@ -420,6 +421,17 @@ func (a *app) doRangeQuery(state appstate.State) tmtypes.ResponseQuery { } } + items := []*types.Lease{} + for _, lease := range leases { + deployment, err := state.Deployment().Get(lease.Deployment) + if err != nil { + a.Log().Error("deployment doesn't exist for lease") + } + if bytes.Equal(deployment.Tenant, tenant) { + items = append(items, lease) + } + } + coll := &types.Leases{Items: items} bytes, err := proto.Marshal(coll) diff --git a/app/market/mocks/client.go b/app/market/mocks/client.go index 8cd7d5c4bf..f23ac94d21 100644 --- a/app/market/mocks/client.go +++ b/app/market/mocks/client.go @@ -1,4 +1,4 @@ -// Code generated by mockery v1.0.0. DO NOT EDIT. +// Code generated by mockery v1.0.0 package mocks import core_types "github.com/tendermint/tendermint/rpc/core/types" diff --git a/app/market/mocks/engine.go b/app/market/mocks/engine.go index a7d5934033..5095010fd9 100644 --- a/app/market/mocks/engine.go +++ b/app/market/mocks/engine.go @@ -1,4 +1,4 @@ -// Code generated by mockery v1.0.0. DO NOT EDIT. +// Code generated by mockery v1.0.0 package mocks import mock "github.com/stretchr/testify/mock" diff --git a/app/market/mocks/facilitator.go b/app/market/mocks/facilitator.go index d6ebcdd605..a18531f05e 100644 --- a/app/market/mocks/facilitator.go +++ b/app/market/mocks/facilitator.go @@ -1,4 +1,4 @@ -// Code generated by mockery v1.0.0. DO NOT EDIT. +// Code generated by mockery v1.0.0 package mocks import mock "github.com/stretchr/testify/mock" diff --git a/cmd/akash/query/account.go b/cmd/akash/query/account.go index dfba84b580..f90b66af33 100644 --- a/cmd/akash/query/account.go +++ b/cmd/akash/query/account.go @@ -11,14 +11,23 @@ func queryAccountCommand() *cobra.Command { cmd := &cobra.Command{ Use: "account [account ...]", Short: "query account", - Args: cobra.ExactArgs(1), RunE: session.WithSession(session.RequireNode(doQueryAccountCommand)), } - + session.AddFlagKeyOptional(cmd, cmd.Flags()) return cmd } func doQueryAccountCommand(session session.Session, cmd *cobra.Command, args []string) error { + if len(args) == 0 { + _, info, err := session.Signer() + if err != nil { + return err + } + if err := handleMessage(session.QueryClient().Account(session.Ctx(), info.PubKey.Address().Bytes())); err != nil { + return err + } + return nil + } for _, arg := range args { key, err := keys.ParseAccountPath(arg) if err != nil { diff --git a/cmd/akash/query/deployment.go b/cmd/akash/query/deployment.go index 6cac550ab4..8ccaf737cf 100644 --- a/cmd/akash/query/deployment.go +++ b/cmd/akash/query/deployment.go @@ -13,13 +13,20 @@ func queryDeploymentCommand() *cobra.Command { Short: "query deployment", RunE: session.WithSession(session.RequireNode(doQueryDeploymentCommand)), } - + session.AddFlagKeyOptional(cmd, cmd.Flags()) return cmd } func doQueryDeploymentCommand(session session.Session, cmd *cobra.Command, args []string) error { if len(args) == 0 { - return handleMessage(session.QueryClient().Deployments(session.Ctx())) + _, info, err := session.Signer() + if err != nil { + return err + } + if err := handleMessage(session.QueryClient().TenantDeployments(session.Ctx(), info.PubKey.Address().Bytes())); err != nil { + return err + } + return nil } for _, arg := range args { key, err := keys.ParseDeploymentPath(arg) diff --git a/cmd/akash/query/lease.go b/cmd/akash/query/lease.go index ca28c769c2..1c74175cf6 100644 --- a/cmd/akash/query/lease.go +++ b/cmd/akash/query/lease.go @@ -13,13 +13,20 @@ func queryLeaseCommand() *cobra.Command { Short: "query lease", RunE: session.WithSession(session.RequireNode(doQueryLeaseCommand)), } - + session.AddFlagKeyOptional(cmd, cmd.Flags()) return cmd } func doQueryLeaseCommand(session session.Session, cmd *cobra.Command, args []string) error { if len(args) == 0 { - return handleMessage(session.QueryClient().Leases(session.Ctx())) + _, info, err := session.Signer() + if err != nil { + return err + } + if err := handleMessage(session.QueryClient().TenantLeases(session.Ctx(), info.PubKey.Address().Bytes())); err != nil { + return err + } + return nil } for _, arg := range args { key, err := keys.ParseLeasePath(arg) diff --git a/cmd/akash/session/flags.go b/cmd/akash/session/flags.go index 6e3e881382..1d10137488 100644 --- a/cmd/akash/session/flags.go +++ b/cmd/akash/session/flags.go @@ -19,6 +19,10 @@ func AddFlagKey(cmd *cobra.Command, flags *pflag.FlagSet) { cmd.MarkFlagRequired(flagKey) } +func AddFlagKeyOptional(cmd *cobra.Command, flags *pflag.FlagSet) { + flags.StringP(flagKey, "k", "", "key name") +} + func AddFlagNonce(cmd *cobra.Command, flags *pflag.FlagSet) { flags.Uint64(flagNonce, 0, "nonce (optional)") } diff --git a/marketplace/mocks/handler.go b/marketplace/mocks/handler.go index f5c4b8451c..81bc2db149 100644 --- a/marketplace/mocks/handler.go +++ b/marketplace/mocks/handler.go @@ -1,4 +1,4 @@ -// Code generated by mockery v1.0.0. DO NOT EDIT. +// Code generated by mockery v1.0.0 package mocks import mock "github.com/stretchr/testify/mock" diff --git a/provider/cluster/kube/mocks/client.go b/provider/cluster/kube/mocks/client.go index 99ed19cd27..a689501e51 100644 --- a/provider/cluster/kube/mocks/client.go +++ b/provider/cluster/kube/mocks/client.go @@ -1,4 +1,4 @@ -// Code generated by mockery v1.0.0. DO NOT EDIT. +// Code generated by mockery v1.0.0 package mocks import cluster "github.com/ovrclk/akash/provider/cluster" diff --git a/provider/cluster/mocks/client.go b/provider/cluster/mocks/client.go index 50f5dd839e..9be9aecc1a 100644 --- a/provider/cluster/mocks/client.go +++ b/provider/cluster/mocks/client.go @@ -1,4 +1,4 @@ -// Code generated by mockery v1.0.0. DO NOT EDIT. +// Code generated by mockery v1.0.0 package mocks import cluster "github.com/ovrclk/akash/provider/cluster" diff --git a/provider/cluster/mocks/cluster.go b/provider/cluster/mocks/cluster.go index 94ec280495..7fabc86f67 100644 --- a/provider/cluster/mocks/cluster.go +++ b/provider/cluster/mocks/cluster.go @@ -1,4 +1,4 @@ -// Code generated by mockery v1.0.0. DO NOT EDIT. +// Code generated by mockery v1.0.0 package mocks import cluster "github.com/ovrclk/akash/provider/cluster" diff --git a/provider/cluster/mocks/deployment.go b/provider/cluster/mocks/deployment.go index 547cd86810..014da97ca6 100644 --- a/provider/cluster/mocks/deployment.go +++ b/provider/cluster/mocks/deployment.go @@ -1,4 +1,4 @@ -// Code generated by mockery v1.0.0. DO NOT EDIT. +// Code generated by mockery v1.0.0 package mocks import mock "github.com/stretchr/testify/mock" diff --git a/provider/cluster/mocks/reservation.go b/provider/cluster/mocks/reservation.go index 41cfa4ac8c..9839140216 100644 --- a/provider/cluster/mocks/reservation.go +++ b/provider/cluster/mocks/reservation.go @@ -1,4 +1,4 @@ -// Code generated by mockery v1.0.0. DO NOT EDIT. +// Code generated by mockery v1.0.0 package mocks import mock "github.com/stretchr/testify/mock" diff --git a/provider/manifest/mocks/handler.go b/provider/manifest/mocks/handler.go index f195df59c4..a1d294e4e6 100644 --- a/provider/manifest/mocks/handler.go +++ b/provider/manifest/mocks/handler.go @@ -1,4 +1,4 @@ -// Code generated by mockery v1.0.0. DO NOT EDIT. +// Code generated by mockery v1.0.0 package mocks import mock "github.com/stretchr/testify/mock" diff --git a/query/client.go b/query/client.go index 113015384e..4053a369eb 100644 --- a/query/client.go +++ b/query/client.go @@ -31,7 +31,10 @@ type Client interface { Leases(ctx context.Context) (*types.Leases, error) Lease(ctx context.Context, id types.LeaseID) (*types.Lease, error) - Get(ctx context.Context, path string, obj proto.Message) error + TenantDeployments(ctx context.Context, tenant []byte) (*types.Deployments, error) + TenantLeases(ctx context.Context, tenant []byte) (*types.Leases, error) + + Get(ctx context.Context, path string, obj proto.Message, data []byte) error } type client struct { @@ -45,7 +48,7 @@ func NewClient(tmc *tmclient.HTTP) Client { func (c *client) Account(ctx context.Context, id []byte) (*types.Account, error) { obj := &types.Account{} path := AccountPath(id) - if err := c.Get(ctx, path, obj); err != nil { + if err := c.Get(ctx, path, obj, nil); err != nil { return nil, err } return obj, nil @@ -54,7 +57,7 @@ func (c *client) Account(ctx context.Context, id []byte) (*types.Account, error) func (c *client) Providers(ctx context.Context) (*types.Providers, error) { obj := &types.Providers{} path := ProvidersPath() - if err := c.Get(ctx, path, obj); err != nil { + if err := c.Get(ctx, path, obj, nil); err != nil { return nil, err } return obj, nil @@ -63,7 +66,7 @@ func (c *client) Providers(ctx context.Context) (*types.Providers, error) { func (c *client) Provider(ctx context.Context, id []byte) (*types.Provider, error) { obj := &types.Provider{} path := ProviderPath(id) - if err := c.Get(ctx, path, obj); err != nil { + if err := c.Get(ctx, path, obj, nil); err != nil { return nil, err } return obj, nil @@ -72,7 +75,7 @@ func (c *client) Provider(ctx context.Context, id []byte) (*types.Provider, erro func (c *client) Deployments(ctx context.Context) (*types.Deployments, error) { obj := &types.Deployments{} path := DeploymentsPath() - if err := c.Get(ctx, path, obj); err != nil { + if err := c.Get(ctx, path, obj, nil); err != nil { return nil, err } return obj, nil @@ -81,7 +84,7 @@ func (c *client) Deployments(ctx context.Context) (*types.Deployments, error) { func (c *client) Deployment(ctx context.Context, id []byte) (*types.Deployment, error) { obj := &types.Deployment{} path := DeploymentPath(id) - if err := c.Get(ctx, path, obj); err != nil { + if err := c.Get(ctx, path, obj, nil); err != nil { return nil, err } return obj, nil @@ -90,7 +93,7 @@ func (c *client) Deployment(ctx context.Context, id []byte) (*types.Deployment, func (c *client) DeploymentGroups(ctx context.Context) (*types.DeploymentGroups, error) { obj := &types.DeploymentGroups{} path := DeploymentGroupsPath() - if err := c.Get(ctx, path, obj); err != nil { + if err := c.Get(ctx, path, obj, nil); err != nil { return nil, err } return obj, nil @@ -99,7 +102,7 @@ func (c *client) DeploymentGroups(ctx context.Context) (*types.DeploymentGroups, func (c *client) DeploymentGroup(ctx context.Context, id types.DeploymentGroupID) (*types.DeploymentGroup, error) { obj := &types.DeploymentGroup{} path := DeploymentGroupPath(id) - if err := c.Get(ctx, path, obj); err != nil { + if err := c.Get(ctx, path, obj, nil); err != nil { return nil, err } return obj, nil @@ -108,7 +111,7 @@ func (c *client) DeploymentGroup(ctx context.Context, id types.DeploymentGroupID func (c *client) DeploymentLeases(ctx context.Context, id []byte) (*types.Leases, error) { obj := &types.Leases{} path := DeploymentLeasesPath(id) - if err := c.Get(ctx, path, obj); err != nil { + if err := c.Get(ctx, path, obj, nil); err != nil { return nil, err } return obj, nil @@ -117,7 +120,7 @@ func (c *client) DeploymentLeases(ctx context.Context, id []byte) (*types.Leases func (c *client) Orders(ctx context.Context) (*types.Orders, error) { obj := &types.Orders{} path := OrdersPath() - if err := c.Get(ctx, path, obj); err != nil { + if err := c.Get(ctx, path, obj, nil); err != nil { return nil, err } return obj, nil @@ -126,7 +129,7 @@ func (c *client) Orders(ctx context.Context) (*types.Orders, error) { func (c *client) Order(ctx context.Context, id types.OrderID) (*types.Order, error) { obj := &types.Order{} path := OrderPath(id) - if err := c.Get(ctx, path, obj); err != nil { + if err := c.Get(ctx, path, obj, nil); err != nil { return nil, err } return obj, nil @@ -135,7 +138,7 @@ func (c *client) Order(ctx context.Context, id types.OrderID) (*types.Order, err func (c *client) Fulfillments(ctx context.Context) (*types.Fulfillments, error) { obj := &types.Fulfillments{} path := FulfillmentsPath() - if err := c.Get(ctx, path, obj); err != nil { + if err := c.Get(ctx, path, obj, nil); err != nil { return nil, err } return obj, nil @@ -144,7 +147,7 @@ func (c *client) Fulfillments(ctx context.Context) (*types.Fulfillments, error) func (c *client) Fulfillment(ctx context.Context, id types.FulfillmentID) (*types.Fulfillment, error) { obj := &types.Fulfillment{} path := FulfillmentPath(id) - if err := c.Get(ctx, path, obj); err != nil { + if err := c.Get(ctx, path, obj, nil); err != nil { return nil, err } return obj, nil @@ -153,7 +156,7 @@ func (c *client) Fulfillment(ctx context.Context, id types.FulfillmentID) (*type func (c *client) Leases(ctx context.Context) (*types.Leases, error) { obj := &types.Leases{} path := LeasesPath() - if err := c.Get(ctx, path, obj); err != nil { + if err := c.Get(ctx, path, obj, nil); err != nil { return nil, err } return obj, nil @@ -162,14 +165,31 @@ func (c *client) Leases(ctx context.Context) (*types.Leases, error) { func (c *client) Lease(ctx context.Context, id types.LeaseID) (*types.Lease, error) { obj := &types.Lease{} path := LeasePath(id) - if err := c.Get(ctx, path, obj); err != nil { + if err := c.Get(ctx, path, obj, nil); err != nil { + return nil, err + } + return obj, nil +} + +func (c *client) TenantDeployments(ctx context.Context, tenant []byte) (*types.Deployments, error) { + obj := &types.Deployments{} + path := DeploymentsPath() + if err := c.Get(ctx, path, obj, tenant); err != nil { + return nil, err + } + return obj, nil +} +func (c *client) TenantLeases(ctx context.Context, tenant []byte) (*types.Leases, error) { + obj := &types.Leases{} + path := LeasesPath() + if err := c.Get(ctx, path, obj, tenant); err != nil { return nil, err } return obj, nil } -func (c *client) Get(ctx context.Context, path string, obj proto.Message) error { - result, err := c.tmc.ABCIQuery(path, nil) +func (c *client) Get(ctx context.Context, path string, obj proto.Message, data []byte) error { + result, err := c.tmc.ABCIQuery(path, data) if err != nil { return err } diff --git a/query/mocks/client.go b/query/mocks/client.go index c79c51df01..d2c74a0f10 100644 --- a/query/mocks/client.go +++ b/query/mocks/client.go @@ -1,4 +1,4 @@ -// Code generated by mockery v1.0.0. DO NOT EDIT. +// Code generated by mockery v1.0.0 package mocks import context "context" @@ -196,13 +196,13 @@ func (_m *Client) Fulfillments(ctx context.Context) (*types.Fulfillments, error) return r0, r1 } -// Get provides a mock function with given fields: ctx, path, obj -func (_m *Client) Get(ctx context.Context, path string, obj proto.Message) error { - ret := _m.Called(ctx, path, obj) +// Get provides a mock function with given fields: ctx, path, obj, data +func (_m *Client) Get(ctx context.Context, path string, obj proto.Message, data []byte) error { + ret := _m.Called(ctx, path, obj, data) var r0 error - if rf, ok := ret.Get(0).(func(context.Context, string, proto.Message) error); ok { - r0 = rf(ctx, path, obj) + if rf, ok := ret.Get(0).(func(context.Context, string, proto.Message, []byte) error); ok { + r0 = rf(ctx, path, obj, data) } else { r0 = ret.Error(0) } @@ -347,3 +347,49 @@ func (_m *Client) Providers(ctx context.Context) (*types.Providers, error) { return r0, r1 } + +// TenantDeployments provides a mock function with given fields: ctx, tenant +func (_m *Client) TenantDeployments(ctx context.Context, tenant []byte) (*types.Deployments, error) { + ret := _m.Called(ctx, tenant) + + var r0 *types.Deployments + if rf, ok := ret.Get(0).(func(context.Context, []byte) *types.Deployments); ok { + r0 = rf(ctx, tenant) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.Deployments) + } + } + + var r1 error + if rf, ok := ret.Get(1).(func(context.Context, []byte) error); ok { + r1 = rf(ctx, tenant) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// TenantLeases provides a mock function with given fields: ctx, tenant +func (_m *Client) TenantLeases(ctx context.Context, tenant []byte) (*types.Leases, error) { + ret := _m.Called(ctx, tenant) + + var r0 *types.Leases + if rf, ok := ret.Get(0).(func(context.Context, []byte) *types.Leases); ok { + r0 = rf(ctx, tenant) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.Leases) + } + } + + var r1 error + if rf, ok := ret.Get(1).(func(context.Context, []byte) error); ok { + r1 = rf(ctx, tenant) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} diff --git a/txutil/mocks/client.go b/txutil/mocks/client.go index f9519ff125..84555dc7ef 100644 --- a/txutil/mocks/client.go +++ b/txutil/mocks/client.go @@ -1,4 +1,4 @@ -// Code generated by mockery v1.0.0. DO NOT EDIT. +// Code generated by mockery v1.0.0 package mocks import core_types "github.com/tendermint/tendermint/rpc/core/types"