Skip to content

Commit

Permalink
Add support for Fn::Cidr
Browse files Browse the repository at this point in the history
  • Loading branch information
mbj committed Jul 2, 2024
1 parent 8c7df18 commit e29aea2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/Stratosphere/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ mkFunc key args = JSON.object [(key, JSON.Array $ fromList args)]
-- @List<AWS::EC2::Subnet::Id>@ then, you can use @RefList "SubnetIds"@ to
-- reference it.
data ValueList a
= GetAZs (Value Text)
= Cidr (Value Text) (Value Text) (Value Text)
| GetAZs (Value Text)
| ImportValueList (Value Text)
| RefList Text
| Split Text (Value a)
Expand All @@ -112,6 +113,7 @@ instance IsList (ValueList a) where
fromList = ValueList

toList = \case
(Cidr _ _ _) -> []
(GetAZs _) -> []
(ImportValueList _) -> []
(RefList _) -> []
Expand All @@ -122,11 +124,20 @@ instance IsList (ValueList a) where

instance JSON.ToJSON a => JSON.ToJSON (ValueList a) where
toJSON = \case
(GetAZs r) -> JSON.object [("Fn::GetAZs", JSON.toJSON r)]
(ImportValueList ref) -> importValueToJSON ref
(RefList ref) -> refToJSON ref
(Split d s) -> mkFunc "Fn::Split" [JSON.toJSON d, JSON.toJSON s]
(ValueList vals) -> JSON.toJSON vals
(Cidr ipBlock count cidrBits) -> JSON.object [("Fn::Cidr", cidrArray ipBlock count cidrBits)]
(GetAZs r) -> JSON.object [("Fn::GetAZs", JSON.toJSON r)]
(ImportValueList ref) -> importValueToJSON ref
(RefList ref) -> refToJSON ref
(Split d s) -> mkFunc "Fn::Split" [JSON.toJSON d, JSON.toJSON s]
(ValueList vals) -> JSON.toJSON vals
where
cidrArray :: Value Text -> Value Text -> Value Text -> JSON.Value
cidrArray ipBlock count cidrBits
= JSON.Array
[ JSON.toJSON ipBlock
, JSON.toJSON count
, JSON.toJSON cidrBits
]

-- | Class used to create a 'Ref' from another type.
class ToRef a b where
Expand Down
3 changes: 3 additions & 0 deletions test/Stratosphere/ValuesSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ spec = do

it "ImportValue and ImportValueList produce the same JSON" $ do
JSON.toJSON (ImportValue "MyVal" :: Value Text) `shouldBe` JSON.toJSON (ImportValueList "MyVal" :: ValueList Text)

it "Cidr produces expected JSON" $ do
JSON.toJSON @(ValueList Text) (Cidr "192.168.0.0/24" "6" "5") `shouldBe` JSON.object [("Fn::Cidr", JSON.Array ["192.168.0.0/24", "6", "5"])]

0 comments on commit e29aea2

Please sign in to comment.