FAQ
Hello list,

I´m using a json config file to read a file with server group
definitions, including group of groups like
this example:

[jdoe@belbo]# cat groups.json
{
"G_Group_PR" : [ "serverA", "serverB" ],
"G_Group_QS" : [ "serverC", "serverD" ],
"G_All" : [ "G_Group_PR", "G_Group_QS" ]
}

now I need to resolve all groups to their member servers to map
actions to them.

this is where I´m stuck with an elegant solution, how can I
effectively parse this hash to
replace all Groups (always starting with G_)?

thanks
,
Stefan

Search Discussions

  • Brandon McCaig at Nov 16, 2011 at 12:21 am
    Hello Stefan,
    On Tue, Nov 15, 2011 at 08:10:12AM -0800, Stefan Wiederoder wrote:
    Hello list,

    I´m using a json config file to read a file with server group
    definitions, including group of groups like
    this example:

    [jdoe@belbo]# cat groups.json
    {
    "G_Group_PR" : [ "serverA", "serverB" ],
    "G_Group_QS" : [ "serverC", "serverD" ],
    "G_All" : [ "G_Group_PR", "G_Group_QS" ]
    }

    now I need to resolve all groups to their member servers to map
    actions to them.

    this is where I´m stuck with an elegant solution, how can I
    effectively parse this hash to
    replace all Groups (always starting with G_)?
    I'm assuming that you are using an existing CPAN[1] module to
    actually parse the JSON data into a Perl data structure. If you
    aren't then you should be. You did say "parse" so I wonder if
    maybe that is the part that you are stuck on. If so, don't worry
    about it, and just use an existing CPAN module to parse the JSON.

    Assuming you had an equivalent Perl data structure (loaded by a
    CPAN module) you could just do a lookup of groups when fetching
    the server name to see if more servers are needed:

    #!/usr/bin/perl

    use strict;
    use warnings;

    my %groups = (
    G_Group_PR => [ qw/serverA serverB/ ],
    G_Group_QS => [ qw/serverC serverD/ ],
    G_All => [ qw/G_Group_PR G_Group_QS/ ],
    );

    sub get_group_servers
    {
    my ($groups, $group) = @_;

    my @servers = @{$groups{$group}};

    my %servers;

    for my $server (@servers)
    {
    if(exists $groups{$server})
    {
    map { $servers{$_} = 1 }
    get_group_servers($groups, $server);
    }
    else
    {
    $servers{$server} = 1;
    }
    }

    return keys %servers;
    }

    for my $group (keys %groups)
    {
    my @servers = get_group_servers(\%groups, $group);

    print "Servers for group '$group':\n";

    for my $server (@servers)
    {
    print " $server\n";
    }

    print "\n";
    }

    __END__

    Output:

    Servers for group 'G_Group_QS':
    serverD
    serverC

    Servers for group 'G_Group_PR':
    serverA
    serverB

    Servers for group 'G_All':
    serverD
    serverC
    serverA
    serverB

    Just how you handle it depends on your use. Maybe it would be
    better to preprocess the data and "expand" the group references
    instead of looking them up at run-time, for example.

    If I'm mistaken about what problem you're having then please
    explain in more detail. :)

    Regards,


    --
    Brandon McCaig <bamccaig@gmail.com> <bamccaig@castopulence.org>
    Castopulence Software <https://www.castopulence.org/>
    Blog <http://www.bamccaig.com/>
    perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
    q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
    tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'


    [1] Comprehensive Perl Archive Network AKA cpan.org
  • Jim Gibson at Nov 16, 2011 at 12:22 am
    On 11/15/11 Tue Nov 15, 2011 8:10 AM, "Stefan Wiederoder"
    <stefanwiederoder@googlemail.com> scribbled:
    Hello list,

    I´m using a json config file to read a file with server group
    definitions, including group of groups like
    this example:

    [jdoe@belbo]# cat groups.json
    {
    "G_Group_PR" : [ "serverA", "serverB" ],
    "G_Group_QS" : [ "serverC", "serverD" ],
    "G_All" : [ "G_Group_PR", "G_Group_QS" ]
    }

    now I need to resolve all groups to their member servers to map
    actions to them.

    this is where I´m stuck with an elegant solution, how can I
    effectively parse this hash to
    replace all Groups (always starting with G_)?
    I don't know json, but have you looked on CPAN?

    <http://search.cpan.org/search?query=json&mode=all> results in a lot of
    potentially helpful modules, such as JSON.pm and JSON::Parse.pm. The latter
    looks particularly spot on for your purpose.
  • John SJ Anderson at Nov 16, 2011 at 11:32 am

    On Tuesday, November 15, 2011 at 11:10 , Stefan Wiederoder wrote:
    Hello list,

    I´m using a json config file to read a file with server group
    definitions, including group of groups like
    this example:

    [jdoe@belbo]# cat groups.json
    {
    "G_Group_PR" : [ "serverA", "serverB" ],
    "G_Group_QS" : [ "serverC", "serverD" ],
    "G_All" : [ "G_Group_PR", "G_Group_QS" ]
    }

    now I need to resolve all groups to their member servers to map
    actions to them.

    this is where I´m stuck with an elegant solution, how can I
    effectively parse this hash to
    replace all Groups (always starting with G_)?

    I think you need to explain your desired outcome a bit further. I'm going to assume you've got the JSON -> Perl data structure bit down, and what you want to do is ask for 'G_All' and get back qw/ serverA serverB serverC serverD / -- is that correct?

    If so, the big question becomes, how many levels of reference are you going to support? If it's just a single level, like in your example, it's pretty easy:

    my %resolved_hash;

    foreach my $key ( keys %inital_hash ) {
    foreach my $val ( @{ $initial_hash{$key} } ) {
    if( $val =~ /^G_/ ) {
    push @{ $resolved_hash{$key} } , @{ $inital_hash{$key} };
    }
    else { push @{ $resolved_hash{$key} } , $val }
    }
    }

    (Caveat: untested, written while having first cup of coffee.)

    If you want to support more than one level, you're going to want to have something recursive -- dive down until there aren't any 'G_*' keys left, then unwind.

    chrs,
    john.
  • Stefan Wiederoder at Nov 17, 2011 at 8:13 am
    Hello Brandon,

    thanks a lot for your help - your code works like a charm, I´ve
    already put in my script
    which is now generating puppet node/classes definitions.

    bye
    ,
    Stefan
  • Rob Dixon at Nov 17, 2011 at 9:09 pm

    On 17/11/2011 07:56, Stefan Wiederoder wrote:
    Hello Brandon,

    thanks a lot for your help - your code works like a charm, I´ve
    already put in my script which is now generating puppet node/classes definitions.
    Sometimes, but very rarely, the answer to a question should include
    working code. It is even more rare that the best solution is to copy and
    paste an answer into working code.

    Ideally, posts on this list will ask for or provide /ideas/ about a
    solution to a problem. But whoever writes the final program is entirely
    responsible for it, including its faults and their consequences.

    We are not bricklayers: we are authors.

    Rob

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedNov 15, '11 at 4:11p
activeNov 17, '11 at 9:09p
posts6
users5
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase