Skip to content

Commit

Permalink
add examples and local write forwarding
Browse files Browse the repository at this point in the history
  • Loading branch information
harjain99 committed Aug 27, 2024
1 parent 1dac83c commit 321141f
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 4 deletions.
135 changes: 135 additions & 0 deletions examples/rds/db-cluster-serverless.gyro
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
aws::vpc vpc
cidr-block: "10.0.0.0/16"

tags: {
Name: "vpc-example"
}
end

aws::subnet subnet-us-east-2a
vpc: $(aws::vpc vpc)
availability-zone: "us-east-2a"
cidr-block: "10.0.0.0/24"
tags: {
Name: "subnet-us-east-2a-example"
}
end

aws::subnet subnet-us-east-2b
vpc: $(aws::vpc vpc)
availability-zone: "us-east-2b"
cidr-block: "10.0.1.0/24"
tags: {
Name: "subnet-us-east-2b-example"
}
end

aws::subnet subnet-us-east-2c
vpc: $(aws::vpc vpc)
availability-zone: "us-east-2c"
cidr-block: "10.0.2.0/24"
tags: {
Name: "subnet-us-east-2c-example"
}
end

aws::db-subnet-group db-subnet-group
name: "db-subnet-group-example"
description: "db subnet group description"
subnets: [
$(aws::subnet subnet-us-east-2a),
$(aws::subnet subnet-us-east-2b),
$(aws::subnet subnet-us-east-2c)
]

tags: {
Name: "db-subnet-group-example"
}
end

aws::security-group security-group-example
name: "example-db-cluster-security-group"
vpc: $(aws::vpc vpc)
description: "Allow web traffic only"
end

aws::security-group-rules security-group-example-rules
security-group: $(aws::security-group security-group-example)
keep-default-egress-rules: true

@for port, type -in [80, 'http', 443, 'https', 999, 'foo']
ingress
description: "allow inbound $(type) traffic, ipv4 only"
cidr-block: "0.0.0.0/0"
protocol: "TCP"
from-port: $(port)
to-port: $(port)
end
@end

egress
description: "allow outbound http traffic, ipv6 only"
ipv6-cidr-block: "::/0"
protocol: "TCP"
from-port: 80
to-port: 80
end
end

aws::kms-key aurora-master-password-encryption
description: "KMS key used to encrypt aurora database master password"

aliases: ["alias/example/aurora-master-password-encryption"]
enabled: "true"
end

aws::db-cluster db-cluster-example
identifier: "aurora-mysql-cluster"
engine: "aurora-mysql"
engine-version: "8.0.mysql_aurora.3.06.0"
availability-zones: ["us-east-2a", "us-east-2b", "us-east-2c"]
db-name: "clusterexample"

master-username: "root"
manage-master-user-password: true
master-user-secret-kms-key: $(aws::kms-key aurora-master-password-encryption)

storage-type: "aurora"

engine-mode: "provisioned"
serverless-v2-scaling-configuration
min-capacity: 1
max-capacity: 3
end

db-subnet-group: $(aws::db-subnet-group db-subnet-group)
vpc-security-groups: [
$(aws::security-group security-group-example)
]
backup-retention-period: 5
preferred-backup-window: "07:00-09:00"
preferred-maintenance-window: "tue:15:00-tue:17:00"
skip-final-snapshot: true
enable-local-write-forwarding: false

tags: {
Name: "aurora-mysql-cluster"
}
copy-tags-to-snapshot: true
storage-encrypted: true
kms-key: $(aws::kms-key aurora-master-password-encryption)
back-track-window: 0
auto-minor-version-upgrade: true
deletion-protection: false
end

aws::db-instance db-instance-example
identifier: "aurora-mysql-cluster-instance"
db-instance-class: "db.serverless"
db-cluster: $(aws::db-cluster db-cluster-example)
engine: "aurora-mysql"
apply-immediately: true
tags: {
Name: "aurora-mysql-cluster-instance"
}
end
52 changes: 48 additions & 4 deletions src/main/java/gyro/aws/rds/DbClusterResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@
import gyro.core.validation.Required;
import gyro.core.validation.ValidStrings;
import software.amazon.awssdk.services.rds.RdsClient;
import software.amazon.awssdk.services.rds.model.CreateDbClusterRequest;
import software.amazon.awssdk.services.rds.model.CreateDbClusterResponse;
import software.amazon.awssdk.services.rds.model.DBCluster;
import software.amazon.awssdk.services.rds.model.DbClusterNotFoundException;
import software.amazon.awssdk.services.rds.model.DescribeDbClustersResponse;
import software.amazon.awssdk.services.rds.model.InvalidDbClusterStateException;
import software.amazon.awssdk.services.rds.model.LocalWriteForwardingStatus;
import software.amazon.awssdk.services.rds.model.ModifyDbClusterRequest;
import software.amazon.awssdk.services.rds.model.RestoreDbClusterFromS3Response;
import software.amazon.awssdk.services.rds.model.RestoreDbClusterFromSnapshotResponse;

Expand Down Expand Up @@ -145,6 +148,7 @@ public class DbClusterResource extends RdsTaggableResource implements Copyable<D
private Integer allocatedStorage;
private Boolean autoMinorVersionUpgrade;
private Boolean copyTagsToSnapshot;
private Boolean enableLocalWriteForwarding;

