Skip to content

Commit

Permalink
feat: list AWS Lightsail buckets
Browse files Browse the repository at this point in the history
  • Loading branch information
vst committed May 4, 2024
1 parent bb0a78a commit 7b801dc
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/Clompse/Cli.hs
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,14 @@ doObjectBucketListConsole rs =
, Tab.column Tab.expand Tab.left Tab.noAlign Tab.noCutMark
, Tab.column Tab.expand Tab.left Tab.noAlign Tab.noCutMark
, Tab.column Tab.expand Tab.left Tab.noAlign Tab.noCutMark
, Tab.column Tab.expand Tab.left Tab.noAlign Tab.noCutMark
]
hs =
Tab.titlesH
[ "#" :: String
, "Profile"
, "Provider"
, "Product"
, "Name"
, "Created"
]
Expand All @@ -306,6 +308,7 @@ doObjectBucketListConsole rs =
[ formatIntegral i
, _objectBucketListItemProfile
, Types.providerCode _objectBucketListItemProvider
, _objectBucketListItemProduct
, _objectBucketListItemName
, maybe "<unknown>" Z.Text.tshow _objectBucketListItemCreatedAt
]
Expand Down
14 changes: 12 additions & 2 deletions src/Clompse/Programs/ListObjectBuckets.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,14 @@ listObjectBucketsForCloudConnection
-> m [Types.ObjectBucket]
listObjectBucketsForCloudConnection (CloudConnectionAws conn) = do
eBucketsS3 <- runExceptT (Providers.Aws.awsListAllS3Buckets conn)
case eBucketsS3 of
bucketsS3 <- case eBucketsS3 of
Left e -> _log (" ERROR (AWS S3): " <> Z.Text.tshow e) >> pure []
Right buckets -> pure (fmap (\(n, c) -> Types.ObjectBucket n Types.ProviderAws (Just c)) buckets)
Right buckets -> pure (fmap (\(n, c) -> Types.ObjectBucket n Types.ProviderAws "S3" (Just c)) buckets)
eBucketsLightsail <- runExceptT (Providers.Aws.awsListAllLightsailBuckets conn)
bucketsLightsail <- case eBucketsLightsail of
Left e -> _log (" ERROR (AWS Lightsail): " <> Z.Text.tshow e) >> pure []
Right buckets -> pure (fmap (\(n, c) -> Types.ObjectBucket n Types.ProviderAws "Lightsail" (Just c)) buckets)
pure $ bucketsS3 <> bucketsLightsail
listObjectBucketsForCloudConnection (CloudConnectionDo _conn) = do
pure []
listObjectBucketsForCloudConnection (CloudConnectionHetzner _conn) = do
Expand All @@ -85,6 +90,7 @@ type ObjectBucketList = [ObjectBucketListItem]
data ObjectBucketListItem = ObjectBucketListItem
{ _objectBucketListItemProfile :: !T.Text
, _objectBucketListItemProvider :: !Types.Provider
, _objectBucketListItemProduct :: !T.Text
, _objectBucketListItemName :: !T.Text
, _objectBucketListItemCreatedAt :: !(Maybe Time.UTCTime)
}
Expand All @@ -101,6 +107,7 @@ instance ADC.HasCodec ObjectBucketListItem where
ObjectBucketListItem
<$> ADC.requiredField "profile" "Name of the cloud profile." ADC..= _objectBucketListItemProfile
<*> ADC.requiredField "provider" "Provider of the object bucket." ADC..= _objectBucketListItemProvider
<*> ADC.requiredField "product" "Product name." ADC..= _objectBucketListItemProduct
<*> ADC.requiredField "name" "Name of the object bucket." ADC..= _objectBucketListItemName
<*> ADC.optionalField "created_at" "Creation time of the object bucket." ADC..= _objectBucketListItemCreatedAt

