Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
feat/enterpriseportal: all subscriptions APIs use enterprise portal DB
Browse files Browse the repository at this point in the history
  • Loading branch information
bobheadxi committed Jul 19, 2024
1 parent 16bc317 commit 23df245
Show file tree
Hide file tree
Showing 22 changed files with 1,130 additions and 293 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ go_test(
"//cmd/enterprise-portal/internal/database",
"//cmd/enterprise-portal/internal/database/databasetest",
"//cmd/enterprise-portal/internal/database/internal/tables",
"//cmd/enterprise-portal/internal/database/internal/utctime",
"//cmd/enterprise-portal/internal/database/subscriptions",
"//cmd/enterprise-portal/internal/database/utctime",
"//internal/license",
"//lib/pointers",
"@com_github_google_uuid//:uuid",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/codyaccess"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/databasetest"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/internal/tables"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/internal/utctime"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/subscriptions"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/utctime"
"github.com/sourcegraph/sourcegraph/internal/license"
"github.com/sourcegraph/sourcegraph/lib/pointers"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ go_library(
deps = [
"//cmd/enterprise-portal/internal/database",
"//cmd/enterprise-portal/internal/database/codyaccess",
"//cmd/enterprise-portal/internal/database/internal/utctime",
"//cmd/enterprise-portal/internal/database/subscriptions",
"//cmd/enterprise-portal/internal/database/utctime",
"//cmd/enterprise-portal/internal/dotcomdb",
"//internal/goroutine",
"//internal/license",
Expand Down
6 changes: 3 additions & 3 deletions cmd/enterprise-portal/internal/database/importer/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/codyaccess"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/internal/utctime"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/subscriptions"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/utctime"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/dotcomdb"
"github.com/sourcegraph/sourcegraph/internal/goroutine"
"github.com/sourcegraph/sourcegraph/internal/license"
Expand Down Expand Up @@ -198,8 +198,8 @@ func (i *Importer) importSubscription(ctx context.Context, dotcomSub *dotcomdb.S
}
return pointers.Ptr(utctime.FromTime(*dotcomSub.ArchivedAt))
}(),
SalesforceSubscriptionID: activeLicense.SalesforceSubscriptionID,
SalesforceOpportunityID: activeLicense.SalesforceOpportunityID,
SalesforceSubscriptionID: pointers.Ptr(database.NewNullStringPtr(activeLicense.SalesforceSubscriptionID)),
SalesforceOpportunityID: pointers.Ptr(database.NewNullStringPtr(activeLicense.SalesforceOpportunityID)),
},
conditions...,
); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ go_library(
deps = [
"//cmd/enterprise-portal/internal/database/internal/pgxerrors",
"//cmd/enterprise-portal/internal/database/internal/upsert",
"//cmd/enterprise-portal/internal/database/internal/utctime",
"//cmd/enterprise-portal/internal/database/utctime",
"//internal/license",
"//lib/enterpriseportal/subscriptions/v1:subscriptions",
"//lib/errors",
Expand All @@ -42,7 +42,7 @@ go_test(
"//cmd/enterprise-portal/internal/database",
"//cmd/enterprise-portal/internal/database/databasetest",
"//cmd/enterprise-portal/internal/database/internal/tables",
"//cmd/enterprise-portal/internal/database/internal/utctime",
"//cmd/enterprise-portal/internal/database/utctime",
"//internal/license",
"//lib/enterpriseportal/subscriptions/v1:subscriptions",
"//lib/pointers",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/jackc/pgx/v5"
"gorm.io/gorm"

"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/internal/utctime"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/utctime"
subscriptionsv1 "github.com/sourcegraph/sourcegraph/lib/enterpriseportal/subscriptions/v1"
"github.com/sourcegraph/sourcegraph/lib/errors"
"github.com/sourcegraph/sourcegraph/lib/pointers"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"gorm.io/gorm"

"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/internal/pgxerrors"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/internal/utctime"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/utctime"
internallicense "github.com/sourcegraph/sourcegraph/internal/license"
subscriptionsv1 "github.com/sourcegraph/sourcegraph/lib/enterpriseportal/subscriptions/v1"
"github.com/sourcegraph/sourcegraph/lib/pointers"
Expand Down Expand Up @@ -399,6 +399,9 @@ WHERE id = @licenseID
"revokedAt": opts.Time,
"licenseID": licenseID,
}); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrSubscriptionLicenseNotFound
}
return nil, errors.Wrap(err, "revoke license")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/databasetest"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/internal/tables"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/internal/utctime"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/subscriptions"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/utctime"
"github.com/sourcegraph/sourcegraph/internal/license"
"github.com/sourcegraph/sourcegraph/lib/pointers"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/internal/pgxerrors"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/internal/upsert"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/internal/utctime"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/utctime"
"github.com/sourcegraph/sourcegraph/lib/errors"
)

