Skip to content

Commit 78f5476

Browse files
committed
Add JSON tags to api config structs and implement FromJSON function
• Implement FromJSON function to unmarshal JSON into config structs • Add JSON tags to all configuration structs to support JSON serialization and deserialization • Modify ToJSON function to return an error if marshaling fails • Add tests for ToJSON and FromJSON functions to ensure proper JSON handling • Fix incorrect YAML tag in cf.Config for Secret field
1 parent a4c4b88 commit 78f5476

File tree

10 files changed

+115
-84
lines changed

10 files changed

+115
-84
lines changed

src/autoscaler/api/config/config.go

Lines changed: 66 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -53,80 +53,80 @@ var defaultLoggingConfig = helpers.LoggingConfig{
5353
}
5454

5555
type SchedulerConfig struct {
56-
SchedulerURL string `yaml:"scheduler_url"`
57-
TLSClientCerts models.TLSCerts `yaml:"tls"`
56+
SchedulerURL string `yaml:"scheduler_url" json:"scheduler_url"`
57+
TLSClientCerts models.TLSCerts `yaml:"tls" json:"tls`
5858
}
5959
type ScalingEngineConfig struct {
60-
ScalingEngineUrl string `yaml:"scaling_engine_url"`
61-
TLSClientCerts models.TLSCerts `yaml:"tls"`
60+
ScalingEngineUrl string `yaml:"scaling_engine_url" json:"scaling_engine_url"`
61+
TLSClientCerts models.TLSCerts `yaml:"tls" json:"tls`
6262
}
6363

6464
type EventGeneratorConfig struct {
65-
EventGeneratorUrl string `yaml:"event_generator_url"`
66-
TLSClientCerts models.TLSCerts `yaml:"tls"`
65+
EventGeneratorUrl string `yaml:"event_generator_url" json:"event_generator_url"`
66+
TLSClientCerts models.TLSCerts `yaml:"tls" json:"tls"`
6767
}
6868
type MetricsForwarderConfig struct {
69-
MetricsForwarderUrl string `yaml:"metrics_forwarder_url"`
70-
MetricsForwarderMtlsUrl string `yaml:"metrics_forwarder_mtls_url"`
69+
MetricsForwarderUrl string `yaml:"metrics_forwarder_url" json:"metrics_forwarder_url"`
70+
MetricsForwarderMtlsUrl string `yaml:"metrics_forwarder_mtls_url" json:"metrics_forwarder_mtls_url"`
7171
}
7272

7373
type PlanDefinition struct {
74-
PlanCheckEnabled bool `yaml:"planCheckEnabled"`
75-
SchedulesCount int `yaml:"schedules_count"`
76-
ScalingRulesCount int `yaml:"scaling_rules_count"`
77-
PlanUpdateable bool `yaml:"plan_updateable"`
74+
PlanCheckEnabled bool `yaml:"planCheckEnabled" json:"planCheckEnabled"`
75+
SchedulesCount int `yaml:"schedules_count" json:"schedules_count"`
76+
ScalingRulesCount int `yaml:"scaling_rules_count" json:"scaling_rules_count"`
77+
PlanUpdateable bool `yaml:"plan_updateable" json:"plan_updateable"`
7878
}
7979

8080
type BrokerCredentialsConfig struct {
81-
BrokerUsername string `yaml:"broker_username"`
82-
BrokerUsernameHash []byte `yaml:"broker_username_hash"`
83-
BrokerPassword string `yaml:"broker_password"`
84-
BrokerPasswordHash []byte `yaml:"broker_password_hash"`
81+
BrokerUsername string `yaml:"broker_username" json:"broker_username"`
82+
BrokerUsernameHash []byte `yaml:"broker_username_hash" json:"broker_username_hash"`
83+
BrokerPassword string `yaml:"broker_password" json:"broker_password"`
84+
BrokerPasswordHash []byte `yaml:"broker_password_hash" json:"broker_password_hash"`
8585
}
8686

8787
type ScalingRulesConfig struct {
88-
CPU LowerUpperThresholdConfig `yaml:"cpu"`
89-
CPUUtil LowerUpperThresholdConfig `yaml:"cpuutil"`
90-
DiskUtil LowerUpperThresholdConfig `yaml:"diskutil"`
91-
Disk LowerUpperThresholdConfig `yaml:"disk"`
88+
CPU LowerUpperThresholdConfig `yaml:"cpu" json:"cpu"`
89+
CPUUtil LowerUpperThresholdConfig `yaml:"cpuutil" json:"cpuutil"`
90+
DiskUtil LowerUpperThresholdConfig `yaml:"diskutil" json:"diskutil"`
91+
Disk LowerUpperThresholdConfig `yaml:"disk" json:"disk"`
9292
}
9393

