Skip to content

Commit fc3846f

Browse files
committed
Merge pull request #3 from alephcloud/js/custom-endpoint
Custom service endpoints
2 parents 9fad507 + 1b67be9 commit fc3846f

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
0.2.1
2+
=====
3+
4+
* Add support for custom endpoints beyond the standard regions.
5+
16
0.2.0
27
=====
38

aws-general.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
-- ------------------------------------------------------ --
44

55
Name: aws-general
6-
Version: 0.2.0
6+
Version: 0.2.1
77
Synopsis: Bindings for Amazon Web Services (AWS) General Reference
88
description:
99
Bindings for Amazon Web Services (AWS) General Reference including AWS Signature V4.

src/Aws/General.hs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import Control.Monad
6666
import Data.Aeson (ToJSON(..), FromJSON(..), withText)
6767
import qualified Data.Attoparsec.Text as AP
6868
import Data.Hashable (Hashable, hashWithSalt, hashUsing)
69+
import qualified Data.List as L
6970
import Data.Monoid
7071
import Data.String
7172
import qualified Data.Text as T
@@ -182,9 +183,13 @@ data Region
182183
| UsEast1
183184
| UsWest1
184185
| UsWest2
185-
deriving (Show, Read, Eq, Ord, Enum, Bounded, Typeable)
186+
| CustomEndpoint !T.Text !Int
187+
-- ^ To override the region settings with a custom service endpoint, e.g.
188+
-- for testing purpose
189+
190+
deriving (Show, Read, Eq, Ord, Typeable)
186191

187-
regionToText :: IsString a => Region -> a
192+
regionToText :: (Monoid a, IsString a) => Region -> a
188193
regionToText ApNortheast1 = "ap-northeast-1"
189194
regionToText ApSoutheast1 = "ap-southeast-1"
190195
regionToText ApSoutheast2 = "ap-southeast-2"
@@ -193,7 +198,22 @@ regionToText SaEast1 = "sa-east-1"
193198
regionToText UsEast1 = "us-east-1"
194199
regionToText UsWest1 = "us-west-1"
195200
regionToText UsWest2 = "us-west-2"
201+
regionToText (CustomEndpoint e p) = "custom:" <> fromString (T.unpack e) <> ":" <> fromString (show p)
196202

203+
-- | Regions are parsed as follows:
204+
--
205+
-- @
206+
-- 'ApNortheast1' ::= "ap-northeast-1"
207+
-- 'ApSoutheast1' ::= "ap-southeast-1"
208+
-- 'ApSoutheast2' ::= "ap-southeast-2"
209+
-- 'EuWest1' ::= "eu-west-1"
210+
-- 'SaEast1' ::= "sa-east-1"
211+
-- 'UsEast1' ::= "us-east-1"
212+
-- 'UsWest1' ::= "us-west-1"
213+
-- 'UsWest2' ::= "us-west-2"
214+
-- 'CustomEndpoint' ::= "custom:" 'T.Text' ":" 'Int'
215+
-- @
216+
--
197217
parseRegion :: P.CharParsing m => m Region
198218
parseRegion =
199219
ApNortheast1 <$ P.text "ap-northeast-1"
@@ -204,12 +224,30 @@ parseRegion =
204224
<|> UsEast1 <$ P.text "us-east-1"
205225
<|> UsWest1 <$ P.text "us-west-1"
206226
<|> UsWest2 <$ P.text "us-west-2"
227+
<|> parseCustomEndpoint
207228
<?> "Region"
229+
where
230+
parseCustomEndpoint = CustomEndpoint
231+
<$> (fmap T.pack $ P.text "custom:" *> many (P.notChar ':'))
232+
<*> (fmap read $ P.text ":" *> some P.digit)
208233

209234
instance AwsType Region where
210235
toText = regionToText
211236
parse = parseRegion
212237

238+
standardRegions :: [Region]
239+
standardRegions =
240+
[ ApNortheast1
241+
, ApSoutheast1
242+
, ApSoutheast2
243+
, EuWest1
244+
, SaEast1
245+
, UsEast1
246+
, UsWest1
247+
, UsWest2
248+
]
249+
250+
213251
{-
214252
instance FromJSON Ec2Region where
215253
parseJSON = withText "Ec2Region" $ either fail return ∘ readEither ∘ T.unpack
@@ -219,10 +257,20 @@ instance ToJSON Ec2Region where
219257
-}
220258

221259
instance Hashable Region where
222-
hashWithSalt = hashUsing fromEnum
260+
hashWithSalt s (CustomEndpoint e p) = s `hashWithSalt` (0 :: Int) `hashWithSalt` (e, p)
261+
hashWithSalt s r =
262+
case L.elemIndex r standardRegions of
263+
Just i -> hashWithSalt s (succ i)
264+
Nothing -> hashWithSalt s (length standardRegions + 1)
223265

224266
instance Q.Arbitrary Region where
225-
arbitrary = Q.elements [minBound..maxBound]
267+
arbitrary = Q.oneof
268+
[ Q.elements standardRegions
269+
, CustomEndpoint <$> arbitraryEndpoint <*> arbitraryPort
270+
]
271+
where
272+
arbitraryEndpoint = fmap T.pack . Q.listOf . Q.elements $ '.' : ['a'..'z']
273+
arbitraryPort = Q.choose (0, 10000)
226274

227275
-- -------------------------------------------------------------------------- --
228276
-- AWS Account Id

0 commit comments

Comments
 (0)