|
| 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