9494
type LowerUpperThresholdConfig struct {
95-
LowerThreshold int `yaml:"lower_threshold"`
96-
UpperThreshold int `yaml:"upper_threshold"`
95+
LowerThreshold int `yaml:"lower_threshold" json:"lower_threshold"`
96+
UpperThreshold int `yaml:"upper_threshold" json:"upper_threshold"`
9797
}
9898

9999
type Config struct {
100-
Logging helpers.LoggingConfig `yaml:"logging"`
101-
BrokerServer helpers.ServerConfig `yaml:"broker_server"`
102-
Server helpers.ServerConfig `yaml:"public_api_server"`
103-
104-
VCAPServer helpers.ServerConfig `yaml:"vcap_server"`
105-
106-
Db map[string]db.DatabaseConfig `yaml:"db"`
107-
BrokerCredentials []BrokerCredentialsConfig `yaml:"broker_credentials"`
108-
APIClientId string `yaml:"api_client_id"`
109-
PlanCheck *PlanCheckConfig `yaml:"plan_check"`
110-
CatalogPath string `yaml:"catalog_path"`
111-
CatalogSchemaPath string `yaml:"catalog_schema_path"`
112-
DashboardRedirectURI string `yaml:"dashboard_redirect_uri"`
113-
PolicySchemaPath string `yaml:"policy_schema_path"`
114-
Scheduler SchedulerConfig `yaml:"scheduler"`
115-
ScalingEngine ScalingEngineConfig `yaml:"scaling_engine"`
116-
EventGenerator EventGeneratorConfig `yaml:"event_generator"`
117-
CF cf.Config `yaml:"cf"`
118-
InfoFilePath string `yaml:"info_file_path"`
119-
MetricsForwarder MetricsForwarderConfig `yaml:"metrics_forwarder"`
120-
Health helpers.HealthConfig `yaml:"health"`
121-
RateLimit models.RateLimitConfig `yaml:"rate_limit"`
122-
CredHelperImpl string `yaml:"cred_helper_impl"`
123-
StoredProcedureConfig *models.StoredProcedureConfig `yaml:"stored_procedure_binding_credential_config"`
124-
ScalingRules ScalingRulesConfig `yaml:"scaling_rules"`
125-
DefaultCustomMetricsCredentialType string `yaml:"default_credential_type"`
100+
Logging helpers.LoggingConfig `yaml:"logging" json:"logging"`
101+
BrokerServer helpers.ServerConfig `yaml:"broker_server" json:"broker_server"`
102+
Server helpers.ServerConfig `yaml:"public_api_server" json:"public_api_server"`
103+
104+
VCAPServer helpers.ServerConfig `yaml:"vcap_server" json:"vcap_server"`
105+
106+
Db map[string]db.DatabaseConfig `yaml:"db" json:"db,omitempty"`
107+
BrokerCredentials []BrokerCredentialsConfig `yaml:"broker_credentials" json:"broker_credentials"`
108+
APIClientId string `yaml:"api_client_id" json:"api_client_id"`
109+
PlanCheck *PlanCheckConfig `yaml:"plan_check" json:"plan_check"`
110+
CatalogPath string `yaml:"catalog_path" json:"catalog_path"`
111+
CatalogSchemaPath string `yaml:"catalog_schema_path" json:"catalog_schema_path"`
112+
DashboardRedirectURI string `yaml:"dashboard_redirect_uri" json:"dashboard_redirect_uri"`
113+
PolicySchemaPath string `yaml:"policy_schema_path" json:"policy_schema_path"`
114+
Scheduler SchedulerConfig `yaml:"scheduler" json:"scheduler"`
115+
ScalingEngine ScalingEngineConfig `yaml:"scaling_engine" json:"scaling_engine"`
116+
EventGenerator EventGeneratorConfig `yaml:"event_generator" json:"event_generator"`
117+
CF cf.Config `yaml:"cf" json:"cf"`
118+
InfoFilePath string `yaml:"info_file_path" json:"info_file_path"`
119+
MetricsForwarder MetricsForwarderConfig `yaml:"metrics_forwarder" json:"metrics_forwarder"`
120+
Health helpers.HealthConfig `yaml:"health" json:"health"`
121+
RateLimit models.RateLimitConfig `yaml:"rate_limit" json:"rate_limit,omitempty"`
122+
CredHelperImpl string `yaml:"cred_helper_impl" json:"cred_helper_impl"`
123+
StoredProcedureConfig *models.StoredProcedureConfig `yaml:"stored_procedure_binding_credential_config" json:"stored_procedure_binding_credential_config"`
124+
ScalingRules ScalingRulesConfig `yaml:"scaling_rules" json:"scaling_rules"`
125+
DefaultCustomMetricsCredentialType string `yaml:"default_credential_type" json:"default_credential_type"`
126126
}
127127

128128
type PlanCheckConfig struct {
129-
PlanDefinitions map[string]PlanDefinition `yaml:"plan_definitions"`
129+
PlanDefinitions map[string]PlanDefinition `yaml:"plan_definitions" json:"plan_definitions"`
130130
}
131131

132132
func defaultConfig() Config {
@@ -274,9 +274,23 @@ func LoadConfig(filepath string, vcapReader configutil.VCAPConfigurationReader)
274274
return &conf, nil
275275
}
276276

277-
func (c *Config) ToJSON() string {
278-
b, _ := json.Marshal(c)
279-
return string(b)
277+
func FromJSON(data []byte) (*Config, error) {
278+
result := &Config{}
279+
err := json.Unmarshal(data, result)
280+
if err != nil {
281+
return nil, fmt.Errorf("failed to unmarshal config from json: %s", err)
282+
}
283+
return result, nil
284+
}
285+
286+
func (c *Config) ToJSON() (string, error) {
287+
b, err := json.Marshal(c)
288+
289+
if err != nil {
290+
err = fmt.Errorf("failed to marshal config to json: %s", err)
291+
return "", err
292+
}
293+
return string(b), nil
280294
}
281295
func (c *Config) Validate() error {
282296
err := c.CF.Validate()

src/autoscaler/api/config/config_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ var _ = Describe("Config", func() {
3131
mockVCAPConfigurationReader = &fakes.FakeVCAPConfigurationReader{}
3232
})
3333

34+
Describe("ToJSON and FromJSON", func() {
35+
JustBeforeEach(func() {
36+
configFile = testhelpers.BytesToFile(configBytes)
37+
conf, err = LoadConfig(configFile, mockVCAPConfigurationReader)
38+
})
39+
40+
It("should return the config in json format", func() {
41+
Expect(err).NotTo(HaveOccurred())
42+
json, err := conf.ToJSON()
43+
44+
unmarshalConfig, err := FromJSON([]byte(json))
45+
Expect(err).NotTo(HaveOccurred())
46+
47+
Expect(unmarshalConfig).To(Equal(conf))
48+
})
49+
})
50+
3451
Describe("Load Config", func() {
3552

3653
When("config is read from env", func() {

src/autoscaler/cf/config.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ import (
77
)
88

99
type ClientConfig struct {
10-
MaxRetries int `yaml:"max_retries"`
11-
MaxRetryWaitMs int64 `yaml:"max_retry_wait_ms"`
12-
IdleConnectionTimeoutMs int64 `yaml:"idle_connection_timeout_ms"`
13-
MaxIdleConnsPerHost int `yaml:"max_idle_conns_per_host_ms"`
14-
SkipSSLValidation bool `yaml:"skip_ssl_validation"`
10+
MaxRetries int `yaml:"max_retries" json:"max_retries,omitempty"`
11+
MaxRetryWaitMs int64 `yaml:"max_retry_wait_ms" json:"max_retry_wait_ms"`
12+
IdleConnectionTimeoutMs int64 `yaml:"idle_connection_timeout_ms" json:"idle_connection_timeout_ms"`
13+
MaxIdleConnsPerHost int `yaml:"max_idle_conns_per_host_ms" json:"max_idle_conns_per_host_ms"`
14+
SkipSSLValidation bool `yaml:"skip_ssl_validation" json:"skip_ssl_validation"`
1515
}
1616

1717
type Config struct {
1818
ClientConfig `yaml:",inline"`
19-
API string `yaml:"api"`
20-
ClientID string `yaml:"client_id"`
21-
Secret string `yaml:"secret"`
22-
PerPage int `yaml:"per_page"`
19+
API string `yaml:"api" json:"api"`
20+
ClientID string `yaml:"client_id" json:"client_id"`
21+
Secret string `yaml:"secret" yaml:"secret"`
22+
PerPage int `yaml:"per_page" json:"per_page"`
2323
}
2424

2525
func (conf *Config) Validate() error {

src/autoscaler/db/db.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ var ErrDoesNotExist = fmt.Errorf("doesn't exist")
3636
var ErrConflict = fmt.Errorf("conflicting entry exists")
3737

3838
type DatabaseConfig struct {
39-
URL string `yaml:"url"`
40-
MaxOpenConnections int32 `yaml:"max_open_connections"`
41-
MaxIdleConnections int32 `yaml:"max_idle_connections"`
42-
ConnectionMaxLifetime time.Duration `yaml:"connection_max_lifetime"`
43-
ConnectionMaxIdleTime time.Duration `yaml:"connection_max_idletime"`
39+
URL string `yaml:"url" json:"url"`
40+
MaxOpenConnections int32 `yaml:"max_open_connections" json:"max_open_connections,omitempty`
41+
MaxIdleConnections int32 `yaml:"max_idle_connections" json:"max_idle_connections,omitempty`
42+
ConnectionMaxLifetime time.Duration `yaml:"connection_max_lifetime " json:"connection_max_lifetime,omitempty`
43+
ConnectionMaxIdleTime time.Duration `yaml:"connection_max_idletime" json:"connection_max_idletime,omitempty"`
4444
}
4545

4646
type PolicyDB interface {

src/autoscaler/helpers/health.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
)
99

1010
type HealthConfig struct {
11-
ServerConfig ServerConfig `yaml:"server_config"`
12-
BasicAuth models.BasicAuth `yaml:"basic_auth"`
13-
ReadinessCheckEnabled bool `yaml:"readiness_enabled"`
11+
ServerConfig ServerConfig `yaml:"server_config" json:"server_config"`
12+
BasicAuth models.BasicAuth `yaml:"basic_auth" json:"basic_auth"`
13+
ReadinessCheckEnabled bool `yaml:"readiness_enabled" json:"readiness_enabled"`
1414
}
1515

1616
var ErrConfiguration = fmt.Errorf("configuration error")

src/autoscaler/helpers/http_server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import (
1212
)
1313

1414
type ServerConfig struct {
15-
Port int `yaml:"port"`
16-
TLS models.TLSCerts `yaml:"tls"`
17-
XFCC models.XFCCAuth `yaml:"xfcc"`
15+
Port int `yaml:"port" json:"port"`
16+
TLS models.TLSCerts `yaml:"tls" json:"tls"`
17+
XFCC models.XFCCAuth `yaml:"xfcc" json:"xfcc"`
1818
}
1919

2020
func NewHTTPServer(logger lager.Logger, conf ServerConfig, handler http.Handler) (ifrit.Runner, error) {

src/autoscaler/helpers/logger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
type LoggingConfig struct {
13-
Level string `yaml:"level"`
13+
Level string `yaml:"level" json:"level"`
1414
}
1515

1616
func InitLoggerFromConfig(conf *LoggingConfig, name string) lager.Logger {

src/autoscaler/models/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ const (
1010
)
1111

1212
type XFCCAuth struct {
13-
ValidOrgGuid string `yaml:"valid_org_guid"`
14-
ValidSpaceGuid string `yaml:"valid_space_guid"`
13+
ValidOrgGuid string `yaml:"valid_org_guid" json:"valid_org_guid"`
14+
ValidSpaceGuid string `yaml:"valid_space_guid" json:"valid_space_guid"`
1515
}
1616

1717
type BrokerContext struct {

src/autoscaler/models/ratelimit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ package models
33
import "time"
44

55
type RateLimitConfig struct {
6-
MaxAmount int `yaml:"max_amount"`
7-
ValidDuration time.Duration `yaml:"valid_duration"`
6+
MaxAmount int `yaml:"max_amount" json:"max_amount,omitempty"`
7+
ValidDuration time.Duration `yaml:"valid_duration" json:"valid_duration,omitempty"`
88
}

src/autoscaler/models/security.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import (
77
)
88

99
type BasicAuth struct {
10-
Username string `yaml:"username"`
11-
UsernameHash string `yaml:"username_hash"`
12-
Password string `yaml:"password"`
13-
PasswordHash string `yaml:"password_hash"`
10+
Username string `yaml:"username" json:"username"`
11+
UsernameHash string `yaml:"username_hash" json:"usernameHash"`
12+
Password string `yaml:"password" json:"password"`
13+
PasswordHash string `yaml:"password_hash" json:"passwordHash"`
1414
}
1515

1616
type TLSCerts struct {
17-
KeyFile string `yaml:"key_file" json:"keyFile"`
18-
CertFile string `yaml:"cert_file" json:"certFile"`
19-
CACertFile string `yaml:"ca_file" json:"caCertFile"`
17+
KeyFile string `yaml:"key_file" json:"key_file"`
18+
CertFile string `yaml:"cert_file" json:"cert_file"`
19+
CACertFile string `yaml:"ca_file" json:"ca_file"`
2020
}
2121

2222
func (t *TLSCerts) CreateClientConfig() (*tls.Config, error) {

0 commit comments

Comments
 (0)