From df9f4646d4f5c51dc214039f655408e3d928b7de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Cor=C3=A9?= Date: Wed, 27 Sep 2023 12:08:17 +0200 Subject: [PATCH] Rename accountProvider to awsAccountProvider As we'll onboard more accountprovider in the future, make it clear that is for Aws. --- cmd/sandbox-api/account_handlers.go | 22 ++++++++++----------- cmd/sandbox-api/handlers.go | 30 ++++++++++++++--------------- cmd/sandbox-api/main.go | 6 +++--- cmd/sandbox-api/workers.go | 8 ++++---- todo.org | 8 ++++---- 5 files changed, 37 insertions(+), 37 deletions(-) diff --git a/cmd/sandbox-api/account_handlers.go b/cmd/sandbox-api/account_handlers.go index c6addd22..38820d73 100644 --- a/cmd/sandbox-api/account_handlers.go +++ b/cmd/sandbox-api/account_handlers.go @@ -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, } } @@ -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() } } @@ -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) @@ -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) @@ -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{ @@ -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) @@ -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) diff --git a/cmd/sandbox-api/handlers.go b/cmd/sandbox-api/handlers.go index 15c62f33..05405bbd 100644 --- a/cmd/sandbox-api/handlers.go +++ b/cmd/sandbox-api/handlers.go @@ -28,7 +28,7 @@ type BaseHandler struct { svc *dynamodb.DynamoDB doc *openapi3.T oaRouter oarouters.Router - accountProvider models.AwsAccountProvider + awsAccountProvider models.AwsAccountProvider } type AdminHandler struct { @@ -36,13 +36,13 @@ type AdminHandler struct { 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, } } @@ -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, } @@ -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, @@ -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) @@ -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) @@ -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) @@ -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) @@ -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, @@ -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{ @@ -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{ @@ -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, @@ -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{ @@ -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) diff --git a/cmd/sandbox-api/main.go b/cmd/sandbox-api/main.go index 0b9d95a6..456cbb91 100644 --- a/cmd/sandbox-api/main.go +++ b/cmd/sandbox-api/main.go @@ -106,7 +106,7 @@ func main() { // DynamoDB // --------------------------------------------------------------------- sandboxdb.CheckEnv() - accountProvider := sandboxdb.NewAwsAccountDynamoDBProviderWithSecret(vaultSecret) + awsAccountProvider := sandboxdb.NewAwsAccountDynamoDBProviderWithSecret(vaultSecret) // --------------------------------------------------------------------- // Setup JWT @@ -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) diff --git a/cmd/sandbox-api/workers.go b/cmd/sandbox-api/workers.go index 4497c17b..7ea2af80 100644 --- a/cmd/sandbox-api/workers.go +++ b/cmd/sandbox-api/workers.go @@ -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 @@ -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 @@ -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 @@ -331,7 +331,7 @@ func NewWorker(baseHandler BaseHandler) Worker { return Worker{ Dbpool: baseHandler.dbpool, - AccountProvider: baseHandler.accountProvider, + AwsAccountProvider: baseHandler.awsAccountProvider, StsClient: stsClient, } } diff --git a/todo.org b/todo.org index d5d5ea4b..951dd000 100644 --- a/todo.org +++ b/todo.org @@ -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.