Skip to content

Commit 70a9d35

Browse files
committed
Finishes porting of legacy- and v0.1-parser to current code-base;
1 parent bf8f3db commit 70a9d35

File tree

6 files changed

+244
-154
lines changed

6 files changed

+244
-154
lines changed

api/broker/binding_request_parser/binding-request-parser.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,32 @@ import (
88
"code.cloudfoundry.org/app-autoscaler/src/autoscaler/models"
99
)
1010

11+
type Parser interface {
12+
// Default policies are specified on service-instance-level. Consequently, we need to leave the
13+
// field Parameters.ScalingPolicy empty when no policy has been specified and instead … let the
14+
// consumer of the BindingRequest decided what to do with this (i.e. he will use then the
15+
// default-policy.)
16+
Parse(bindingReqParams string, ccAppGuid models.GUID) (models.AppScalingConfig, error)
17+
}
18+
19+
20+
// ================================================================================
21+
// Parsing or validation errors
22+
// ================================================================================
23+
1124
type JsonSchemaError []gojsonschema.ResultError
1225

1326
func (e *JsonSchemaError) Error() string {
1427
var errors []gojsonschema.ResultError = *e
1528
return fmt.Sprintf("%s", errors)
1629
}
1730

18-
type Parser interface {
19-
// Default policies are specified on service-instance-level. Consequently, we need to leave the
20-
// field Parameters.ScalingPolicy empty when no policy has been specified and instead … let the
21-
// consumer of the BindingRequest decided what to do with this (i.e. he will use then the
22-
// default-policy.)
23-
Parse(bindingReqParams string, ccAppGuid models.GUID) (models.AppScalingConfig, error)
31+
32+
// This error type is used, when the passed binding-request fails to validate against the schema.
33+
type BindReqNoAppGuid struct{}
34+
35+
func (e BindReqNoAppGuid) Error() string {
36+
return "error: service must be bound to an application; Please provide a GUID of an app!"
2437
}
38+
39+
var _ error = BindReqNoAppGuid{}

api/broker/binding_request_parser/binding-request-parser_test.go

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package binding_request_parser_test
22

33
import (
4-
"path/filepath"
5-
64
. "github.com/onsi/ginkgo/v2"
75
. "github.com/onsi/gomega"
86

@@ -14,40 +12,31 @@ import (
1412
)
1513

1614
var _ = Describe("BindingRequestParsers", func() {
17-
const cleanSchemaFilePath string = "file://./binding-request.json"
15+
const v0_1SchemaFilePath string = "file://./v0_1/meta.schema.json"
16+
const legacySchemaFilePath string = "file://./legacy/schema.json"
17+
1818
const validModernBindingRequestRaw string = `
19-
{
20-
"configuration": {
21-
"app_guid": "8d0cee08-23ad-4813-a779-ad8118ea0b91",
22-
"custom_metrics": {
23-
"metric_submission_strategy": {
24-
"allow_from": "bound_app"
19+
{
20+
"schema-version": "0.1",
21+
"configuration": {
22+
"app_guid": "8d0cee08-23ad-4813-a779-ad8118ea0b91",
23+
"custom_metrics": {
24+
"metric_submission_strategy": {
25+
"allow_from": "bound_app"
26+
}
2527
}
26-
}
27-
},
28-
"scaling-policy": {
29-
"instance_min_count": 1,
30-
"instance_max_count": 4,
31-
"scaling_rules": [
32-
{
33-
"metric_type": "memoryutil",
34-
"breach_duration_secs": 600,
28+
},
29+
"instance_min_count": 1,
30+
"instance_max_count": 5,
31+
"scaling_rules": [
32+
{
33+
"metric_type": "memoryused",
3534
"threshold": 30,
3635
"operator": "<",
37-
"cool_down_secs": 300,
3836
"adjustment": "-1"
39-
},
40-
{
41-
"metric_type": "memoryutil",
42-
"breach_duration_secs": 600,
43-
"threshold": 90,
44-
"operator": ">=",
45-
"cool_down_secs": 300,
46-
"adjustment": "+1"
47-
}
48-
]
49-
}
50-
}`
37+
}
38+
]
39+
}`
5140
const validLegacyBindingRequestRaw string = `
5241
{
5342
"configuration": {
@@ -85,18 +74,19 @@ var _ = Describe("BindingRequestParsers", func() {
8574
err error
8675
)
8776
var _ = BeforeEach(func() {
88-
v0_1Parser, err = brp_v1.NewFromFile(cleanSchemaFilePath)
77+
v0_1Parser, err = brp_v1.NewFromFile(v0_1SchemaFilePath, models.X509Certificate)
8978
Expect(err).NotTo(HaveOccurred())
9079
})
9180
Context("When using the new format for binding-requests", func() {
9281
Context("and parsing a valid and complete one", func() {
9382
It("should return a correctly populated BindingRequestParameters", func() {
9483
bindingRequestRaw := validModernBindingRequestRaw
84+
ccAppGuid := models.GUID("") // Raw request is about creating a service-key;
9585

96-
bindingRequest, err := v0_1Parser.Parse(bindingRequestRaw)
86+
bindingRequest, err := v0_1Parser.Parse(bindingRequestRaw, ccAppGuid)
9787

9888
Expect(err).NotTo(HaveOccurred())
99-
Expect(bindingRequest.Configuration.AppGUID).To(
89+
Expect(bindingRequest.GetConfiguration().GetAppGUID()).To(
10090
Equal(models.GUID("8d0cee08-23ad-4813-a779-ad8118ea0b91")))
10191
})
10292
})
@@ -105,14 +95,12 @@ var _ = Describe("BindingRequestParsers", func() {
10595

10696
Describe("LegacyBindingRequestParser", func() {
10797
var (
108-
legacyParser lp.LegacyBindingRequestParser
109-
schemaFilePathAbs string
98+
legacyParser lp.BindingRequestParser
11099
err error
111100
)
112101
var _ = BeforeEach(func() {
113-
schemaFilePathAbs, err = filepath.Abs("./legacy/schema.json")
114102
Expect(err).NotTo(HaveOccurred())
115-
legacyParser, err = lp.New("file://" + schemaFilePathAbs)
103+
legacyParser, err = lp.New(legacySchemaFilePath, models.X509Certificate)
116104
Expect(err).NotTo(HaveOccurred())
117105
})
118106

api/broker/binding_request_parser/combined_parser/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
package combined_parser
55

66
// import (
7-
// "code.cloudfoundry.org/app-autoscaler/src/autoscaler/api/binding_request_parser"
7+
// "code.cloudfoundry.org/app-autoscaler/src/autoscaler/api/broker/binding_request_parser"
88
// )
99

1010
// // Combined parser that tries out all other parser that are associated to it in order

api/broker/binding_request_parser/legacy/parser.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)