Skip to content

Commit

Permalink
GetLicense to store
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell committed Aug 21, 2020
1 parent ba76a36 commit bd8c220
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 33 deletions.
3 changes: 1 addition & 2 deletions kotsadm/pkg/handlers/config.go
Expand Up @@ -18,7 +18,6 @@ import (
kotsadmconfig "github.com/replicatedhq/kots/kotsadm/pkg/config"
"github.com/replicatedhq/kots/kotsadm/pkg/downstream"
"github.com/replicatedhq/kots/kotsadm/pkg/kotsutil"
"github.com/replicatedhq/kots/kotsadm/pkg/license"
"github.com/replicatedhq/kots/kotsadm/pkg/logger"
"github.com/replicatedhq/kots/kotsadm/pkg/preflight"
"github.com/replicatedhq/kots/kotsadm/pkg/render"
Expand Down Expand Up @@ -199,7 +198,7 @@ func LiveAppConfig(w http.ResponseWriter, r *http.Request) {
return
}

appLicense, err := license.Get(foundApp.ID)
appLicense, err := store.GetStore().GetLicenseForApp(foundApp.ID)
if err != nil {
logger.Error(err)
liveAppConfigResponse.Error = "failed to get license for app"
Expand Down
3 changes: 1 addition & 2 deletions kotsadm/pkg/handlers/license.go
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/replicatedhq/kots/kotsadm/pkg/app"
"github.com/replicatedhq/kots/kotsadm/pkg/kotsutil"
"github.com/replicatedhq/kots/kotsadm/pkg/license"
kotsadmlicense "github.com/replicatedhq/kots/kotsadm/pkg/license"
"github.com/replicatedhq/kots/kotsadm/pkg/logger"
"github.com/replicatedhq/kots/kotsadm/pkg/online"
"github.com/replicatedhq/kots/kotsadm/pkg/registry"
Expand Down Expand Up @@ -175,7 +174,7 @@ func GetLicense(w http.ResponseWriter, r *http.Request) {
return
}

license, err := kotsadmlicense.Get(foundApp.ID)
license, err := store.GetStore().GetLicenseForApp(foundApp.ID)
if err != nil {
logger.Error(err)
w.WriteHeader(500)
Expand Down
26 changes: 0 additions & 26 deletions kotsadm/pkg/license/license.go
Expand Up @@ -2,7 +2,6 @@ package license

import (
"bytes"
"database/sql"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -11,7 +10,6 @@ import (
apptypes "github.com/replicatedhq/kots/kotsadm/pkg/app/types"
"github.com/replicatedhq/kots/kotsadm/pkg/kotsutil"
"github.com/replicatedhq/kots/kotsadm/pkg/logger"
"github.com/replicatedhq/kots/kotsadm/pkg/persistence"
"github.com/replicatedhq/kots/kotsadm/pkg/preflight"
registrytypes "github.com/replicatedhq/kots/kotsadm/pkg/registry/types"
"github.com/replicatedhq/kots/kotsadm/pkg/render"
Expand Down Expand Up @@ -129,27 +127,3 @@ func GetCurrentLicenseString(a *apptypes.App) (string, error) {
}
return string(kotsLicense), nil
}

// GetLicense gets the current (latest) license for an application with the given app id
func Get(appID string) (*kotsv1beta1.License, error) {
db := persistence.MustGetPGSession()
query := `select kots_license from app_version where app_id = $1 order by sequence desc limit 1`
row := db.QueryRow(query, appID)

var licenseStr sql.NullString
if err := row.Scan(&licenseStr); err != nil {
return nil, errors.Wrap(err, "failed to scan")
}

if licenseStr.Valid {
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode([]byte(licenseStr.String), nil, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to decode license yaml")
}
license := obj.(*kotsv1beta1.License)
return license, nil
}

return nil, nil
}
33 changes: 33 additions & 0 deletions kotsadm/pkg/store/s3pg/license_store.go
@@ -0,0 +1,33 @@
package s3pg

import (
"database/sql"

"github.com/pkg/errors"
"github.com/replicatedhq/kots/kotsadm/pkg/persistence"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
"github.com/replicatedhq/kots/kotskinds/client/kotsclientset/scheme"
)

func (s S3PGStore) GetLicenseForApp(appID string) (*kotsv1beta1.License, error) {
db := persistence.MustGetPGSession()
query := `select kots_license from app_version where app_id = $1 order by sequence desc limit 1`
row := db.QueryRow(query, appID)

var licenseStr sql.NullString
if err := row.Scan(&licenseStr); err != nil {
return nil, errors.Wrap(err, "failed to scan")
}

if licenseStr.Valid {
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode([]byte(licenseStr.String), nil, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to decode license yaml")
}
license := obj.(*kotsv1beta1.License)
return license, nil
}

return nil, nil
}
6 changes: 6 additions & 0 deletions kotsadm/pkg/store/store_interface.go
Expand Up @@ -10,6 +10,7 @@ import (
sessiontypes "github.com/replicatedhq/kots/kotsadm/pkg/session/types"
supportbundletypes "github.com/replicatedhq/kots/kotsadm/pkg/supportbundle/types"
usertypes "github.com/replicatedhq/kots/kotsadm/pkg/user/types"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
troubleshootredact "github.com/replicatedhq/troubleshoot/pkg/redact"
)

Expand All @@ -23,6 +24,7 @@ type KOTSStore interface {
SessionStore
AppStatusStore
AppStore
LicenseStore
}

type RegistryStore interface {
Expand Down Expand Up @@ -92,3 +94,7 @@ type AppStore interface {
IsGitOpsEnabledForApp(string) (bool, error)
SetUpdateCheckerSpec(string, string) error
}

type LicenseStore interface {
GetLicenseForApp(string) (*kotsv1beta1.License, error)
}
2 changes: 1 addition & 1 deletion kotsadm/pkg/updatechecker/updatechecker.go
Expand Up @@ -185,7 +185,7 @@ func CheckForUpdates(appID string, deploy bool) (int64, error) {
return 0, errors.Wrap(err, "failed to load kotskinds from path")
}

latestLicense, err := license.Get(a.ID)
latestLicense, err := store.GetStore().GetLicenseForApp(a.ID)
if err != nil {
return 0, errors.Wrap(err, "failed to get latest license")
}
Expand Down
3 changes: 1 addition & 2 deletions kotsadm/pkg/upstream/upstream.go
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/pkg/errors"
"github.com/replicatedhq/kots/kotsadm/pkg/kotsutil"
"github.com/replicatedhq/kots/kotsadm/pkg/license"
"github.com/replicatedhq/kots/kotsadm/pkg/logger"
"github.com/replicatedhq/kots/kotsadm/pkg/preflight"
"github.com/replicatedhq/kots/kotsadm/pkg/store"
Expand Down Expand Up @@ -81,7 +80,7 @@ func DownloadUpdate(appID string, archiveDir string, toCursor string) (sequence
return 0, errors.Wrap(err, "failed to get new app sequence")
}

latestLicense, err := license.Get(a.ID)
latestLicense, err := store.GetStore().GetLicenseForApp(a.ID)
if err != nil {
return 0, errors.Wrap(err, "failed to get latest license")
}
Expand Down

0 comments on commit bd8c220

Please sign in to comment.