Expand All @@ -110,6 +117,7 @@ instance Cassava.ToNamedRecord ObjectBucketListItem where
Cassava.namedRecord
[ "profile" Cassava..= _objectBucketListItemProfile
, "provider" Cassava..= Types.providerCode _objectBucketListItemProvider
, "product" Cassava..= _objectBucketListItemProduct
, "name" Cassava..= _objectBucketListItemName
, "created_at" Cassava..= fmap Z.Text.tshow _objectBucketListItemCreatedAt
]
Expand All @@ -120,6 +128,7 @@ instance Cassava.DefaultOrdered ObjectBucketListItem where
V.fromList
[ "profile"
, "provider"
, "product"
, "name"
, "created_at"
]
Expand All @@ -133,6 +142,7 @@ toObjectBucketList ListObjectBucketsResult {..} =
ObjectBucketListItem
{ _objectBucketListItemProfile = p
, _objectBucketListItemProvider = _objectBucketProvider
, _objectBucketListItemProduct = _objectBucketProduct
, _objectBucketListItemName = _objectBucketName
, _objectBucketListItemCreatedAt = _objectBucketCreatedAt
}
41 changes: 41 additions & 0 deletions src/Clompse/Providers/Aws.hs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,47 @@ awsLightsailListAllInstancesForRegion cfg reg = do
fmap (fmap (reg,)) . liftIO . Aws.runResourceT . C.runConduit $ prog


-- *** Buckets


awsListAllLightsailBuckets
:: MonadIO m
=> MonadError AwsError m
=> AwsConnection
-> m [(T.Text, Time.UTCTime)]
awsListAllLightsailBuckets cfg = do
regions <- awsLightsailListAllRegions cfg
res <- liftIO . Async.withTaskGroup 4 $ \tg -> Async.mapTasks tg (fmap (runExceptT . awsListAllLightsailBucketsForRegion cfg) regions)
case concat <$> sequence res of
Left e -> throwError e
Right x -> pure x


awsListAllLightsailBucketsForRegion
:: MonadIO m
=> MonadError AwsError m
=> AwsConnection
-> Aws.Region
-> m [(T.Text, Time.UTCTime)]
awsListAllLightsailBucketsForRegion cfg reg = do
env <- (\x -> x {Aws.region = reg}) <$> _envFromConnection cfg
let prog = Aws.send env Aws.Lightsail.newGetBuckets
resIs <- liftIO . Aws.runResourceT $ prog
-- NOTE: Amazonka does not support pagination over Lightsail buckets.
-- let prog =
-- Aws.paginate env Aws.Lightsail.newGetBuckets
-- .| CL.concatMap (L.view $ Aws.Lightsail.Lens.getBucketsResponse_buckets . L._Just)
-- .| CL.consume
-- resIs <- liftIO . Aws.runResourceT . C.runConduit $ prog
let buckets = fromMaybe [] $ resIs L.^. Aws.Lightsail.Lens.getBucketsResponse_buckets
pure $ mapMaybe mkTuple buckets
where
mkTuple b =
let name = b L.^. Aws.Lightsail.Lens.bucket_name
time = b L.^. Aws.Lightsail.Lens.bucket_createdAt
in (,) <$> name <*> time


-- * Helpers


Expand Down
2 changes: 2 additions & 0 deletions src/Clompse/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ instance ADC.HasCodec ServerIpInfo where
data ObjectBucket = ObjectBucket
{ _objectBucketName :: !T.Text
, _objectBucketProvider :: !Provider
, _objectBucketProduct :: !T.Text
, _objectBucketCreatedAt :: !(Maybe Time.UTCTime)
}
deriving (Eq, Generic, Show)
Expand All @@ -196,4 +197,5 @@ instance ADC.HasCodec ObjectBucket where
ObjectBucket
<$> ADC.requiredField "name" "Bucket name." ADC..= _objectBucketName
<*> ADC.requiredField "provider" "Cloud provider." ADC..= _objectBucketProvider
<*> ADC.requiredField "product" "Product name." ADC..= _objectBucketProduct
<*> ADC.optionalField "created_at" "Creation timestamp." ADC..= _objectBucketCreatedAt

0 comments on commit 7b801dc

Please sign in to comment.