Skip to content

Instantly share code, notes, and snippets.

@angrycub
Last active October 17, 2017 10:22
Show Gist options
  • Save angrycub/df85121a41ac333aa6bf to your computer and use it in GitHub Desktop.
Save angrycub/df85121a41ac333aa6bf to your computer and use it in GitHub Desktop.
Snippet to forcibly remove a CS Bucket from a Riak CS User
Force_remove_s3_bucket = fun(UserID, Bucket) ->
Update_user_buckets = fun(User, Bucket) ->
Buckets = User#rcs_user_v2.buckets,
%% At this point any siblings from the read of the
%% user record have been resolved so the user bucket
%% list should have 0 or 1 buckets that share a name
%% with `Bucket'.
case [B || B <- Buckets, B#moss_bucket_v1.name =:= Bucket#moss_bucket_v1.name] of
[] ->
{ok, User#rcs_user_v2{buckets=[Bucket | Buckets]}};
[ExistingBucket] ->
case
(Bucket#moss_bucket_v1.last_action == deleted andalso
ExistingBucket#moss_bucket_v1.last_action == created)
orelse
(Bucket#moss_bucket_v1.last_action == created andalso
ExistingBucket#moss_bucket_v1.last_action == deleted) of
true ->
UpdBuckets = [Bucket | lists:delete(ExistingBucket, Buckets)],
{ok, User#rcs_user_v2{buckets=UpdBuckets}};
false ->
{ok, ignore}
end
end
end,
{ok, RC} = riak_cs_riak_client:checkout(),
{ok, {User, UserObj}} = riak_cs_user:get_user(UserID, RC),
BucketRecord = #moss_bucket_v1{name=Bucket,
last_action=deleted,
creation_date=riak_cs_wm_utils:iso_8601_datetime(),
modification_time=os:timestamp()},
{ok, UpdUser} = Update_user_buckets(User, BucketRecord),
riak_cs_user:save_user(UpdUser, UserObj, RC),
riak_cs_riak_client:checkin(RC)
end.
%% To run :
% Force_remove_s3_bucket("S3UserKey","S3Bucket").
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment