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

Account abstraction #32

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
209 changes: 130 additions & 79 deletions cmd/sandbox-api/account_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"fmt"
"net/http"

sandboxdb "github.com/rhpds/sandbox/internal/dynamodb"

"github.com/jackc/pgx/v4"
"github.com/rhpds/sandbox/internal/api/v1"
v1 "github.com/rhpds/sandbox/internal/api/v1"
"github.com/rhpds/sandbox/internal/log"
"github.com/rhpds/sandbox/internal/models"

Expand All @@ -15,61 +17,105 @@ 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,
}
}

type Account interface {
}

// GetAccountsHandler returns all accounts
// GET /accounts
// GET /accounts/{kind}
func (h *AccountHandler) GetAccountsHandler(w http.ResponseWriter, r *http.Request) {
enc := json.NewEncoder(w)
enc.SetIndent("", " ")

serviceUuid := r.URL.Query().Get("service_uuid")
kind := chi.URLParam(r, "kind")
switch kind {
case "aws":
var (
err error
accounts []models.AwsAccount
)
if serviceUuid != "" {
// Get the account from DynamoDB
accounts, err = h.AwsAccountProvider.FetchAllByServiceUuid(serviceUuid)

} else {
accounts, err = h.AwsAccountProvider.FetchAll()
}
if err != nil {
log.Logger.Error("GET accounts", "error", err)

w.WriteHeader(http.StatusInternalServerError)
enc.Encode(v1.Error{
HTTPStatusCode: 500,
Message: "Error reading accounts",
})
return
}

if len(accounts) == 0 {
w.WriteHeader(http.StatusNotFound)
} else {
w.WriteHeader(http.StatusOK)
}

// Print accounts using JSON
if err := enc.Encode(accounts); err != nil {
log.Logger.Error("GET accounts", "error", err)
w.WriteHeader(http.StatusInternalServerError)
enc.Encode(v1.Error{
HTTPStatusCode: 500,
Message: "Error reading account",
})
}
case "ocp":
log.Logger.Warn("OCP accounts")
accountProvider := sandboxdb.NewOcpAccountDynamoDBProvider()
var (
err error
accounts []models.OcpAccount
)
if serviceUuid != "" {
accounts, err = accountProvider.FetchAllByServiceUuid(serviceUuid)

} else {
log.Logger.Warn("OCP accounts.1")
accounts, err = accountProvider.FetchAll()
log.Logger.Warn("OCP accounts.2")
}
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
enc.Encode(v1.Error{
HTTPStatusCode: 500,
Message: "Error reading accounts",
})
return
}

if len(accounts) == 0 {
w.WriteHeader(http.StatusNotFound)
} else {
w.WriteHeader(http.StatusOK)
}

// Print accounts using JSON
if err := enc.Encode(accounts); err != nil {
log.Logger.Error("GET accounts", "error", err)
w.WriteHeader(http.StatusInternalServerError)
enc.Encode(v1.Error{
HTTPStatusCode: 500,
Message: "Error reading account",
})
}
}

var (
accounts []models.AwsAccount
err error
)
if serviceUuid != "" {
// Get the account from DynamoDB
accounts, err = h.accountProvider.FetchAllByServiceUuid(serviceUuid)

} else {
accounts, err = h.accountProvider.FetchAll()
}

if err != nil {
log.Logger.Error("GET accounts", "error", err)

w.WriteHeader(http.StatusInternalServerError)
enc.Encode(v1.Error{
HTTPStatusCode: 500,
Message: "Error reading accounts",
})
return
}

if len(accounts) == 0 {
w.WriteHeader(http.StatusNotFound)
} else {
w.WriteHeader(http.StatusOK)
}

// Print accounts using JSON
if err := enc.Encode(accounts); err != nil {
log.Logger.Error("GET accounts", "error", err)
w.WriteHeader(http.StatusInternalServerError)
enc.Encode(v1.Error{
HTTPStatusCode: 500,
Message: "Error reading account",
})
}
}

