Skip to content

Commit a040502

Browse files
committed
sdk work
1 parent 2d208c8 commit a040502

7 files changed

Lines changed: 73 additions & 3 deletions

File tree

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ mkdocs==1.3.1
1616
mkdocs-click==0.8.0
1717
twine~=4.0.1
1818
python-dateutil~=2.8.2
19-
boto3
19+
boto3
20+
jmespath

setup.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = pybritive
3-
version = 0.10.2
3+
version = 0.11.0
44
author = Britive Inc.
55
author_email = support@britive.com
66
description = A pure Python CLI for Britive
@@ -27,6 +27,7 @@ install_requires =
2727
cryptography
2828
python-dateutil
2929
britive>=2.14.0
30+
jmespath
3031

3132
[options.packages.find]
3233
where = src

src/pybritive/britive_cli.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from datetime import datetime
1616
import os
1717
import sys
18+
import jmespath
1819

1920

2021
default_table_format = 'fancy_grid'
@@ -596,6 +597,16 @@ def request_withdraw(self, profile):
596597
def clear_gcloud_auth_key_files(self):
597598
self.config.clear_gcloud_auth_key_files()
598599

600+
def sdk(self, method, parameters={}, query=None):
601+
self.login()
602+
func = self.b
603+
for m in method.split('.'):
604+
func = getattr(func, m)
605+
response = func(**parameters)
606+
self.print(jmespath.search(query, response) if query else response)
607+
608+
609+
599610

600611

601612

src/pybritive/cli_interface.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from .commands.cache import cache as group_cache
1212
from .commands.request import request as group_request
1313
from .commands.clear import clear as group_clear
14+
from .commands.sdk import sdk as command_sdk
1415
import sys
1516
import os
1617

@@ -48,6 +49,7 @@ def cli(version):
4849
cli.add_command(group_cache)
4950
cli.add_command(group_request)
5051
cli.add_command(group_clear)
52+
cli.add_command(command_sdk)
5153

5254

5355
if __name__ == "__main__":

src/pybritive/commands/sdk.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import click
2+
from ..helpers.build_britive import build_britive
3+
from ..options.britive_options import britive_options
4+
5+
6+
@click.command(
7+
context_settings=dict(
8+
ignore_unknown_options=True,
9+
allow_extra_args=True
10+
)
11+
)
12+
@build_britive
13+
@britive_options(names='query,output_format,tenant,token,passphrase,federation_provider')
14+
@click.argument('method')
15+
def sdk(ctx, query, output_format, tenant, token, passphrase, federation_provider, method):
16+
"""Exposes the Britive Python SDK methods to the CLI.
17+
18+
Documentation on each SDK method can be found inside the Python SDK itself and on Github
19+
(https://github.com/britive/python-sdk). The Python package `britive` is a dependency of the CLI
20+
already so the SDK is available without installing any extra packages.
21+
22+
It is left up to the caller to provide the proper `method` and `parameters` based on the documentation
23+
of the API call being performed.
24+
25+
The authenticated identity must have the appropriate permissions to perform the actions being requested.
26+
General end users of Britive will not have these permissions. This call (and the larger SDK) is generally
27+
meant for administrative functionality.
28+
29+
Example of use:
30+
31+
* pybritive sdk users.list
32+
33+
* pybritive sdk tags.create --name testtag --description "test tag"
34+
35+
* pybritive sdk users.list --query '[].email'
36+
37+
* pybritive sdk profiles.create --application-id <id> --name testprofile
38+
39+
"""
40+
parameters = {ctx.args[i][2:]: ctx.args[i + 1] for i in range(0, len(ctx.args), 2)}
41+
ctx.obj.britive.sdk(
42+
method=method,
43+
parameters=parameters,
44+
query=query
45+
)

src/pybritive/options/britive_options.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from ..options.federation_provider import option as federation_provider
2323
from ..options.gcloud_key_file import option as gcloud_key_file
2424
from ..options.verbose import option as verbose
25+
from ..options.query import option as query
2526

2627
options_map = {
2728
'tenant': tenant,
@@ -47,7 +48,8 @@
4748
'aws_credentials_file': aws_credentials_file,
4849
'federation_provider': federation_provider,
4950
'gcloud_key_file': gcloud_key_file,
50-
'verbose': verbose
51+
'verbose': verbose,
52+
'query': query
5153
}
5254

5355

src/pybritive/options/query.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import click
2+
3+
4+
option = click.option(
5+
'--query',
6+
default=None,
7+
help='JMESPath query to apply to the response.'
8+
)

0 commit comments

Comments
 (0)