Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow yaml / json objects in config files for #1039

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 51 additions & 15 deletions pkg/broker/service_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,52 @@ func (svc *ServiceDefinition) ProvisionDefaultOverrideProperty() string {
// ProvisionDefaultOverrides returns the deserialized JSON object for the
// operator-provided property overrides.
func (svc *ServiceDefinition) ProvisionDefaultOverrides() (map[string]any, error) {
return unmarshalViper(svc.ProvisionDefaultOverrideProperty())
return unmarshalViperMap(svc.ProvisionDefaultOverrideProperty())
}

func ProvisionGlobalDefaults() (map[string]any, error) {
return unmarshalViper(GlobalProvisionDefaults)
return unmarshalViperMap(GlobalProvisionDefaults)
}

func unmarshalViper(key string) (map[string]any, error) {
// Some config values were historically expected to be provided as a string.
// That is because these values were sourced from ENV Vars. But these values can
// be provided via a config file.
// E.g. consider the value .service.x.plans which would need to be a string in the config,
// file even though it would be more readable as a list of objects.
func unmarshalViperList(key string) ([]map[string]any, error) {
vals := []map[string]any{}
if viper.IsSet(key) {
val := viper.Get(key)

switch v := val.(type) {
case string:
if err := json.Unmarshal([]byte(v), &vals); err != nil {
return nil, fmt.Errorf("failed unmarshaling config value %s: %w", key, err)
}
case []map[string]any:
vals = v
}
}
return vals, nil
}

// Some config values were historically expected to be provided as a string.
// That is because these values were sourced from ENV Vars. But these values can
// be provided via a config file.
// E.g. consider the value .service.x.provision.defaults which would need to be a
// string in the config file even though it would be more readable as a map object.
func unmarshalViperMap(key string) (map[string]any, error) {
vals := make(map[string]any)
if viper.IsSet(key) {
val := viper.GetString(key)
if err := json.Unmarshal([]byte(val), &vals); err != nil {
return nil, fmt.Errorf("failed unmarshaling config value %s", key)

val := viper.Get(key)
switch v := val.(type) {
case string:
if err := json.Unmarshal([]byte(v), &vals); err != nil {
return nil, fmt.Errorf("failed unmarshaling config value %s", key)
}
case map[string]interface{}:
vals = v
}
}
return vals, nil
Expand Down Expand Up @@ -270,13 +303,19 @@ func (svc *ServiceDefinition) UserDefinedPlans(maintenanceInfo *domain.Maintenan
// but we need [{"id":"1234", "name":"foo", "service_properties":{"A": 1, "B": 2}}]
// Go doesn't support this natively so we do it manually here.
rawPlans := []json.RawMessage{}

// Unmarshal the plans from the viper configuration which is just a JSON list
// of plans
if userPlanJSON := viper.GetString(svc.UserDefinedPlansProperty()); userPlanJSON != "" {
if err := json.Unmarshal([]byte(userPlanJSON), &rawPlans); err != nil {
return []ServicePlan{}, err
}

userPlan, err := unmarshalViperList(svc.UserDefinedPlansProperty())
if err != nil {
return []ServicePlan{}, err
}
bytes, err := json.Marshal(userPlan)
if err != nil {
return []ServicePlan{}, err
}
if err := json.Unmarshal(bytes, &rawPlans); err != nil {
return []ServicePlan{}, err
}

// Unmarshal tile plans if they're included, which are a JSON object where
Expand Down Expand Up @@ -385,10 +424,7 @@ func (svc *ServiceDefinition) bindDefaults() []varcontext.DefaultVariable {
// For example, to create a default database name based on a user-provided instance name.
// Therefore, they get executed conditionally if a user-provided variable does not exist.
// Computed variables get executed either unconditionally or conditionally for greater flexibility.
func (svc *ServiceDefinition) variables(
constants map[string]any,
userProvidedParameters map[string]any,
plan ServicePlan) (*varcontext.VarContext, error) {
func (svc *ServiceDefinition) variables(constants map[string]any, userProvidedParameters map[string]any, plan ServicePlan) (*varcontext.VarContext, error) {

globalDefaults, err := ProvisionGlobalDefaults()
if err != nil {
Expand Down
34 changes: 33 additions & 1 deletion pkg/broker/service_definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
)

var _ = Describe("ServiceDefinition", func() {

Describe("CatalogEntry", func() {
Context("Metadata", func() {
var serviceDefinition broker.ServiceDefinition
Expand Down Expand Up @@ -377,10 +378,41 @@ var _ = Describe("ServiceDefinition", func() {

It("should return an error", func() {
_, err := service.UserDefinedPlans(maintenanceInfo)
Expect(err).To(MatchError("invalid character 'i' looking for beginning of object key string"))
Expect(err).To(MatchError(ContainSubstring("invalid character 'i' looking for beginning of object key string")))

})
})
When("a plan is provided in configuration as an object", func() {
BeforeEach(func() {
// fakePlanName, fakePlanID, fakePlanDescription, fakePlanProperty
fakeServicePlanConfigObject := []map[string]interface{}{
{
"name": fakePlanName,
"id": fakePlanID,
"description": fakePlanDescription,
"additional_property": fakePlanProperty,
},
{
"name": fmt.Sprintf("second-%s", fakePlanName),
"id": fmt.Sprintf("second-%s", fakePlanID),
"description": fmt.Sprintf("second-%s", fakePlanDescription),
"additional_property": fmt.Sprintf("second-%s", fakePlanProperty),
},
}
viper.Set("service.fake-service.provision.defaults", map[string]interface{}{
"test": "value",
})
viper.Set("service.fake-service.plans", fakeServicePlanConfigObject)
})
It("should work", func() {
plan, err := service.UserDefinedPlans(maintenanceInfo)
Expect(err).To(Not(HaveOccurred()))
Expect(plan).To(Not(HaveLen(0)))
provisionOverrides, err := service.ProvisionDefaultOverrides()
Expect(err).To(Not(HaveOccurred()))
Expect(provisionOverrides).To(Equal(map[string]interface{}{"test": `value`}))
})
})

})

Expand Down
Loading