// GetAccountHandler returns an account
Expand All @@ -80,40 +126,45 @@ func (h *AccountHandler) GetAccountHandler(w http.ResponseWriter, r *http.Reques

// Grab the parameters from Params
accountName := chi.URLParam(r, "account")
kind := chi.URLParam(r, "kind")

// We don't need 'kind' param for now as it is checked and validated
// by the swagger openAPI spec.

// Get the account from DynamoDB
sandbox, err := h.accountProvider.FetchByName(accountName)
if err != nil {
if err == models.ErrAccountNotFound {
log.Logger.Warn("GET account", "error", err)
w.WriteHeader(http.StatusNotFound)
enc.Encode(v1.Error{
HTTPStatusCode: http.StatusNotFound,
Message: "Account not found",
})
return
}
log.Logger.Error("GET account", "error", err)

w.WriteHeader(http.StatusInternalServerError)
enc.Encode(v1.Error{
HTTPStatusCode: 500,
Message: "Error reading account",
})
return
}
// Print account using JSON
if err := enc.Encode(sandbox); err != nil {
log.Logger.Error("GET account", "error", err)
w.WriteHeader(http.StatusInternalServerError)
enc.Encode(v1.Error{
HTTPStatusCode: 500,
Message: "Error reading account",
})
}
switch kind {
case "aws":
// Get the account from DynamoDB
sandbox, err := h.AwsAccountProvider.FetchByName(accountName)
if err != nil {
if err == models.ErrAccountNotFound {
log.Logger.Warn("GET account", "error", err)
w.WriteHeader(http.StatusNotFound)
enc.Encode(v1.Error{
HTTPStatusCode: http.StatusNotFound,
Message: "Account not found",
})
return
}
log.Logger.Error("GET account", "error", err)

w.WriteHeader(http.StatusInternalServerError)
enc.Encode(v1.Error{
HTTPStatusCode: 500,
Message: "Error reading account",
})
return
}
// Print account using JSON
if err := enc.Encode(sandbox); err != nil {
log.Logger.Error("GET account", "error", err)
w.WriteHeader(http.StatusInternalServerError)
enc.Encode(v1.Error{
HTTPStatusCode: 500,
Message: "Error reading account",
})
}
case "ocp":
log.Logger.Warn("Implementing")
}
}

func (h *AccountHandler) CleanupAccountHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -124,7 +175,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 @@ -145,7 +196,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 @@ -173,7 +224,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 @@ -233,7 +284,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
39 changes: 30 additions & 9 deletions cmd/sandbox-api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strconv"
"time"

sandboxdb "github.com/rhpds/sandbox/internal/dynamodb"

"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/getkin/kin-openapi/openapi3"
oarouters "github.com/getkin/kin-openapi/routers"
Expand All @@ -26,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 @@ -51,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 @@ -109,7 +111,7 @@ func (h *BaseHandler) CreatePlacementHandler(w http.ResponseWriter, r *http.Requ
switch request.Kind {
case "AwsSandbox":
// Create the placement in AWS
accounts, err := h.accountProvider.Request(placementRequest.ServiceUuid, request.Count, placementRequest.Annotations)
accounts, err := h.awsAccountProvider.Request(placementRequest.ServiceUuid, request.Count, placementRequest.Annotations)
if err != nil {
if err == models.ErrNoEnoughAccountsAvailable {
w.WriteHeader(http.StatusInsufficientStorage)
Expand All @@ -134,6 +136,25 @@ func (h *BaseHandler) CreatePlacementHandler(w http.ResponseWriter, r *http.Requ
log.Logger.Info("AWS sandbox booked", "account", account.Name, "service_uuid", placementRequest.ServiceUuid)
resources = append(resources, account)
}
case "OcpAccount":
accountProvider := sandboxdb.NewOcpAccountDynamoDBProvider()
accounts, err := accountProvider.Request(placementRequest.ServiceUuid, request.Count, placementRequest.Annotations)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
render.Render(w, r, &v1.Error{
Err: err,
HTTPStatusCode: http.StatusInternalServerError,
Message: "Error creating account on OCP",
})
log.Logger.Error("CreatePlacementHandler", "error", err)
return
}

for _, account := range accounts {
log.Logger.Info("OCP sandbox booked", "account", account.Name, "service_uuid", placementRequest.ServiceUuid)
resources = append(resources, account)
}

default:
w.WriteHeader(http.StatusBadRequest)
render.Render(w, r, &v1.Error{
Expand Down Expand Up @@ -255,7 +276,7 @@ func (h *BaseHandler) GetPlacementHandler(w http.ResponseWriter, r *http.Request
log.Logger.Error("GetPlacementHandler", "error", err)
return
}
placement.LoadResourcesWithCreds(h.accountProvider)
placement.LoadResourcesWithCreds(h.awsAccountProvider)

w.WriteHeader(http.StatusOK)
render.Render(w, r, placement)
Expand All @@ -265,7 +286,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 @@ -333,7 +354,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.FetchAllByServiceUuid(serviceUuid)
accounts, err := h.awsAccountProvider.FetchAllByServiceUuid(serviceUuid)
if err != nil {
log.Logger.Error("GET accounts", "error", err)

Expand Down Expand Up @@ -436,7 +457,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.FetchAllByServiceUuid(serviceUuid)
accounts, err := h.awsAccountProvider.FetchAllByServiceUuid(serviceUuid)
if err != nil {
log.Logger.Error("GET accounts", "error", err)
w.WriteHeader(http.StatusInternalServerError)
Expand Down
2 changes: 1 addition & 1 deletion cmd/sandbox-api/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func NewWorker(baseHandler BaseHandler) Worker {

return Worker{
Dbpool: baseHandler.dbpool,
AccountProvider: baseHandler.accountProvider,
AccountProvider: baseHandler.awsAccountProvider,
StsClient: stsClient,
}
}
Loading