Skip to content
This repository has been archived by the owner on Sep 12, 2022. It is now read-only.

Commit

Permalink
#45 Add ExpandCidrIp stage for tree preprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Kleinhans committed Dec 13, 2016
1 parent 1bb42dc commit 1a98961
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/StackFormation/PreProcessor/Stage/Tree/ExpandCidrIp.php
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
}
}

0 comments on commit 1a98961

Please sign in to comment.