Skip to content

Commit 391c6af

Browse files
committed
messaging properly when missing properites in validation
1 parent 64d1d65 commit 391c6af

File tree

6 files changed

+180
-15
lines changed

6 files changed

+180
-15
lines changed
-12.1 KB
Loading

.idea/uiDesigner.xml

Lines changed: 124 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.redis.autoscaler;
2+
3+
import org.springframework.http.HttpEntity;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.MethodArgumentNotValidException;
7+
import org.springframework.web.bind.annotation.ControllerAdvice;
8+
import org.springframework.web.bind.annotation.ExceptionHandler;
9+
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
@ControllerAdvice
14+
public class ValidationExceptionHandler {
15+
16+
@ExceptionHandler(MethodArgumentNotValidException.class)
17+
public ResponseEntity<Map<String,String>> failedValidation(MethodArgumentNotValidException ex) {
18+
19+
Map<String, String> errors = new HashMap<>();
20+
errors.put("message", ex.getMessage());
21+
errors.put("status", String.valueOf(HttpStatus.BAD_REQUEST.value()));
22+
23+
ex.getBindingResult().getFieldErrors().forEach(error ->
24+
errors.put(error.getField(), error.getDefaultMessage())
25+
);
26+
return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST);
27+
}
28+
}

autoscaler/redis-cloud-autoscaler/src/main/java/com/redis/autoscaler/controllers/RulesController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.redis.autoscaler.documents.TriggerType;
66
import com.redis.autoscaler.services.RedisCloudDatabaseService;
77
import com.redis.autoscaler.services.SchedulingService;
8+
import jakarta.validation.Valid;
89
import org.slf4j.Logger;
910
import org.springframework.http.HttpEntity;
1011
import org.springframework.http.HttpStatus;
@@ -36,7 +37,7 @@ public HttpEntity<Integer> getNumDatabases() throws IOException, InterruptedExce
3637

3738

3839
@PostMapping
39-
public HttpEntity<Object> createRule(@RequestBody Rule rule) {
40+
public HttpEntity<Object> createRule(@Valid @RequestBody Rule rule) {
4041
LOG.info("Received request to create rule: {}", rule);
4142

4243
String ruleValidityError = rule.getValidationError();

autoscaler/redis-cloud-autoscaler/src/main/java/com/redis/autoscaler/documents/Rule.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.redis.autoscaler.documents;
22

3+
import com.fasterxml.jackson.annotation.JsonProperty;
34
import com.redis.om.spring.annotations.Document;
45
import com.redis.om.spring.annotations.Indexed;
6+
import jakarta.validation.constraints.NotNull;
57
import lombok.Data;
68
import org.slf4j.Logger;
79
import org.springframework.data.annotation.Id;
@@ -12,31 +14,41 @@ public class Rule {
1214
private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(Rule.class);
1315

1416
@Indexed
15-
protected String dbId;
17+
@Id
18+
private String ruleId;
1619

20+
@JsonProperty(required = true)
1721
@Indexed
18-
@Id
19-
protected String ruleId;
22+
private String dbId;
2023

2124
@Indexed
22-
protected RuleType ruleType;
25+
@JsonProperty(required = true)
26+
@NotNull(message = "ruleType is required")
27+
private RuleType ruleType;
2328

2429
@Indexed
25-
protected ScaleType scaleType;
30+
@JsonProperty(required = true)
31+
@NotNull(message = "scaleType is required")
32+
private ScaleType scaleType;
2633

2734
@Indexed
28-
protected TriggerType triggerType;
35+
@JsonProperty(required = true)
36+
@NotNull(message = "triggerType is required")
37+
private TriggerType triggerType;
38+
39+
private String triggerValue;
2940

30-
protected String triggerValue;
41+
@JsonProperty(required = true)
42+
@NotNull(message = "scaleValue is required")
43+
private double scaleValue;
3144

32-
protected double scaleValue;
33-
protected double scaleCeiling;
34-
protected double scaleFloor;
45+
private double scaleCeiling;
46+
private double scaleFloor;
3547

3648
@Override
3749
public String toString(){
38-
return String.format("Rule: dbId=%s, ruleId=%s, ruleType=%s, scaleType=%s, scaleValue=%f, scaleCeiling=%f, scaleFloor=%f",
39-
dbId, ruleId, ruleType, scaleType, scaleValue, scaleCeiling, scaleFloor);
50+
return String.format("Rule: dbId=%s, ruleId=%s, ruleType=%s, scaleType=%s, scaleValue=%f, scaleCeiling=%f, scaleFloor=%f, triggerType=%s, triggerValue=%s",
51+
dbId, ruleId, ruleType, scaleType, scaleValue, scaleCeiling, scaleFloor, triggerType, triggerValue);
4052
}
4153

4254

@@ -92,7 +104,7 @@ public String getValidationError(){
92104
return "";
93105
}
94106

95-
protected boolean isMultipleOfPointOne() {
107+
private boolean isMultipleOfPointOne() {
96108
LOG.info("Validating scale value: {}", scaleValue);
97109
double compValue = scaleValue - Math.floor(scaleValue);
98110
double epsilon = 1e-9; // Small tolerance for floating-point precision

redeploy-autoscaler.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

33
./gradlew clean bootjar
4-
gcloud compute scp ./autoscaler/redis-cloud-autoscaler/build/libs/redis-cloud-autoscaler-0.0.2.jar autoscaler-vm:~/autoscaler.jar --zone=us-east1-b
4+
gcloud compute scp ./autoscaler/redis-cloud-autoscaler/build/libs/redis-cloud-autoscaler-0.0.3.jar autoscaler-vm:~/autoscaler.jar --zone=us-east1-b
55
# run command to move the jar to /usr/local/bin/autoscaler.jar, change owner to autoscaler, and restart the autoscaler service
66
gcloud compute ssh --zone=us-east1-b autoscaler-vm --command "sudo cp ~/autoscaler.jar /usr/local/bin/autoscaler.jar && sudo chown autoscaler:autoscaler /usr/local/bin/autoscaler.jar && sudo systemctl restart autoscaler"

0 commit comments

Comments
 (0)