[78] | 1 | #!/bin/env perl |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | use LATMOS::Accounts; |
---|
| 6 | use Getopt::Long; |
---|
| 7 | use Pod::Usage; |
---|
[339] | 8 | use LATMOS::Accounts::Utils; |
---|
[78] | 9 | |
---|
[339] | 10 | =head1 NAME |
---|
| 11 | |
---|
| 12 | la-create -- Tools to create object in LATMOS::Account system |
---|
| 13 | |
---|
| 14 | =head1 SYNOPSIS |
---|
| 15 | |
---|
| 16 | la-create [options] obj_id |
---|
| 17 | |
---|
| 18 | =cut |
---|
| 19 | |
---|
[78] | 20 | GetOptions( |
---|
| 21 | 'c|config=s' => \my $config, |
---|
| 22 | 'b|base=s' => \my $base, |
---|
| 23 | 'o|object=s' => \my $otype, |
---|
[339] | 24 | 'f=s' => \my $inputfile, |
---|
| 25 | 'ro' => \my $with_ro, |
---|
| 26 | 'u|update' => \my $allow_update, |
---|
[78] | 27 | 'help' => sub { pod2usage(0) }, |
---|
| 28 | ) or pod2usage(); |
---|
| 29 | |
---|
| 30 | $otype ||= 'user'; |
---|
| 31 | |
---|
[339] | 32 | =head1 OPTIONS |
---|
| 33 | |
---|
| 34 | =over 4 |
---|
| 35 | |
---|
| 36 | =item -c|--config configfile |
---|
| 37 | |
---|
[594] | 38 | Use this configuration file instead of the default one. |
---|
[339] | 39 | |
---|
| 40 | =item -b|--base basename |
---|
| 41 | |
---|
[594] | 42 | Query this specific base instead of the default one. |
---|
[339] | 43 | |
---|
| 44 | =item -o|object object_type |
---|
| 45 | |
---|
[594] | 46 | Query will be performed on this object. Default is the 'User' object. |
---|
[339] | 47 | |
---|
| 48 | =item --ro |
---|
| 49 | |
---|
| 50 | Include also read-only attributes as comment |
---|
| 51 | |
---|
| 52 | =item -u|--update |
---|
| 53 | |
---|
| 54 | If the object already exists, then updating it |
---|
| 55 | |
---|
| 56 | =item -f FILE |
---|
| 57 | |
---|
| 58 | Use this file to get attributes (- use stdin) |
---|
[595] | 59 | If this option is not present, you'll have a file opened in your prefered editor to edit. |
---|
[339] | 60 | |
---|
| 61 | =back |
---|
| 62 | |
---|
| 63 | =cut |
---|
| 64 | |
---|
[655] | 65 | if (!$ARGV[0]) {print "You must specify 'obj_id', aborting\n"; pod2usage(); } |
---|
| 66 | |
---|
[457] | 67 | my $LA = LATMOS::Accounts->new($config, noacl => 1); |
---|
[78] | 68 | my $labase = $base ? $LA->base($base) : $LA->default_base; |
---|
| 69 | $labase && $labase->load or die "Cannot load base"; |
---|
| 70 | |
---|
| 71 | $labase->is_supported_object($otype) or die "$otype object unsupported\n"; |
---|
[339] | 72 | my $objname = $ARGV[0]; |
---|
[78] | 73 | |
---|
[339] | 74 | sub input_from_handle { |
---|
| 75 | my ($fh) = @_; |
---|
| 76 | my %attr = LATMOS::Accounts::Utils::parse_obj_file($fh); |
---|
| 77 | if (my $obj = $labase->get_object($otype, $objname)) { |
---|
| 78 | if ($allow_update) { |
---|
| 79 | my $res = $obj->set_c_fields(%attr); |
---|
| 80 | if ($res) { |
---|
| 81 | print "Changes applied\n"; |
---|
| 82 | $labase->commit; |
---|
[602] | 83 | $LA->call_batch_sync; |
---|
[339] | 84 | } |
---|
| 85 | return $res; |
---|
| 86 | } else { |
---|
| 87 | die "Object $otype $objname already exists, aborting\n"; |
---|
| 88 | } |
---|
[78] | 89 | } else { |
---|
[339] | 90 | my $res = $labase->create_c_object($otype, $objname, %attr); |
---|
| 91 | if($res) { |
---|
| 92 | print "Changes applied\n"; |
---|
| 93 | $labase->commit; |
---|
[602] | 94 | $LA->call_batch_sync; |
---|
[78] | 95 | } |
---|
[339] | 96 | return $res; |
---|
| 97 | } |
---|
| 98 | } |
---|
[78] | 99 | |
---|
[339] | 100 | if ($inputfile) { |
---|
| 101 | my $handle; |
---|
| 102 | if ($inputfile eq '-') { |
---|
| 103 | $handle = *STDIN; |
---|
| 104 | } else { |
---|
| 105 | open($handle, '<', $inputfile) or die |
---|
| 106 | "Cannot open input file $@\n"; |
---|
| 107 | } |
---|
| 108 | my $res = input_from_handle($handle); |
---|
| 109 | close($handle); |
---|
| 110 | exit(!$res); |
---|
| 111 | } else { |
---|
| 112 | exit ! LATMOS::Accounts::Utils::dump_read_temp_file( |
---|
| 113 | sub { |
---|
| 114 | my ($fh) = @_; |
---|
| 115 | $labase->text_empty_dump($fh, $otype, |
---|
| 116 | { |
---|
| 117 | only_rw => !$with_ro, |
---|
[78] | 118 | } |
---|
[339] | 119 | ); |
---|
| 120 | }, |
---|
| 121 | sub { |
---|
| 122 | my ($fh) = @_; |
---|
| 123 | return input_from_handle($fh); |
---|
[78] | 124 | } |
---|
[339] | 125 | ); |
---|
[78] | 126 | } |
---|