package controllers import ( "net/http" "code.crute.us/mcrute/cloud-identity-broker/app" "code.crute.us/mcrute/golib/echo/controller" "github.com/labstack/echo/v4" ) type jsonRegion struct { Name string `json:"name"` Enabled bool `json:"enabled"` Default bool `json:"default"` CredentialsURL string `json:"credentials_url,omitempty"` } type APIRegionListHandler struct { *AWSAPI urls app.AppURL } func NewAPIRegionListHandler(a *AWSAPI) echo.HandlerFunc { al := &APIRegionListHandler{ AWSAPI: a, urls: app.AppURL{}, } h := &controller.ContentTypeNegotiatingHandler{ DefaultHandler: al.Handle, Handlers: map[string]echo.HandlerFunc{ contentTypeV1: al.Handle, contentTypeV2: al.Handle, }, } return h.Handle } func (h *APIRegionListHandler) Handle(c echo.Context) error { rc, err := h.GetContext(c) // Does authorization checks if err != nil { return err } regions, err := rc.AWS.GetRegionList() if err != nil { c.Logger().Errorf("Failed to load region list: %w", err) return echo.ErrInternalServerError } out := make([]*jsonRegion, len(regions)) for i, r := range regions { out[i] = &jsonRegion{ Name: r.Name, Enabled: r.Enabled, Default: rc.Account.DefaultRegion == r.Name, } if r.Enabled { out[i].CredentialsURL = h.urls.AccountCredentials( c, "aws", rc.Account.ShortName, r.Name) } } return c.JSON(http.StatusOK, out) }