controller_handlers.go 7.0 KB

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