This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#45 Add ExpandCidrIp stage for tree preprocessor
- Loading branch information
Julian Kleinhans
committed
Dec 13, 2016
1 parent
1bb42dc
commit 1a98961
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/StackFormation/PreProcessor/Stage/Tree/ExpandCidrIp.php
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace StackFormation\PreProcessor\Stage\Tree; | ||
|
||
use StackFormation\PreProcessor\Stage\AbstractTreePreProcessorStage; | ||
use StackFormation\PreProcessor\Rootline; | ||
|
||
class ExpandCidrIp extends AbstractTreePreProcessorStage | ||
{ | ||
|
||
/** | ||
* This one will resolve comma-separated list of IP addresses | ||
* | ||
* @param string $path | ||
* @param string $value | ||
* @param Rootline $rootLineReferences | ||
* @return bool | ||
*/ | ||
function invoke($path, $value, Rootline $rootLineReferences) { | ||
if (!preg_match('+/SecurityGroupIngress/.*/CidrIp+', $path) || strpos($value, ',') === false) { | ||
return false; // indicate that nothing has been touched | ||
} | ||
|
||
$parentRootlineItem = $rootLineReferences->parent(1); /* @var $parentRootlineItem RootlineItem */ | ||
$parent = $parentRootlineItem->getValue(); /* @var $parent RecursiveArrayObject */ | ||
|
||
$grandParentRootlineItem = $rootLineReferences->parent(2); /* @var $grandParentRootlineItem RootlineItem */ | ||
$grandParent = $grandParentRootlineItem->getValue(); /* @var $grandParent RecursiveArrayObject */ | ||
|
||
// remove original item | ||
$grandParent->offsetUnset($parentRootlineItem->getKey()); | ||
|
||
// add a new line for every csl item | ||
foreach (explode(',', $value) as $cidrIp) { | ||
$newItem = clone $parent; | ||
$newItem->CidrIp = $cidrIp; | ||
$grandParent->append($newItem); | ||
} | ||
|
||
return true; // indicate that something has changed | ||
} | ||
} |