Skip to content

Commit

Permalink
Add validations
Browse files Browse the repository at this point in the history
  • Loading branch information
harjain99 committed Sep 25, 2024
1 parent 9618f0e commit ff533c7
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 11 deletions.
18 changes: 13 additions & 5 deletions src/main/java/gyro/google/cloudsql/DbAclEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,19 @@ public void copyFrom(AclEntry model) {

@Override
public String primaryKey() {
return String.format(
"Acl Entry [Value: %s, Expiration Time: %s, Name: %s]",
getValue(),
getExpirationTime(),
getName());
StringBuilder sb = new StringBuilder();
sb.append("Acl Entry [Value: ").append(getValue());

if (getName() != null) {
sb.append(", Name: ").append(getName());
}

if (getExpirationTime() != null) {
sb.append(", Expiration Time: ").append(getExpirationTime());
}

sb.append("]");
return sb.toString();
}

public AclEntry toAclEntry() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void setPointInTimeRecoveryEnabled(Boolean pointInTimeRecoveryEnabled) {
* The start time for the daily backup configuration in UTC timezone in the 24 hour format - `HH:MM`.
*/
@Updatable
@Regex("[0-2][0-9]:[0-5][0-9]")
@Regex("([01][0-9]|2[0-3]):([0-5][0-9])")
public String getStartTime() {
return startTime;
}
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/gyro/google/cloudsql/DbInsightsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,7 @@ public String primaryKey() {

public InsightsConfig toInsightsConfig() {
InsightsConfig config = new InsightsConfig();

if (getQueryInsightsEnabled() != null) {
config.setQueryInsightsEnabled(getQueryInsightsEnabled());
}
config.setQueryInsightsEnabled(getQueryInsightsEnabled());

if (getQueryPlansPerMinute() != null) {
config.setQueryPlansPerMinute(getQueryPlansPerMinute());
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/gyro/google/cloudsql/DbMaintenanceWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import gyro.core.resource.Diffable;
import gyro.core.resource.Updatable;
import gyro.core.validation.Range;
import gyro.core.validation.Required;
import gyro.core.validation.ValidStrings;
import gyro.google.Copyable;

Expand All @@ -35,6 +36,7 @@ public class DbMaintenanceWindow extends Diffable implements Copyable<Maintenanc
*/
@Range(min = 1, max = 7)
@Updatable
@Required
public Integer getDay() {
return day;
}
Expand All @@ -48,6 +50,7 @@ public void setDay(Integer day) {
*/
@Range(min = 0, max = 23)
@Updatable
@Required
public Integer getHour() {
return hour;
}
Expand All @@ -62,6 +65,7 @@ public void setHour(Integer hour) {
*/
@ValidStrings({ "canary", "stable", "week5" })
@Updatable
@Required
public String getUpdateTrack() {
return updateTrack;
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/gyro/google/cloudsql/DbPasswordValidationPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@

package gyro.google.cloudsql;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.google.api.services.sqladmin.model.PasswordValidationPolicy;
import gyro.core.resource.Diffable;
import gyro.core.resource.Updatable;
import gyro.core.validation.Required;
import gyro.core.validation.ValidStrings;
import gyro.core.validation.ValidationError;
import gyro.google.Copyable;

public class DbPasswordValidationPolicy extends Diffable implements Copyable<PasswordValidationPolicy> {
Expand Down Expand Up @@ -148,4 +153,20 @@ public PasswordValidationPolicy toPasswordValidationPolicy() {

return policy;
}

@Override
public List<ValidationError> validate(Set<String> configuredFields) {
List<ValidationError> errors = new ArrayList<>();

if (configuredFields.contains("enable-password-policy") && !getEnablePasswordPolicy() &&
(getDisallowUsernameSubstring() != null || getMinLength() != null || getComplexity() != null
|| getPasswordChangeInterval() != null || getReuseInterval() != null)) {
errors.add(new ValidationError(
this,
"enable-password-policy",
"'enable-password-policy' needs to be set to 'true' explicitly, in order to enable other password validation options."));
}

return errors;
}
}
15 changes: 15 additions & 0 deletions src/main/java/gyro/google/cloudsql/DbPscConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import gyro.core.resource.Updatable;
import gyro.core.validation.DependsOn;
import gyro.core.validation.Required;
import gyro.core.validation.ValidationError;
import gyro.google.Copyable;

public class DbPscConfig extends Diffable implements Copyable<PscConfig> {
Expand Down Expand Up @@ -82,4 +83,18 @@ public PscConfig toPscConfig() {

return config;
}

@Override
public List<ValidationError> validate() {
List<ValidationError> errors = new ArrayList<>();

if (!getPscEnabled() && !getAllowedConsumerProjects().isEmpty()) {
errors.add(new ValidationError(
this,
"allowed-consumer-projects",
"'psc-enabled' should be set to 'true' in order to set 'allowed-consumer-projects'."));
}

return errors;
}
}
2 changes: 2 additions & 0 deletions src/main/java/gyro/google/cloudsql/DbReplicationCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import gyro.core.resource.Diffable;
import gyro.core.resource.Output;
import gyro.core.resource.Updatable;
import gyro.core.validation.Required;
import gyro.google.Copyable;

public class DbReplicationCluster extends Diffable implements Copyable<ReplicationCluster> {
Expand All @@ -37,6 +38,7 @@ public class DbReplicationCluster extends Diffable implements Copyable<Replicati
* Only for Enterprise Plus edition instances.
*/
@Updatable
@Required
public String getFailoverDrReplicaName() {
return failoverDrReplicaName;
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/gyro/google/cloudsql/DbSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,16 @@ public class DbSettings extends Diffable implements Copyable<Settings> {
private DbMaintenanceWindow maintenanceWindow;
private DbPasswordValidationPolicy passwordValidationPolicy;
private String pricingPlan;
private Long settingsVersion;
private DbSqlServerAuditConfig sqlServerAuditConfig;
private Boolean storageAutoResize;
private Long storageAutoResizeLimit;
private String tier;
private String timeZone;
private Map<String, String> userLabels;

// Read-only
private Long settingsVersion;

/**
* The activation policy specifies when the instance is activated.
*/
Expand Down

0 comments on commit ff533c7

Please sign in to comment.