Skip to content

Commit 71571fc

Browse files
authored
Initial code (#1)
* first code * add namespace API class
1 parent d366c6b commit 71571fc

File tree

13 files changed

+285
-0
lines changed

13 files changed

+285
-0
lines changed

.github/workflows/build-test.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Build and Test
2+
3+
on:
4+
push:
5+
tags-ignore:
6+
- '*.*'
7+
branches:
8+
- master
9+
pull_request:
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
strategy:
15+
matrix:
16+
python-version: [3.6, 3.7, 3.8]
17+
steps:
18+
- name: Checkout latest code
19+
uses: actions/checkout@v2
20+
- name: Set up Python ${{ matrix.python-version }}
21+
uses: actions/setup-python@v2
22+
with:
23+
python-version: ${{ matrix.python-version }}
24+
- name: Install dependencies
25+
run: |
26+
make init
27+
- name: Test
28+
run: |
29+
make test

.github/workflows/publish-release.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Publish release
2+
3+
on:
4+
release:
5+
types: [created]
6+
7+
jobs:
8+
publish-release:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Checkout latest code
13+
uses: actions/checkout@v2
14+
- name: Set up Python 3.7
15+
uses: actions/setup-python@v2
16+
with:
17+
python-version: 3.7
18+
- name: Install dependencies
19+
run: |
20+
make init
21+
- name: Test
22+
run: |
23+
make test
24+
- name: bulid package
25+
run: |
26+
python setup.py sdist
27+
- name: Publish a Python distribution to PyPI
28+
uses: pypa/gh-action-pypi-publish@master
29+
with:
30+
user: __token__
31+
password: ${{ secrets.pypi_password }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,5 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
py3/

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
init:
3+
pip install -r requirements.txt
4+
5+
test:
6+
python -m unittest
7+
8+
.PHONY: init test

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,29 @@
11
# staroid-python
22
Staroid python client library
3+
4+
## Install
5+
6+
```bash
7+
pip install staroid
8+
```
9+
10+
## Quickstart
11+
12+
13+
Initialize
14+
15+
```python
16+
from staroid import Staroid
17+
18+
# with no argument, it searches configuration ~/.staroid/config first and then try in-cluster configuration
19+
strd = Staroid()
20+
21+
# alternatively, pass access token through the argument
22+
strd = Staroid(access_token="<access token>", org="<org_name>")
23+
```
24+
25+
Select cluster
26+
27+
```python
28+
cluster1 = strd.cluster("<cluster_name>")
29+
```

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pyyaml
2+
requests

setup.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import setuptools
2+
3+
with open("README.md", "r") as fh:
4+
long_description = fh.read()
5+
6+
setuptools.setup(
7+
name="staroid", # Replace with your own username
8+
version="0.0.1",
9+
author="Staroid",
10+
author_email="support@staroid.com",
11+
description="Python client library for Staroid cloud platform",
12+
long_description=long_description,
13+
long_description_content_type="text/markdown",
14+
url="https://github.com/staroids/staroid-python",
15+
packages=setuptools.find_packages(),
16+
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
17+
classifiers=[
18+
# How mature is this project? Common values are
19+
# 3 - Alpha
20+
# 4 - Beta
21+
# 5 - Production/Stable
22+
# Indicate who your project is intended for
23+
'Development Status :: 5 - Production/Stable',
24+
25+
# Indicate who your project is intended for
26+
'Intended Audience :: Developers',
27+
'Topic :: Software Development :: Libraries',
28+
29+
30+
# Specify the Python versions you support here. In particular, ensure
31+
'Programming Language :: Python :: 3.6',
32+
'Programming Language :: Python :: 3.7',
33+
'Programming Language :: Python :: 3.8'
34+
35+
# Pick your license as you wish (should match "license" above)
36+
"License :: OSI Approved :: MIT License",
37+
"Operating System :: OS Independent",
38+
],
39+
python_requires='>=3.6',
40+
install_requires=[
41+
'requests',
42+
'pyyaml',
43+
44+
]
45+
)

staroid/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .staroid import Staroid
2+
from .cluster import ClusterApi
3+
from .namespace import NamespaceApi

staroid/cluster.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import requests
2+
import logging
3+
import json
4+
5+
class ClusterApi:
6+
"""Cluster api"""
7+
8+
def __init__(self, staroid):
9+
self.__staroid = staroid
10+
11+
def get_all(self):
12+
r = self.__staroid._api_get("orgs/{}/vc".format(self.__staroid.get_org()))
13+
if r.status_code == 200:
14+
return json.loads(r.text)
15+
else:
16+
logging.error("Can not get clusters {}", r.status_code)
17+
return None

staroid/namespace.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import requests
2+
import logging
3+
import json
4+
5+
class NamespaceApi:
6+
"""Namespace api"""
7+
8+
def __init__(self, cluster_api):
9+
self.__staroid = cluster_api.__staroid
10+
self.__cluster_api = cluster_api
11+
12+
def get_all(self):
13+
r = self.__staroid._api_get(
14+
"orgs/{}/vc/{}/instance".format(
15+
self.__staroid.get_org(),
16+
17+
))
18+
if r.status_code == 200:
19+
return json.loads(r.text)
20+
else:
21+
logging.error("Can not get clusters {}", r.status_code)
22+
return None

staroid/staroid.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import os
2+
import yaml
3+
import logging
4+
import requests
5+
import json
6+
from .cluster import ClusterApi
7+
8+
class Staroid:
9+
"""Staroid client object"""
10+
11+
def __init__(self, access_token=None, org=None, config_path="~/.staroid/config.yaml"):
12+
self.__api_addr = "https://staroid.com/api"
13+
self.__read_config(config_path)
14+
15+
if access_token != None:
16+
self.__access_token = access_token
17+
18+
if org != None:
19+
self.__org = org
20+
21+
def __read_config(self, config_path):
22+
try:
23+
with open(config_path, "r") as f:
24+
logging.info("Read configuration from " + config_path)
25+
data = yaml.load(f, Loader=yaml.FullLoader)
26+
self.__access_token = data.get("access_token", None)
27+
self.__org = data.get("default_org", None)
28+
except EnvironmentError:
29+
pass
30+
31+
32+
def cluster(self):
33+
c = ClusterApi(self)
34+
return c
35+
36+
def get_access_token(self):
37+
return self.__access_token
38+
39+
def get_org(self):
40+
return self.__org
41+
42+
def with_org(self, org):
43+
self.__org = org
44+
return self
45+
46+
def get_all_orgs(self):
47+
r = self._api_get(self, "orgs/")
48+
if r.status_code == 200:
49+
return json.loads(r.text)
50+
else:
51+
logging.error("Can't get orgs")
52+
return None
53+
54+
def __get_request_url(self, path):
55+
request_url = "{}/{}".format(self.__api_addr, path)
56+
return request_url
57+
58+
def __get_request_headers(self):
59+
headers = {
60+
"Authorization": "token {}".format(self.__access_token),
61+
"Content-Type": "application/json"
62+
}
63+
return headers
64+
65+
def _api_get(self, path):
66+
url = self.__get_request_url(path);
67+
headers = self.__get_request_headers()
68+
69+
r = requests.get(url, headers=headers)
70+
return r
71+
72+
73+
def _api_post(self, path, payload):
74+
url = self.__get_request_url(path);
75+
headers = self.__get_request_headers()
76+
77+
r = requests.get(url, headers=headers, data=json.dumps(payload))
78+
return r

tests/__init__.py

Whitespace-only changes.

tests/test_staroid.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import unittest
2+
import tempfile
3+
4+
from staroid import Staroid
5+
6+
class TestStaroid(unittest.TestCase):
7+
def test_initialize(self):
8+
s = Staroid()
9+
10+
def test_read_config(self):
11+
# given
12+
fp = tempfile.NamedTemporaryFile()
13+
fp.write(b"access_token: abc\ndefault_org: GITHUB/user1")
14+
fp.flush()
15+
16+
# when
17+
s = Staroid(config_path=fp.name)
18+
19+
# then
20+
self.assertEqual("abc", s.get_access_token())
21+
self.assertEqual("GITHUB/user1", s.get_org())

0 commit comments

Comments
 (0)