Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions src/MGRAST/lib/JobDB/Job.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1252,23 +1252,19 @@ sub get_timestamp {
#################

sub user_delete {
my ($self, $user) = @_;

my ($self, $user, $reason) = @_;
my $jobdbm = $self->_master();
my $mgid = $self->metagenome_id;

if ($self->public) {
return(0, "Unable to delete metagenome '$mgid' as it has been made public. If someone is sharing this data with you please contact them with inquiries. However, if you believe you have reached this message in error please contact the <a href='mailto:mg-rast\@mcs.anl.gov'>MG-RAST mailing list</a>.");
}

unless( $user and $user->has_right(undef, 'delete', 'metagenome', $mgid) ) {
unless( $user && ($user->has_right(undef, 'delete', 'metagenome', $mgid) || $user->has_star_right('delete','metagenome')) ) {
return (0, "Unable to delete metagenome '$mgid'. If someone is sharing this data with you please contact them with inquiries. However, if you believe you have reached this message in error please contact the <a href='mailto:mg-rast\@mcs.anl.gov'>MG-RAST mailing list</a>.");
}

unless ($self->viewable) {
return (0, "Unable to delete metagenome '$mgid' because it is still processing.");
}

# remove from project
if ($self->primary_project) {
$self->primary_project->remove_job($self);
Expand All @@ -1278,24 +1274,25 @@ sub user_delete {
$self->viewable('null');

# set status as deleted
$jobdbm->JobAttributes->create({ job => $self, tag => 'deleted', value => 'deleted by ' . $user->login });
my $message = $reason || 'deleted by '.$user->login;
$self->data('deleted', $message);

# delete rights
my $webappdb = DBMaster->new(-database => $Conf::webapplication_db,
-backend => $Conf::webapplication_backend,
-host => $Conf::webapplication_host,
-user => $Conf::webapplication_user);
my $job_rights = $webappdb->Rights->get_objects( { data_type => 'metagenome', data_id => $mgid } );
my $job_rights = $webappdb->Rights->get_objects( { data_type => 'metagenome', data_id => $mgid } );
foreach my $r (@$job_rights) {
$r->delete;
}

# delete analysis tables
use MGRAST::Analysis;
my $analysisDB = MGRAST::Analysis->new($self->_master->db_handle);
my $analysisDB = new MGRAST::Analysis( $jobdbm->db_handle );
$analysisDB->delete_job($self->job_id);

######## delete shock nodes ##########
######## delete AWE / Shock ##########

# get mgrast auth
my $mgrast_token = undef;
Expand All @@ -1304,24 +1301,41 @@ sub user_delete {
my $rep = Auth::globus_token($key);
$mgrast_token = $rep ? $rep->{access_token} : undef;
}
my @auth = ('Authorization', "OAuth ".$mgrast_token);

# get shock nodes
# get handles
my $agent = LWP::UserAgent->new;
my $json = JSON->new;
$json = $json->utf8();
$json->max_size(0);
$json->allow_nonref;

my @auth = ('Authorization', "OAuth ".$mgrast_token);
# get AWE job
my $ajobs = [];
eval {
my $get = $agent->get($Conf::awe_url.'/job?query&limit=0&info.name='.$self->job_id, @auth);
$ajobs = $json->decode( $get->content )->{data};
};

# delete AWE job
if ($@) {
return (0, "Unable to delete metagenome '$mgid' from AWE: ".$@);
} else {
foreach my $j (@$ajobs) {
$agent->delete($Conf::awe_url.'/job/'.$j->{id}, @auth);
}
}

# get shock nodes
my $nodes = [];
eval {
my $get = $agent->get($Conf::shock_url.'/node?query&limit=0&type=metagenome&id=mgm'.$self->metagenome_id, @auth);
my $get = $agent->get($Conf::shock_url.'/node?query&limit=0&type=metagenome&id=mgm'.$mgid, @auth);
$nodes = $json->decode( $get->content )->{data};
};

# delete nodes
# delete shock nodes
if ($@) {
print STDERR "unable to retrieve shock nodes for metagenome: ".$self->metagenome_id."\n";
return (0, "Unable to delete metagenome '$mgid' from Shock: ".$@);
} else {
# modify shock nodes
foreach my $n (@$nodes) {
Expand Down
121 changes: 111 additions & 10 deletions src/MGRAST/lib/resources/job.pm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ sub new {

# Add name / attributes
$self->{name} = "job";
$self->{job_actions} = {
reserve => 1,
create => 1,
submit => 1,
share => 1,
public => 1,
delete => 1,
addproject => 1
};
$self->{attributes} = {
reserve => { "timestamp" => [ 'date', 'time the metagenome was first reserved' ],
"metagenome_id" => [ "string", "unique MG-RAST metagenome identifier" ],
Expand All @@ -25,6 +34,10 @@ sub new {
create => { "timestamp" => [ 'date', 'time the metagenome was first reserved' ],
"options" => [ "string", "job pipeline option string" ],
"job_id" => [ "int", "unique MG-RAST job identifier" ] },
submit => { "awe_id" => [ "string", "ID of AWE job" ],
"log" => [ "string", "log of sumbission" ] },
delete => { "deleted" => [ 'boolean', 'the metagenome is deleted' ],
"error" => [ "string", "error message if unable to delete" ] },
addproject => { "project_id" => [ "string", "unique MG-RAST project identifier" ],
"project_name" => [ "string", "MG-RAST project name" ],
"status" => [ 'string', 'status of action' ] },
Expand All @@ -35,7 +48,7 @@ sub new {
};
$self->{create_param} = {
'metagenome_id' => ["string", "unique MG-RAST metagenome identifier"],
"input_id" => ["string", "shock node id of input sequence file (optional)"],
'input_id' => ["string", "shock node id of input sequence file (optional)"],
'sequence_type' => ["cv", [["WGS", "whole genome shotgun sequenceing"],
["Amplicon", "amplicon sequenceing"],
["MT", "metatranscriptome sequenceing"]] ]
Expand Down Expand Up @@ -97,12 +110,25 @@ sub info {
'description' => "Submit an existing MG-RAST job to AWE pipeline.",
'method' => "POST",
'type' => "synchronous",
'attributes' => { "awe_id" => ["ID of AWE job"] },
'attributes' => $self->{attributes}{submit},
'parameters' => { 'options' => {},
'required' => {},
'body' => { "metagenome_id" => [ "string", "unique MG-RAST metagenome identifier" ],
'body' => { "metagenome_id" => ["string", "unique MG-RAST metagenome identifier"],
"input_id" => ["string", "shock node id of input sequence file"] } }
},
{ 'name' => "share",
'request' => $self->cgi->url."/".$self->name."/share",
'description' => "Share metagenome with another user.",
'method' => "POST",
'type' => "synchronous",
'attributes' => { "shared" => ['list', ['string', 'user metagenome shared with']] },
'parameters' => { 'options' => {},
'required' => {},
'body' => { "metagenome_id" => ["string", "unique MG-RAST metagenome identifier"],
"user_id" => ["string", "unique user identifier to share with"],
"user_email" => ["string", "user email to share with"].
"edit" => ["boolean", "if true edit rights shared, else (default) view rights only"] } }
},
{ 'name' => "public",
'request' => $self->cgi->url."/".$self->name."/public",
'description' => "Change status of metagenome to public.",
Expand All @@ -111,7 +137,18 @@ sub info {
'attributes' => { "public" => ['boolean', 'the metagenome is public'] },
'parameters' => { 'options' => {},
'required' => {},
'body' => { "metagenome_id" => [ "string", "unique MG-RAST metagenome identifier" ] } }
'body' => { "metagenome_id" => ["string", "unique MG-RAST metagenome identifier"] } }
},
{ 'name' => "delete",
'request' => $self->cgi->url."/".$self->name."/delete",
'description' => "Delete metagenome.",
'method' => "POST",
'type' => "synchronous",
'attributes' => $self->{attributes}{delete},
'parameters' => { 'options' => {},
'required' => {},
'body' => { "metagenome_id" => ["string", "unique MG-RAST metagenome identifier"],
"reason" => ["string", "reason for deleting metagenome"] } }
},
{ 'name' => "addproject",
'request' => $self->cgi->url."/".$self->name."/addproject",
Expand All @@ -121,8 +158,8 @@ sub info {
'attributes' => $self->{attributes}{addproject},
'parameters' => { 'options' => {},
'required' => {},
'body' => { "metagenome_id" => [ "string", "unique MG-RAST metagenome identifier" ],
"project_id" => [ "string", "unique MG-RAST project identifier" ] } }
'body' => { "metagenome_id" => ["string", "unique MG-RAST metagenome identifier"],
"project_id" => ["string", "unique MG-RAST project identifier"] } }
},
{ 'name' => "kb2mg",
'request' => $self->cgi->url."/".$self->name."/kb2mg",
Expand Down Expand Up @@ -157,8 +194,7 @@ sub request {
# determine sub-module to use
if (scalar(@{$self->rest}) == 0) {
$self->info();
} elsif ( ($self->rest->[0] eq 'reserve') || ($self->rest->[0] eq 'create') || ($self->rest->[0] eq 'submit') ||
($self->rest->[0] eq 'public') || ($self->rest->[0] eq 'addproject') ) {
} elsif (exists $self->{job_actions}{ $self->rest->[0] }) {
$self->job_action($self->rest->[0]);
} elsif (($self->rest->[0] eq 'kb2mg') || ($self->rest->[0] eq 'mg2kb')) {
$self->id_lookup($self->rest->[0]);
Expand All @@ -178,6 +214,7 @@ sub job_action {
my $data = {};
my $post = $self->get_post_data();

# job does not exist yet
if ($action eq 'reserve') {
# get from shock node if given
if (exists $post->{input_id}) {
Expand Down Expand Up @@ -210,7 +247,9 @@ sub job_action {
job_id => $job->{job_id},
kbase_id => (exists($post->{kbase_id}) && $post->{kbase_id}) ? $self->reserve_kbase_id($mgid): undef
};
} elsif (($action eq 'create') || ($action eq 'submit') || ($action eq 'public') || ($action eq 'addproject')) {
}
# we have a job in DB, do something
else {
# check id format
my (undef, $id) = $post->{metagenome_id} =~ /^(mgm)?(\d+\.\d+)$/;
if (! $id) {
Expand Down Expand Up @@ -261,7 +300,61 @@ sub job_action {
$self->return_data( {"ERROR" => join("\n", @log)}, 400 );
}
my (undef, $awe_id) = split(/\t/, $log[1]);
$data = { awe_id => $awe_id, log => join("\n", @log) };
$data = {
awe_id => $awe_id,
log => join("\n", @log)
};
} elsif ($action eq 'share') {
# get user to share with
my $share_user = undef;
if ($post->{user_id}) {
my (undef, $uid) = $post->{user_id} =~ /^(mgu)?(\d+)$/;
$share_user = $master->User->init({ _id => $uid });
} elsif ($post->{user_email}) {
$share_user = $master->User->init({ email => $post->{user_email} });
} else {
$self->return_data( {"ERROR" => "Missing required parameter user_id or user_email"}, 404 );
}
unless ($share_user && ref($share_user)) {
$self->return_data( {"ERROR" => "Unable to find user to share with"}, 404 );
}
# share rights if not owner
unless ($share_user->_id eq $job->owner->_id) {
my @rights = ('view');
if ($post->{edit}) {
push @rights, 'edit';
}
foreach my $name (@rights) {
my $right_query = {
name => $name,
data_type => 'metagenome',
data_id => $job->metagenome_id,
scope => $share_user->get_user_scope
};
unless(scalar( @{$master->Rights->get_objects($right_query)} )) {
$right_query->{granted} = 1;
$right_query->{delegated} = 1;
my $right = $master->Rights->create($right_query);
unless (ref $right) {
$self->return_data( {"ERROR" => "Failed to create ".$name." right in the user database, aborting."}, 500 );
}
}
}
}
# get all who can view / skip owner
my $view_query = {
name => 'view',
data_type => 'metagenome',
data_id => $job->metagenome_id
};
my $shared = [];
my $owner_user = $master->User->init({ _id => $job->owner->_id });
my $view_rights = $master->Rights->get_objects($view_query);
foreach my $vr (@$view_rights) {
next if (($owner_user->get_user_scope->_id eq $vr->scope->_id) || ($vr->scope->name =~ /^token\:/));
push @$shared, $vr->scope->name_readable;
}
$data = { shared => $shared };
} elsif ($action eq 'public') {
# update shock nodes
my $nodes = $self->get_shock_query({'type' => 'metagenome', 'id' => 'mgm'.$job->{metagenome_id}}, $self->mgrast_token);
Expand All @@ -274,6 +367,14 @@ sub job_action {
# update db
$job->public(1);
$data = { public => $job->public ? 1 : 0 };
} elsif ($action eq 'delete') {
# Auf Wiedersehen!
my $reason = $post->{reason} || "";
my ($status, $message) = $job->user_delete($self->user, $reason);
$data = {
deleted => $status,
error => $message
};
} elsif ($action eq 'addproject') {
# check id format
my (undef, $pid) = $post->{project_id} =~ /^(mgp)?(\d+)$/;
Expand Down
Loading