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 7e86a43 commit fa7d5bc
Show file tree
Hide file tree
Showing 22 changed files with 1,092 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package subscriptionsservice

import (
"encoding/json"
"strings"

"google.golang.org/protobuf/types/known/timestamppb"

Expand Down Expand Up @@ -107,7 +108,8 @@ 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 ""
}
Expand Down
Loading

0 comments on commit fa7d5bc

Please sign in to comment.