Last active
February 22, 2017 15:59
-
-
Save electrofelix/e8922e013a5cd5e9f3fbfb11de52bb58 to your computer and use it in GitHub Desktop.
puppet hash manipulate
This file contains hidden or 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
| # desired output | |
| # gerrit_groups => { | |
| # "Administrators" => { | |
| # "members" => ["admin", "creator"] | |
| # }, | |
| # "Project Creators" => { | |
| # "members" => ["creator"] | |
| # } | |
| # } |
This file contains hidden or 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
| # input data format | |
| $gerrit_users = { | |
| "admin" => { | |
| "full_name" => "Administrator", | |
| "groups" => ["Administrators"], | |
| }, | |
| "creator" => { | |
| "full_name" => "Project Creator", | |
| "groups" => [ "Administrators", "Project Creators" ], | |
| }, | |
| } | |
| # will result in "Illegal attempt to assign via [index/key]" error | |
| $gerrit_groups = {} | |
| $gerrit_users.filter |$user| { has_key($user[1], "groups") }.map |$username, $userdata| { | |
| $userdata["groups"].map |$group| { | |
| if $gerrit_groups.fetch($group,{}).fetch("members", nil) { | |
| $gerrit_groups[$group]["members"] << $username | |
| } else { | |
| $gerrit_groups[$group] = { "members" => [ $username ] } | |
| } | |
| } | |
| } |
This file contains hidden or 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
| gerrit_users = { | |
| "admin" => { | |
| "full_name" => "Administrator", | |
| "groups" => ["Administrators"], | |
| }, | |
| "creator" => { | |
| "full_name" => "Project Creator", | |
| "groups" => [ "Administrators", "Project Creators" ], | |
| }, | |
| } | |
| gerrit_groups = Hash.new | |
| gerrit_users.select { |username, userdata| | |
| userdata.has_key?("groups") | |
| }.map { |username, userdata| | |
| userdata["groups"].map { |group| | |
| if gerrit_groups.fetch(group, {}).fetch("members", nil) | |
| gerrit_groups[group]["members"] << username | |
| else | |
| gerrit_groups[group] = { "members" => [ username ] } | |
| end | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment