Skip to content

Commit

Permalink
Merge pull request #21 from nathanielvarona/improvement/global-enviro…
Browse files Browse the repository at this point in the history
…nment-variables

[API, CLI] Globalized the Use of Environment Variables
  • Loading branch information
nathanielvarona authored Mar 27, 2023
2 parents fb73a80 + 3d498df commit a6546fa
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 42 deletions.
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,23 @@ export PRITUNL_API_TOKEN="XXXXXXXXXXXXXXXXXXXXXX"
export PRITUNL_API_SECRET="XXXXXXXXXXXXXXXXXXXXX"
```

Initializing an API object.
Initializing an API Instance.

```python
import os
# Import the object
from pritunl_api import Pritunl

pritunl = Pritunl(
url=os.environ.get('PRITUNL_BASE_URL'),
token=os.environ.get('PRITUNL_API_TOKEN'),
secret=os.environ.get('PRITUNL_API_SECRET')
)
# Create an instance
pritunl = Pritunl()

# Your Pritunl API Client Object is now ready to use!
## You can also initialize an instance by manually providing the arguments.
# pritunl = Pritunl(
# url="<PRITUNL API URL>",
# token="<PRITUNL API TOKEN>",
# secret="<PRITUNL API SECRET>"
# )

# Your Pritunl API Client instance is now ready to use!
pritunl.<FEATURE>.<METHOD>
```

Expand Down
11 changes: 6 additions & 5 deletions pritunl_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.1.4"
__version__ = "1.1.5"

import json
import logging as log
Expand All @@ -10,6 +10,7 @@
import requests
import time
import uuid
import os

requests.packages.urllib3.disable_warnings(
requests.packages.urllib3.exceptions.InsecureRequestWarning)
Expand All @@ -22,10 +23,10 @@ def __init__(self, msg):


class Pritunl:
def __init__(self, url, token, secret):
self.BASE_URL = self.clean_url(url)
self.API_TOKEN = token
self.API_SECRET = secret
def __init__(self, url=None, token=None, secret=None):
self.BASE_URL = self.clean_url(os.environ.get('PRITUNL_BASE_URL') if not url else url)
self.API_TOKEN = os.environ.get('PRITUNL_API_TOKEN') if not token else token
self.API_SECRET = os.environ.get('PRITUNL_API_SECRET') if not secret else secret

# Sub classes
self.server = self.ServerClass(self)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ def profile_key(pritunl_obj, org_id, usr_id):
key_view_url = pritunl_obj.BASE_URL + key.json()['view_url']
return key_uri_url, key_view_url
else:
return None
return None, None
File renamed without changes.
10 changes: 5 additions & 5 deletions pritunl_api_cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import click

# Pritunl
from .commands import user
from .commands import users

@click.group()
def run():
Expand All @@ -13,7 +13,7 @@ def run():
@click.option('--user-name')
@click.option('--get-profile-key-only', is_flag=True)
def get_user(**kwargs):
user.get_user(**kwargs)
users.get_user(**kwargs)

# Create User
@run.command()
Expand All @@ -24,22 +24,22 @@ def get_user(**kwargs):
@click.option('--yubikey-id')
@click.option('--from-csv-file', type=click.Path(exists=True))
def create_user(**kwargs):
user.create_user(**kwargs)
users.create_user(**kwargs)

# Update User
@run.command()
@click.option('--org-name')
@click.option('--user-name')
@click.option('--disable/--enable', default=False)
def update_user(**kwargs):
user.update_user(**kwargs)
users.update_user(**kwargs)

# Delete User
@run.command()
@click.option('--org-name')
@click.option('--user-name')
def delete_user(**kwargs):
user.delete_user(**kwargs)
users.delete_user(**kwargs)

if __name__ == '__main__':
run()
8 changes: 0 additions & 8 deletions pritunl_api_cli/commands/__init__.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
import json
from urllib.parse import urlparse

from . import pritunl
from .utils.query import org_user
from pritunl_api import Pritunl
pritunl = Pritunl()

from pritunl_api.utils.query import org_user
from pritunl_api.utils.genkey import profile_key

import click

Expand All @@ -14,7 +17,7 @@

def get_user(**kwargs):
org, user = org_user(pritunl_obj=pritunl, org_name=kwargs['org_name'], user_name=kwargs['user_name'])
key = pritunl.key.get(org_id=org['id'], usr_id=user['id'])
key_uri_url, key_view_url = profile_key(pritunl_obj=pritunl, org_id=org['id'], usr_id=user['id'])

if kwargs['get_profile_key_only']:
console.print(
Expand All @@ -23,8 +26,8 @@ def get_user(**kwargs):
style="green bold", sep='\n'
)
console.print(
f"PROFILE URI (PRITUNNL CLIENT IMPORT PROFILE): '{urlparse(pritunl.BASE_URL)._replace(scheme='pritunl').geturl() + key.json()['uri_url']}'",
f"PROFILE URL (WEB VIEW PROFILE): '{pritunl.BASE_URL + key.json()['view_url']}'",
f"PROFILE URI (PRITUNNL CLIENT IMPORT PROFILE): '{key_uri_url}'",
f"PROFILE URL (WEB VIEW PROFILE): '{key_view_url}'",
style="blue", sep='\n', end='\n \n', new_line_start=True
)
else:
Expand All @@ -51,22 +54,16 @@ def __create_user(org_id, user_name, user_email):

create_user = pritunl.user.post(org_id=org_id, data=user_data)
for user in create_user:
key = pritunl.key.get(org_id=user['organization'], usr_id=user['id'])
context = {
'key_urls': {
'uri_url': urlparse(pritunl.BASE_URL)._replace(scheme='pritunl').geturl() + key.json()['uri_url'],
'view_url': pritunl.BASE_URL + key.json()['view_url'],
}
}
key_uri_url, key_view_url = profile_key(pritunl_obj=pritunl, org_id=org['id'], usr_id=user['id'])
console.print(
f"USER `{user['name']}` WITH AN EMAIL `{user['email']}` FOR `{user['organization_name']}` ORGANIZATION IS SUCCESSFULLY CREATED!",
f"TEMPORARY PROFILE KEY (Expires after 24 hours)",
style="green bold", sep='\n', new_line_start=True
)

console.print(
f"PROFILE URI (PRITUNNL CLIENT IMPORT PROFILE): '{context['key_urls']['uri_url']}'",
f"PROFILE URL (WEB VIEW PROFILE): '{context['key_urls']['view_url']}'",
f"PROFILE URI (PRITUNNL CLIENT IMPORT PROFILE): '{key_uri_url}'",
f"PROFILE URL (WEB VIEW PROFILE): '{key_view_url}'",
style="blue", sep='\n', end='\n \n', new_line_start=True
)

Expand Down Expand Up @@ -124,6 +121,7 @@ def update_user(**kwargs):
style="green bold", sep='\n', new_line_start=True
)


def delete_user(**kwargs):
org, user = org_user(pritunl_obj=pritunl, org_name=kwargs['org_name'], user_name=kwargs['user_name'])
response = pritunl.user.delete(org_id=org['id'], usr_id=user['id'])
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pritunl-api"
version = "1.1.4"
version = "1.1.5"
description = "Pritunl API Client for Python"
authors = ["Nathaniel Varona <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit a6546fa

Please sign in to comment.