Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for endpoint sub type (and metadata) #3423

Merged
merged 2 commits into from
Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/jetstream/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ func TestLoginToCNSI(t *testing.T) {
DopplerLoggingEndpoint: mockDopplerEndpoint,
}

expectedCNSIRow := sqlmock.NewRows([]string{"guid", "name", "cnsi_type", "api_endpoint", "auth_endpoint", "token_endpoint", "doppler_logging_endpoint", "skip_ssl_validation", "client_id", "client_secret", "allow_sso"}).
AddRow(mockCNSIGUID, mockCNSI.Name, stringCFType, mockUAA.URL, mockCNSI.AuthorizationEndpoint, mockCNSI.TokenEndpoint, mockCNSI.DopplerLoggingEndpoint, true, mockCNSI.ClientId, cipherClientSecret, true)
expectedCNSIRow := sqlmock.NewRows([]string{"guid", "name", "cnsi_type", "api_endpoint", "auth_endpoint", "token_endpoint", "doppler_logging_endpoint", "skip_ssl_validation", "client_id", "client_secret", "allow_sso", "sub_type", "meta_data"}).
AddRow(mockCNSIGUID, mockCNSI.Name, stringCFType, mockUAA.URL, mockCNSI.AuthorizationEndpoint, mockCNSI.TokenEndpoint, mockCNSI.DopplerLoggingEndpoint, true, mockCNSI.ClientId, cipherClientSecret, true, "", "")

