Skip to content

Commit

Permalink
[stack-deploy] Fix maybe stack reads
Browse files Browse the repository at this point in the history
* Originally I thought AWS would return an empty result on a query for a stack that does not exist.
* Instead it returns an error.
* Unfortunately there is no exception free way (I know) to read a paginator from amazonka.
  • Loading branch information
mbj committed Jan 4, 2024
1 parent 01dc7ec commit 73a5306
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions stack-deploy/src/StackDeploy/Stack.hs
Original file line number Diff line number Diff line change
@@ -14,7 +14,9 @@ import Control.Lens (Lens', set, view)
import Data.Conduit ((.|))
import StackDeploy.Prelude
import StackDeploy.Types
import UnliftIO.Exception (catchJust)

import qualified Amazonka
import qualified Amazonka.CloudFormation.DescribeStacks as CF
import qualified Amazonka.CloudFormation.Types as CF
import qualified Data.Conduit as Conduit
@@ -88,14 +90,35 @@ readCloudFormationStack
:: forall env . AWS.Env env
=> StackDeploy.InstanceName
-> MIO env (Maybe CF.Stack)
readCloudFormationStack name
= Conduit.runConduit
$ AWS.nestedResourceC describeSpecificStack (fromMaybe [] . (.stacks))
.| Conduit.find ((convert name ==) . (.stackName))
readCloudFormationStack instanceName = catchJust handleNotFoundError read pure
where
read
= Conduit.runConduit
$ AWS.nestedResourceC describeSpecificStack (fromMaybe [] . (.stacks))
.| Conduit.find ((instanceNameText ==) . (.stackName))

instanceNameText = convert instanceName

describeSpecificStack :: CF.DescribeStacks
describeSpecificStack =
set CF.describeStacks_stackName (pure $ convert name) CF.newDescribeStacks
set CF.describeStacks_stackName (pure instanceNameText) CF.newDescribeStacks

handleNotFoundError :: Amazonka.Error -> Maybe (Maybe CF.Stack)
handleNotFoundError
(Amazonka.ServiceError
Amazonka.ServiceError'
{ code = Amazonka.ErrorCode "ValidationError"
, message = Just actualMessage
}
)
= if actualMessage == expectedMessage
then pure empty
else empty
handleNotFoundError _error = empty

expectedMessage :: Amazonka.ErrorMessage
expectedMessage =
Amazonka.ErrorMessage $ "Stack with id " <> toText instanceNameText <> " does not exist"

readCloudFormationStackPresent
:: forall env . AWS.Env env

0 comments on commit 73a5306

Please sign in to comment.