Skip to content

Commit 4dc7342

Browse files
authored
Merge pull request #139 from cloudfoundry/integrate-configuration-object
chore(autoscaling-policy): Displays the Configuration Information
2 parents b29d095 + a24fc98 commit 4dc7342

File tree

7 files changed

+93
-21
lines changed

7 files changed

+93
-21
lines changed

api/apihelper.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func (helper *APIHelper) CreatePolicy(data interface{}) error {
266266
}
267267
defer resp.Body.Close()
268268

269-
raw, err := ioutil.ReadAll(resp.Body)
269+
raw, err := io.ReadAll(resp.Body)
270270

271271
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
272272

@@ -303,7 +303,7 @@ func (helper *APIHelper) DeletePolicy() error {
303303
}
304304
defer resp.Body.Close()
305305

306-
raw, err := ioutil.ReadAll(resp.Body)
306+
raw, err := io.ReadAll(resp.Body)
307307
if resp.StatusCode != http.StatusOK {
308308
var errorMsg string
309309
switch resp.StatusCode {
@@ -358,7 +358,7 @@ func (helper *APIHelper) GetAggregatedMetrics(metricName string, startTime, endT
358358
}
359359
defer resp.Body.Close()
360360

361-
raw, err := ioutil.ReadAll(resp.Body)
361+
raw, err := io.ReadAll(resp.Body)
362362
if resp.StatusCode != http.StatusOK {
363363
var errorMsg string
364364
switch resp.StatusCode {
@@ -424,7 +424,7 @@ func (helper *APIHelper) GetHistory(startTime, endTime int64, asc bool, page uin
424424
}
425425
defer resp.Body.Close()
426426

427-
raw, err := ioutil.ReadAll(resp.Body)
427+
raw, err := io.ReadAll(resp.Body)
428428
if resp.StatusCode != http.StatusOK {
429429
var errorMsg string
430430
switch resp.StatusCode {

api/apihelper_test.go

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var _ = Describe("API Helper Test", func() {
2929
err error
3030
apiServer *ghttp.Server
3131
apihelper *APIHelper
32-
fakePolicy ScalingPolicy = ScalingPolicy{
32+
fakePolicy = ScalingPolicy{
3333
InstanceMin: 1,
3434
InstanceMax: 2,
3535
ScalingRules: []*ScalingRule{
@@ -44,6 +44,56 @@ var _ = Describe("API Helper Test", func() {
4444
},
4545
},
4646
}
47+
fakePolicyWithConfiguration = ScalingPolicy{
48+
InstanceMin: 1,
49+
InstanceMax: 2,
50+
ScalingRules: []*ScalingRule{
51+
{
52+
MetricType: "memoryused",
53+
StatWindowSeconds: 300,
54+
BreachDurationSeconds: 600,
55+
Threshold: 30,
56+
Operator: "<=",
57+
CoolDownSeconds: 300,
58+
Adjustment: "-1",
59+
},
60+
},
61+
Configuration: &Configuration{
62+
CustomMetrics: struct {
63+
MetricSubmissionStrategy struct {
64+
AllowFrom string `json:"allow_from"`
65+
} `json:"metric_submission_strategy"`
66+
}{
67+
MetricSubmissionStrategy: struct {
68+
AllowFrom string `json:"allow_from"`
69+
}{
70+
AllowFrom: "bound_app",
71+
},
72+
},
73+
},
74+
}
75+
expectedPolicyWithConfigurationJSON = `{
76+
"instance_min_count": 1,
77+
"instance_max_count": 2,
78+
"scaling_rules": [
79+
{
80+
"metric_type": "memoryused",
81+
"stat_window_secs": 300,
82+
"breach_duration_secs": 600,
83+
"threshold": 30,
84+
"operator": "<=",
85+
"cool_down_secs": 300,
86+
"adjustment": "-1"
87+
}
88+
],
89+
"configuration": {
90+
"custom_metrics": {
91+
"metric_submission_strategy": {
92+
"allow_from": "bound_app"
93+
}
94+
}
95+
}
96+
}`
4797
)
4898

4999
BeforeEach(func() {
@@ -174,7 +224,7 @@ var _ = Describe("API Helper Test", func() {
174224
})
175225

176226
Context("Get policy", func() {
177-
var urlpath string = "/v1/apps/" + fakeAppId + "/policy"
227+
var urlpath = "/v1/apps/" + fakeAppId + "/policy"
178228

179229
Context("Succeed with valid auth token", func() {
180230
BeforeEach(func() {
@@ -186,12 +236,12 @@ var _ = Describe("API Helper Test", func() {
186236
)
187237
})
188238

189-
It("succeed", func() {
239+
It("succeed with policy only", func() {
190240
response, err := apihelper.GetPolicy()
191241
Expect(err).NotTo(HaveOccurred())
192242

193243
var actualPolicy ScalingPolicy
194-
_ = json.Unmarshal([]byte(response), &actualPolicy)
244+
_ = json.Unmarshal(response, &actualPolicy)
195245
Expect(actualPolicy).To(MatchFields(IgnoreExtras, Fields{
196246
"InstanceMin": BeNumerically("==", fakePolicy.InstanceMin),
197247
"InstanceMax": BeNumerically("==", fakePolicy.InstanceMax),
@@ -207,6 +257,19 @@ var _ = Describe("API Helper Test", func() {
207257
"Adjustment": Equal(fakePolicy.ScalingRules[0].Adjustment),
208258
}))
209259
})
260+
It("succeed with policy and configuration", func() {
261+
apiServer.RouteToHandler("GET", urlpath,
262+
ghttp.CombineHandlers(
263+
ghttp.RespondWithJSONEncoded(http.StatusOK, &fakePolicyWithConfiguration),
264+
ghttp.VerifyHeaderKV("Authorization", fakeAccessToken),
265+
),
266+
)
267+
response, err := apihelper.GetPolicy()
268+
Expect(err).NotTo(HaveOccurred())
269+
270+
Expect(response).To(MatchJSON(expectedPolicyWithConfigurationJSON))
271+
272+
})
210273
})
211274

212275
Context("Unauthorized Access", func() {
@@ -748,7 +811,7 @@ var _ = Describe("API Helper Test", func() {
748811

749812
})
750813

751-
Context("Get Histrory", func() {
814+
Context("Get History", func() {
752815
var urlpath = "/v1/apps/" + fakeAppId + "/scaling_histories"
753816
var now int64
754817
var histories, reversedHistories []*AppScalingHistory

commands/retrieve_history.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func RetrieveHistory(cliConnection api.Connection, appName string, startTime, en
105105
data [][]string
106106
)
107107

108-
for true {
108+
for {
109109
next, data, err = apihelper.GetHistory(startTime, endTime, asc, page)
110110
if err != nil {
111111
return err

devbox.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@
309309
},
310310
"python@latest": {
311311
"last_modified": "2024-08-14T11:41:26Z",
312-
"plugin_version": "0.0.3",
312+
"plugin_version": "0.0.4",
313313
"resolved": "github:NixOS/nixpkgs/0cb2fd7c59fed0cd82ef858cbcbdb552b9a33465#python3",
314314
"source": "devbox-search",
315315
"version": "3.12.4",

main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var BuildVcsId string
3333
var BuildVcsIdDate string
3434

3535
func (as *AutoScaler) GetMetadata() plugin.PluginMetadata {
36-
version := getVetsion()
36+
version := getVersion()
3737

3838
return plugin.PluginMetadata{
3939
Name: "AutoScaler",
@@ -114,7 +114,7 @@ OPTIONS:
114114
}
115115
}
116116

117-
func getVetsion() plugin.VersionType {
117+
func getVersion() plugin.VersionType {
118118
// We set a default version 0.0.0, and then we try to parse the version from the build flags
119119
// errors are ignored, as we will use the default version in case of errors
120120
version := plugin.VersionType{Major: 0, Minor: 0, Build: 0}
@@ -129,14 +129,14 @@ func main() {
129129

130130
args := os.Args[1:]
131131
if len(args) == 0 {
132-
version := getVetsion()
132+
version := getVersion()
133133
fmt.Printf("Upstream Version: %d.%d.%d\n", version.Major, version.Minor, version.Build)
134134
fmt.Println("Build Prerelease: ", BuildPrerelease)
135135
fmt.Println("Build Version: ", BuildMeta)
136136
fmt.Println("Build Date: ", BuildDate)
137137
fmt.Println("VCS Url:", BuildVcsUrl)
138138
fmt.Println("VCS Identifier: ", BuildVcsId)
139-
fmt.Println("VCS Identififer Date: ", BuildVcsIdDate)
139+
fmt.Println("VCS Identifier Date: ", BuildVcsIdDate)
140140
}
141141
plugin.Start(new(AutoScaler))
142142
}

models/models.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@ type ScalingType int
44

55
type ScalingStatus int
66

7+
type Configuration struct {
8+
CustomMetrics struct {
9+
MetricSubmissionStrategy struct {
10+
AllowFrom string `json:"allow_from"`
11+
} `json:"metric_submission_strategy"`
12+
} `json:"custom_metrics"`
13+
}
14+
715
type ScalingPolicy struct {
8-
InstanceMin int `json:"instance_min_count"`
9-
InstanceMax int `json:"instance_max_count"`
10-
ScalingRules []*ScalingRule `json:"scaling_rules,omitempty"`
11-
Schedules *ScalingSchedules `json:"schedules,omitempty"`
16+
InstanceMin int `json:"instance_min_count"`
17+
InstanceMax int `json:"instance_max_count"`
18+
ScalingRules []*ScalingRule `json:"scaling_rules,omitempty"`
19+
Schedules *ScalingSchedules `json:"schedules,omitempty"`
20+
Configuration *Configuration `json:"configuration,omitempty"`
1221
}
1322

1423
type ScalingRule struct {

ui/message.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ const (
1212
SetAPIEndpoint = "Setting AutoScaler api endpoint to %s..."
1313
UnsetAPIEndpoint = "Unsetting AutoScaler api endpoint."
1414
InvalidAPIEndpoint = "Invalid AutoScaler API endpoint : %s"
15-
InvalidSSLCerts = "Issue connnecting to %s: %s\nTIP: Use --skip-ssl-validation to continue with an insecure API endpoint."
16-
InconsistentDomain = "Failed to set AutoScaler domain to %s since it is inconsitent with the domain of CF API %s."
15+
InvalidSSLCerts = "Issue connecting to %s: %s\nTIP: Use --skip-ssl-validation to continue with an insecure API endpoint."
16+
InconsistentDomain = "Failed to set AutoScaler domain to %s since it is inconsistent with the domain of CF API %s."
1717

1818
Unauthorized = "Unauthorized. Failed to access AutoScaler API endpoint %s."
1919
LoginRequired = "You must be logged in %s first."

0 commit comments

Comments
 (0)