feat(billing): Add service handlers for monthly active user counts
This commit is contained in:
1 parent
d84eb01516
commit
280e9abb89
20 files changed
+1304
-102
No files matched your search
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/auth/ldap"
|
||||
"github.com/hashicorp/boundary/internal/auth/oidc"
|
||||
"github.com/hashicorp/boundary/internal/auth/password"
|
||||
"github.com/hashicorp/boundary/internal/billing"
|
||||
"github.com/hashicorp/boundary/internal/credential"
|
||||
credstatic "github.com/hashicorp/boundary/internal/credential/static"
|
||||
"github.com/hashicorp/boundary/internal/credential/vault"
|
||||
@@ -39,6 +40,7 @@ type (
|
||||
ConnectionRepoFactory func() (*session.ConnectionRepository, error)
|
||||
WorkerAuthRepoStorageFactory func() (*server.WorkerAuthRepositoryStorage, error)
|
||||
PluginStorageBucketRepoFactory func() (*pluginstorage.Repository, error)
|
||||
BillingRepoFactory func() (*billing.Repository, error)
|
||||
)
|
||||
|
||||
// Downstreamers provides at least a minimum interface that must be met by a
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/auth/oidc"
|
||||
"github.com/hashicorp/boundary/internal/auth/password"
|
||||
"github.com/hashicorp/boundary/internal/authtoken"
|
||||
"github.com/hashicorp/boundary/internal/billing"
|
||||
"github.com/hashicorp/boundary/internal/census"
|
||||
"github.com/hashicorp/boundary/internal/cmd/base"
|
||||
"github.com/hashicorp/boundary/internal/cmd/config"
|
||||
@@ -148,6 +149,7 @@ type Controller struct {
|
||||
PluginRepoFn common.PluginRepoFactory
|
||||
TargetRepoFn target.RepositoryFactory
|
||||
WorkerAuthRepoStorageFn common.WorkerAuthRepoStorageFactory
|
||||
BillingRepoFn common.BillingRepoFactory
|
||||
|
||||
scheduler *scheduler.Scheduler
|
||||
|
||||
@@ -448,6 +450,9 @@ func New(ctx context.Context, conf *Config) (*Controller, error) {
|
||||
c.WorkerAuthRepoStorageFn = func() (*server.WorkerAuthRepositoryStorage, error) {
|
||||
return server.NewRepositoryStorage(ctx, dbase, dbase, c.kms)
|
||||
}
|
||||
c.BillingRepoFn = func() (*billing.Repository, error) {
|
||||
return billing.NewRepository(ctx, dbase, dbase)
|
||||
}
|
||||
|
||||
// Check that credentials are available at startup, to avoid some harmless
|
||||
// but nasty-looking errors
|
||||
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/daemon/controller/handlers/accounts"
|
||||
"github.com/hashicorp/boundary/internal/daemon/controller/handlers/authmethods"
|
||||
"github.com/hashicorp/boundary/internal/daemon/controller/handlers/authtokens"
|
||||
"github.com/hashicorp/boundary/internal/daemon/controller/handlers/billing"
|
||||
"github.com/hashicorp/boundary/internal/daemon/controller/handlers/credentiallibraries"
|
||||
"github.com/hashicorp/boundary/internal/daemon/controller/handlers/credentials"
|
||||
"github.com/hashicorp/boundary/internal/daemon/controller/handlers/credentialstores"
|
||||
@@ -349,6 +350,13 @@ func (c *Controller) registerGrpcServices(s *grpc.Server) error {
|
||||
opsservices.RegisterHealthServiceServer(s, hs)
|
||||
c.HealthService = hs
|
||||
}
|
||||
if _, ok := currentServices[services.BillingService_ServiceDesc.ServiceName]; !ok {
|
||||
bs, err := billing.NewService(c.baseContext, c.BillingRepoFn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create billing handler service: %w", err)
|
||||
}
|
||||
services.RegisterBillingServiceServer(s, bs)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -419,6 +427,9 @@ func registerGrpcGatewayEndpoints(ctx context.Context, gwMux *runtime.ServeMux,
|
||||
if err := services.RegisterPolicyServiceHandlerFromEndpoint(ctx, gwMux, gatewayTarget, dialOptions); err != nil {
|
||||
return fmt.Errorf("failed to register policy handler: %w", err)
|
||||
}
|
||||
if err := services.RegisterBillingServiceHandlerFromEndpoint(ctx, gwMux, gatewayTarget, dialOptions); err != nil {
|
||||
return fmt.Errorf("failed to register billing service handler: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -137,6 +137,7 @@ func TestHandleImplementedPaths(t *testing.T) {
|
||||
"v1/targets/someid",
|
||||
"v1/users",
|
||||
"v1/users/someid",
|
||||
"v1/billing:monthly-active-users",
|
||||
},
|
||||
"POST": {
|
||||
// Creation end points
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package billing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/billing"
|
||||
"github.com/hashicorp/boundary/internal/daemon/controller/auth"
|
||||
"github.com/hashicorp/boundary/internal/daemon/controller/common"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
|
||||
"github.com/hashicorp/boundary/internal/types/action"
|
||||
"github.com/hashicorp/boundary/internal/types/resource"
|
||||
pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/billing"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
)
|
||||
|
||||
var (
|
||||
// IdActions contains the set of actions that can be performed on
|
||||
// individual resources
|
||||
IdActions = action.NewActionSet()
|
||||
|
||||
// CollectionActions contains the set of actions that can be performed on
|
||||
// this collection
|
||||
CollectionActions = action.NewActionSet(
|
||||
action.MonthlyActiveUsers,
|
||||
)
|
||||
)
|
||||
|
||||
func init() {
|
||||
// TODO: refactor to remove IdActions and CollectionActions package variables
|
||||
action.RegisterResource(resource.Billing, IdActions, CollectionActions)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
pbs.UnsafeBillingServiceServer
|
||||
|
||||
repoFn common.BillingRepoFactory
|
||||
}
|
||||
|
||||
var _ pbs.BillingServiceServer = (*Service)(nil)
|
||||
|
||||
// NewService returns a billing service which handles billing related requests to boundary.
|
||||
func NewService(
|
||||
ctx context.Context,
|
||||
repoFn common.BillingRepoFactory,
|
||||
) (Service, error) {
|
||||
const op = "billing.NewService"
|
||||
if repoFn == nil {
|
||||
return Service{}, errors.New(ctx, errors.InvalidParameter, op, "missing billing repository")
|
||||
}
|
||||
return Service{
|
||||
repoFn: repoFn,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s Service) MonthlyActiveUsers(ctx context.Context, req *pbs.MonthlyActiveUsersRequest) (*pbs.MonthlyActiveUsersResponse, error) {
|
||||
const op = "billing.(Service).MonthlyActiveUsers"
|
||||
|
||||
authResults := s.authResult(ctx, action.MonthlyActiveUsers)
|
||||
if authResults.Error != nil {
|
||||
return nil, errors.Wrap(ctx, authResults.Error, op)
|
||||
}
|
||||
|
||||
// Get the billing information
|
||||
repo, err := s.repoFn()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
var startTime, endTime *time.Time
|
||||
if req.GetStartTime() != "" {
|
||||
st, err := time.Parse("2006-01", req.GetStartTime())
|
||||
if err != nil {
|
||||
return nil, errors.New(ctx, errors.InvalidTimeStamp, op, "start time is in an invalid format")
|
||||
}
|
||||
startTime = &st
|
||||
}
|
||||
if req.GetEndTime() != "" {
|
||||
et, err := time.Parse("2006-01", req.GetEndTime())
|
||||
if err != nil {
|
||||
return nil, errors.New(ctx, errors.InvalidTimeStamp, op, "end time is in an invalid format")
|
||||
}
|
||||
endTime = &et
|
||||
}
|
||||
|
||||
months, err := repo.MonthlyActiveUsers(
|
||||
ctx,
|
||||
billing.WithStartTime(startTime),
|
||||
billing.WithEndTime(endTime),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
var activeUsers []*pb.ActiveUsers
|
||||
for _, month := range months {
|
||||
maud := &pb.ActiveUsers{
|
||||
StartTime: timestamppb.New(month.StartTime),
|
||||
EndTime: timestamppb.New(month.EndTime),
|
||||
Count: month.ActiveUsersCount,
|
||||
}
|
||||
activeUsers = append(activeUsers, maud)
|
||||
}
|
||||
|
||||
return &pbs.MonthlyActiveUsersResponse{Items: activeUsers}, nil
|
||||
}
|
||||
|
||||
func (s Service) authResult(ctx context.Context, a action.Type) auth.VerifyResults {
|
||||
opts := []auth.Option{auth.WithType(resource.Billing), auth.WithAction(a)}
|
||||
return auth.Verify(ctx, opts...)
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package billing_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
"github.com/hashicorp/boundary/globals"
|
||||
"github.com/hashicorp/boundary/internal/billing"
|
||||
"github.com/hashicorp/boundary/internal/daemon/controller/auth"
|
||||
billingservice "github.com/hashicorp/boundary/internal/daemon/controller/handlers/billing"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
|
||||
"github.com/hashicorp/boundary/internal/iam"
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/billing"
|
||||
"google.golang.org/protobuf/testing/protocmp"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_MonthlyActiveUsers(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
|
||||
repoFn := func() (*billing.Repository, error) {
|
||||
return billing.TestRepo(t, conn), nil
|
||||
}
|
||||
billing.TestGenerateActiveUsers(t, conn)
|
||||
|
||||
wrap := db.TestWrapper(t)
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iam.TestRepo(t, conn, wrap), nil
|
||||
}
|
||||
|
||||
today := time.Now().UTC()
|
||||
threeMonthsAgo := time.Date(today.AddDate(0, -3, 0).Year(), today.AddDate(0, -3, 0).Month(), 1, 0, 0, 0, 0, time.UTC).Format("2006-01")
|
||||
oneMonthAgo := time.Date(today.AddDate(0, -1, 0).Year(), today.AddDate(0, -1, 0).Month(), 1, 0, 0, 0, 0, time.UTC).Format("2006-01")
|
||||
badFormat := time.Date(today.Year(), today.Month(), 15, 0, 0, 0, 0, time.UTC).String()
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
req *pbs.MonthlyActiveUsersRequest
|
||||
res *pbs.MonthlyActiveUsersResponse
|
||||
errContains string
|
||||
}{
|
||||
{
|
||||
name: "Valid no options, current and previous months",
|
||||
req: &pbs.MonthlyActiveUsersRequest{},
|
||||
res: &pbs.MonthlyActiveUsersResponse{
|
||||
Items: []*pb.ActiveUsers{
|
||||
{
|
||||
Count: 0,
|
||||
StartTime: timestamppb.New(time.Date(today.Year(), today.Month(), 1, 0, 0, 0, 0, time.UTC)),
|
||||
EndTime: timestamppb.New(time.Date(today.Year(), today.Month(), today.Day(), today.Hour(), 0, 0, 0, time.UTC)),
|
||||
},
|
||||
{
|
||||
Count: 6,
|
||||
StartTime: timestamppb.New(time.Date(today.Year(), today.Month()-1, 1, 0, 0, 0, 0, time.UTC)),
|
||||
EndTime: timestamppb.New(time.Date(today.Year(), today.Month(), 1, 0, 0, 0, 0, time.UTC)),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Valid start time",
|
||||
req: &pbs.MonthlyActiveUsersRequest{StartTime: threeMonthsAgo},
|
||||
res: &pbs.MonthlyActiveUsersResponse{
|
||||
Items: []*pb.ActiveUsers{
|
||||
{
|
||||
Count: 0,
|
||||
StartTime: timestamppb.New(time.Date(today.Year(), today.Month(), 1, 0, 0, 0, 0, time.UTC)),
|
||||
EndTime: timestamppb.New(time.Date(today.Year(), today.Month(), today.Day(), today.Hour(), 0, 0, 0, time.UTC)),
|
||||
},
|
||||
{
|
||||
Count: 6,
|
||||
StartTime: timestamppb.New(time.Date(today.Year(), today.Month()-1, 1, 0, 0, 0, 0, time.UTC)),
|
||||
EndTime: timestamppb.New(time.Date(today.Year(), today.Month(), 1, 0, 0, 0, 0, time.UTC)),
|
||||
},
|
||||
{
|
||||
Count: 6,
|
||||
StartTime: timestamppb.New(time.Date(today.Year(), today.Month()-2, 1, 0, 0, 0, 0, time.UTC)),
|
||||
EndTime: timestamppb.New(time.Date(today.Year(), today.Month()-1, 1, 0, 0, 0, 0, time.UTC)),
|
||||
},
|
||||
{
|
||||
Count: 6,
|
||||
StartTime: timestamppb.New(time.Date(today.Year(), today.Month()-3, 1, 0, 0, 0, 0, time.UTC)),
|
||||
EndTime: timestamppb.New(time.Date(today.Year(), today.Month()-2, 1, 0, 0, 0, 0, time.UTC)),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Valid start and end time",
|
||||
req: &pbs.MonthlyActiveUsersRequest{StartTime: threeMonthsAgo, EndTime: oneMonthAgo},
|
||||
res: &pbs.MonthlyActiveUsersResponse{
|
||||
Items: []*pb.ActiveUsers{
|
||||
{
|
||||
Count: 6,
|
||||
StartTime: timestamppb.New(time.Date(today.Year(), today.Month()-3, 1, 0, 0, 0, 0, time.UTC)),
|
||||
EndTime: timestamppb.New(time.Date(today.Year(), today.Month()-2, 1, 0, 0, 0, 0, time.UTC)),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Invalid end time without start time",
|
||||
req: &pbs.MonthlyActiveUsersRequest{EndTime: oneMonthAgo},
|
||||
errContains: "end time set without start time",
|
||||
},
|
||||
{
|
||||
name: "Invalid end time before start time",
|
||||
req: &pbs.MonthlyActiveUsersRequest{StartTime: oneMonthAgo, EndTime: threeMonthsAgo},
|
||||
errContains: "start time is not before end time",
|
||||
},
|
||||
{
|
||||
name: "Invalid start time equals end time",
|
||||
req: &pbs.MonthlyActiveUsersRequest{StartTime: threeMonthsAgo, EndTime: threeMonthsAgo},
|
||||
errContains: "start time is not before end time",
|
||||
},
|
||||
{
|
||||
name: "Invalid start time format",
|
||||
req: &pbs.MonthlyActiveUsersRequest{StartTime: badFormat},
|
||||
errContains: "start time is in an invalid format",
|
||||
},
|
||||
{
|
||||
name: "Invalid end time format",
|
||||
req: &pbs.MonthlyActiveUsersRequest{StartTime: threeMonthsAgo, EndTime: badFormat},
|
||||
errContains: "end time is in an invalid format",
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
b, err := billingservice.NewService(ctx, repoFn)
|
||||
require.NoError(t, err, "Couldn't create new billing service.")
|
||||
|
||||
got, gErr := b.MonthlyActiveUsers(auth.DisabledAuthTestContext(iamRepoFn, scope.Global.String(), auth.WithUserId(globals.AnyAuthenticatedUserId)), tc.req)
|
||||
if tc.errContains != "" {
|
||||
require.ErrorContains(t, gErr, tc.errContains)
|
||||
require.Nil(t, got)
|
||||
return
|
||||
} else {
|
||||
require.NoError(t, gErr)
|
||||
}
|
||||
assert.Empty(t,
|
||||
cmp.Diff(
|
||||
got,
|
||||
tc.res,
|
||||
protocmp.Transform(),
|
||||
protocmp.SortRepeatedFields(got),
|
||||
cmpopts.SortSlices(func(a, b string) bool {
|
||||
return a < b
|
||||
}),
|
||||
))
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user