generated from vst/haskell-template-hebele
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from vst/14-add-firewall-information-to-servers
feat: attach firewall information to servers
- Loading branch information
Showing
7 changed files
with
262 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,7 @@ import qualified Data.Conduit as C | |
import qualified Data.Conduit.List as CL | ||
import qualified Data.HashMap.Strict as HM | ||
import qualified Data.List as List | ||
import Data.Maybe (fromMaybe, mapMaybe, maybeToList) | ||
import Data.Maybe (catMaybes, fromMaybe, mapMaybe, maybeToList) | ||
import qualified Data.Text as T | ||
import qualified Data.Time as Time | ||
import qualified Zamazingo.Net as Z.Net | ||
|
@@ -43,7 +43,7 @@ listServersEc2 | |
=> AwsConnection | ||
-> m [Types.Server] | ||
listServersEc2 cfg = do | ||
instances <- awsEc2ListAllInstances cfg | ||
instances <- awsEc2ListAllInstancesWithSecurityGroups cfg | ||
pure (fmap ec2InstanceToServer instances) | ||
|
||
|
||
|
@@ -202,19 +202,20 @@ awsEc2ListAllSecurityGroupsForRegion cfg reg = do | |
|
||
-- -- *** Instances with Security Groups | ||
|
||
-- awsEc2ListAllInstancesWithSecurityGroups | ||
-- :: MonadIO m | ||
-- => MonadError AwsError m | ||
-- => AwsConnection | ||
-- -> m [(Aws.Region, Aws.Ec2.Instance, [Aws.Ec2.SecurityGroup])] | ||
-- awsEc2ListAllInstancesWithSecurityGroups cfg = do | ||
-- instancesWithRegions <- awsEc2ListAllInstances cfg | ||
-- securityGroups <- awsEc2ListAllSecurityGroups cfg | ||
-- pure (fmap (\(r, i) -> (r, i, findSecurityGroups securityGroups i)) instancesWithRegions) | ||
-- where | ||
-- findSecurityGroups sgs i = | ||
-- let sids = catMaybes $ foldMap (fmap (L.^. Aws.Ec2.Lens.groupIdentifier_groupId)) (i L.^. Aws.Ec2.Lens.instance_securityGroups) | ||
-- in concatMap (\gi -> filter (\sg -> sg L.^. Aws.Ec2.Lens.securityGroup_groupId == gi) sgs) sids | ||
awsEc2ListAllInstancesWithSecurityGroups | ||
:: MonadIO m | ||
=> MonadError AwsError m | ||
=> AwsConnection | ||
-> m [(Aws.Region, Aws.Ec2.Instance, Maybe Int, Maybe Integer, Maybe Integer, [Aws.Ec2.SecurityGroup])] | ||
awsEc2ListAllInstancesWithSecurityGroups cfg = do | ||
instancesWithRegions <- awsEc2ListAllInstances cfg | ||
securityGroups <- awsEc2ListAllSecurityGroups cfg | ||
pure (fmap (\(r, i, m1, m2, m3) -> (r, i, m1, m2, m3, findSecurityGroups securityGroups i)) instancesWithRegions) | ||
where | ||
findSecurityGroups sgs i = | ||
let sids = catMaybes $ foldMap (fmap (L.^. Aws.Ec2.Lens.groupIdentifier_groupId)) (i L.^. Aws.Ec2.Lens.instance_securityGroups) | ||
in concatMap (\gi -> filter (\sg -> sg L.^. Aws.Ec2.Lens.securityGroup_groupId == gi) sgs) sids | ||
|
||
|
||
-- ** S3 Buckets | ||
|
||
|
@@ -239,8 +240,8 @@ awsListAllS3Buckets cfg = do | |
-- ** Converters | ||
|
||
|
||
ec2InstanceToServer :: (Aws.Region, Aws.Ec2.Instance, Maybe Int, Maybe Integer, Maybe Integer) -> Types.Server | ||
ec2InstanceToServer (region, i@Aws.Ec2.Instance' {..}, mCpu, mRam, mDisks) = | ||
ec2InstanceToServer :: (Aws.Region, Aws.Ec2.Instance, Maybe Int, Maybe Integer, Maybe Integer, [Aws.Ec2.SecurityGroup]) -> Types.Server | ||
ec2InstanceToServer (region, i@Aws.Ec2.Instance' {..}, mCpu, mRam, mDisks, _sgs) = | ||
Types.Server | ||
{ Types._serverId = instanceId | ||
, Types._serverName = awsEc2InstanceName i | ||
|
@@ -253,9 +254,38 @@ ec2InstanceToServer (region, [email protected]' {..}, mCpu, mRam, mDisks) = | |
, Types._serverRegion = Aws.fromRegion region | ||
, Types._serverType = Just (Aws.Ec2.fromInstanceType instanceType) | ||
, Types._serverIpInfo = ec2InstanceToServerIpInfo i | ||
, Types._serverFirewalls = fmap toFirewall _sgs | ||
} | ||
|
||
|
||
toFirewall :: Aws.Ec2.SecurityGroup -> Types.Firewall | ||
toFirewall sgs = | ||
let fid = sgs L.^. Aws.Ec2.Lens.securityGroup_groupId | ||
name = sgs L.^. Aws.Ec2.Lens.securityGroup_groupName | ||
inbound = fromMaybe [] $ sgs L.^. Aws.Ec2.Lens.securityGroup_ipPermissions | ||
outbound = fromMaybe [] $ sgs L.^. Aws.Ec2.Lens.securityGroup_ipPermissionsEgress | ||
in Types.Firewall | ||
{ _firewallId = fid | ||
, _firewallName = Just name | ||
, _firewallRulesInbound = fmap toFirewallRule inbound | ||
, _firewallRulesOutbound = fmap toFirewallRule outbound | ||
, _firewallCreatedAt = Nothing | ||
} | ||
|
||
|
||
toFirewallRule :: Aws.Ec2.IpPermission -> Types.FirewallRule | ||
toFirewallRule ip = | ||
let proto = ip L.^. Aws.Ec2.Lens.ipPermission_ipProtocol | ||
fromPort = fromIntegral . fromMaybe 0 $ ip L.^. Aws.Ec2.Lens.ipPermission_fromPort | ||
toPort = fromIntegral . fromMaybe 0 $ ip L.^. Aws.Ec2.Lens.ipPermission_toPort | ||
ips = fromMaybe [] $ ip L.^. Aws.Ec2.Lens.ipPermission_ipRanges | ||
in Types.FirewallRule | ||
{ _firewallRuleProtocol = proto | ||
, _firewallRulePorts = [Types.FirewallRulePorts {_firewallRulePortsFrom = fromPort, _firewallRulePortsTo = toPort}] | ||
, _firewallRuleEntities = fmap (L.^. Aws.Ec2.Lens.ipRange_cidrIp) ips | ||
} | ||
|
||
|
||
ec2InstanceToServerState :: Aws.Ec2.Types.InstanceState -> Types.State | ||
ec2InstanceToServerState Aws.Ec2.Types.InstanceState' {..} = | ||
case name of | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.