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

Commit

Permalink
feat/enterpriseportal: subscriptions read APIs use enterprise portal DB
Browse files Browse the repository at this point in the history
  • Loading branch information
bobheadxi committed Jul 23, 2024
1 parent f63679f commit d279b4e
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 347 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestCodyGatewayStore(t *testing.T) {
} {
l, err := subscriptions.NewLicensesStore(db).CreateLicenseKey(ctx,
subscriptionID,
&subscriptions.LicenseKey{
&subscriptions.DataLicenseKey{
Info: license.Info{
// Set properties that are easy to assert on later
Tags: []string{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func (i *Importer) importLicense(ctx context.Context, subscriptionID string, dot
tr.SetAttributes(attribute.Bool("already_imported", false))

if _, err := i.licenses.CreateLicenseKey(ctx, subscriptionID,
&subscriptions.LicenseKey{
&subscriptions.DataLicenseKey{
Info: license.Info{
Tags: dotcomLicense.Tags,
UserCount: uint(pointers.DerefZero(dotcomLicense.UserCount)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,9 @@ func (c CreateLicenseOpts) getLicenseID() (string, error) {
return licenseID.String(), nil
}

// LicenseKey corresponds to *subscriptionsv1.EnterpriseSubscriptionLicenseKey
// DataLicenseKey corresponds to *subscriptionsv1.EnterpriseSubscriptionLicenseKey
// and the 'ENTERPRISE_SUBSCRIPTION_LICENSE_TYPE_KEY' license type.
type LicenseKey struct {
type DataLicenseKey struct {
Info internallicense.Info
// Signed license key with the license information in Info.
SignedKey string
Expand All @@ -259,7 +259,7 @@ type LicenseKey struct {
func (s *LicensesStore) CreateLicenseKey(
ctx context.Context,
subscriptionID string,
license *LicenseKey,
license *DataLicenseKey,
opts CreateLicenseOpts,
) (_ *LicenseWithConditions, err error) {
// Special behaviour: the license key embeds the creation time, and it must
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestLicensesStore(t *testing.T) {
}

got, err := licenses.CreateLicenseKey(ctx, subscriptionID1,
&subscriptions.LicenseKey{
&subscriptions.DataLicenseKey{
Info: license.Info{
Tags: []string{"foo"},
CreatedAt: time.Time{}.Add(1 * time.Hour),
Expand All @@ -94,7 +94,7 @@ func TestLicensesStore(t *testing.T) {
createdLicenses = append(createdLicenses, got)

got, err = licenses.CreateLicenseKey(ctx, subscriptionID1,
&subscriptions.LicenseKey{
&subscriptions.DataLicenseKey{
Info: license.Info{
Tags: []string{"baz"},
CreatedAt: time.Time{}.Add(24 * time.Hour),
Expand All @@ -116,7 +116,7 @@ func TestLicensesStore(t *testing.T) {
createdLicenses = append(createdLicenses, got)

got, err = licenses.CreateLicenseKey(ctx, subscriptionID2,
&subscriptions.LicenseKey{
&subscriptions.DataLicenseKey{
Info: license.Info{
Tags: []string{"tag"},
CreatedAt: time.Time{}.Add(24 * time.Hour),
Expand All @@ -139,7 +139,7 @@ func TestLicensesStore(t *testing.T) {

t.Run("createdAt does not match", func(t *testing.T) {
_, err = licenses.CreateLicenseKey(ctx, subscriptionID2,
&subscriptions.LicenseKey{
&subscriptions.DataLicenseKey{
Info: license.Info{
Tags: []string{"tag"},
CreatedAt: time.Time{}.Add(24 * time.Hour),
Expand All @@ -155,7 +155,7 @@ func TestLicensesStore(t *testing.T) {
})
t.Run("expiresAt does not match", func(t *testing.T) {
_, err = licenses.CreateLicenseKey(ctx, subscriptionID2,
&subscriptions.LicenseKey{
&subscriptions.DataLicenseKey{
Info: license.Info{
Tags: []string{"tag"},
CreatedAt: time.Time{},
Expand Down
3 changes: 1 addition & 2 deletions cmd/enterprise-portal/internal/dotcomdb/dotcomdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import (

"github.com/sourcegraph/log/logtest"

"github.com/sourcegraph/sourcegraph-accounts-sdk-go/scopes"

sams "github.com/sourcegraph/sourcegraph-accounts-sdk-go"
"github.com/sourcegraph/sourcegraph-accounts-sdk-go/scopes"

"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/codyaccessservice"
"github.com/sourcegraph/sourcegraph/cmd/enterprise-portal/internal/database"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ go_test(
embed = [":subscriptionsservice"],
deps = [
"//cmd/enterprise-portal/internal/database/subscriptions",
"//cmd/enterprise-portal/internal/dotcomdb",
"//cmd/enterprise-portal/internal/samsm2m",
"//lib/enterpriseportal/subscriptions/v1:subscriptions",
"//lib/managedservicesplatform/iam",
Expand Down
101 changes: 57 additions & 44 deletions cmd/enterprise-portal/internal/subscriptionsservice/adapters.go
Original file line number Diff line number Diff line change
@@ -1,75 +1,88 @@
package subscriptionsservice

import (
"encoding/json"

"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/dotcomdb"
subscriptionsv1 "github.com/sourcegraph/sourcegraph/lib/enterpriseportal/subscriptions/v1"
"github.com/sourcegraph/sourcegraph/lib/errors"
"github.com/sourcegraph/sourcegraph/lib/managedservicesplatform/iam"
"github.com/sourcegraph/sourcegraph/lib/pointers"
)

func convertLicenseAttrsToProto(attrs *dotcomdb.LicenseAttributes) *subscriptionsv1.EnterpriseSubscriptionLicense {
conds := []*subscriptionsv1.EnterpriseSubscriptionLicenseCondition{
{
Status: subscriptionsv1.EnterpriseSubscriptionLicenseCondition_STATUS_CREATED,
LastTransitionTime: timestamppb.New(attrs.CreatedAt),
},
}
if attrs.RevokedAt != nil {
conds = append(conds, &subscriptionsv1.EnterpriseSubscriptionLicenseCondition{
Status: subscriptionsv1.EnterpriseSubscriptionLicenseCondition_STATUS_REVOKED,
LastTransitionTime: timestamppb.New(*attrs.RevokedAt),
Message: pointers.DerefZero(attrs.RevokeReason),
})
func convertLicenseToProto(license *subscriptions.LicenseWithConditions) (*subscriptionsv1.EnterpriseSubscriptionLicense, error) {
conds := make([]*subscriptionsv1.EnterpriseSubscriptionLicenseCondition, len(license.Conditions))
for i, c := range license.Conditions {
conds[i] = &subscriptionsv1.EnterpriseSubscriptionLicenseCondition{
LastTransitionTime: timestamppb.New(c.TransitionTime.AsTime()),
Status: subscriptionsv1.EnterpriseSubscriptionLicenseCondition_Status(
subscriptionsv1.EnterpriseSubscriptionLicenseCondition_Status_value[c.Status],
),
Message: pointers.DerefZero(c.Message),
}
}

return &subscriptionsv1.EnterpriseSubscriptionLicense{
Id: subscriptionsv1.EnterpriseSubscriptionLicenseIDPrefix + attrs.ID,
SubscriptionId: subscriptionsv1.EnterpriseSubscriptionIDPrefix + attrs.SubscriptionID,
proto := &subscriptionsv1.EnterpriseSubscriptionLicense{
Id: subscriptionsv1.EnterpriseSubscriptionLicenseIDPrefix + license.ID,
SubscriptionId: subscriptionsv1.EnterpriseSubscriptionIDPrefix + license.SubscriptionID,
Conditions: conds,
License: &subscriptionsv1.EnterpriseSubscriptionLicense_Key{
}

switch t := license.LicenseType; t {
case subscriptionsv1.EnterpriseSubscriptionLicenseType_ENTERPRISE_SUBSCRIPTION_LICENSE_TYPE_KEY.String():
var data subscriptions.DataLicenseKey
if err := json.Unmarshal(license.LicenseData, &data); err != nil {
return proto, errors.Wrap(err, "unmarshal license data")
}
proto.License = &subscriptionsv1.EnterpriseSubscriptionLicense_Key{
Key: &subscriptionsv1.EnterpriseSubscriptionLicenseKey{
InfoVersion: pointers.DerefZero(attrs.InfoVersion),
InfoVersion: uint32(data.Info.Version()),
Info: &subscriptionsv1.EnterpriseSubscriptionLicenseKey_Info{
Tags: attrs.Tags,
UserCount: pointers.DerefZero(attrs.UserCount),
ExpireTime: timestamppb.New(*attrs.ExpiresAt),
SalesforceSubscriptionId: pointers.DerefZero(attrs.SalesforceSubscriptionID),
SalesforceOpportunityId: pointers.DerefZero(attrs.SalesforceOpportunityID),
Tags: data.Info.Tags,
UserCount: uint64(data.Info.UserCount),
ExpireTime: timestamppb.New(data.Info.ExpiresAt),
SalesforceSubscriptionId: pointers.DerefZero(data.Info.SalesforceSubscriptionID),
SalesforceOpportunityId: pointers.DerefZero(data.Info.SalesforceOpportunityID),
},
LicenseKey: attrs.LicenseKey,
InstanceId: pointers.DerefZero(attrs.InstanceID),
LicenseKey: data.SignedKey,
InstanceId: "", // TODO
},
},
}
default:
return proto, errors.Newf("unknown license type %q", t)
}

return proto, nil
}

func convertSubscriptionToProto(subscription *subscriptions.Subscription, attrs *dotcomdb.SubscriptionAttributes) *subscriptionsv1.EnterpriseSubscription {
// Dotcom equivalent missing is surprising, but let's not panic just yet
if attrs == nil {
attrs = &dotcomdb.SubscriptionAttributes{
ID: subscription.ID,
func convertSubscriptionToProto(subscription *subscriptions.SubscriptionWithConditions) *subscriptionsv1.EnterpriseSubscription {
conds := make([]*subscriptionsv1.EnterpriseSubscriptionCondition, len(subscription.Conditions))
for i, c := range subscription.Conditions {
conds[i] = &subscriptionsv1.EnterpriseSubscriptionCondition{
LastTransitionTime: timestamppb.New(c.TransitionTime.AsTime()),
Status: subscriptionsv1.EnterpriseSubscriptionCondition_Status(
subscriptionsv1.EnterpriseSubscriptionCondition_Status_value[c.Status],
),
Message: pointers.DerefZero(c.Message),
}
}
conds := []*subscriptionsv1.EnterpriseSubscriptionCondition{
{
Status: subscriptionsv1.EnterpriseSubscriptionCondition_STATUS_CREATED,
LastTransitionTime: timestamppb.New(attrs.CreatedAt),
},
}
if attrs.ArchivedAt != nil {
conds = append(conds, &subscriptionsv1.EnterpriseSubscriptionCondition{
Status: subscriptionsv1.EnterpriseSubscriptionCondition_STATUS_ARCHIVED,
LastTransitionTime: timestamppb.New(*attrs.ArchivedAt),
})

var sf *subscriptionsv1.EnterpriseSubscription_SalesforceMetadata
if subscription.SalesforceSubscriptionID != nil || subscription.SalesforceOpportunityID != nil {
sf = &subscriptionsv1.EnterpriseSubscription_SalesforceMetadata{
SubscriptionId: pointers.DerefZero(subscription.SalesforceSubscriptionID),
OpportunityId: pointers.DerefZero(subscription.SalesforceOpportunityID),
}
}

return &subscriptionsv1.EnterpriseSubscription{
Id: subscriptionsv1.EnterpriseSubscriptionIDPrefix + attrs.ID,
Id: subscriptionsv1.EnterpriseSubscriptionIDPrefix + subscription.ID,
Conditions: conds,
InstanceDomain: pointers.DerefZero(subscription.InstanceDomain),
DisplayName: pointers.DerefZero(subscription.DisplayName),
Salesforce: sf,
}
}

Expand Down
Loading

0 comments on commit d279b4e

Please sign in to comment.