Files
paralus/internal/cluster/dao/clusteroperatorbootstrap.go
nirav-rafay c66bdc25cd restructure rcloud-base as a single base controller (#37)
* restructure rcloud-base as a single base controller
* updated master.rest
* moved sentry from internal to pkg as it is used by relay
* removing unused rpc and it's dependencies
* Fix usermgmt tests
* Don't redefine variables in rest file
Co-authored-by: Abin Simon <abin.simon@rafay.co>
2022-03-03 17:59:06 +05:30

85 lines
2.8 KiB
Go

package dao
import (
"context"
"database/sql"
"github.com/RafaySystems/rcloud-base/internal/models"
"github.com/RafaySystems/rcloud-base/internal/persistence/provider/pg"
"github.com/RafaySystems/rcloud-base/pkg/log"
"github.com/uptrace/bun"
)
var _log = log.GetLogger()
// ClusterOperatorBootstrapDao is the interface for cluster operator bootstrap
type ClusterOperatorBootstrapDao interface {
// create edge operator bootstrap
CreateOperatorBootstrap(ctx context.Context, bootstrap *models.ClusterOperatorBootstrap) error
// GetOperatorBootstrap
GetOperatorBootstrap(ctx context.Context, edgeID string) (*models.ClusterOperatorBootstrap, error)
}
// clusterOperatorBootstrapDao implements ClusterOperatorBootstrapDao
type clusterOperatorBootstrapDao struct {
dao pg.EntityDAO
}
// ClusterOperatorBootstrapDao return new cluster credentials dao
func NewClusterOperatorBootstrapDao(dao pg.EntityDAO) ClusterOperatorBootstrapDao {
return &clusterOperatorBootstrapDao{
dao: dao,
}
}
func (es *clusterOperatorBootstrapDao) CreateOperatorBootstrap(ctx context.Context, bootstrap *models.ClusterOperatorBootstrap) error {
_log.Infow("CreateOperatorBootstrap: Creating operator bootstrap data", "cluster", bootstrap.ClusterId)
err := es.dao.GetInstance().RunInTx(ctx, &sql.TxOptions{}, func(ctx context.Context, tx bun.Tx) error {
var bstrap *models.ClusterOperatorBootstrap
entity, err := es.dao.GetX(ctx, "edge_id", bootstrap.ClusterId, &bstrap)
if err != nil {
_log.Infow("CreateOperatorBootstrap: No existing boostrap data detected", "edge", bootstrap.ClusterId)
} else {
_log.Infow("CreateOperatorBootstrap: Removing existing boostrap data", "edge", bootstrap.ClusterId)
bstrap = entity.(*models.ClusterOperatorBootstrap)
err = es.dao.DeleteX(ctx, "edge_id", bstrap.ClusterId, bstrap)
if err != nil {
_log.Errorw("Error while deleting bootstrap data", "Error", err)
return err
}
_log.Infow("CreateOperatorBootstrap: Deleted existing boostrap data", "cluster", bootstrap.ClusterId)
}
_, err = tx.NewInsert().Model(bootstrap).Exec(ctx)
if err != nil {
_log.Errorw("Error inserting bootstrap data", "Error", err)
return err
}
_log.Infow("Inserted bootstrap data", "cluster", bootstrap.ClusterId)
return nil
})
if err != nil {
_log.Errorw("Exception while adding bootstrap data", "Error:", err)
}
return nil
}
func (es *clusterOperatorBootstrapDao) GetOperatorBootstrap(ctx context.Context, clusterid string) (*models.ClusterOperatorBootstrap, error) {
var bootstrap models.ClusterOperatorBootstrap
entity, err := es.dao.GetX(ctx, "clusterid", clusterid, bootstrap)
if err != nil {
_log.Errorw("Error while fetching bootstrap data using tx ", "Error", err)
return nil, err
}
return entity.(*models.ClusterOperatorBootstrap), err
}