| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package api
- import (
- "encoding/json"
- "net/http"
- "net/url"
- "strconv"
- "github.com/go-chi/chi"
- "github.com/porter-dev/porter/internal/models"
- "gorm.io/gorm"
- )
- type HandleUpdateNotificationConfigForm struct {
- Payload struct {
- Enabled bool `json:"enabled"`
- Success bool `json:"success"`
- Failure bool `json:"failure"`
- } `json:"payload"`
- Namespace string `json:"namespace"`
- ClusterID uint `json:"cluster_id"`
- }
- // HandleUpdateNotificationConfig updates notification settings for a given release
- func (app *App) HandleUpdateNotificationConfig(w http.ResponseWriter, r *http.Request) {
- name := chi.URLParam(r, "name")
- form := &HandleUpdateNotificationConfigForm{}
- if err := json.NewDecoder(r.Body).Decode(form); err != nil {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- release, err := app.Repo.Release().ReadRelease(form.ClusterID, name, form.Namespace)
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- w.WriteHeader(http.StatusNotFound)
- return
- }
- app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
- Code: ErrReleaseReadData,
- Errors: []string{"release not found"},
- }, w)
- }
- // either create a new notification config or update the current one
- newConfig := &models.NotificationConfig{
- Enabled: form.Payload.Enabled,
- Success: form.Payload.Success,
- Failure: form.Payload.Failure,
- }
- if release.NotificationConfig == 0 {
- newConfig, err = app.Repo.NotificationConfig().CreateNotificationConfig(newConfig)
- if err != nil {
- app.handleErrorInternal(err, w)
- return
- }
- release.NotificationConfig = newConfig.ID
- release, err = app.Repo.Release().UpdateRelease(release)
- } else {
- newConfig.ID = release.NotificationConfig
- newConfig, err = app.Repo.NotificationConfig().UpdateNotificationConfig(newConfig)
- }
- if err != nil {
- app.handleErrorInternal(err, w)
- return
- }
- w.WriteHeader(http.StatusOK)
- }
- // HandleGetNotificationConfig gets the notification config for a given release
- func (app *App) HandleGetNotificationConfig(w http.ResponseWriter, r *http.Request) {
- projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
- if err != nil || projID == 0 {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- vals, err := url.ParseQuery(r.URL.RawQuery)
- name := chi.URLParam(r, "name")
- namespace := vals["namespace"][0]
- clusterID, err := strconv.ParseUint(vals["cluster_id"][0], 10, 64)
- if err != nil {
- app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
- Code: ErrReleaseReadData,
- Errors: []string{"release not found"},
- }, w)
- return
- }
- release, err := app.Repo.Release().ReadRelease(uint(clusterID), name, namespace)
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- w.WriteHeader(http.StatusNotFound)
- return
- }
- app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
- Code: ErrReleaseReadData,
- Errors: []string{"release not found"},
- }, w)
- return
- }
- config := &models.NotificationConfigExternal{
- Enabled: true,
- Success: true,
- Failure: true,
- }
- if release.NotificationConfig != 0 {
- notifConfig, err := app.Repo.NotificationConfig().ReadNotificationConfig(release.NotificationConfig)
- if err != nil {
- app.handleErrorInternal(err, w)
- }
- config = notifConfig.Externalize()
- }
- err = json.NewEncoder(w).Encode(config)
- if err != nil {
- app.handleErrorInternal(err, w)
- }
- }
|