Skip to content

Commit

Permalink
Rename accountProvider to awsAccountProvider
Browse files Browse the repository at this point in the history
As we'll onboard more accountprovider in the future, make it clear that
is for Aws.
  • Loading branch information
fridim committed Sep 27, 2023
1 parent c842725 commit df9f464
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 37 deletions.
22 changes: 11 additions & 11 deletions cmd/sandbox-api/account_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (
)

type AccountHandler struct {
accountProvider models.AwsAccountProvider
awsAccountProvider models.AwsAccountProvider
}

func NewAccountHandler(accountProvider models.AwsAccountProvider) *AccountHandler {
func NewAccountHandler(awsAccountProvider models.AwsAccountProvider) *AccountHandler {
return &AccountHandler{
accountProvider: accountProvider,
awsAccountProvider: awsAccountProvider,
}
}

Expand All @@ -42,13 +42,13 @@ func (h *AccountHandler) GetAccountsHandler(w http.ResponseWriter, r *http.Reque
)
if serviceUuid != "" {
// Get the account from DynamoDB
accounts, err = h.accountProvider.FetchAllByServiceUuid(serviceUuid)
accounts, err = h.awsAccountProvider.FetchAllByServiceUuid(serviceUuid)

} else {
if available != "" && available == "true" {
accounts, err = h.accountProvider.FetchAllAvailable()
accounts, err = h.awsAccountProvider.FetchAllAvailable()
} else {
accounts, err = h.accountProvider.FetchAll()
accounts, err = h.awsAccountProvider.FetchAll()
}
}

Expand Down Expand Up @@ -93,7 +93,7 @@ func (h *AccountHandler) GetAccountHandler(w http.ResponseWriter, r *http.Reques
// by the swagger openAPI spec.

// Get the account from DynamoDB
sandbox, err := h.accountProvider.FetchByName(accountName)
sandbox, err := h.awsAccountProvider.FetchByName(accountName)
if err != nil {
if err == models.ErrAccountNotFound {
log.Logger.Warn("GET account", "error", err)
Expand Down Expand Up @@ -132,7 +132,7 @@ func (h *AccountHandler) CleanupAccountHandler(w http.ResponseWriter, r *http.Re
// by the swagger openAPI spec.

// Get the account from DynamoDB
sandbox, err := h.accountProvider.FetchByName(accountName)
sandbox, err := h.awsAccountProvider.FetchByName(accountName)
if err != nil {
if err == models.ErrAccountNotFound {
log.Logger.Warn("GET account", "error", err)
Expand All @@ -153,7 +153,7 @@ func (h *AccountHandler) CleanupAccountHandler(w http.ResponseWriter, r *http.Re
return
}
// Mark account for cleanup
if err := h.accountProvider.MarkForCleanup(sandbox.Name); err != nil {
if err := h.awsAccountProvider.MarkForCleanup(sandbox.Name); err != nil {
log.Logger.Error("PUT account cleanup", "error", err)
w.WriteHeader(http.StatusInternalServerError)
render.Render(w, r, &v1.Error{
Expand Down Expand Up @@ -181,7 +181,7 @@ func (h *BaseHandler) LifeCycleAccountHandler(action string) http.HandlerFunc {
reqId := GetReqID(r.Context())

// Get the account from DynamoDB
sandbox, err := h.accountProvider.FetchByName(accountName)
sandbox, err := h.awsAccountProvider.FetchByName(accountName)
if err != nil {
if err == models.ErrAccountNotFound {
log.Logger.Warn("GET account", "error", err)
Expand Down Expand Up @@ -242,7 +242,7 @@ func (h *BaseHandler) GetStatusAccountHandler(w http.ResponseWriter, r *http.Req
// by the swagger openAPI spec.

// Get the account from DynamoDB
sandbox, err := h.accountProvider.FetchByName(accountName)
sandbox, err := h.awsAccountProvider.FetchByName(accountName)
if err != nil {
if err == models.ErrAccountNotFound {
log.Logger.Warn("GET account", "error", err)
Expand Down
30 changes: 15 additions & 15 deletions cmd/sandbox-api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ type BaseHandler struct {
svc *dynamodb.DynamoDB
doc *openapi3.T
oaRouter oarouters.Router
accountProvider models.AwsAccountProvider
awsAccountProvider models.AwsAccountProvider
}

type AdminHandler struct {
BaseHandler
tokenAuth *jwtauth.JWTAuth
}

func NewBaseHandler(svc *dynamodb.DynamoDB, dbpool *pgxpool.Pool, doc *openapi3.T, oaRouter oarouters.Router, accountProvider models.AwsAccountProvider) *BaseHandler {
func NewBaseHandler(svc *dynamodb.DynamoDB, dbpool *pgxpool.Pool, doc *openapi3.T, oaRouter oarouters.Router, awsAccountProvider models.AwsAccountProvider) *BaseHandler {
return &BaseHandler{
svc: svc,
dbpool: dbpool,
doc: doc,
oaRouter: oaRouter,
accountProvider: accountProvider,
awsAccountProvider: awsAccountProvider,
}
}

Expand All @@ -53,7 +53,7 @@ func NewAdminHandler(b *BaseHandler, tokenAuth *jwtauth.JWTAuth) *AdminHandler {
dbpool: b.dbpool,
doc: b.doc,
oaRouter: b.oaRouter,
accountProvider: b.accountProvider,
awsAccountProvider: b.awsAccountProvider,
},
tokenAuth: tokenAuth,
}
Expand Down Expand Up @@ -111,7 +111,7 @@ func (h *BaseHandler) CreatePlacementHandler(w http.ResponseWriter, r *http.Requ
switch request.Kind {
case "AwsSandbox", "AwsAccount", "aws_account":
// Create the placement in AWS
accounts, err := h.accountProvider.Request(
accounts, err := h.awsAccountProvider.Request(
placementRequest.ServiceUuid,
placementRequest.Reservation,
request.Count,
Expand Down Expand Up @@ -262,7 +262,7 @@ func (h *BaseHandler) GetPlacementHandler(w http.ResponseWriter, r *http.Request
log.Logger.Error("GetPlacementHandler", "error", err)
return
}
placement.LoadActiveResourcesWithCreds(h.accountProvider)
placement.LoadActiveResourcesWithCreds(h.awsAccountProvider)

w.WriteHeader(http.StatusOK)
render.Render(w, r, placement)
Expand All @@ -272,7 +272,7 @@ func (h *BaseHandler) GetPlacementHandler(w http.ResponseWriter, r *http.Request
func (h *BaseHandler) DeletePlacementHandler(w http.ResponseWriter, r *http.Request) {
serviceUuid := chi.URLParam(r, "uuid")

err := models.DeletePlacementByServiceUuid(h.dbpool, h.accountProvider, serviceUuid)
err := models.DeletePlacementByServiceUuid(h.dbpool, h.awsAccountProvider, serviceUuid)
if err != nil {
if err == pgx.ErrNoRows {
w.WriteHeader(http.StatusNotFound)
Expand Down Expand Up @@ -341,7 +341,7 @@ func (h *BaseHandler) LifeCyclePlacementHandler(action string) http.HandlerFunc
if err == pgx.ErrNoRows {
// Legacy services don't have a placement, but stop them anyway

accounts, err := h.accountProvider.FetchAllActiveByServiceUuid(serviceUuid)
accounts, err := h.awsAccountProvider.FetchAllActiveByServiceUuid(serviceUuid)
if err != nil {
log.Logger.Error("GET accounts", "error", err)

Expand Down Expand Up @@ -445,7 +445,7 @@ func (h *BaseHandler) GetStatusPlacementHandler(w http.ResponseWriter, r *http.R
if err == pgx.ErrNoRows {
// Legacy services don't have a placement, but get status using the serviceUUID

accounts, err := h.accountProvider.FetchAllActiveByServiceUuid(serviceUuid)
accounts, err := h.awsAccountProvider.FetchAllActiveByServiceUuid(serviceUuid)
if err != nil {
log.Logger.Error("GET accounts", "error", err)
w.WriteHeader(http.StatusInternalServerError)
Expand Down Expand Up @@ -872,7 +872,7 @@ func (h *BaseHandler) CreateReservationHandler(w http.ResponseWriter, r *http.Re
}

// Validate the request
if message, err := reservationRequest.Validate(h.accountProvider); err != nil {
if message, err := reservationRequest.Validate(h.awsAccountProvider); err != nil {
w.WriteHeader(http.StatusBadRequest)
render.Render(w, r, &v1.Error{
Err: err,
Expand Down Expand Up @@ -916,7 +916,7 @@ func (h *BaseHandler) CreateReservationHandler(w http.ResponseWriter, r *http.Re
return
}

go reservation.Initialize(h.dbpool, h.accountProvider)
go reservation.Initialize(h.dbpool, h.awsAccountProvider)

w.WriteHeader(http.StatusAccepted)
render.Render(w, r, &v1.ReservationResponse{
Expand Down Expand Up @@ -963,7 +963,7 @@ func (h *BaseHandler) DeleteReservationHandler(w http.ResponseWriter, r *http.Re
return
}

go reservation.Remove(h.dbpool, h.accountProvider)
go reservation.Remove(h.dbpool, h.awsAccountProvider)

w.WriteHeader(http.StatusAccepted)
render.Render(w, r, &v1.ReservationResponse{
Expand Down Expand Up @@ -1014,7 +1014,7 @@ func (h *BaseHandler) UpdateReservationHandler(w http.ResponseWriter, r *http.Re
}

// Validate the request
if message, err := reservationReq.Validate(h.accountProvider); err != nil {
if message, err := reservationReq.Validate(h.awsAccountProvider); err != nil {
w.WriteHeader(http.StatusBadRequest)
render.Render(w, r, &v1.Error{
Err: err,
Expand All @@ -1038,7 +1038,7 @@ func (h *BaseHandler) UpdateReservationHandler(w http.ResponseWriter, r *http.Re
}

// Async Update the reservation
go reservation.Update(h.dbpool, h.accountProvider, reservationReq)
go reservation.Update(h.dbpool, h.awsAccountProvider, reservationReq)

w.WriteHeader(http.StatusAccepted)
render.Render(w, r, &v1.ReservationResponse{
Expand Down Expand Up @@ -1108,7 +1108,7 @@ func (h *BaseHandler) GetReservationResourcesHandler(w http.ResponseWriter, r *h
return
}

accounts, err := h.accountProvider.FetchAllByReservation(reservation.Name)
accounts, err := h.awsAccountProvider.FetchAllByReservation(reservation.Name)

if err != nil {
log.Logger.Error("GET accounts", "error", err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/sandbox-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func main() {
// DynamoDB
// ---------------------------------------------------------------------
sandboxdb.CheckEnv()
accountProvider := sandboxdb.NewAwsAccountDynamoDBProviderWithSecret(vaultSecret)
awsAccountProvider := sandboxdb.NewAwsAccountDynamoDBProviderWithSecret(vaultSecret)

// ---------------------------------------------------------------------
// Setup JWT
Expand All @@ -128,10 +128,10 @@ func main() {
// to the handler maker.
// When we need to migrate to Postgresql, we can pass a different "Provider" which will
// implement the same interface.
accountHandler := NewAccountHandler(accountProvider)
accountHandler := NewAccountHandler(awsAccountProvider)

// Factory for handlers which need connections to both databases
baseHandler := NewBaseHandler(accountProvider.Svc, dbPool, doc, oaRouter, accountProvider)
baseHandler := NewBaseHandler(awsAccountProvider.Svc, dbPool, doc, oaRouter, awsAccountProvider)

// Admin handler adds tokenAuth to the baseHandler
adminHandler := NewAdminHandler(baseHandler, tokenAuth)
Expand Down
8 changes: 4 additions & 4 deletions cmd/sandbox-api/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Worker struct {
Dbpool *pgxpool.Pool

// Account provider to interact with the database
AccountProvider models.AwsAccountProvider
AwsAccountProvider models.AwsAccountProvider

// AWS client to manage the accounts
StsClient *sts.Client
Expand Down Expand Up @@ -55,7 +55,7 @@ func (w Worker) Execute(j *models.LifecycleResourceJob) error {
switch j.ResourceType {
case "AwsSandbox", "AwsAccount", "aws_account":
// Get the sandbox
sandbox, err := w.AccountProvider.FetchByName(j.ResourceName)
sandbox, err := w.AwsAccountProvider.FetchByName(j.ResourceName)
if err != nil {
log.Logger.Error("Error fetching sandbox", "error", err)
return err
Expand Down Expand Up @@ -199,7 +199,7 @@ WorkerLoop:
}

// Get all accounts in the placement
if err := placement.LoadActiveResources(w.AccountProvider); err != nil {
if err := placement.LoadActiveResources(w.AwsAccountProvider); err != nil {
log.Logger.Error("Error loading resources", "error", err, "placement", placement)
job.SetStatus("error")
continue WorkerLoop
Expand Down Expand Up @@ -331,7 +331,7 @@ func NewWorker(baseHandler BaseHandler) Worker {

return Worker{
Dbpool: baseHandler.dbpool,
AccountProvider: baseHandler.accountProvider,
AwsAccountProvider: baseHandler.awsAccountProvider,
StsClient: stsClient,
}
}
8 changes: 4 additions & 4 deletions todo.org
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@
**** DONE Update OpenAPI schema to support passing the group parameter
**** DONE patch handlers
***** DONE safeguard: do not allow reservation if capacity (after reservation) is <20%
*** TODO conan: preserve group info when cleaning up
*** TODO update AWS sandbox prometheus endpoint to show group information
*** TODO update sandbox-list: add group column
*** TODO patch legacy scripts to filter out accounts that are in a group
*** TODO conan: preserve reservation when cleaning up
*** TODO update AWS sandbox prometheus endpoint to show reservation information
*** TODO update sandbox-list: add reservation column
*** TODO patch legacy scripts to filter out accounts that are in a reservation
*** TODO expiration
**** make sure a comment is added to accounts when their expiration expires, so we can find them back.

0 comments on commit df9f464

Please sign in to comment.