Expand Down Expand Up @@ -205,8 +205,8 @@ type UpsertSubscriptionOptions struct {
CreatedAt utctime.Time
ArchivedAt *utctime.Time

SalesforceSubscriptionID *string
SalesforceOpportunityID *string
SalesforceSubscriptionID *sql.NullString
SalesforceOpportunityID *sql.NullString

// ForceUpdate indicates whether to force update all fields of the subscription
// record.
Expand Down Expand Up @@ -235,9 +235,14 @@ func (opts UpsertSubscriptionOptions) apply(ctx context.Context, db upsert.Exece
return b.Exec(ctx, db)
}

var ErrInvalidArgument = errors.New("invalid argument")

// Upsert upserts a subscription record based on the given options. If the
// operation has additional application meaning, conditions can be provided
// for insert as well.
//
// Constraint errors are returned as a human-friendly error that wraps
// ErrInvalidArgument.
func (s *Store) Upsert(
ctx context.Context,
subscriptionID string,
Expand Down Expand Up @@ -267,7 +272,12 @@ func (s *Store) Upsert(
if err := opts.apply(ctx, tx, subscriptionID); err != nil {
if pgxerrors.IsContraintError(err, "idx_enterprise_portal_subscriptions_display_name") {
return nil, errors.WithSafeDetails(
errors.Newf("display_name %q is already in use", opts.DisplayName.String),
errors.Wrapf(ErrInvalidArgument, "display_name %q is already in use", opts.DisplayName.String),
"%+v", err)
}
if pgxerrors.IsContraintError(err, "idx_enterprise_portal_subscriptions_instance_domain") {
return nil, errors.WithSafeDetails(
errors.Wrapf(ErrInvalidArgument, "instance_domain %q is assigned to another subscription", opts.DisplayName.String),
"%+v", err)
}
return nil, errors.Wrap(err, "upsert")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/jackc/pgx/v5"
"gorm.io/gorm"

"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/internal/utctime"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/utctime"
subscriptionsv1 "github.com/sourcegraph/sourcegraph/lib/enterpriseportal/subscriptions/v1"
"github.com/sourcegraph/sourcegraph/lib/errors"
"github.com/sourcegraph/sourcegraph/lib/pointers"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/databasetest"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/internal/tables"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/internal/utctime"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/subscriptions"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/utctime"
subscriptionsv1 "github.com/sourcegraph/sourcegraph/lib/enterpriseportal/subscriptions/v1"
"github.com/sourcegraph/sourcegraph/lib/pointers"
)
Expand Down
10 changes: 10 additions & 0 deletions cmd/enterprise-portal/internal/database/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@ func NewNullString(v string) sql.NullString {
Valid: v != "",
}
}

func NewNullStringPtr(v *string) sql.NullString {
if v == nil {
return sql.NullString{}
}
return sql.NullString{
String: *v,
Valid: *v != "",
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "utctime",
srcs = ["utctime.go"],
importpath = "github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/internal/utctime",
importpath = "github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/utctime",
visibility = ["//cmd/enterprise-portal:__subpackages__"],
deps = [
"//lib/errors",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@ go_library(
"//cmd/enterprise-portal/internal/connectutil",
"//cmd/enterprise-portal/internal/database",
"//cmd/enterprise-portal/internal/database/subscriptions",
"//cmd/enterprise-portal/internal/database/utctime",
"//cmd/enterprise-portal/internal/dotcomdb",
"//cmd/enterprise-portal/internal/samsm2m",
"//internal/collections",
"//internal/license",
"//internal/trace",
"//lib/enterpriseportal/subscriptions/v1:subscriptions",
"//lib/enterpriseportal/subscriptions/v1/v1connect",
"//lib/errors",
"//lib/managedservicesplatform/iam",
"//lib/pointers",
"@com_connectrpc_connect//:connect",
"@com_github_google_uuid//:uuid",
"@com_github_sourcegraph_log//:log",
"@com_github_sourcegraph_sourcegraph_accounts_sdk_go//:sourcegraph-accounts-sdk-go",
"@com_github_sourcegraph_sourcegraph_accounts_sdk_go//clients/v1:clients",
Expand Down
52 changes: 51 additions & 1 deletion cmd/enterprise-portal/internal/subscriptionsservice/adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ package subscriptionsservice

import (
"encoding/json"
"fmt"
"strings"
"time"

"connectrpc.com/connect"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/subscriptions"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database/utctime"
"github.com/sourcegraph/sourcegraph/internal/license"
subscriptionsv1 "github.com/sourcegraph/sourcegraph/lib/enterpriseportal/subscriptions/v1"
"github.com/sourcegraph/sourcegraph/lib/errors"
"github.com/sourcegraph/sourcegraph/lib/managedservicesplatform/iam"
Expand Down Expand Up @@ -107,8 +113,52 @@ func convertProtoToIAMTupleRelation(action subscriptionsv1.PermissionRelation) i
func convertProtoRoleToIAMTupleObject(role subscriptionsv1.Role, subscriptionID string) iam.TupleObject {
switch role {
case subscriptionsv1.Role_ROLE_SUBSCRIPTION_CUSTOMER_ADMIN:
return iam.ToTupleObject(iam.TupleTypeCustomerAdmin, subscriptionID)
return iam.ToTupleObject(iam.TupleTypeCustomerAdmin,
strings.TrimPrefix(subscriptionID, subscriptionsv1.EnterpriseSubscriptionIDPrefix))
default:
return ""
}
}

// convertLicenseKeyToLicenseKeyData converts a create-license request into an
// actual license key. It only returns valid Connect errors.
func convertLicenseKeyToLicenseKeyData(
createdAt utctime.Time,
sub *subscriptions.Subscription,
key *subscriptionsv1.EnterpriseSubscriptionLicenseKey,
) (*subscriptions.DataLicenseKey, error) {
expires := key.GetInfo().GetExpireTime().AsTime()
if expires.Before(time.Now()) {
return nil, connect.NewError(connect.CodeInvalidArgument, errors.New("expiry must be in the future"))
}
tags := key.GetInfo().GetTags()
providedTagPrefixes := map[string]struct{}{}
for _, t := range tags {
providedTagPrefixes[strings.SplitN(t, ":", 2)[0]] = struct{}{}
}
if _, exists := providedTagPrefixes["customer"]; !exists && sub.DisplayName != nil {
tags = append(tags, fmt.Sprintf("customer:%s", *sub.DisplayName))
}

info := license.Info{
Tags: tags,
UserCount: uint(key.GetInfo().GetUserCount()),
CreatedAt: createdAt.AsTime(),
ExpiresAt: expires.UTC(),

// Inherited from subscription
SalesforceSubscriptionID: sub.SalesforceSubscriptionID,
SalesforceOpportunityID: sub.SalesforceOpportunityID,
}

// TODO
// signedKey, _, err := licensing.GenerateProductLicenseKey(info)
// if err != nil {
// return nil, connectutil.InternalError(ctx, logger, err, "")
// }

return &subscriptions.DataLicenseKey{
Info: info,
SignedKey: "TODO",
}, nil
}
Loading

0 comments on commit 23df245

Please sign in to comment.