// Read-only
private String endpointAddress;
Expand Down Expand Up @@ -186,6 +190,10 @@ public void setAvailabilityZones(List<String> availabilityZones) {
*/
@Updatable
public Long getBackTrackWindow() {
if (backTrackWindow == null) {
backTrackWindow = 0L;
}

return backTrackWindow;
}

Expand Down Expand Up @@ -659,6 +667,23 @@ public void setCopyTagsToSnapshot(Boolean copyTagsToSnapshot) {
this.copyTagsToSnapshot = copyTagsToSnapshot;
}

/**
* When set to ``true``, read replicas can forward write operations to the writer DB instance in the DB cluster.
* By default, write operations aren't allowed on reader DB instances.
*/
@Updatable
public Boolean getEnableLocalWriteForwarding() {
if (enableGlobalWriteForwarding == null) {
enableGlobalWriteForwarding = false;
}

return enableLocalWriteForwarding;
}

public void setEnableLocalWriteForwarding(Boolean enableLocalWriteForwarding) {
this.enableLocalWriteForwarding = enableLocalWriteForwarding;
}

/**
* DNS hostname to access the primary instance of the cluster.
*/
Expand Down Expand Up @@ -753,6 +778,12 @@ public void copyFrom(DBCluster cluster) {
config.copyFrom(cluster.serverlessV2ScalingConfiguration());
setServerlessV2ScalingConfiguration(config);
}

setEnableLocalWriteForwarding(
LocalWriteForwardingStatus.REQUESTED.equals(cluster.localWriteForwardingStatus()) ||
LocalWriteForwardingStatus.ENABLING.equals(cluster.localWriteForwardingStatus()) ||
LocalWriteForwardingStatus.ENABLED.equals(cluster.localWriteForwardingStatus())
);
}

@Override
Expand Down Expand Up @@ -892,8 +923,13 @@ protected void doCreate(GyroUI ui, State state) {
}

} else {
CreateDbClusterRequest.Builder builder = CreateDbClusterRequest.builder();
if (Boolean.TRUE.equals(getEnableGlobalWriteForwarding())) {
builder.enableGlobalWriteForwarding(getEnableGlobalWriteForwarding());
}

CreateDbClusterResponse response = client.createDBCluster(
r -> r.availabilityZones(getAvailabilityZones())
builder.availabilityZones(getAvailabilityZones())
.backtrackWindow(getBackTrackWindow())
.backupRetentionPeriod(getBackupRetentionPeriod())
.characterSetName(getCharacterSetName())
Expand Down Expand Up @@ -925,7 +961,6 @@ protected void doCreate(GyroUI ui, State state) {
.map(SecurityGroupResource::getId)
.collect(Collectors.toList()) : null)
.dbClusterInstanceClass(getDbClusterInstanceClass())
.enableGlobalWriteForwarding(getEnableGlobalWriteForwarding())
.iops(getIops())
.manageMasterUserPassword(getManageMasterUserPassword())
.masterUserSecretKmsKeyId(getMasterUserSecretKmsKey().getId())
Expand All @@ -935,6 +970,8 @@ protected void doCreate(GyroUI ui, State state) {
.allocatedStorage(getAllocatedStorage())
.autoMinorVersionUpgrade(getAutoMinorVersionUpgrade())
.copyTagsToSnapshot(getCopyTagsToSnapshot())
.enableLocalWriteForwarding(getEnableLocalWriteForwarding())
.build()
);

setArn(response.dbCluster().dbClusterArn());
Expand Down Expand Up @@ -981,8 +1018,13 @@ protected void doUpdate(Resource config, Set<String> changedProperties) {
.collect(Collectors.toList()) : null;

try {
ModifyDbClusterRequest.Builder builder = ModifyDbClusterRequest.builder();
if (!Objects.equals(getEnableGlobalWriteForwarding(), current.getEnableGlobalWriteForwarding())) {
builder.enableGlobalWriteForwarding(getEnableGlobalWriteForwarding());
}

client.modifyDBCluster(
r -> r.applyImmediately(getApplyImmediately())
builder.applyImmediately(getApplyImmediately())
.backtrackWindow(Objects.equals(getBackTrackWindow(), current.getBackTrackWindow())
? null : getBackTrackWindow())
.backupRetentionPeriod(Objects.equals(
Expand Down Expand Up @@ -1015,7 +1057,6 @@ protected void doUpdate(Resource config, Set<String> changedProperties) {
.vpcSecurityGroupIds(Objects.equals(getVpcSecurityGroups(), current.getVpcSecurityGroups())
? null : vpcSecurityGroupIds)
.dbClusterInstanceClass(getDbClusterInstanceClass())
.enableGlobalWriteForwarding(getEnableGlobalWriteForwarding())
.iops(getIops())
.manageMasterUserPassword(getManageMasterUserPassword())
.masterUserSecretKmsKeyId(getMasterUserSecretKmsKey().getId())
Expand All @@ -1025,6 +1066,9 @@ protected void doUpdate(Resource config, Set<String> changedProperties) {
.allocatedStorage(getAllocatedStorage())
.autoMinorVersionUpgrade(getAutoMinorVersionUpgrade())
.copyTagsToSnapshot(getCopyTagsToSnapshot())
.enableLocalWriteForwarding(Objects.equals(getEnableLocalWriteForwarding(),
current.getEnableLocalWriteForwarding()) ? null : getEnableLocalWriteForwarding())
.build()
);
} catch (InvalidDbClusterStateException ex) {
throw new GyroException(ex.getLocalizedMessage());
Expand Down

0 comments on commit 321141f

Please sign in to comment.