Skip to content
Open
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
77 changes: 34 additions & 43 deletions api/pkg/api/handler/tenantaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package handler

import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
Expand Down Expand Up @@ -182,52 +181,44 @@ func (ctah CreateTenantAccountHandler) Handle(c echo.Context) error {
// Generate a unique account number
accountNumber := common.GenerateAccountNumber()

// Start a db tx
tx, err := cdb.BeginTx(ctx, ctah.dbSession, &sql.TxOptions{})
if err != nil {
logger.Error().Err(err).Msg("unable to start transaction")
return cutil.NewAPIErrorResponse(c, http.StatusInternalServerError, "Error creating Tenant Account", nil)
}
txCommitted := false
defer common.RollbackTx(ctx, tx, &txCommitted)

ta, err := taDAO.Create(ctx, tx, cdbm.TenantAccountCreateInput{
AccountNumber: accountNumber,
TenantID: tenantID,
TenantOrg: *tenantOrg,
InfrastructureProviderID: ip.ID,
InfrastructureProviderOrg: ip.Org,
Status: cdbm.TenantAccountStatusInvited,
CreatedBy: dbUser.ID,
})
if err != nil {
logger.Error().Err(err).Msg("error creating TenantAccount in DB")
// rollback transaction
return cutil.NewAPIErrorResponse(c, http.StatusInternalServerError, "Failed to create tenant account", nil)
}

// Create a status detail record for the tenantaccount
sdDAO := cdbm.NewStatusDetailDAO(ctah.dbSession)
ssd, serr := sdDAO.CreateFromParams(ctx, tx, ta.ID.String(), *cdb.GetStrPtr(cdbm.TenantAccountStatusInvited),
cdb.GetStrPtr("received tenant account creation request, pending accept"))
if serr != nil {
logger.Error().Err(serr).Msg("error creating Status Detail DB entry")
// rollback transaction
return cutil.NewAPIErrorResponse(c, http.StatusInternalServerError, "Failed to create Status Detail for TenantAccount", nil)
}
if ssd == nil {
logger.Error().Msg("Status Detail DB entry not returned from CreateFromParams")
return cutil.NewAPIErrorResponse(c, http.StatusInternalServerError, "Failed to get new Status Detail for TenantAccount", nil)
}

// Commit transaction
err = tx.Commit()
var ta *cdbm.TenantAccount
var ssd *cdbm.StatusDetail

err = cdb.WithTx(ctx, ctah.dbSession, func(tx *cdb.Tx) error {
var derr error
ta, derr = taDAO.Create(ctx, tx, cdbm.TenantAccountCreateInput{
AccountNumber: accountNumber,
TenantID: tenantID,
TenantOrg: *tenantOrg,
InfrastructureProviderID: ip.ID,
InfrastructureProviderOrg: ip.Org,
Status: cdbm.TenantAccountStatusInvited,
CreatedBy: dbUser.ID,
})
if derr != nil {
logger.Error().Err(derr).Msg("error creating TenantAccount in DB")
return cutil.NewAPIError(http.StatusInternalServerError, "Failed to create tenant account", nil)
}

// Create a status detail record for the tenantaccount
ssd, derr = sdDAO.CreateFromParams(ctx, tx, ta.ID.String(), *cdb.GetStrPtr(cdbm.TenantAccountStatusInvited),
cdb.GetStrPtr("received tenant account creation request, pending accept"))
if derr != nil {
logger.Error().Err(derr).Msg("error creating Status Detail DB entry")
return cutil.NewAPIError(http.StatusInternalServerError, "Failed to create Status Detail for TenantAccount", nil)
}
if ssd == nil {
logger.Error().Msg("Status Detail DB entry not returned from CreateFromParams")
return cutil.NewAPIError(http.StatusInternalServerError, "Failed to get new Status Detail for TenantAccount", nil)
}

return nil
})
if err != nil {
logger.Error().Err(err).Msg("error committing subnet transaction to DB")
return cutil.NewAPIErrorResponse(c, http.StatusInternalServerError, "Failed to create Tenant Account", nil)
return common.HandleTxError(c, logger, err, "Failed to create Tenant Account, DB transaction error")
}
// set committed so, deferred cleanup functions will do nothing
txCommitted = true

// Create response
apiInstance := model.NewAPITenantAccount(ta, []cdbm.StatusDetail{*ssd}, 0)
Expand Down
Loading