controller_handlers.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package config
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/julienschmidt/httprouter"
  6. "github.com/opencost/opencost/pkg/env"
  7. "github.com/opencost/opencost/pkg/proto"
  8. )
  9. var protocol = proto.HTTP()
  10. func (c *Controller) cloudCostChecks() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  11. // If Pipeline is nil, always return 503
  12. if c == nil {
  13. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  14. http.Error(w, "ConfigController: is nil", http.StatusServiceUnavailable)
  15. }
  16. }
  17. if !env.IsCloudCostEnabled() {
  18. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  19. http.Error(w, "ConfigController: is not enabled", http.StatusServiceUnavailable)
  20. }
  21. }
  22. return nil
  23. }
  24. // GetEnableConfigHandler creates a handler from a http request which enables an integration via the integrationController
  25. func (c *Controller) GetExportConfigHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  26. // perform basic checks to ensure that the pipeline can be accessed
  27. fn := c.cloudCostChecks()
  28. if fn != nil {
  29. return fn
  30. }
  31. // Return valid handler func
  32. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  33. w.Header().Set("Content-Type", "application/json")
  34. integrationKey := r.URL.Query().Get("integrationKey")
  35. configs, err := c.ExportConfigs(integrationKey)
  36. if err != nil {
  37. http.Error(w, err.Error(), http.StatusBadRequest)
  38. return
  39. }
  40. protocol.WriteDataWithMessage(w, configs, "Configurations have been sanitized to protect secrets")
  41. }
  42. }
  43. // GetEnableConfigHandler creates a handler from a http request which enables an integration via the integrationController
  44. func (c *Controller) GetEnableConfigHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  45. // perform basic checks to ensure that the pipeline can be accessed
  46. fn := c.cloudCostChecks()
  47. if fn != nil {
  48. return fn
  49. }
  50. // Return valid handler func
  51. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  52. w.Header().Set("Content-Type", "application/json")
  53. integrationKey := r.URL.Query().Get("integrationKey")
  54. if integrationKey == "" {
  55. http.Error(w, "required parameter 'integrationKey' is missing", http.StatusBadRequest)
  56. return
  57. }
  58. source := r.URL.Query().Get("source")
  59. if source == "" {
  60. http.Error(w, "required parameter 'source' is missing", http.StatusBadRequest)
  61. return
  62. }
  63. err := c.EnableConfig(integrationKey, source)
  64. if err != nil {
  65. http.Error(w, err.Error(), http.StatusBadRequest)
  66. return
  67. }
  68. protocol.WriteData(w, fmt.Sprintf("Successfully enabled integration with key %s from source %s", integrationKey, source))
  69. }
  70. }
  71. // GetDisableConfigHandler creates a handler from a http request which disables an integration via the integrationController
  72. func (c *Controller) GetDisableConfigHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  73. // perform basic checks to ensure that the pipeline can be accessed
  74. fn := c.cloudCostChecks()
  75. if fn != nil {
  76. return fn
  77. }
  78. // Return valid handler func
  79. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  80. w.Header().Set("Content-Type", "application/json")
  81. integrationKey := r.URL.Query().Get("integrationKey")
  82. if integrationKey == "" {
  83. http.Error(w, "required parameter 'integrationKey' is missing", http.StatusBadRequest)
  84. return
  85. }
  86. source := r.URL.Query().Get("source")
  87. if source == "" {
  88. http.Error(w, "required parameter 'source' is missing", http.StatusBadRequest)
  89. return
  90. }
  91. err := c.DisableConfig(integrationKey, source)
  92. if err != nil {
  93. http.Error(w, err.Error(), http.StatusBadRequest)
  94. return
  95. }
  96. protocol.WriteData(w, fmt.Sprintf("Successfully disabled integration with key %s from source %s", integrationKey, source))
  97. }
  98. }
  99. // GetDeleteConfigHandler creates a handler from a http request which deletes an integration via the integrationController
  100. // if there are no other integrations with the given integration key, it also clears the data.
  101. func (c *Controller) GetDeleteConfigHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  102. // perform basic checks to ensure that the pipeline can be accessed
  103. fn := c.cloudCostChecks()
  104. if fn != nil {
  105. return fn
  106. }
  107. // Return valid handler func
  108. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  109. w.Header().Set("Content-Type", "application/json")
  110. integrationKey := r.URL.Query().Get("integrationKey")
  111. if integrationKey == "" {
  112. http.Error(w, "required parameter 'integrationKey' is missing", http.StatusBadRequest)
  113. return
  114. }
  115. source := r.URL.Query().Get("source")
  116. if source == "" {
  117. http.Error(w, "required parameter 'source' is missing", http.StatusBadRequest)
  118. return
  119. }
  120. err := c.DeleteConfig(integrationKey, source)
  121. if err != nil {
  122. http.Error(w, err.Error(), http.StatusBadRequest)
  123. return
  124. }
  125. protocol.WriteData(w, fmt.Sprintf("Successfully deleted integration with key %s from source %s", integrationKey, source))
  126. for _, intStat := range c.GetStatus() {
  127. if intStat.Key == integrationKey {
  128. protocol.WriteData(w, fmt.Sprintf("Found addition integration with integration key %s from source %s. If you wish to delete this data do so manually or delete all integrations with matching keys", integrationKey, intStat.Source))
  129. return
  130. }
  131. }
  132. protocol.WriteData(w, fmt.Sprintf("Successfully deleted cloud cost data with key %s", integrationKey))
  133. }
  134. }