Skip to content

Commit

Permalink
Support --owner in all bucket level commands (#2494)
Browse files Browse the repository at this point in the history
* Support --owner in all bucket level commands

* Add changelog
  • Loading branch information
romasku authored Dec 24, 2021
1 parent a0988c1 commit 9eeb199
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.D/2494.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added support `--owner` argument in blob bucket level commands to allow referring to another users bucket by name.
4 changes: 4 additions & 0 deletions CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,7 @@ Name | Description|
|----|------------|
|_--help_|Show this message and exit.|
|_--cluster CLUSTER_|Look on a specified cluster \(the current cluster by default).|
|_--owner TEXT_|Owner of bucket to assume for named bucket \(the current user by default)|



Expand Down Expand Up @@ -1281,6 +1282,7 @@ Name | Description|
|----|------------|
|_--help_|Show this message and exit.|
|_--cluster CLUSTER_|Perform on a specified cluster \(the current cluster by default).|
|_--owner TEXT_|Owner of bucket to assume for named bucket \(the current user by default)|



Expand Down Expand Up @@ -1330,6 +1332,7 @@ Name | Description|
|----|------------|
|_--help_|Show this message and exit.|
|_--cluster CLUSTER_|Perform on a specified cluster \(the current cluster by default).|
|_--owner TEXT_|Owner of bucket to assume for named bucket \(the current user by default)|



Expand Down Expand Up @@ -1371,6 +1374,7 @@ Name | Description|
|_--help_|Show this message and exit.|
|_--cluster CLUSTER_|Look on a specified cluster \(the current cluster by default).|
|_\--full-uri_|Output full bucket URI.|
|_--owner TEXT_|Owner of bucket to assume for named bucket \(the current user by default)|



Expand Down
4 changes: 4 additions & 0 deletions neuro-cli/docs/blob.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ Get storage usage for `BUCKET`.
| :--- | :--- |
| _--help_ | Show this message and exit. |
| _--cluster CLUSTER_ | Look on a specified cluster \(the current cluster by default\). |
| _--owner TEXT_ | Owner of bucket to assume for named bucket \(the current user by default\) |



Expand Down Expand Up @@ -329,6 +330,7 @@ Remove bucket `BUCKET`.
| :--- | :--- |
| _--help_ | Show this message and exit. |
| _--cluster CLUSTER_ | Perform on a specified cluster \(the current cluster by default\). |
| _--owner TEXT_ | Owner of bucket to assume for named bucket \(the current user by default\) |



Expand Down Expand Up @@ -381,6 +383,7 @@ $ neuro blob set-bucket-publicity my-bucket private
| :--- | :--- |
| _--help_ | Show this message and exit. |
| _--cluster CLUSTER_ | Perform on a specified cluster \(the current cluster by default\). |
| _--owner TEXT_ | Owner of bucket to assume for named bucket \(the current user by default\) |



Expand Down Expand Up @@ -426,6 +429,7 @@ Get bucket `BUCKET`.
| _--help_ | Show this message and exit. |
| _--cluster CLUSTER_ | Look on a specified cluster \(the current cluster by default\). |
| _--full-uri_ | Output full bucket URI. |
| _--owner TEXT_ | Owner of bucket to assume for named bucket \(the current user by default\) |



Expand Down
72 changes: 59 additions & 13 deletions neuro-cli/src/neuro_cli/blob_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,16 +332,26 @@ async def importbucket(
type=CLUSTER,
help="Look on a specified cluster (the current cluster by default).",
)
@option(
"--owner",
type=str,
help="Owner of bucket to assume for named bucket (the current user by default)",
)
@argument("bucket", type=BUCKET)
@option("--full-uri", is_flag=True, help="Output full bucket URI.")
async def statbucket(
root: Root, cluster: Optional[str], bucket: str, full_uri: bool
root: Root,
cluster: Optional[str],
owner: Optional[str],
bucket: str,
full_uri: bool,
) -> None:
"""
Get bucket BUCKET.
"""
bucket_id = await resolve_bucket(bucket, client=root.client, cluster_name=cluster)
bucket_obj = await root.client.buckets.get(bucket_id, cluster_name=cluster)
bucket_obj = await root.client.buckets.get(
bucket, cluster_name=cluster, bucket_owner=owner
)
if full_uri:
uri_fmtr: URIFormatter = str
else:
Expand All @@ -363,26 +373,39 @@ async def statbucket(
type=CLUSTER,
help="Look on a specified cluster (the current cluster by default).",
)
@option(
"--owner",
type=str,
help="Owner of bucket to assume for named bucket (the current user by default)",
)
@argument("bucket", type=BUCKET)
async def du(root: Root, cluster: Optional[str], bucket: str) -> None:
async def du(
root: Root, cluster: Optional[str], owner: Optional[str], bucket: str
) -> None:
"""
Get storage usage for BUCKET.
"""
bucket_obj = await root.client.buckets.get(bucket, cluster_name=cluster)
bucket_obj = await root.client.buckets.get(
bucket, cluster_name=cluster, bucket_owner=owner
)

base_str = f"Calculating bucket {bucket_obj.name or bucket_obj.id} disk usage"
bucket_str = bucket_obj.name or bucket_obj.id
if bucket_obj.owner != root.client.config.username:
bucket_str += f" (owner {bucket_obj.owner})"

base_str = f"Calculating bucket {bucket_str} disk usage"

with root.status(base_str) as status:
async with root.client.buckets.get_disk_usage(
bucket_obj.id, cluster
bucket_obj.id, cluster_name=cluster, bucket_owner=bucket_obj.owner
) as usage_it:
async for usage in usage_it:
status.update(
f"{base_str}: total size {format_size(usage.total_bytes)}, "
f"objects count {usage.object_count}"
)
root.print(
f"Bucket {bucket_obj.name or bucket_obj.id} disk usage:\n"
f"Bucket {bucket_str} disk usage:\n"
f"Total size: {format_size(usage.total_bytes)}\n"
f"Objects count: {usage.object_count}"
)
Expand All @@ -394,16 +417,28 @@ async def du(root: Root, cluster: Optional[str], bucket: str) -> None:
type=CLUSTER,
help="Perform on a specified cluster (the current cluster by default).",
)
@option(
"--owner",
type=str,
help="Owner of bucket to assume for named bucket (the current user by default)",
)
@argument("buckets", type=BUCKET, nargs=-1, required=True)
async def rmbucket(root: Root, cluster: Optional[str], buckets: Sequence[str]) -> None:
async def rmbucket(
root: Root, cluster: Optional[str], owner: Optional[str], buckets: Sequence[str]
) -> None:
"""
Remove bucket BUCKET.
"""
for bucket in buckets:
bucket_id = await resolve_bucket(
bucket, client=root.client, cluster_name=cluster
bucket,
client=root.client,
cluster_name=cluster,
bucket_owner=owner,
)
await root.client.buckets.rm(
bucket_id, cluster_name=cluster, bucket_owner=owner
)
await root.client.buckets.rm(bucket_id, cluster_name=cluster)
if root.verbosity >= 0:
root.print(f"Bucket with id '{bucket_id}' was successfully removed.")

