controller_handlers.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. config, err := ParseConfig(configType, r.Body)
  57. if err != nil {
  58. http.Error(w, err.Error(), http.StatusBadRequest)
  59. return
  60. }
  61. err = c.CreateConfig(config)
  62. if err != nil {
  63. http.Error(w, err.Error(), http.StatusBadRequest)
  64. return
  65. }
  66. protocol.WriteData(w, fmt.Sprintf("Successfully added integration with key %s", config.Key()))
  67. }
  68. }
  69. func ParseConfig(configType string, body io.Reader) (cloud.KeyedConfig, error) {
  70. buf := new(bytes.Buffer)
  71. _, err := buf.ReadFrom(body)
  72. if err != nil {
  73. return nil, fmt.Errorf("failed to read body: %w", err)
  74. }
  75. bytes := buf.Bytes()
  76. switch strings.ToLower(configType) {
  77. case S3ConfigType:
  78. config := &aws.S3Configuration{}
  79. err = json.Unmarshal(bytes, config)
  80. if err != nil {
  81. return nil, fmt.Errorf("error unmarshalling S3 Configuration: %w", err)
  82. }
  83. return config, nil
  84. case AthenaConfigType:
  85. config := &aws.AthenaConfiguration{}
  86. err = json.Unmarshal(bytes, config)
  87. if err != nil {
  88. return nil, fmt.Errorf("error unmarshalling Athena Configuration: %w", err)
  89. }
  90. return config, nil
  91. case BigQueryConfigType:
  92. config := &gcp.BigQueryConfiguration{}
  93. err = json.Unmarshal(bytes, config)
  94. if err != nil {
  95. return nil, fmt.Errorf("error unmarshalling Big Query Configuration: %w", err)
  96. }
  97. return config, nil
  98. case AzureStorageConfigType:
  99. config := &azure.StorageConfiguration{}
  100. err = json.Unmarshal(bytes, config)
  101. if err != nil {
  102. return nil, fmt.Errorf("error unmarshalling Azure Storage Configuration: %w", err)
  103. }
  104. return config, nil
  105. }
  106. return nil, fmt.Errorf("provided config type was not recognised %s", configType)
  107. }
  108. // GetEnableConfigHandler creates a handler from a http request which enables an integration via the integrationController
  109. func (c *Controller) GetEnableConfigHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  110. // perform basic checks to ensure that the pipeline can be accessed
  111. fn := c.cloudCostChecks()
  112. if fn != nil {
  113. return fn
  114. }
  115. // Return valid handler func
  116. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  117. w.Header().Set("Content-Type", "application/json")
  118. integrationKey := r.URL.Query().Get("integrationKey")
  119. if integrationKey == "" {
  120. http.Error(w, "required parameter 'integrationKey' is missing", http.StatusBadRequest)
  121. return
  122. }
  123. source := r.URL.Query().Get("source")
  124. if source == "" {
  125. http.Error(w, "required parameter 'source' is missing", http.StatusBadRequest)
  126. return
  127. }
  128. err := c.EnableConfig(integrationKey, source)
  129. if err != nil {
  130. http.Error(w, err.Error(), http.StatusBadRequest)
  131. return
  132. }
  133. protocol.WriteData(w, fmt.Sprintf("Successfully enabled integration with key %s from source %s", integrationKey, source))
  134. }
  135. }
  136. // GetDisableConfigHandler creates a handler from a http request which disables an integration via the integrationController
  137. func (c *Controller) GetDisableConfigHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  138. // perform basic checks to ensure that the pipeline can be accessed
  139. fn := c.cloudCostChecks()
  140. if fn != nil {
  141. return fn
  142. }
  143. // Return valid handler func
  144. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  145. w.Header().Set("Content-Type", "application/json")
  146. integrationKey := r.URL.Query().Get("integrationKey")
  147. if integrationKey == "" {
  148. http.Error(w, "required parameter 'integrationKey' is missing", http.StatusBadRequest)
  149. return
  150. }
  151. source := r.URL.Query().Get("source")
  152. if source == "" {
  153. http.Error(w, "required parameter 'source' is missing", http.StatusBadRequest)
  154. return
  155. }
  156. err := c.DisableConfig(integrationKey, source)
  157. if err != nil {
  158. http.Error(w, err.Error(), http.StatusBadRequest)
  159. return
  160. }
  161. protocol.WriteData(w, fmt.Sprintf("Successfully disabled integration with key %s from source %s", integrationKey, source))
  162. }
  163. }
  164. // GetDeleteConfigHandler creates a handler from a http request which deletes an integration via the integrationController
  165. // if there are no other integrations with the given integration key, it also clears the data.
  166. func (c *Controller) GetDeleteConfigHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  167. // perform basic checks to ensure that the pipeline can be accessed
  168. fn := c.cloudCostChecks()
  169. if fn != nil {
  170. return fn
  171. }
  172. // Return valid handler func
  173. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  174. w.Header().Set("Content-Type", "application/json")
  175. integrationKey := r.URL.Query().Get("integrationKey")
  176. if integrationKey == "" {
  177. http.Error(w, "required parameter 'integrationKey' is missing", http.StatusBadRequest)
  178. return
  179. }
  180. err := c.DeleteConfig(integrationKey, ConfigControllerSource.String())
  181. if err != nil {
  182. http.Error(w, err.Error(), http.StatusBadRequest)
  183. return
  184. }
  185. protocol.WriteData(w, fmt.Sprintf("Successfully deleted integration with key %s", integrationKey))
  186. }
  187. }