Skip to content

Commit

Permalink
rpk: Make rpk license epoch ts aware
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Blafford committed Aug 5, 2022
1 parent 39949aa commit 3cda1d5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/go/rpk/pkg/api/admin/api_features.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type LicenseProperties struct {
Version int `json:"format_version"`
Organization string `json:"org"`
Type string `json:"type"`
Expires int `json:"expires"`
Expires int64 `json:"expires"`
}

// GetFeatures returns information about the available features.
Expand Down
23 changes: 14 additions & 9 deletions src/go/rpk/pkg/cli/cmd/cluster/license/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"time"

"github.com/redpanda-data/redpanda/src/go/rpk/pkg/api/admin"
"github.com/redpanda-data/redpanda/src/go/rpk/pkg/config"
Expand All @@ -22,7 +23,7 @@ func newInfoCommand(fs afero.Fs) *cobra.Command {
Organization: Organization the license was generated for.
Type: Type of license: free, enterprise, etc.
Expires: Number of days the license is valid until or -1 if is expired.
Expires: Expiration date of the license
Version: License schema version.
`,
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -47,10 +48,13 @@ func newInfoCommand(fs afero.Fs) *cobra.Command {
if info.Properties != (admin.LicenseProperties{}) {
expired := info.Properties.Expires < 0
if format == "json" {
tm := time.Unix(info.Properties.Expires, 0).Format("Jan 2 2006")
props, err := json.MarshalIndent(struct {
admin.LicenseProperties
Expired bool `json:"license_expired,omitempty"`
}{info.Properties, expired}, "", " ")
Organization string
Type string
Expires string
Expired bool `json:"license_expired,omitempty"`
}{info.Properties.Organization, info.Properties.Type, tm, expired}, "", " ")
out.MaybeDie(err, "unable to print license information as json: %v", err)
fmt.Printf("%s\n", props)
} else {
Expand All @@ -70,16 +74,17 @@ func printLicenseInfo(p admin.LicenseProperties, expired bool) {
out.Section("LICENSE INFORMATION")
licenseFormat := `Organization: %v
Type: %v
Expires: %v days
Version: %v
Expires: %v
`
if expired {
licenseFormat += `License Expired: true
`
}
fmt.Printf(licenseFormat, p.Organization, p.Type, p.Expires, p.Version)
if p.Expires < 30 && p.Expires >= 0 {
tm := time.Unix(p.Expires, 0)
fmt.Printf(licenseFormat, p.Organization, p.Type, tm.Format("Jan 2 2006"))
diff := time.Until(tm)
daysLeft := int(diff.Hours() / 24)
if daysLeft < 30 && daysLeft >= 0 {
fmt.Fprintln(os.Stderr, "warning: your license will expire soon")
return
}
}
22 changes: 6 additions & 16 deletions tests/rptest/tests/rpk_cluster_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,29 +206,19 @@ def test_upload_and_query_cluster_license_rpk(self):
output = self._rpk.license_set(tf.name)
assert "Successfully uploaded license" in output

def get_license():
output = self._rpk.license_info()
resp = json.loads(output)
if resp['org'] == "redpanda-testing":
return True

return False

wait_until(get_license,
wait_until(lambda: self._rpk.license_info() != "{}",
timeout_sec=10,
backoff_sec=1,
retry_on_exc=True,
err_msg="unable to retrieve license information")

expected_license = {
'expires':
(datetime.date(2122, 6, 6) - datetime.date.today()).days,
'format_version': 0,
'org': 'redpanda-testing',
'type': 'enterprise'
'Expires': "Jul 11 2122",
'Organization': 'redpanda-testing',
'Type': 'enterprise'
}
output = self._rpk.license_info()
assert expected_license == json.loads(output)
result = json.loads(self._rpk.license_info())
assert expected_license == result, result

@cluster(num_nodes=3)
def test_upload_cluster_license_rpk(self):
Expand Down

0 comments on commit 3cda1d5

Please sign in to comment.