1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
#!/usr/bin/perl
#
# M00se on the L00se
package Chef::Rest;
use strict;
use warnings;
use LWP::UserAgent;
use URI;
use Params::Validate qw(:all);
use JSON::Syck;
sub new {
my $self = shift;
my %p = validate(@_,
{
content_type => { type => SCALAR },
},
);
my $ref = {
'ua' => LWP::UserAgent->new,
'content_type' => $p{'content_type'},
};
bless $ref, $self;
}
sub load {
my $self = shift;
my $data = shift;
return JSON::Syck::Load($data);
}
sub get {
my $self = shift;
my %p = validate(@_,
{
url => { type => SCALAR },
params => { type => ARRAYREF, optional => 1 },
},
);
my $url = URI->new($p{'url'});
if (defined($p{'params'})) {
$url->query_form($p{'params'});
}
my $req = HTTP::Request->new('GET' => $url);
$req->content_type($self->{'content_type'});
return $self->ua->request($req);
}
sub delete {
my $self = shift;
my %p = validate(@_,
{
url => { type => SCALAR },
},
);
my $req = HTTP::Request->new('DELETE' => $p{'url'});
$req->content_type($self->{'content_type'});
return $self->ua->request($req);
}
sub put {
my $self = shift;
my %p = validate(@_,
{
url => { type => SCALAR },
data => 1,
},
);
my $data = JSON::Syck::Dump($p{'data'});
my $req = HTTP::Request->new('PUT' => $p{'url'});
$req->content_type($self->{'content_type'});
$req->content_length(do { use bytes; length($data) });
$req->content($data);
return $self->ua->request($req);
}
sub post {
my $self = shift;
my %p = validate(@_,
{
url => { type => SCALAR },
data => { required => 1 },
},
);
my $data = JSON::Syck::Dump($p{'data'});
my $req = HTTP::Request->new('POST' => $p{'url'});
$req->content_type($self->{'content_type'});
$req->content_length(do { use bytes; length($data) });
$req->content($data);
return $self->{ua}->request($req);
}
my $rest = Chef::Rest->new(content_type => 'application/json');
while (my @passwd = getpwent) {
print "Ensuring we have $passwd[0]\n";
$rest->post(
url => 'http://localhost:4000/search/user/entries',
data => {
id => $passwd[0],
name => $passwd[0],
uid => $passwd[2],
gid => $passwd[3],
gecos => $passwd[6],
dir => $passwd[7],
shell => $passwd[8],
change => '',
expire => $passwd[9],
}
)
}
|