Skip to content

Commit

Permalink
Merge pull request #1003 from replicatedhq/divolgin/license-api
Browse files Browse the repository at this point in the history
move license API to Go
  • Loading branch information
divolgin committed Aug 25, 2020
2 parents ecc820c + 799534c commit b309f72
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 73 deletions.
71 changes: 0 additions & 71 deletions kotsadm/api/src/controllers/titled/LicenseAPI.ts

This file was deleted.

4 changes: 2 additions & 2 deletions kotsadm/pkg/apiserver/server.go
Expand Up @@ -91,8 +91,8 @@ func Start() {
r.Path("/api/v1/redact/spec/{slug}").Methods("DELETE").HandlerFunc(handlers.DeleteRedact)
r.Path("/api/v1/redact/enabled/{slug}").Methods("OPTIONS", "POST").HandlerFunc(handlers.SetRedactEnabled)

// proxy for license/titled api
r.Path("/license/v1/license").Methods("GET").HandlerFunc(handlers.NodeProxy(upstream))
// This the handler for license API and should be called by the application only.
r.Path("/license/v1/license").Methods("GET").HandlerFunc(handlers.GetPlatformLicenseCompatibility)

// Apps
r.Path("/api/v1/apps").Methods("OPTIONS", "GET").HandlerFunc(handlers.ListApps)
Expand Down
81 changes: 81 additions & 0 deletions kotsadm/pkg/handlers/license.go
Expand Up @@ -479,3 +479,84 @@ func GetOnlineInstallStatus(w http.ResponseWriter, r *http.Request) {

JSON(w, 200, status)
}

// This the handler for license API and should be called by the application only.
func GetPlatformLicenseCompatibility(w http.ResponseWriter, r *http.Request) {
apps, err := store.GetStore().ListInstalledApps()
if err != nil {
if store.GetStore().IsNotFound(err) {
JSON(w, http.StatusNotFound, struct{}{})
return
}
logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}

if len(apps) == 0 {
JSON(w, http.StatusNotFound, struct{}{})
return
}

if len(apps) > 1 {
JSON(w, http.StatusBadRequest, struct{}{})
return
}

app := apps[0]
license, err := store.GetStore().GetLicenseForApp(app.ID)
if err != nil {
logger.Error(err)
JSON(w, http.StatusInternalServerError, struct{}{})
return
}

type licenseFieldType struct {
Field string `json:"field"`
Title string `json:"title"`
Type string `json:"type"`
Value interface{} `json:"value"`
HideFromCustomer bool `json:"hide_from_customer,omitempty"`
}

type licenseType struct {
LicenseID string `json:"license_id"`
InstallationID string `json:"installation_id"`
Assignee string `json:"assignee"`
ReleaseChannel string `json:"release_channel"`
LicenseType string `json:"license_type"`
ExpirationTime string `json:"expiration_time,omitempty"`
Fields []licenseFieldType `json:"fields"`
}

platformLicense := licenseType{
LicenseID: license.Spec.LicenseID,
InstallationID: app.ID,
Assignee: license.Spec.CustomerName,
ReleaseChannel: license.Spec.ChannelName,
LicenseType: license.Spec.LicenseType,
Fields: make([]licenseFieldType, 0),
}

for k, e := range license.Spec.Entitlements {
if k == "expires_at" {
if e.Value.StrVal != "" {
platformLicense.ExpirationTime = e.Value.StrVal
}
continue
}

field := licenseFieldType{
Field: k,
Title: e.Title,
Type: e.ValueType,
Value: e.Value.Value(),
HideFromCustomer: e.IsHidden,
}

platformLicense.Fields = append(platformLicense.Fields, field)
}

JSON(w, http.StatusOK, platformLicense)
return
}
13 changes: 13 additions & 0 deletions kotsadm/pkg/store/s3pg/s3pg_store.go
@@ -1,4 +1,17 @@
package s3pg

import (
"database/sql"

"github.com/pkg/errors"
)

type S3PGStore struct {
}

func (s S3PGStore) IsNotFound(err error) bool {
if err == nil {
return false
}
return errors.Cause(err) == sql.ErrNoRows
}
2 changes: 2 additions & 0 deletions kotsadm/pkg/store/store_interface.go
Expand Up @@ -26,6 +26,8 @@ type KOTSStore interface {
AppStore
LicenseStore
ClusterStore

IsNotFound(err error) bool
}

type RegistryStore interface {
Expand Down

0 comments on commit b309f72

Please sign in to comment.