Skip to content

Commit

Permalink
Adjust licensing based on vendor bump
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Hiltgen <[email protected]>
  • Loading branch information
Daniel Hiltgen committed Aug 17, 2018
1 parent 5bcb5a9 commit 01cfcf0
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 100 deletions.
74 changes: 31 additions & 43 deletions cli/command/formatter/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,22 @@ import (
)

const (
defaultSubscriptionsTableFormat = "table {{.Num}}\t{{.Owner}}\t{{.ProductID}}\t{{.CurrentPeriodEnd}}\t{{.ComponentsString}}"
defaultSubscriptionsTableFormat = "table {{.Num}}\t{{.Owner}}\t{{.ProductID}}\t{{.Expires}}\t{{.ComponentsString}}"
defaultSubscriptionsQuietFormat = "{{.Num}}:{{.Summary}}"

numHeader = "NUM"
ownerHeader = "OWNER"
licenseNameHeader = "NAME"
idHeader = "ID"
dockerIDHeader = "DOCKER ID"
productIDHeader = "PRODUCT ID"
createdByIDHeader = "CREATED BY ID"
productRatePlanHeader = "PRODUCT RATE PLAN"
productRatePlanIDHeader = "PRODUCT RATE PLAN ID"
initialPeriodStartHeader = "INITIAL PERIOD START"
currentPeriodStartHeader = "CURRENT PERIOD START"
currentPeriodEndHeader = "CURRENT PERIOD END"
stateHeader = "STATE"
eusaHeader = "EUSA"
pricingComponentsHeader = "PRICING COMPONENTS"
numHeader = "NUM"
ownerHeader = "OWNER"
licenseNameHeader = "NAME"
idHeader = "ID"
dockerIDHeader = "DOCKER ID"
productIDHeader = "PRODUCT ID"
productRatePlanHeader = "PRODUCT RATE PLAN"
productRatePlanIDHeader = "PRODUCT RATE PLAN ID"
startHeader = "START"
expiresHeader = "EXPIRES"
stateHeader = "STATE"
eusaHeader = "EUSA"
pricingComponentsHeader = "PRICING COMPONENTS"
)

// NewSubscriptionsFormat returns a Format for rendering using a license Context
Expand Down Expand Up @@ -58,21 +56,19 @@ func SubscriptionsWrite(ctx Context, subs []licenseutils.LicenseDisplay) error {
}
licenseCtx := licenseContext{}
licenseCtx.header = map[string]string{
"Num": numHeader,
"Owner": ownerHeader,
"Name": licenseNameHeader,
"ID": idHeader,
"DockerID": dockerIDHeader,
"ProductID": productIDHeader,
"CreatedByID": createdByIDHeader,
"ProductRatePlan": productRatePlanHeader,
"ProductRatePlanID": productRatePlanIDHeader,
"InitialPeriodStart": initialPeriodStartHeader,
"CurrentPeriodStart": currentPeriodStartHeader,
"CurrentPeriodEnd": currentPeriodEndHeader,
"State": stateHeader,
"Eusa": eusaHeader,
"ComponentsString": pricingComponentsHeader,
"Num": numHeader,
"Owner": ownerHeader,
"Name": licenseNameHeader,
"ID": idHeader,
"DockerID": dockerIDHeader,
"ProductID": productIDHeader,
"ProductRatePlan": productRatePlanHeader,
"ProductRatePlanID": productRatePlanIDHeader,
"Start": startHeader,
"Expires": expiresHeader,
"State": stateHeader,
"Eusa": eusaHeader,
"ComponentsString": pricingComponentsHeader,
}
return ctx.Write(&licenseCtx, render)
}
Expand Down Expand Up @@ -119,10 +115,6 @@ func (c *licenseContext) ProductID() string {
return c.l.ProductID
}

func (c *licenseContext) CreatedByID() string {
return c.l.CreatedByID
}

func (c *licenseContext) ProductRatePlan() string {
return c.l.ProductRatePlan
}
Expand All @@ -131,16 +123,12 @@ func (c *licenseContext) ProductRatePlanID() string {
return c.l.ProductRatePlanID
}

func (c *licenseContext) InitialPeriodStart() time.Time {
return c.l.InitialPeriodStart
}

