<commit_before>{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards   #-}
{-# LANGUAGE ViewPatterns      #-}

-- Module      : Khan.CLI.Group
-- Copyright   : (c) 2013 <NAME> <<EMAIL>>
-- License     : This Source Code Form is subject to the terms of
--               the Mozilla Public License, v. 2.0.
--               A copy of the MPL can be found in the LICENSE file or
--               you can obtain it at http://mozilla.org/MPL/2.0/.
-- Maintainer  : <NAME> <<EMAIL>>
-- Stability   : experimental
-- Portability : non-portable (GHC extensions)

module Khan.CLI.Group (commands) where

import qualified Data.Text                as Text
import           Khan.Internal
import           Khan.Internal.Ansible
import qualified Khan.Model.SecurityGroup as Security
import           Khan.Prelude
import           Network.AWS.EC2

data Group = Group
    { gRole    :: !Role
    , gEnv     :: !Env
    , gAnsible :: !Bool
    }

groupParser :: Parser Group
groupParser = Group
    <$> roleOption
    <*> envOption
    <*> ansibleOption

instance Options Group

instance Naming Group where
    names Group{..} = unversioned gRole gEnv

data Authorise = Authorise
    { aRole    :: !Role
    , aEnv     :: !Env
    , aRules   :: [IpPermissionType]
    , aAnsible :: !Bool
    }

authoriseParser :: Parser Authorise
authoriseParser = Authorise
    <$> roleOption
    <*> envOption
    <*> many (customOption "rule" "RULE" parseRule mempty
        "tcp|udp|icmp:from_port:to_port:[group|0.0.0.0,...]")
    <*> ansibleOption

instance Options Authorise

instance Naming Authorise where
    names Authorise{..} = unversioned aRole aEnv

commands :: Mod CommandFields Command
commands = group "group" "Long description." $ mconcat
    [ command "info" info groupParser
        "Long long long long description."
    , command "create" (modify Security.create) groupParser
        "Long long long long description."
    , command "delete" (modify Security.delete) groupParser
        "Long long long long description."
    , command "authorise" authorise authoriseParser
        "Long long long long description."
    ]

info :: Common -> Group -> AWS ()
info _ (names -> Names{..}) =
    Security.find groupName >>= maybe (return ()) (log_ . format)
  where
    format SecurityGroupItemType{..} = Text.init . Text.unlines $
        [ "OwnerId             = "  <> sgitOwnerId
        , "GroupId             = "  <> sgitGroupId
        , "GroupName           = "  <> sgitGroupName
        , "GroupDescription    = "  <> sgitGroupDescription
        , "VpcId               = "  <> fromMaybe "''" sgitVpcId
        , "IpPermissionsEgress = [" <> showRules sgitIpPermissionsEgress <> "]"
        , "IpPermissions       = [" <> showRules sgitIpPermissions <> "]"
        ]

modify :: (Text -> AWS Bool) -> Common -> Group -> AWS ()
modify f c g@Group{..}
    | not gAnsible = void $ f groupName
    | otherwise    = capture c "security group {}" [groupName] $ f groupName
  where
    Names{..} = names g

authorise :: Common -> Authorise -> AWS ()
authorise c a@Authorise{..}
    | not aAnsible = void $ Security.update groupName aRules
    | otherwise    = capture c "security group {}" [groupName] $
        Security.update groupName aRules
  where
    Names{..} = names a<commit_msg>Updating help text for the group command<commit_after>{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards   #-}
{-# LANGUAGE ViewPatterns      #-}

-- Module      : Khan.CLI.Group
-- Copyright   : (c) 2013 <NAME> <<EMAIL>>
-- License     : This Source Code Form is subject to the terms of
--               the Mozilla Public License, v. 2.0.
--               A copy of the MPL can be found in the LICENSE file or
--               you can obtain it at http://mozilla.org/MPL/2.0/.
-- Maintainer  : <NAME> <<EMAIL>>
-- Stability   : experimental
-- Portability : non-portable (GHC extensions)

module Khan.CLI.Group (commands) where

import qualified Data.Text                as Text
import           Khan.Internal
import           Khan.Internal.Ansible
import qualified Khan.Model.SecurityGroup as Security
import           Khan.Prelude
import           Network.AWS.EC2

data Group = Group
    { gRole    :: !Role
    , gEnv     :: !Env
    , gAnsible :: !Bool
    }

groupParser :: Parser Group
groupParser = Group
    <$> roleOption
    <*> envOption
    <*> ansibleOption

instance Options Group

instance Naming Group where
    names Group{..} = unversioned gRole gEnv

data Update = Update
    { uRole    :: !Role
    , uEnv     :: !Env
    , uRules   :: [IpPermissionType]
    , uAnsible :: !Bool
    }

updateParser :: Parser Update
updateParser = Update
    <$> roleOption
    <*> envOption
    <*> many (customOption "rule" "RULE" parseRule mempty
        "tcp|udp|icmp:from_port:to_port:[group|0.0.0.0,...]")
    <*> ansibleOption

instance Options Update

instance Naming Update where
    names Update{..} = unversioned uRole uEnv

commands :: Mod CommandFields Command
commands = group "group" "Security Groups." $ mconcat
    [ command "info" info groupParser
        "Display information about a security group."
    , command "create" (modify Security.create) groupParser
        "Create a security group."
    , command "delete" (modify Security.delete) groupParser
        "Delete a security group."
    , command "update" update updateParser
        "Update a security group with a new rule set."
    ]

info :: Common -> Group -> AWS ()
info _ (names -> Names{..}) =
    Security.find groupName >>= maybe (return ()) (log_ . format)
  where
    format SecurityGroupItemType{..} = Text.init . Text.unlines $
        [ "OwnerId             = "  <> sgitOwnerId
        , "GroupId             = "  <> sgitGroupId
        , "GroupName           = "  <> sgitGroupName
        , "GroupDescription    = "  <> sgitGroupDescription
        , "VpcId               = "  <> fromMaybe "''" sgitVpcId
        , "IpPermissionsEgress = [" <> showRules sgitIpPermissionsEgress <> "]"
        , "IpPermissions       = [" <> showRules sgitIpPermissions <> "]"
        ]

modify :: (Text -> AWS Bool) -> Common -> Group -> AWS ()
modify f c g@Group{..}
    | not gAnsible = void $ f groupName
    | otherwise    = capture c "security group {}" [groupName] $ f groupName
  where
    Names{..} = names g

update :: Common -> Update -> AWS ()
update c u@Update{..}
    | not uAnsible = void $ Security.update groupName uRules
    | otherwise    = capture c "security group {}" [groupName] $
        Security.update groupName uRules
  where
    Names{..} = names u
{'fraction_non_alphanumeric': 0.11867733655443152, 'fraction_numerical': 0.003925713422920127, 'mean_word_length': 2.379591836734694, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '474326', 'n_tokens_mistral': 2018, 'n_tokens_neox': 1974, 'n_words': 582}