MogileFS-Client
view release on metacpan or search on metacpan
lib/MogileFS/Admin.pm view on Meta::CPAN
# get a hashref of the domains we know about in the format of
# { domain_name => { class_name => mindevcount, class_name => mindevcount, ... }, ... }
sub get_domains {
my MogileFS::Admin $self = shift;
my $res = $self->{backend}->do_request("get_domains", {})
or return undef;
my $ret = {};
foreach my $i (1..$res->{domains}) {
$ret->{$res->{"domain$i"}} = {
map {
$res->{"domain${i}class${_}name"} =>
{ mindevcount => $res->{"domain${i}class${_}mindevcount"},
replpolicy => $res->{"domain${i}class${_}replpolicy"} || '',
hashtype => $res->{"domain${i}class${_}hashtype"} || '',
}
} (1..$res->{"domain${i}classes"})
};
}
return $ret;
}
# create a new domain
sub create_domain {
my MogileFS::Admin $self = shift;
return undef if $self->{readonly};
my $domain = shift;
my $res = $self->{backend}->do_request("create_domain", { domain => $domain });
return undef unless $res->{domain} eq $domain;
return 1;
}
# delete a domain
sub delete_domain {
my MogileFS::Admin $self = shift;
return undef if $self->{readonly};
my $domain = shift;
$self->{backend}->do_request("delete_domain", { domain => $domain })
or return undef;
return 1;
}
# create a class within a domain
sub create_class {
my MogileFS::Admin $self = shift;
# wrapper around _mod_class(create)
return $self->_mod_class(@_, 'create');
}
# update a class's mindevcount within a domain
sub update_class {
my MogileFS::Admin $self = shift;
# wrapper around _mod_class(update)
return $self->_mod_class(@_, 'update');
}
# delete a class
sub delete_class {
my MogileFS::Admin $self = shift;
return undef if $self->{readonly};
my ($domain, $class) = @_;
$self->{backend}->do_request("delete_class", {
domain => $domain,
class => $class,
}) or return undef;
return 1;
}
# create a host
sub create_host {
my MogileFS::Admin $self = shift;
my $host = shift;
return undef unless $host;
my $args = shift;
return undef unless ref $args eq 'HASH';
return undef unless $args->{ip} && $args->{port};
return $self->_mod_host($host, $args, 'create');
}
# edit a host
sub update_host {
my MogileFS::Admin $self = shift;
my $host = shift;
return undef unless $host;
my $args = shift;
return undef unless ref $args eq 'HASH';
return $self->_mod_host($host, $args, 'update');
}
# delete a host
sub delete_host {
my MogileFS::Admin $self = shift;
return undef if $self->{readonly};
my $host = shift;
return undef unless $host;
$self->{backend}->do_request("delete_host", { host => $host })
or return undef;
return 1;
}
# create a new device
sub create_device {
my MogileFS::Admin $self = shift;
return undef if $self->{readonly};
my (%opts) = @_; #hostname or hostid, devid, state (optional)
my $res = $self->{backend}->do_request("create_device", \%opts)
or return undef;
return 1;
}
# edit a device
sub update_device {
my MogileFS::Admin $self = shift;
return undef if $self->{readonly};
my $host = shift;
my $device = shift;
return undef unless $host;
return undef unless $device;
my $args = shift;
return undef unless ref $args eq 'HASH';
# TODO: provide a native update_device in the MogileFS::Admin command set.
if ($args->{status}){
$self->change_device_state($host, $device, $args->{status}) or return undef;
}
if ($args->{weight}){
$self->change_device_weight($host, $device, $args->{weight}) or return undef;
}
return 1;
}
# change the state of a device; pass in the hostname of the host the
# device is located on, the device id number, and the state you want
# the host to be set to. returns 1 on success, undef on error.
sub change_device_state {
my MogileFS::Admin $self = shift;
return undef if $self->{readonly};
my ($host, $device, $state) = @_;
my $res = $self->{backend}->do_request("set_state", {
host => $host,
device => $device,
state => $state,
}) or return undef;
return 1;
}
# change the weight of a device by passing in the hostname and
# the device id
sub change_device_weight {
my MogileFS::Admin $self = shift;
return undef if $self->{readonly};
my ($host, $device, $weight) = @_;
$weight += 0;
my $res = $self->{backend}->do_request("set_weight", {
host => $host,
device => $device,
weight => $weight,
}) or return undef;
return 1;
}
# returns a hash (list) of key => weight
sub _get_slave_keys {
my MogileFS::Admin $self = shift;
( run in 1.509 second using v1.01-cache-2.11-cpan-39bf76dae61 )