| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- package api
- import (
- "encoding/json"
- "net/http"
- "strconv"
- "github.com/go-chi/chi"
- "github.com/porter-dev/porter/internal/forms"
- ints "github.com/porter-dev/porter/internal/models/integrations"
- )
- // HandleListClusterIntegrations lists the cluster integrations available to the
- // instance
- func (app *App) HandleListClusterIntegrations(w http.ResponseWriter, r *http.Request) {
- clusters := ints.PorterClusterIntegrations
- w.WriteHeader(http.StatusOK)
- if err := json.NewEncoder(w).Encode(&clusters); err != nil {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- }
- // HandleListRegistryIntegrations lists the image registry integrations available to the
- // instance
- func (app *App) HandleListRegistryIntegrations(w http.ResponseWriter, r *http.Request) {
- registries := ints.PorterRegistryIntegrations
- w.WriteHeader(http.StatusOK)
- if err := json.NewEncoder(w).Encode(®istries); err != nil {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- }
- // HandleListHelmRepoIntegrations lists the Helm repo integrations available to the
- // instance
- func (app *App) HandleListHelmRepoIntegrations(w http.ResponseWriter, r *http.Request) {
- hrs := ints.PorterHelmRepoIntegrations
- w.WriteHeader(http.StatusOK)
- if err := json.NewEncoder(w).Encode(&hrs); err != nil {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- }
- // HandleListRepoIntegrations lists the repo integrations available to the
- // instance
- func (app *App) HandleListRepoIntegrations(w http.ResponseWriter, r *http.Request) {
- repos := ints.PorterGitRepoIntegrations
- w.WriteHeader(http.StatusOK)
- if err := json.NewEncoder(w).Encode(&repos); err != nil {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- }
- // HandleCreateGCPIntegration creates a new GCP integration in the DB
- func (app *App) HandleCreateGCPIntegration(w http.ResponseWriter, r *http.Request) {
- session, err := app.store.Get(r, app.cookieName)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- userID, _ := session.Values["user_id"].(uint)
- projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
- if err != nil || projID == 0 {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- form := &forms.CreateGCPIntegrationForm{
- UserID: userID,
- ProjectID: uint(projID),
- }
- // decode from JSON to form value
- if err := json.NewDecoder(r.Body).Decode(form); err != nil {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- // validate the form
- if err := app.validator.Struct(form); err != nil {
- app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
- return
- }
- // convert the form to a gcp integration
- gcp, err := form.ToGCPIntegration()
- if err != nil {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- // handle write to the database
- gcp, err = app.repo.GCPIntegration.CreateGCPIntegration(gcp)
- if err != nil {
- app.handleErrorDataWrite(err, w)
- return
- }
- app.logger.Info().Msgf("New gcp integration created: %d", gcp.ID)
- w.WriteHeader(http.StatusCreated)
- gcpExt := gcp.Externalize()
- if err := json.NewEncoder(w).Encode(gcpExt); err != nil {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- }
- // HandleCreateAWSIntegration creates a new AWS integration in the DB
- func (app *App) HandleCreateAWSIntegration(w http.ResponseWriter, r *http.Request) {
- session, err := app.store.Get(r, app.cookieName)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- userID, _ := session.Values["user_id"].(uint)
- projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
- if err != nil || projID == 0 {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- form := &forms.CreateAWSIntegrationForm{
- UserID: userID,
- ProjectID: uint(projID),
- }
- // decode from JSON to form value
- if err := json.NewDecoder(r.Body).Decode(form); err != nil {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- // validate the form
- if err := app.validator.Struct(form); err != nil {
- app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
- return
- }
- // convert the form to a aws integration
- aws, err := form.ToAWSIntegration()
- if err != nil {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- // handle write to the database
- aws, err = app.repo.AWSIntegration.CreateAWSIntegration(aws)
- if err != nil {
- app.handleErrorDataWrite(err, w)
- return
- }
- app.logger.Info().Msgf("New aws integration created: %d", aws.ID)
- w.WriteHeader(http.StatusCreated)
- awsExt := aws.Externalize()
- if err := json.NewEncoder(w).Encode(awsExt); err != nil {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- }
- // HandleCreateBasicAuthIntegration creates a new basic auth integration in the DB
- func (app *App) HandleCreateBasicAuthIntegration(w http.ResponseWriter, r *http.Request) {
- session, err := app.store.Get(r, app.cookieName)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- userID, _ := session.Values["user_id"].(uint)
- projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
- if err != nil || projID == 0 {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- form := &forms.CreateBasicAuthIntegrationForm{
- UserID: userID,
- ProjectID: uint(projID),
- }
- // decode from JSON to form value
- if err := json.NewDecoder(r.Body).Decode(form); err != nil {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- // validate the form
- if err := app.validator.Struct(form); err != nil {
- app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
- return
- }
- // convert the form to a gcp integration
- basic, err := form.ToBasicIntegration()
- if err != nil {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- // handle write to the database
- basic, err = app.repo.BasicIntegration.CreateBasicIntegration(basic)
- if err != nil {
- app.handleErrorDataWrite(err, w)
- return
- }
- app.logger.Info().Msgf("New basic integration created: %d", basic.ID)
- w.WriteHeader(http.StatusCreated)
- basicExt := basic.Externalize()
- if err := json.NewEncoder(w).Encode(basicExt); err != nil {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- }
|