Forráskód Böngészése

Export `config.ParseConfig()` (#3469)

Signed-off-by: Nik Willwerth <nwillwerth@kubecost.com>
nik-kc 5 hónapja
szülő
commit
c0b7ce4d40

+ 4 - 4
pkg/cloud/config/controller_handlers.go

@@ -29,7 +29,7 @@ func (c *Controller) cloudCostChecks() func(w http.ResponseWriter, r *http.Reque
 	return nil
 }
 
-// GetEnableConfigHandler creates a handler from a http request which enables an integration via the integrationController
+// GetExportConfigHandler creates a handler from a http request which exports an integration via the integrationController
 func (c *Controller) GetExportConfigHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 	// perform basic checks to ensure that the pipeline can be accessed
 	fn := c.cloudCostChecks()
@@ -52,7 +52,7 @@ func (c *Controller) GetExportConfigHandler() func(w http.ResponseWriter, r *htt
 	}
 }
 
-// GetEnableConfigHandler creates a handler from a http request which enables an integration via the integrationController
+// GetAddConfigHandler creates a handler from a http request which adds an integration via the integrationController
 func (c *Controller) GetAddConfigHandler() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 	// perform basic checks to ensure that the pipeline can be accessed
 	fn := c.cloudCostChecks()
@@ -66,7 +66,7 @@ func (c *Controller) GetAddConfigHandler() func(w http.ResponseWriter, r *http.R
 
 		configType := r.URL.Query().Get("type")
 
-		config, err := parseConfig(configType, r.Body)
+		config, err := ParseConfig(configType, r.Body)
 		if err != nil {
 			http.Error(w, err.Error(), http.StatusBadRequest)
 			return
@@ -82,7 +82,7 @@ func (c *Controller) GetAddConfigHandler() func(w http.ResponseWriter, r *http.R
 	}
 }
 
-func parseConfig(configType string, body io.Reader) (cloud.KeyedConfig, error) {
+func ParseConfig(configType string, body io.Reader) (cloud.KeyedConfig, error) {
 	buf := new(bytes.Buffer)
 	_, err := buf.ReadFrom(body)
 	if err != nil {

+ 135 - 0
pkg/cloud/config/controller_handlers_test.go

@@ -0,0 +1,135 @@
+package config
+
+import (
+	"bytes"
+	"reflect"
+	"strings"
+	"testing"
+
+	"github.com/opencost/opencost/core/pkg/util/json"
+	"github.com/opencost/opencost/pkg/cloud/aws"
+	"github.com/opencost/opencost/pkg/cloud/azure"
+	"github.com/opencost/opencost/pkg/cloud/gcp"
+)
+
+func Test_ParseConfig_InvalidType(t *testing.T) {
+	body := strings.NewReader("{}")
+
+	_, err := ParseConfig("invalid_type", body)
+	if err == nil {
+		t.Fatalf("expected error, got none")
+	}
+}
+
+func Test_ParseConfig_S3(t *testing.T) {
+	config := &aws.S3Configuration{
+		Bucket:  "bucket",
+		Region:  "region",
+		Account: "account",
+		Authorizer: &aws.AccessKey{
+			ID:     "id",
+			Secret: "secret",
+		},
+	}
+
+	configBytes, err := json.Marshal(config)
+	if err != nil {
+		t.Fatalf("failed to marshal config: %v", err)
+	}
+
+	parsedConfig, err := ParseConfig(S3ConfigType, bytes.NewReader(configBytes))
+	if err != nil {
+		t.Fatalf("failed to parse config: %v", err)
+	}
+
+	if !reflect.DeepEqual(config, parsedConfig) {
+		t.Fatalf("parsed config does not match original config:\n%+v\n%+v", parsedConfig, config)
+	}
+}
+
+func Test_ParseConfig_Athena(t *testing.T) {
+	config := &aws.AthenaConfiguration{
+		Bucket:    "bucket",
+		Region:    "region",
+		Database:  "database",
+		Catalog:   "catalog",
+		Table:     "table",
+		Workgroup: "workgroup",
+		Account:   "account",
+		Authorizer: &aws.AccessKey{
+			ID:     "id",
+			Secret: "secret",
+		},
+		CURVersion: "curversion",
+	}
+
+	configBytes, err := json.Marshal(config)
+	if err != nil {
+		t.Fatalf("failed to marshal config: %v", err)
+	}
+
+	parsedConfig, err := ParseConfig(AthenaConfigType, bytes.NewReader(configBytes))
+	if err != nil {
+		t.Fatalf("failed to parse config: %v", err)
+	}
+
+	if !reflect.DeepEqual(config, parsedConfig) {
+		t.Fatalf("parsed config does not match original config:\n%+v\n%+v", parsedConfig, config)
+	}
+}
+
+func Test_ParseConfig_BigQuery(t *testing.T) {
+	config := &gcp.BigQueryConfiguration{
+		ProjectID:            "projectid",
+		Dataset:              "dataset",
+		Table:                "table",
+		ExcludePartitionTime: false,
+		Authorizer: &gcp.ServiceAccountKey{
+			Key: map[string]string{
+				"key": "value",
+			},
+		},
+	}
+
+	configBytes, err := json.Marshal(config)
+	if err != nil {
+		t.Fatalf("failed to marshal config: %v", err)
+	}
+
+	parsedConfig, err := ParseConfig(BigQueryConfigType, bytes.NewReader(configBytes))
+	if err != nil {
+		t.Fatalf("failed to parse config: %v", err)
+	}
+
+	if !reflect.DeepEqual(config, parsedConfig) {
+		t.Fatalf("parsed config does not match original config:\n%+v\n%+v", parsedConfig, config)
+	}
+}
+
+func Test_ParseConfig_Azure(t *testing.T) {
+	config := &azure.StorageConfiguration{
+		SubscriptionID: "subscriptionid",
+		Account:        "account",
+		Container:      "container",
+		Path:           "path",
+		Cloud:          "cloud",
+		Authorizer: &azure.SharedKeyCredential{
+			AccessKey: "accesskey",
+			Account:   "account",
+		},
+	}
+
+	configBytes, err := json.Marshal(config)
+	if err != nil {
+		t.Fatalf("failed to marshal config: %v", err)
+	}
+
+	parsedConfig, err := ParseConfig(AzureStorageConfigType, bytes.NewReader(configBytes))
+	if err != nil {
+		t.Fatalf("failed to parse config: %v", err)
+	}
+
+	if !reflect.DeepEqual(config, parsedConfig) {
+		t.Fatalf("parsed config does not match original config:\n%+v\n%+v", parsedConfig, config)
+	}
+}