Skip to content

Commit

Permalink
Merge pull request #306 from mbj/add/env-spec-render-value
Browse files Browse the repository at this point in the history
[stack-deploy] Add envSpecValue to public interface
  • Loading branch information
mbj authored Jul 3, 2024
2 parents 3239364 + 40a9d9b commit fdf0916
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions stack-deploy/src/StackDeploy/EnvSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ module StackDeploy.EnvSpec
, envSpecEcsTaskDefinitionEnvironment
, envSpecLambdaEnvironment
, envSpecPosixEnvironment
, envSpecValue
, readEnvSpecFromEnvironment
, readEnvSpecFromStack
, readEnvSpecValueFromStack
)
where

Expand Down Expand Up @@ -82,7 +84,7 @@ data EnvSpecValue
envSpecEcsTaskDefinitionEnvironment :: [EnvSpec] -> [ECS.TaskDefinition.KeyValuePairProperty]
envSpecEcsTaskDefinitionEnvironment entries = render <$> List.sortOn (.name) entries
where
render (EnvSpec key value) = mkPair key $ renderValue value
render (EnvSpec key value) = mkPair key $ envSpecValue value

mkPair :: EnvSpecName -> CFT.Value Text -> ECS.TaskDefinition.KeyValuePairProperty
mkPair key value
Expand All @@ -105,7 +107,7 @@ envSpecLambdaEnvironment entries
variables :: Map Text (CFT.Value Text)
variables = fromList $ render <$> List.sortOn (.name) entries

render (EnvSpec key value) = (convert @Text key, renderValue value)
render (EnvSpec key value) = (convert @Text key, envSpecValue value)

-- | Construct a posix environment list
--
Expand Down Expand Up @@ -142,7 +144,20 @@ envSpecPosixEnvironment stack = traverse render . List.sortOn (.name)
-- >>> value
-- "test-stack"
readEnvSpecFromStack :: forall m . MonadIO m => CF.Stack -> EnvSpec -> m Text
readEnvSpecFromStack stack EnvSpec{..} = case value of
readEnvSpecFromStack stack EnvSpec{..} = readEnvSpecValueFromStack stack value

-- | Read an env spec value as AWS resources would do
--
-- Primary use case is to initialize operations CLI to the same state an equivalent AWS resource would have.
--
-- >>> let envSpecValue = StackDeploy.EnvSpecStackName
-- >>> let epochTime = Time.posixSecondsToUTCTime 0
-- >>> let stack = CF.newStack "test-stack" epochTime CF.StackStatus_UPDATE_COMPLETE
-- >>> value <- StackDeploy.readEnvSpecValueFromStack stack envSpecValue
-- >>> value
-- "test-stack"
readEnvSpecValueFromStack :: forall m . MonadIO m => CF.Stack -> EnvSpecValue -> m Text
readEnvSpecValueFromStack stack = \case
EnvSpecStackId -> maybe failAbsentStackId pure stack.stackId
EnvSpecStackName -> pure stack.stackName
EnvSpecStackOutput output -> liftIO $ StackDeploy.fetchStackOutput stack output
Expand All @@ -159,8 +174,9 @@ readEnvSpecFromStack stack EnvSpec{..} = case value of
readEnvSpecFromEnvironment :: EnvSpec -> MIO env Text
readEnvSpecFromEnvironment EnvSpec{..} = convert <$> Environment.getEnv (convertVia @Text name)

renderValue :: EnvSpecValue -> CFT.Value Text
renderValue = \case
-- | Render env spec value to stratosphere expression
envSpecValue :: EnvSpecValue -> CFT.Value Text
envSpecValue = \case
EnvSpecStackId -> CFT.awsStackId
EnvSpecStackName -> CFT.awsStackName
EnvSpecStackOutput output -> output.value
Expand Down

0 comments on commit fdf0916

Please sign in to comment.