Expand All @@ -414,14 +449,23 @@ async def rmbucket(root: Root, cluster: Optional[str], buckets: Sequence[str]) -
type=CLUSTER,
help="Perform on a specified cluster (the current cluster by default).",
)
@option(
"--owner",
type=str,
help="Owner of bucket to assume for named bucket (the current user by default)",
)
@argument("bucket", type=BUCKET, required=True)
@argument(
"public_level",
type=click.Choice(["public", "private"], case_sensitive=False),
required=True,
)
async def set_bucket_publicity(
root: Root, cluster: Optional[str], bucket: str, public_level: str
root: Root,
cluster: Optional[str],
owner: Optional[str],
bucket: str,
public_level: str,
) -> None:
"""
Change public access settings for BUCKET
Expand All @@ -432,7 +476,9 @@ async def set_bucket_publicity(
neuro blob set-bucket-publicity my-bucket private
"""
public = public_level == "public"
await root.client.buckets.set_public_access(bucket, public, cluster_name=cluster)
await root.client.buckets.set_public_access(
bucket, public, cluster_name=cluster, bucket_owner=owner
)
if root.verbosity >= 0:
root.print(
f"Bucket '{bucket}' was made {'public' if public else 'non-public'}."
Expand Down
12 changes: 10 additions & 2 deletions neuro-cli/src/neuro_cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,13 +495,21 @@ async def resolve_disk(


async def resolve_bucket(
id_or_name: str, *, client: Client, cluster_name: Optional[str] = None
id_or_name: str,
*,
client: Client,
cluster_name: Optional[str] = None,
bucket_owner: Optional[str] = None,
) -> str:
# Temporary fast path.
if re.fullmatch(BUCKET_ID_PATTERN, id_or_name):
return id_or_name

bucket = await client.buckets.get(id_or_name, cluster_name)
bucket = await client.buckets.get(
id_or_name,
cluster_name=cluster_name,
bucket_owner=bucket_owner,
)
return bucket.id


Expand Down

0 comments on commit 9eeb199

Please sign in to comment.