controller_handlers.go 5.5 KB

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