mock.ExpectQuery(selectAnyFromCNSIs).
WithArgs(mockCNSIGUID).
Expand Down
24 changes: 22 additions & 2 deletions src/jetstream/cnsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ func (p *portalProxy) RegisterEndpoint(c echo.Context, fetchInfo interfaces.Info

cnsiClientId := c.FormValue("cnsi_client_id")
cnsiClientSecret := c.FormValue("cnsi_client_secret")
subType := c.FormValue("sub_type")

if cnsiClientId == "" {
cnsiClientId = p.GetConfig().CFClient
cnsiClientSecret = p.GetConfig().CFClientSecret
}

newCNSI, err := p.DoRegisterEndpoint(cnsiName, apiEndpoint, skipSSLValidation, cnsiClientId, cnsiClientSecret, ssoAllowed, fetchInfo)
newCNSI, err := p.DoRegisterEndpoint(cnsiName, apiEndpoint, skipSSLValidation, cnsiClientId, cnsiClientSecret, ssoAllowed, subType, fetchInfo)
if err != nil {
return err
}
Expand All @@ -72,7 +73,7 @@ func (p *portalProxy) RegisterEndpoint(c echo.Context, fetchInfo interfaces.Info
return nil
}

func (p *portalProxy) DoRegisterEndpoint(cnsiName string, apiEndpoint string, skipSSLValidation bool, clientId string, clientSecret string, ssoAllowed bool, fetchInfo interfaces.InfoFunc) (interfaces.CNSIRecord, error) {
func (p *portalProxy) DoRegisterEndpoint(cnsiName string, apiEndpoint string, skipSSLValidation bool, clientId string, clientSecret string, ssoAllowed bool, subType string, fetchInfo interfaces.InfoFunc) (interfaces.CNSIRecord, error) {

if len(cnsiName) == 0 || len(apiEndpoint) == 0 {
return interfaces.CNSIRecord{}, interfaces.NewHTTPShadowError(
Expand Down Expand Up @@ -129,6 +130,7 @@ func (p *portalProxy) DoRegisterEndpoint(cnsiName string, apiEndpoint string, sk
newCNSI.ClientId = clientId
newCNSI.ClientSecret = clientSecret
newCNSI.SSOAllowed = ssoAllowed
newCNSI.SubType = subType

err = p.setCNSIRecord(guid, newCNSI)

Expand Down Expand Up @@ -264,6 +266,24 @@ func marshalClusterList(clusterList []*interfaces.ConnectedEndpoint) ([]byte, er
return jsonString, nil
}

func (p *portalProxy) UpdateEndointMetadata(guid string, metadata string) error {
log.Debug("UpdateEndointMetadata")
cnsiRepo, err := cnsis.NewPostgresCNSIRepository(p.DatabaseConnectionPool)
if err != nil {
log.Errorf(dbReferenceError, err)
return fmt.Errorf(dbReferenceError, err)
}

err = cnsiRepo.UpdateMetadata(guid, metadata)
if err != nil {
msg := "Unable to update endpoint metadata: %v"
log.Errorf(msg, err)
return fmt.Errorf(msg, err)
}

return nil
}

func (p *portalProxy) GetCNSIRecord(guid string) (interfaces.CNSIRecord, error) {
log.Debug("GetCNSIRecord")
cnsiRepo, err := cnsis.NewPostgresCNSIRepository(p.DatabaseConnectionPool)
Expand Down
2 changes: 1 addition & 1 deletion src/jetstream/cnsi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestRegisterCFCluster(t *testing.T) {
defer db.Close()

mock.ExpectExec(insertIntoCNSIs).
WithArgs(sqlmock.AnyArg(), "Some fancy CF Cluster", "cf", mockV2Info.URL, mockAuthEndpoint, mockTokenEndpoint, mockDopplerEndpoint, true, mockClientId, sqlmock.AnyArg(), false).
WithArgs(sqlmock.AnyArg(), "Some fancy CF Cluster", "cf", mockV2Info.URL, mockAuthEndpoint, mockTokenEndpoint, mockDopplerEndpoint, true, mockClientId, sqlmock.AnyArg(), false, "", "").
WillReturnResult(sqlmock.NewResult(1, 1))

if err := pp.RegisterEndpoint(ctx, getCFPlugin(pp, "cf").Info); err != nil {
Expand Down
26 changes: 26 additions & 0 deletions src/jetstream/datastore/20190305144600_EndpointSubtype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package datastore

import (
"database/sql"

"bitbucket.org/liamstask/goose/lib/goose"
)

func init() {
RegisterMigration(20190305144600, "EndpointSubtype", func(txn *sql.Tx, conf *goose.DBConf) error {

addColumn := "ALTER TABLE cnsis ADD sub_type VARCHAR(64) DEFAULT NULL"
_, err := txn.Exec(addColumn)
if err != nil {
return err
}

addColumn = "ALTER TABLE cnsis ADD meta_data TEXT DEFAULT NULL"
_, err = txn.Exec(addColumn)
if err != nil {
return err
}

return nil
})
}
10 changes: 5 additions & 5 deletions src/jetstream/mock_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,18 @@ func expectOneRow() sqlmock.Rows {

func expectCFRow() sqlmock.Rows {
return sqlmock.NewRows(rowFieldsForCNSI).
AddRow(mockCFGUID, "Some fancy CF Cluster", "cf", mockAPIEndpoint, mockAuthEndpoint, mockAuthEndpoint, mockDopplerEndpoint, true, mockClientId, cipherClientSecret, true)
AddRow(mockCFGUID, "Some fancy CF Cluster", "cf", mockAPIEndpoint, mockAuthEndpoint, mockAuthEndpoint, mockDopplerEndpoint, true, mockClientId, cipherClientSecret, true, "", "")
}

func expectCERow() sqlmock.Rows {
return sqlmock.NewRows(rowFieldsForCNSI).
AddRow(mockCEGUID, "Some fancy HCE Cluster", "hce", mockAPIEndpoint, mockAuthEndpoint, mockAuthEndpoint, "", true, mockClientId, cipherClientSecret, true)
AddRow(mockCEGUID, "Some fancy HCE Cluster", "hce", mockAPIEndpoint, mockAuthEndpoint, mockAuthEndpoint, "", true, mockClientId, cipherClientSecret, true, "", "")
}

func expectCFAndCERows() sqlmock.Rows {
return sqlmock.NewRows(rowFieldsForCNSI).
AddRow(mockCFGUID, "Some fancy CF Cluster", "cf", mockAPIEndpoint, mockAuthEndpoint, mockAuthEndpoint, mockDopplerEndpoint, true, mockClientId, cipherClientSecret).
AddRow(mockCEGUID, "Some fancy HCE Cluster", "hce", mockAPIEndpoint, mockAuthEndpoint, mockAuthEndpoint, "", true, mockClientId, cipherClientSecret)
AddRow(mockCFGUID, "Some fancy CF Cluster", "cf", mockAPIEndpoint, mockAuthEndpoint, mockAuthEndpoint, mockDopplerEndpoint, true, mockClientId, cipherClientSecret, false, "", "").
AddRow(mockCEGUID, "Some fancy HCE Cluster", "hce", mockAPIEndpoint, mockAuthEndpoint, mockAuthEndpoint, "", true, mockClientId, cipherClientSecret, false, "", "")
}

func expectTokenRow() sqlmock.Rows {
Expand Down Expand Up @@ -276,7 +276,7 @@ const (
getDbVersion = `SELECT version_id FROM goose_db_version WHERE is_applied = '1' ORDER BY id DESC LIMIT 1`
)

var rowFieldsForCNSI = []string{"guid", "name", "cnsi_type", "api_endpoint", "auth_endpoint", "token_endpoint", "doppler_logging_endpoint", "skip_ssl_validation", "client_id", "client_secret", "allow_sso"}
var rowFieldsForCNSI = []string{"guid", "name", "cnsi_type", "api_endpoint", "auth_endpoint", "token_endpoint", "doppler_logging_endpoint", "skip_ssl_validation", "client_id", "client_secret", "allow_sso", "sub_type", "meta_data"}

var mockEncryptionKey = make([]byte, 32)

Expand Down
8 changes: 4 additions & 4 deletions src/jetstream/oauth_requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ func TestDoOauthFlowRequestWithValidToken(t *testing.T) {

// p.GetCNSIRecord(r.GUID) -> cnsiRepo.Find(guid)

expectedCNSIRecordRow := sqlmock.NewRows([]string{"guid", "name", "cnsi_type", "api_endpoint", "auth_endpoint", "token_endpoint", "doppler_logging_endpoint", "skip_ssl_validation", "client_id", "client_secret", "allow_sso"}).
AddRow(mockCNSI.GUID, mockCNSI.Name, mockCNSI.CNSIType, mockURLasString, mockCNSI.AuthorizationEndpoint, mockCNSI.TokenEndpoint, mockCNSI.DopplerLoggingEndpoint, true, mockCNSI.ClientId, cipherClientSecret, true)
expectedCNSIRecordRow := sqlmock.NewRows([]string{"guid", "name", "cnsi_type", "api_endpoint", "auth_endpoint", "token_endpoint", "doppler_logging_endpoint", "skip_ssl_validation", "client_id", "client_secret", "allow_sso", "sub_type", "meta_data"}).
AddRow(mockCNSI.GUID, mockCNSI.Name, mockCNSI.CNSIType, mockURLasString, mockCNSI.AuthorizationEndpoint, mockCNSI.TokenEndpoint, mockCNSI.DopplerLoggingEndpoint, true, mockCNSI.ClientId, cipherClientSecret, true, "", "")
mock.ExpectQuery(selectAnyFromCNSIs).
WithArgs(mockCNSIGUID).
WillReturnRows(expectedCNSIRecordRow)
Expand Down Expand Up @@ -238,8 +238,8 @@ func TestDoOauthFlowRequestWithExpiredToken(t *testing.T) {
WillReturnRows(expectedCNSITokenRow)

// p.GetCNSIRecord(r.GUID) -> cnsiRepo.Find(guid)
expectedCNSIRecordRow := sqlmock.NewRows([]string{"guid", "name", "cnsi_type", "api_endpoint", "auth_endpoint", "token_endpoint", "doppler_logging_endpoint", "skip_ssl_validation", "client_id", "client_secret", "allow_sso"}).
AddRow(mockCNSI.GUID, mockCNSI.Name, mockCNSI.CNSIType, mockURLasString, mockCNSI.AuthorizationEndpoint, mockCNSI.TokenEndpoint, mockCNSI.DopplerLoggingEndpoint, true, mockCNSI.ClientId, cipherClientSecret, true)
expectedCNSIRecordRow := sqlmock.NewRows([]string{"guid", "name", "cnsi_type", "api_endpoint", "auth_endpoint", "token_endpoint", "doppler_logging_endpoint", "skip_ssl_validation", "client_id", "client_secret", "allow_sso", "sub_type", "meta_data"}).
AddRow(mockCNSI.GUID, mockCNSI.Name, mockCNSI.CNSIType, mockURLasString, mockCNSI.AuthorizationEndpoint, mockCNSI.TokenEndpoint, mockCNSI.DopplerLoggingEndpoint, true, mockCNSI.ClientId, cipherClientSecret, true, "", "")
mock.ExpectQuery(selectAnyFromCNSIs).
WithArgs(mockCNSIGUID).
WillReturnRows(expectedCNSIRecordRow)
Expand Down
4 changes: 2 additions & 2 deletions src/jetstream/passthrough_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ func TestValidateCNSIListWithValidGUID(t *testing.T) {
_, _, _, pp, db, mock := setupHTTPTest(req)
defer db.Close()

expectedCNSIRecordRow := sqlmock.NewRows([]string{"guid", "name", "cnsi_type", "api_endpoint", "auth_endpoint", "token_endpoint", "doppler_logging_endpoint", "skip_ssl_validation", "client_id", "client_secret", "allow_sso"}).
AddRow("valid-guid-abc123", "mock-name", "cf", "http://localhost", "http://localhost", "http://localhost", mockDopplerEndpoint, true, mockClientId, cipherClientSecret, true)
expectedCNSIRecordRow := sqlmock.NewRows([]string{"guid", "name", "cnsi_type", "api_endpoint", "auth_endpoint", "token_endpoint", "doppler_logging_endpoint", "skip_ssl_validation", "client_id", "client_secret", "allow_sso", "sub_type", "meta_data"}).
AddRow("valid-guid-abc123", "mock-name", "cf", "http://localhost", "http://localhost", "http://localhost", mockDopplerEndpoint, true, mockClientId, cipherClientSecret, true, "", "")
mock.ExpectQuery(selectAnyFromCNSIs).
WithArgs("valid-guid-abc123").
WillReturnRows(expectedCNSIRecordRow)
Expand Down
2 changes: 1 addition & 1 deletion src/jetstream/plugins/cloudfoundry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (c *CloudFoundrySpecification) cfLoginHook(context echo.Context) error {
log.Infof("Auto-registering cloud foundry endpoint %s as \"%s\"", cfAPI, autoRegName)

// Auto-register the Cloud Foundry
cfCnsi, err = c.portalProxy.DoRegisterEndpoint(autoRegName, cfAPI, true, c.portalProxy.GetConfig().CFClient, c.portalProxy.GetConfig().CFClientSecret, false, cfEndpointSpec.Info)
cfCnsi, err = c.portalProxy.DoRegisterEndpoint(autoRegName, cfAPI, true, c.portalProxy.GetConfig().CFClient, c.portalProxy.GetConfig().CFClientSecret, false, "", cfEndpointSpec.Info)
if err != nil {
log.Fatal("Could not auto-register Cloud Foundry endpoint", err)
return nil
Expand Down
1 change: 1 addition & 0 deletions src/jetstream/repository/cnsis/cnsis.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Repository interface {
Delete(guid string) error
Save(guid string, cnsiRecord interfaces.CNSIRecord, encryptionKey []byte) error
Update(guid string, ssoAllowed bool) error
UpdateMetadata(guid string, metadata string) error
}

type Endpoint interface {
Expand Down
92 changes: 82 additions & 10 deletions src/jetstream/repository/cnsis/pgsql_cnsis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,32 @@ import (
log "github.com/sirupsen/logrus"
)

var listCNSIs = `SELECT guid, name, cnsi_type, api_endpoint, auth_endpoint, token_endpoint, doppler_logging_endpoint, skip_ssl_validation, client_id, client_secret, sso_allowed
var listCNSIs = `SELECT guid, name, cnsi_type, api_endpoint, auth_endpoint, token_endpoint, doppler_logging_endpoint, skip_ssl_validation, client_id, client_secret, sso_allowed, sub_type, meta_data
FROM cnsis`

var listCNSIsByUser = `SELECT c.guid, c.name, c.cnsi_type, c.api_endpoint, c.doppler_logging_endpoint, t.user_guid, t.token_expiry, c.skip_ssl_validation, t.disconnected, t.meta_data
var listCNSIsByUser = `SELECT c.guid, c.name, c.cnsi_type, c.api_endpoint, c.doppler_logging_endpoint, t.user_guid, t.token_expiry, c.skip_ssl_validation, t.disconnected, t.meta_data, c.sub_type, c.meta_data as endpoint_metadata
FROM cnsis c, tokens t
WHERE c.guid = t.cnsi_guid AND t.token_type=$1 AND t.user_guid=$2 AND t.disconnected = '0'`

var findCNSI = `SELECT guid, name, cnsi_type, api_endpoint, auth_endpoint, token_endpoint, doppler_logging_endpoint, skip_ssl_validation, client_id, client_secret, sso_allowed
var findCNSI = `SELECT guid, name, cnsi_type, api_endpoint, auth_endpoint, token_endpoint, doppler_logging_endpoint, skip_ssl_validation, client_id, client_secret, sso_allowed, sub_type, meta_data
FROM cnsis
WHERE guid=$1`

var findCNSIByAPIEndpoint = `SELECT guid, name, cnsi_type, api_endpoint, auth_endpoint, token_endpoint, doppler_logging_endpoint, skip_ssl_validation, client_id, client_secret, sso_allowed
var findCNSIByAPIEndpoint = `SELECT guid, name, cnsi_type, api_endpoint, auth_endpoint, token_endpoint, doppler_logging_endpoint, skip_ssl_validation, client_id, client_secret, sso_allowed, sub_type, meta_data
FROM cnsis
WHERE api_endpoint=$1`

var saveCNSI = `INSERT INTO cnsis (guid, name, cnsi_type, api_endpoint, auth_endpoint, token_endpoint, doppler_logging_endpoint, skip_ssl_validation, client_id, client_secret, sso_allowed)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)`
var saveCNSI = `INSERT INTO cnsis (guid, name, cnsi_type, api_endpoint, auth_endpoint, token_endpoint, doppler_logging_endpoint, skip_ssl_validation, client_id, client_secret, sso_allowed, sub_type, meta_data)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)`

var deleteCNSI = `DELETE FROM cnsis WHERE guid = $1`

// Just update the SSO Allowed state for now
var updateCNSI = `UPDATE cnsis SET sso_allowed = $1 WHERE guid = $2`

// Update the metadata
var updateCNSIMetadata = `UPDATE cnsis SET metadata = $1 WHERE guid = $2`

// PostgresCNSIRepository is a PostgreSQL-backed CNSI repository
type PostgresCNSIRepository struct {
db *sql.DB
Expand All @@ -55,6 +58,7 @@ func InitRepositoryProvider(databaseProvider string) {
saveCNSI = datastore.ModifySQLStatement(saveCNSI, databaseProvider)
deleteCNSI = datastore.ModifySQLStatement(deleteCNSI, databaseProvider)
updateCNSI = datastore.ModifySQLStatement(updateCNSI, databaseProvider)
updateCNSIMetadata = datastore.ModifySQLStatement(updateCNSIMetadata, databaseProvider)
}

// List - Returns a list of CNSI Records
Expand All @@ -74,11 +78,13 @@ func (p *PostgresCNSIRepository) List(encryptionKey []byte) ([]*interfaces.CNSIR
pCNSIType string
pURL string
cipherTextClientSecret []byte
subType sql.NullString
metadata sql.NullString
)

cnsi := new(interfaces.CNSIRecord)

err := rows.Scan(&cnsi.GUID, &cnsi.Name, &pCNSIType, &pURL, &cnsi.AuthorizationEndpoint, &cnsi.TokenEndpoint, &cnsi.DopplerLoggingEndpoint, &cnsi.SkipSSLValidation, &cnsi.ClientId, &cipherTextClientSecret, &cnsi.SSOAllowed)
err := rows.Scan(&cnsi.GUID, &cnsi.Name, &pCNSIType, &pURL, &cnsi.AuthorizationEndpoint, &cnsi.TokenEndpoint, &cnsi.DopplerLoggingEndpoint, &cnsi.SkipSSLValidation, &cnsi.ClientId, &cipherTextClientSecret, &cnsi.SSOAllowed, &subType, &metadata)
if err != nil {
return nil, fmt.Errorf("Unable to scan CNSI records: %v", err)
}
Expand All @@ -89,6 +95,14 @@ func (p *PostgresCNSIRepository) List(encryptionKey []byte) ([]*interfaces.CNSIR
return nil, fmt.Errorf("Unable to parse API Endpoint: %v", err)
}

if subType.Valid {
cnsi.SubType = subType.String
}

if metadata.Valid {
cnsi.Metadata = metadata.String
}

if len(cipherTextClientSecret) > 0 {
plaintextClientSecret, err := crypto.DecryptToken(encryptionKey, cipherTextClientSecret)
if err != nil {
Expand Down Expand Up @@ -129,14 +143,25 @@ func (p *PostgresCNSIRepository) ListByUser(userGUID string) ([]*interfaces.Conn
pCNSIType string
pURL string
disconnected bool
subType sql.NullString
metadata sql.NullString
)

cluster := new(interfaces.ConnectedEndpoint)
err := rows.Scan(&cluster.GUID, &cluster.Name, &pCNSIType, &pURL, &cluster.DopplerLoggingEndpoint, &cluster.Account, &cluster.TokenExpiry, &cluster.SkipSSLValidation, &disconnected, &cluster.TokenMetadata)
err := rows.Scan(&cluster.GUID, &cluster.Name, &pCNSIType, &pURL, &cluster.DopplerLoggingEndpoint, &cluster.Account, &cluster.TokenExpiry, &cluster.SkipSSLValidation,
&disconnected, &cluster.TokenMetadata, &subType, &metadata)
if err != nil {
return nil, fmt.Errorf("Unable to scan cluster records: %v", err)
}

if subType.Valid {
cluster.SubType = subType.String
}

if metadata.Valid {
cluster.EndpointMetadata = metadata.String
}

cluster.CNSIType = pCNSIType

if cluster.APIEndpoint, err = url.Parse(pURL); err != nil {
Expand Down Expand Up @@ -173,12 +198,14 @@ func (p *PostgresCNSIRepository) findBy(query, match string, encryptionKey []byt
pCNSIType string
pURL string
cipherTextClientSecret []byte
subType sql.NullString
metadata sql.NullString
)

cnsi := new(interfaces.CNSIRecord)

err := p.db.QueryRow(query, match).Scan(&cnsi.GUID, &cnsi.Name, &pCNSIType, &pURL,
&cnsi.AuthorizationEndpoint, &cnsi.TokenEndpoint, &cnsi.DopplerLoggingEndpoint, &cnsi.SkipSSLValidation, &cnsi.ClientId, &cipherTextClientSecret, &cnsi.SSOAllowed)
&cnsi.AuthorizationEndpoint, &cnsi.TokenEndpoint, &cnsi.DopplerLoggingEndpoint, &cnsi.SkipSSLValidation, &cnsi.ClientId, &cipherTextClientSecret, &cnsi.SSOAllowed, &subType, &metadata)

switch {
case err == sql.ErrNoRows:
Expand All @@ -189,6 +216,14 @@ func (p *PostgresCNSIRepository) findBy(query, match string, encryptionKey []byt
// do nothing
}

if subType.Valid {
cnsi.SubType = subType.String
}

if metadata.Valid {
cnsi.Metadata = metadata.String
}

cnsi.CNSIType = pCNSIType

if cnsi.APIEndpoint, err = url.Parse(pURL); err != nil {
Expand Down Expand Up @@ -218,7 +253,7 @@ func (p *PostgresCNSIRepository) Save(guid string, cnsi interfaces.CNSIRecord, e
}
if _, err := p.db.Exec(saveCNSI, guid, cnsi.Name, fmt.Sprintf("%s", cnsi.CNSIType),
fmt.Sprintf("%s", cnsi.APIEndpoint), cnsi.AuthorizationEndpoint, cnsi.TokenEndpoint, cnsi.DopplerLoggingEndpoint, cnsi.SkipSSLValidation,
cnsi.ClientId, cipherTextClientSecret, cnsi.SSOAllowed); err != nil {
cnsi.ClientId, cipherTextClientSecret, cnsi.SSOAllowed, cnsi.SubType, cnsi.Metadata); err != nil {
return fmt.Errorf("Unable to Save CNSI record: %v", err)
}

Expand Down Expand Up @@ -271,3 +306,40 @@ func (p *PostgresCNSIRepository) Update(guid string, ssoAllowed bool) error {

return nil
}

// UpdateMetadata - Update an endpoint's metadata
func (p *PostgresCNSIRepository) UpdateMetadata(guid string, metadata string) error {
log.Debug("UpdateMetadata")

if guid == "" {
msg := "Unable to update Endpoint without a valid guid."
log.Debug(msg)
return errors.New(msg)
}

var err error

result, err := p.db.Exec(updateCNSIMetadata, metadata, guid)
if err != nil {
msg := "Unable to UPDATE endpoint: %v"
log.Debugf(msg, err)
return fmt.Errorf(msg, err)
}

rowsUpdates, err := result.RowsAffected()
if err != nil {
return errors.New("Unable to UPDATE endpoint: could not determine number of rows that were updated")
}

if rowsUpdates < 1 {
return errors.New("Unable to UPDATE endpoint: no rows were updated")
}

if rowsUpdates > 1 {
log.Warn("UPDATE endpoint: More than 1 row was updated (expected only 1)")
}

log.Debug("Endpoint UPDATE complete")

return nil
}
Loading