func (c *licenseContext) CurrentPeriodStart() time.Time {
return c.l.CurrentPeriodStart
func (c *licenseContext) Start() *time.Time {
return c.l.Start
}

func (c *licenseContext) CurrentPeriodEnd() *time.Time {
return c.l.CurrentPeriodEnd
func (c *licenseContext) Expires() *time.Time {
return c.l.Expires
}

func (c *licenseContext) State() string {
Expand Down
82 changes: 39 additions & 43 deletions cli/command/formatter/licenses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ func TestSubscriptionContextWrite(t *testing.T) {
// Table format
{
Context{Format: NewSubscriptionsFormat("table", false)},
`NUM OWNER PRODUCT ID CURRENT PERIOD END PRICING COMPONENTS
`NUM OWNER PRODUCT ID EXPIRES PRICING COMPONENTS
1 owner1 productid1 2020-01-01 10:00:00 +0000 UTC compstring
2 owner2 productid2 2020-01-01 10:00:00 +0000 UTC compstring
`,
},
{
Context{Format: NewSubscriptionsFormat("table", true)},
`1:License Name: name1 Quantity: 10 nodes -
2:License Name: name2 Quantity: 20 nodes -
`1:License Name: name1 Quantity: 10 nodes Expiration date: 2020-01-01
2:License Name: name2 Quantity: 20 nodes Expiration date: 2020-01-01
`,
},
{
Expand Down Expand Up @@ -95,10 +95,10 @@ owner2
Num: 1,
Owner: "owner1",
Subscription: model.Subscription{
ID: "id1",
Name: "name1",
ProductID: "productid1",
CurrentPeriodEnd: &expiration,
ID: "id1",
Name: "name1",
ProductID: "productid1",
Expires: &expiration,
PricingComponents: model.PricingComponents{
&model.SubscriptionPricingComponent{
Name: "nodes",
Expand All @@ -112,10 +112,10 @@ owner2
Num: 2,
Owner: "owner2",
Subscription: model.Subscription{
ID: "id2",
Name: "name2",
ProductID: "productid2",
CurrentPeriodEnd: &expiration,
ID: "id2",
Name: "name2",
ProductID: "productid2",
Expires: &expiration,
PricingComponents: model.PricingComponents{
&model.SubscriptionPricingComponent{
Name: "nodes",
Expand Down Expand Up @@ -144,10 +144,10 @@ func TestSubscriptionContextWriteJSON(t *testing.T) {
Num: 1,
Owner: "owner1",
Subscription: model.Subscription{
ID: "id1",
Name: "name1",
ProductID: "productid1",
CurrentPeriodEnd: &expiration,
ID: "id1",
Name: "name1",
ProductID: "productid1",
Expires: &expiration,
PricingComponents: model.PricingComponents{
&model.SubscriptionPricingComponent{
Name: "nodes",
Expand All @@ -161,10 +161,10 @@ func TestSubscriptionContextWriteJSON(t *testing.T) {
Num: 2,
Owner: "owner2",
Subscription: model.Subscription{
ID: "id2",
Name: "name2",
ProductID: "productid2",
CurrentPeriodEnd: &expiration,
ID: "id2",
Name: "name2",
ProductID: "productid2",
Expires: &expiration,
PricingComponents: model.PricingComponents{
&model.SubscriptionPricingComponent{
Name: "nodes",
Expand All @@ -177,17 +177,15 @@ func TestSubscriptionContextWriteJSON(t *testing.T) {
}
expectedJSONs := []map[string]interface{}{
{
"Owner": "owner1",
"ComponentsString": "compstring",
"CreatedByID": "",
"CurrentPeriodEnd": "2020-01-01T10:00:00Z",
"CurrentPeriodStart": "0001-01-01T00:00:00Z",
"DockerID": "",
"Eusa": nil,
"ID": "id1",
"InitialPeriodStart": "0001-01-01T00:00:00Z",
"Name": "name1",
"Num": float64(1),
"Owner": "owner1",
"ComponentsString": "compstring",
"Expires": "2020-01-01T10:00:00Z",
"DockerID": "",
"Eusa": nil,
"ID": "id1",
"Start": nil,
"Name": "name1",
"Num": float64(1),
"PricingComponents": []interface{}{
map[string]interface{}{
"name": "nodes",
Expand All @@ -198,20 +196,18 @@ func TestSubscriptionContextWriteJSON(t *testing.T) {
"ProductRatePlan": "",
"ProductRatePlanID": "",
"State": "",
"Summary": "License Name: name1\tQuantity: 10 nodes\t - ",
"Summary": "License Name: name1\tQuantity: 10 nodes\tExpiration date: 2020-01-01",
},
{
"Owner": "owner2",
"ComponentsString": "compstring",
"CreatedByID": "",
"CurrentPeriodEnd": "2020-01-01T10:00:00Z",
"CurrentPeriodStart": "0001-01-01T00:00:00Z",
"DockerID": "",
"Eusa": nil,
"ID": "id2",
"InitialPeriodStart": "0001-01-01T00:00:00Z",
"Name": "name2",
"Num": float64(2),
"Owner": "owner2",
"ComponentsString": "compstring",
"Expires": "2020-01-01T10:00:00Z",
"DockerID": "",
"Eusa": nil,
"ID": "id2",
"Start": nil,
"Name": "name2",
"Num": float64(2),
"PricingComponents": []interface{}{
map[string]interface{}{
"name": "nodes",
Expand All @@ -222,7 +218,7 @@ func TestSubscriptionContextWriteJSON(t *testing.T) {
"ProductRatePlan": "",
"ProductRatePlanID": "",
"State": "",
"Summary": "License Name: name2\tQuantity: 20 nodes\t - ",
"Summary": "License Name: name2\tQuantity: 20 nodes\tExpiration date: 2020-01-01",
},
}

Expand Down
10 changes: 10 additions & 0 deletions internal/licenseutils/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type (
downloadLicenseFromHubFunc func(ctx context.Context, authToken, subscriptionID string) (license *model.IssuedLicense, err error)
parseLicenseFunc func(license []byte) (parsedLicense *model.IssuedLicense, err error)
storeLicenseFunc func(ctx context.Context, dclnt licensing.WrappedDockerClient, licenses *model.IssuedLicense, localRootDir string) error
loadLocalLicenseFunc func(ctx context.Context, dclnt licensing.WrappedDockerClient) (*model.Subscription, error)
}
)

Expand Down Expand Up @@ -92,3 +93,12 @@ func (c *fakeLicensingClient) StoreLicense(ctx context.Context, dclnt licensing.
}
return nil
}

func (c *fakeLicensingClient) LoadLocalLicense(ctx context.Context, dclnt licensing.WrappedDockerClient) (*model.Subscription, error) {

if c.loadLocalLicenseFunc != nil {
return c.loadLocalLicenseFunc(ctx, dclnt)

}
return nil, nil
}
5 changes: 2 additions & 3 deletions internal/licenseutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"

"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/licensing"
"github.com/docker/licensing/model"
"github.com/pkg/errors"
Expand Down Expand Up @@ -98,7 +97,7 @@ func (u HubUser) GetAvailableLicenses(ctx context.Context) ([]LicenseDisplay, er
// Filter out expired licenses
i := 0
for _, s := range subs {
if s.State != "expired" && s.CurrentPeriodEnd != nil {
if s.State != "expired" && s.Expires != nil {
owner := ""
if s.DockerID == u.User.ID {
owner = u.User.Username
Expand Down Expand Up @@ -181,7 +180,7 @@ func doLoadLocalIssuedLicense(ctx context.Context, filename string, lclient lice
}

// ApplyLicense will store a license on the local system
func ApplyLicense(ctx context.Context, dclient client.APIClient, license *model.IssuedLicense) error {
func ApplyLicense(ctx context.Context, dclient licensing.WrappedDockerClient, license *model.IssuedLicense) error {
info, err := dclient.Info(ctx)
if err != nil {
return err
Expand Down
22 changes: 11 additions & 11 deletions internal/licenseutils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,18 @@ func TestGetAvailableLicensesHappy(t *testing.T) {
if dockerID == "orgid" {
return []*model.Subscription{
{
State: "expired",
CurrentPeriodEnd: &expiration,
State: "expired",
Expires: &expiration,
},
{
State: "active",
DockerID: "orgid",
CurrentPeriodEnd: &expiration,
State: "active",
DockerID: "orgid",
Expires: &expiration,
},
{
State: "active",
DockerID: "invalidid",
CurrentPeriodEnd: &expiration,
State: "active",
DockerID: "invalidid",
Expires: &expiration,
},
}, nil
} else if dockerID == "userid" {
Expand All @@ -111,9 +111,9 @@ func TestGetAvailableLicensesHappy(t *testing.T) {
State: "expired",
},
{
State: "active",
DockerID: "userid",
CurrentPeriodEnd: &expiration,
State: "active",
DockerID: "userid",
Expires: &expiration,
PricingComponents: model.PricingComponents{
{
Name: "comp1",
Expand Down

0 comments on commit 01cfcf0

Please sign in to comment.