@@ -9,12 +9,12 @@ import (
99 "code.cloudfoundry.org/app-autoscaler/src/autoscaler/models"
1010)
1111
12- type LegacyBindingRequestParser struct {
12+ type BindingRequestParser struct {
1313 schema * gojsonschema.Schema
1414 defaultCustomMetricsCredentialType models.CustomMetricsBindingAuthScheme
1515}
1616
17- var _ binding_request_parser.Parser = LegacyBindingRequestParser {}
17+ var _ binding_request_parser.Parser = BindingRequestParser {}
1818
1919// New creates a new LegacyBindingRequestParser with the JSON schema loaded from the specified file
2020// path.
@@ -23,16 +23,24 @@ var _ binding_request_parser.Parser = LegacyBindingRequestParser{}
2323// "file:///path/to/schema.json").
2424//
2525// Returns an error if the schema file cannot be loaded or parsed.
26- func New (schemaFilePath string ) (LegacyBindingRequestParser , error ) {
26+ func New (
27+ schemaFilePath string , defaultCustomMetricsCredentialType models.CustomMetricsBindingAuthScheme ,
28+ ) (BindingRequestParser , error ) {
2729 schemaLoader := gojsonschema .NewReferenceLoader (schemaFilePath )
2830 schema , err := gojsonschema .NewSchema (schemaLoader )
2931 if err != nil {
30- return LegacyBindingRequestParser {}, err
32+ return BindingRequestParser {}, err
3133 }
32- return LegacyBindingRequestParser {schema : schema }, nil
34+
35+ parser := BindingRequestParser {
36+ schema : schema ,
37+ defaultCustomMetricsCredentialType : defaultCustomMetricsCredentialType ,
38+ }
39+
40+ return parser , nil
3341}
3442
35- func (p LegacyBindingRequestParser ) Parse (
43+ func (p BindingRequestParser ) Parse (
3644 bindingReqParams string , ccAppGuid models.GUID ,
3745) (models.AppScalingConfig , error ) {
3846 validationErr := p .Validate (bindingReqParams )
@@ -49,7 +57,7 @@ func (p LegacyBindingRequestParser) Parse(
4957 return p .toBindingParameters (parsedParameters , ccAppGuid )
5058}
5159
52- func (p LegacyBindingRequestParser ) Validate (bindingReqParams string ) error {
60+ func (p BindingRequestParser ) Validate (bindingReqParams string ) error {
5361 documentLoader := gojsonschema .NewStringLoader (bindingReqParams )
5462 validationResult , err := p .schema .Validate (documentLoader )
5563 if err != nil {
@@ -65,7 +73,7 @@ func (p LegacyBindingRequestParser) Validate(bindingReqParams string) error {
6573 return nil
6674}
6775
68- func (p LegacyBindingRequestParser ) toBindingParameters (
76+ func (p BindingRequestParser ) toBindingParameters (
6977 bindingReqParams policyAndBindingCfg , ccAppGuid models.GUID ,
7078) (models.AppScalingConfig , error ) {
7179 appGuid := ccAppGuid
@@ -117,13 +125,18 @@ This is an programming-error.`,
117125
118126 bindingConfig := * models .NewBindingConfig (appGuid , customMetricsBindAuthScheme )
119127 policyDefinition := readPolicyDefinition (bindingReqParams )
120- scalingPolicy := models .NewScalingPolicy (customMetricsStrat , & policyDefinition )
128+ scalingPolicy := models .NewScalingPolicy (customMetricsStrat , policyDefinition )
121129
122130 return * models .NewAppScalingConfig (bindingConfig , * scalingPolicy ), nil
123131}
124132
125- func readPolicyDefinition (bindingReqParams policyAndBindingCfg ) models.PolicyDefinition {
126- // 🚧 To-do: What if no policy has been provided?
133+ func readPolicyDefinition (bindingReqParams policyAndBindingCfg ) * models.PolicyDefinition {
134+ noPolicyIsSet := bindingReqParams .InstanceMin == 0 && bindingReqParams .InstanceMax == 0 &&
135+ len (bindingReqParams .ScalingRules ) == 0 && bindingReqParams .Schedules == nil
136+ if noPolicyIsSet {
137+ return nil
138+ }
139+
127140
128141 policyDefinition := models.PolicyDefinition {
129142 InstanceMin : bindingReqParams .InstanceMin ,
@@ -176,5 +189,5 @@ func readPolicyDefinition(bindingReqParams policyAndBindingCfg) models.PolicyDef
176189 }
177190 }
178191
179- return policyDefinition
192+ return & policyDefinition
180193}
0 commit comments