controller_handlers.go 8.3 KB

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