Skip to content

Commit 6687911

Browse files
committed
Add a Snowflake class that sets conventional defaults and turns itself into a secret and SSM params
1 parent 648d54f commit 6687911

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

iac/src/main/java/com/compassion/commons/iac/CDKUtils.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
import lombok.AccessLevel;
1717
import lombok.RequiredArgsConstructor;
18+
import lombok.Setter;
19+
import lombok.experimental.Accessors;
1820
import lombok.experimental.Delegate;
1921
import software.amazon.awscdk.Stack;
2022
import software.amazon.awscdk.services.ec2.ISubnet;
@@ -119,11 +121,14 @@ default Function.Builder lambdaFromPOM(String artifactId, Path... pomPaths) thro
119121
}
120122
}
121123

124+
@Setter @Accessors(fluent = true)
122125
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
123126
class ParamFromSecretBuilder implements Builder<CfnParameter> {
124127
@Delegate
125128
private final CfnParameter.Builder delegate;
126129
private final String path;
130+
131+
private String pathDescription;
127132

128133
// Assumes the last path fragment is the access path inside the secret
129134
public ParamFromSecretBuilder secret(ISecret s) {
@@ -135,5 +140,23 @@ public ParamFromSecretBuilder secret(ISecret s, String secretPath) {
135140
value(SECRET_PATH + s.getSecretName() + "/" + secretPath);
136141
return this;
137142
}
143+
144+
/**
145+
* Sets the description for the parameter. If you have already set a
146+
* path description via {@link #pathDescription(String)} that will be
147+
* appended to the end of this description to form the final description.
148+
* If not, this description will be used as-is.
149+
* @param baseDescription the base description for related parameters
150+
* at the same level of the hierarchy.
151+
* @return this for chaining
152+
*/
153+
public ParamFromSecretBuilder description(String baseDescription) {
154+
if (pathDescription == null) {
155+
delegate.description(baseDescription);
156+
} else {
157+
delegate.description(baseDescription + " - " + pathDescription);
158+
}
159+
return this;
160+
}
138161
}
139162
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.compassion.commons.iac;
2+
3+
import org.jooq.lambda.Seq;
4+
5+
import com.compassion.commons.Utilities;
6+
import com.compassion.commons.config.CIEnvironment;
7+
import com.compassion.commons.config.CredentialConfig.ConfigWithUserPassword;
8+
import com.compassion.commons.iac.CDKUtils.ParamFromSecretBuilder;
9+
import com.fasterxml.jackson.core.JsonProcessingException;
10+
11+
import lombok.Getter;
12+
import lombok.Setter;
13+
import lombok.experimental.Accessors;
14+
import software.amazon.awscdk.SecretValue;
15+
import software.amazon.awscdk.services.secretsmanager.ISecret;
16+
import software.amazon.awscdk.services.secretsmanager.Secret;
17+
import software.constructs.Construct;
18+
19+
@Getter @Setter @Accessors(chain = true)
20+
public class CISnowflake extends ConfigWithUserPassword {
21+
public enum CISnowflakeRole {
22+
WRITER,
23+
READER,
24+
DEVELOPER
25+
}
26+
27+
private String database, role;
28+
29+
public CISnowflake withDefaults(CIEnvironment env, String svc, String domain, CISnowflakeRole roleType) {
30+
switch (env) {
31+
case Prod -> setDatabase(domain).setRole(domain + "_" + roleType).setUser(svc + "_SVC");
32+
case Stage -> setDatabase(domain + "_STAGE").setRole(domain + "_STAGE_" + roleType).setUser(svc + "_STAGE_SVC");
33+
default -> setDatabase(domain + "_DEVINT").setRole(domain + "_DEVINT_" + roleType).setUser(svc + "_DEVINT_SVC");
34+
case Cloud -> throw new IllegalArgumentException("Snowflake does not support the Cloud environment");
35+
}
36+
return this;
37+
}
38+
39+
public Secret.Builder asSecret(Construct stack, String id) {
40+
try {
41+
return Secret.Builder.create(stack, id).secretStringValue(SecretValue.unsafePlainText(toPlaceholderJson()));
42+
} catch (JsonProcessingException e) {
43+
throw new IllegalArgumentException("Cannot generate placeholder JSON from " + this, e);
44+
}
45+
}
46+
47+
public Seq<ParamFromSecretBuilder> asParams(CDKUtils utils, String path, ISecret secret) {
48+
return Seq.of(
49+
utils.newParam(path + "/user").pathDescription("Username").secret(secret),
50+
utils.newParam(path + "/password").pathDescription("Password").secret(secret),
51+
Utilities.cast(utils.newParam(path + "/database").pathDescription("Database").value(getDatabase())),
52+
Utilities.cast(utils.newParam(path + "/role").pathDescription("Database Role").value(getRole()))
53+
);
54+
}
55+
}

0 commit comments

Comments
 (0)