controller_handlers.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package config
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "strings"
  9. "github.com/julienschmidt/httprouter"
  10. proto "github.com/opencost/opencost/core/pkg/protocol"
  11. "github.com/opencost/opencost/pkg/cloud"
  12. "github.com/opencost/opencost/pkg/cloud/aws"
  13. "github.com/opencost/opencost/pkg/cloud/azure"
  14. "github.com/opencost/opencost/pkg/cloud/gcp"
  15. )
  16. var protocol = proto.HTTP()
  17. func (c *Controller) cloudCostChecks() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  18. // If Pipeline is nil, always return 503
  19. if c == nil {
  20. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  21. http.Error(w, "ConfigController: is nil", http.StatusServiceUnavailable)
  22. }
  23. }
  24. return nil
  25. }
  26. // GetExportConfigHandler creates a handler from a http request which exports an integration via the integrationController
  27. func (c *Controller) GetExportConfigHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  28. // perform basic checks to ensure that the pipeline can be accessed
  29. fn := c.cloudCostChecks()
  30. if fn != nil {
  31. return fn
  32. }
  33. // Return valid handler func
  34. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  35. w.Header().Set("Content-Type", "application/json")
  36. integrationKey := r.URL.Query().Get("integrationKey")
  37. configs, err := c.ExportConfigs(integrationKey)
  38. if err != nil {
  39. http.Error(w, err.Error(), http.StatusBadRequest)
  40. return
  41. }
  42. protocol.WriteDataWithMessage(w, configs, "Configurations have been sanitized to protect secrets")
  43. }
  44. }
  45. // GetAddConfigHandler creates a handler from a http request which adds an integration via the integrationController
  46. func (c *Controller) GetAddConfigHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  47. // perform basic checks to ensure that the pipeline can be accessed
  48. fn := c.cloudCostChecks()
  49. if fn != nil {
  50. return fn
  51. }
  52. // Return valid handler func
  53. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  54. w.Header().Set("Content-Type", "application/json")
  55. configType := r.URL.Query().Get("type")
  56. if configType == "" {
  57. http.Error(w, "'type' parameter is required", http.StatusBadRequest)
  58. return
  59. }
  60. config, err := ParseConfig(configType, r.Body)
  61. if err != nil {
  62. http.Error(w, err.Error(), http.StatusBadRequest)
  63. return
  64. }
  65. err = c.CreateConfig(config)
  66. if err != nil {
  67. http.Error(w, err.Error(), http.StatusBadRequest)
  68. return
  69. }
  70. protocol.WriteData(w, fmt.Sprintf("Successfully added integration with key %s", config.Key()))
  71. }
  72. }
  73. // GetUpdateConfigHandler creates a handler from a http request which updates an existing integration via the integrationController
  74. func (c *Controller) GetUpdateConfigHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  75. // perform basic checks to ensure that the pipeline can be accessed
  76. fn := c.cloudCostChecks()
  77. if fn != nil {
  78. return fn
  79. }
  80. // Return valid handler func
  81. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  82. w.Header().Set("Content-Type", "application/json")
  83. configType := r.URL.Query().Get("type")
  84. if configType == "" {
  85. http.Error(w, "'type' parameter is required", http.StatusBadRequest)
  86. return
  87. }
  88. config, err := ParseConfig(configType, r.Body)
  89. if err != nil {
  90. http.Error(w, err.Error(), http.StatusBadRequest)
  91. return
  92. }
  93. err = c.UpdateConfig(config)
  94. if err != nil {
  95. http.Error(w, err.Error(), http.StatusBadRequest)
  96. return
  97. }
  98. protocol.WriteData(w, fmt.Sprintf("Successfully updated integration with key %s", config.Key()))
  99. }
  100. }
  101. func ParseConfig(configType string, body io.Reader) (cloud.KeyedConfig, error) {
  102. buf := new(bytes.Buffer)
  103. _, err := buf.ReadFrom(body)
  104. if err != nil {
  105. return nil, fmt.Errorf("failed to read body: %w", err)
  106. }
  107. bytes := buf.Bytes()
  108. switch strings.ToLower(configType) {
  109. case S3ConfigType:
  110. config := &aws.S3Configuration{}
  111. err = json.Unmarshal(bytes, config)
  112. if err != nil {
  113. return nil, fmt.Errorf("error unmarshalling S3 Configuration: %w", err)
  114. }
  115. return config, nil
  116. case AthenaConfigType:
  117. config := &aws.AthenaConfiguration{}
  118. err = json.Unmarshal(bytes, config)
  119. if err != nil {
  120. return nil, fmt.Errorf("error unmarshalling Athena Configuration: %w", err)
  121. }
  122. return config, nil
  123. case BigQueryConfigType:
  124. config := &gcp.BigQueryConfiguration{}
  125. err = json.Unmarshal(bytes, config)
  126. if err != nil {
  127. return nil, fmt.Errorf("error unmarshalling Big Query Configuration: %w", err)
  128. }
  129. return config, nil
  130. case AzureStorageConfigType:
  131. config := &azure.StorageConfiguration{}
  132. err = json.Unmarshal(bytes, config)
  133. if err != nil {
  134. return nil, fmt.Errorf("error unmarshalling Azure Storage Configuration: %w", err)
  135. }
  136. return config, nil
  137. }
  138. return nil, fmt.Errorf("provided config type was not recognised %s", configType)
  139. }
  140. // GetEnableConfigHandler creates a handler from a http request which enables an integration via the integrationController
  141. func (c *Controller) GetEnableConfigHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  142. // perform basic checks to ensure that the pipeline can be accessed
  143. fn := c.cloudCostChecks()
  144. if fn != nil {
  145. return fn
  146. }
  147. // Return valid handler func
  148. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  149. w.Header().Set("Content-Type", "application/json")
  150. integrationKey := r.URL.Query().Get("integrationKey")
  151. if integrationKey == "" {
  152. http.Error(w, "required parameter 'integrationKey' is missing", http.StatusBadRequest)
  153. return
  154. }
  155. source := r.URL.Query().Get("source")
  156. if source == "" {
  157. http.Error(w, "required parameter 'source' is missing", http.StatusBadRequest)
  158. return
  159. }
  160. err := c.EnableConfig(integrationKey, source)
  161. if err != nil {
  162. http.Error(w, err.Error(), http.StatusBadRequest)
  163. return
  164. }
  165. protocol.WriteData(w, fmt.Sprintf("Successfully enabled integration with key %s from source %s", integrationKey, source))
  166. }
  167. }
  168. // GetDisableConfigHandler creates a handler from a http request which disables an integration via the integrationController
  169. func (c *Controller) GetDisableConfigHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  170. // perform basic checks to ensure that the pipeline can be accessed
  171. fn := c.cloudCostChecks()
  172. if fn != nil {
  173. return fn
  174. }
  175. // Return valid handler func
  176. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  177. w.Header().Set("Content-Type", "application/json")
  178. integrationKey := r.URL.Query().Get("integrationKey")
  179. if integrationKey == "" {
  180. http.Error(w, "required parameter 'integrationKey' is missing", http.StatusBadRequest)
  181. return
  182. }
  183. source := r.URL.Query().Get("source")
  184. if source == "" {
  185. http.Error(w, "required parameter 'source' is missing", http.StatusBadRequest)
  186. return
  187. }
  188. err := c.DisableConfig(integrationKey, source)
  189. if err != nil {
  190. http.Error(w, err.Error(), http.StatusBadRequest)
  191. return
  192. }
  193. protocol.WriteData(w, fmt.Sprintf("Successfully disabled integration with key %s from source %s", integrationKey, source))
  194. }
  195. }
  196. // GetDeleteConfigHandler creates a handler from a http request which deletes an integration via the integrationController
  197. // if there are no other integrations with the given integration key, it also clears the data.
  198. func (c *Controller) GetDeleteConfigHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  199. // perform basic checks to ensure that the pipeline can be accessed
  200. fn := c.cloudCostChecks()
  201. if fn != nil {
  202. return fn
  203. }
  204. // Return valid handler func
  205. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  206. w.Header().Set("Content-Type", "application/json")
  207. integrationKey := r.URL.Query().Get("integrationKey")
  208. if integrationKey == "" {
  209. http.Error(w, "required parameter 'integrationKey' is missing", http.StatusBadRequest)
  210. return
  211. }
  212. err := c.DeleteConfig(integrationKey, ConfigControllerSource.String())
  213. if err != nil {
  214. http.Error(w, err.Error(), http.StatusBadRequest)
  215. return
  216. }
  217. protocol.WriteData(w, fmt.Sprintf("Successfully deleted integration with key %s", integrationKey))
  218. }
  219. }