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 ExpandCslPort stage for tree preprocessor
- Loading branch information
Julian Kleinhans
committed
Dec 13, 2016
1 parent
1a98961
commit 5b9b0d4
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/StackFormation/PreProcessor/Stage/Tree/ExpandCslPort.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,43 @@ | ||
<?php | ||
|
||
namespace StackFormation\PreProcessor\Stage\Tree; | ||
|
||
use StackFormation\PreProcessor\Stage\AbstractTreePreProcessorStage; | ||
use StackFormation\PreProcessor\Rootline; | ||
|
||
class ExpandCslPort extends AbstractTreePreProcessorStage | ||
{ | ||
/** | ||
* This one converts "Port" into "FromPort" and "ToPort" and will resolve comma-separated lists | ||
* | ||
* @param string $path | ||
* @param string $value | ||
* @param Rootline $rootLineReferences | ||
* @return bool | ||
*/ | ||
function invoke($path, $value, Rootline $rootLineReferences) { | ||
if (!preg_match('+/SecurityGroupIngress/.*/Port$+', $path)) { | ||
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 $port) { | ||
$newItem = clone $parent; | ||
$newItem->FromPort = $port; | ||
$newItem->ToPort = $port; | ||
$newItem->offsetUnset('Port'); | ||
$grandParent->append($newItem); | ||
} | ||
|
||
return true; // indicate that something has changed | ||
} | ||
} |