Skip to content

Commit 99525c0

Browse files
authored
make account optional. auto configure when not defined (#3)
1 parent 211f098 commit 99525c0

File tree

8 files changed

+135
-40
lines changed

8 files changed

+135
-40
lines changed

README.md

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,34 @@ pip install staroid
1010
## Quickstart
1111

1212

13-
Initialize
14-
1513
```python
1614
from staroid import Staroid
1715

18-
# with no argument, it searches configuration ~/.staroid/config first and then try in-cluster configuration
19-
strd = Staroid()
16+
star = Staroid()
17+
18+
# create a ske kubernetes cluster
19+
my_cluster = star.cluster().create("my cluster", "gcp us-west1")
2020

21-
# alternatively, pass access token through the argument
22-
strd = Staroid(access_token="<access token>", org="<org_name>")
21+
# create a namespace and deploy project
22+
ns = star.namespace(my_cluster).create("my_app", "GITHUB/staroids/namespace:master")
2323
```
2424

25-
Select cluster
25+
## Configuration
2626

27-
```python
28-
cluster1 = strd.cluster("<cluster_name>")
29-
```
27+
### Environment variables
28+
29+
| Env variable | optional | description |
30+
| ------------- | ---------- | ------------ |
31+
| STAROID_ACCESS_TOKEN | false | Access token generated from [here](https://staroid.com/settings/accesstokens) |
32+
| STAROID_ACCOUNT | true | Account to use. e.g. `GITHUB/my_github_login` |
33+
34+
### Config file
35+
36+
Altanatively, you can create `~/.staroid/config.yaml` file like below.
37+
38+
```yaml
39+
access_token: <your access token> # access token from https://staroid.com/settings/accesstokens
40+
account: <account> # optional. e.g. "GITHUB/my_github_login"
41+
```
42+
43+
When both config file and environment variables exist, environment variable will take precedence.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="staroid", # Replace with your own username
8-
version="0.0.2",
8+
version="0.0.3",
99
license='MIT',
1010
author="Staroid",
1111
author_email="support@staroid.com",

staroid/cluster.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def get(self, name):
5757
return cluster_found
5858

5959
def get_all(self):
60-
r = self.__staroid._api_get("orgs/{}/vc".format(self.__staroid.get_org()))
60+
r = self.__staroid._api_get("orgs/{}/vc".format(self.__staroid.get_account()))
6161
if r.status_code == 200:
6262
json_object_list = json.loads(r.text)
6363
cluster_list = []
@@ -71,7 +71,7 @@ def get_all(self):
7171

7272
def create(self, name, cluster="gcp us-west1"):
7373
r = self.__staroid._api_post(
74-
"orgs/{}/vc".format(self.__staroid.get_org()),
74+
"orgs/{}/vc".format(self.__staroid.get_account()),
7575
{
7676
"name": name,
7777
"clusterId": CLUSTER_ID_MAP[cluster]
@@ -92,7 +92,7 @@ def delete(self, name):
9292
cluster_to_del = self.get(name)
9393
if cluster_to_del != None:
9494
r = self.__staroid._api_delete(
95-
"orgs/{}/vc/{}".format(self.__staroid.get_org(), cluster_to_del.id())
95+
"orgs/{}/vc/{}".format(self.__staroid.get_account(), cluster_to_del.id())
9696
)
9797
else:
9898
return None

staroid/namespace.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def __init__(self, staroid, cluster):
4141
def get_all(self):
4242
r = self.__staroid._api_get(
4343
"orgs/{}/vc/{}/instance".format(
44-
self.__staroid.get_org(),
44+
self.__staroid.get_account(),
4545
self.__cluster.id()
4646
)
4747
)
@@ -69,7 +69,7 @@ def get(self, instance_name):
6969
def get_by_id(self, instance_id):
7070
r = self.__staroid._api_get(
7171
"orgs/{}/vc/{}/instance/{}".format(
72-
self.__staroid.get_org(),
72+
self.__staroid.get_account(),
7373
self.__cluster.id(),
7474
instance_id
7575
)
@@ -86,7 +86,7 @@ def create(self, instance_name, commit_url):
8686

8787
r = self.__staroid._api_post(
8888
"orgs/{}/vc/{}/instance".format(
89-
self.__staroid.get_org(),
89+
self.__staroid.get_account(),
9090
self.__cluster.id()
9191
),
9292
{
@@ -114,7 +114,7 @@ def delete(self, instance_name):
114114

115115
r = self.__staroid._api_delete(
116116
"orgs/{}/vc/{}/instance/{}".format(
117-
self.__staroid.get_org(),
117+
self.__staroid.get_account(),
118118
self.__cluster.id(),
119119
ns.id()
120120
)
@@ -136,7 +136,7 @@ def start(self, instance_name):
136136

137137
r = self.__staroid._api_put(
138138
"orgs/{}/vc/{}/instance/{}/resume".format(
139-
self.__staroid.get_org(),
139+
self.__staroid.get_account(),
140140
self.__cluster.id(),
141141
ns.id()
142142
)
@@ -158,7 +158,7 @@ def stop(self, instance_name):
158158

159159
r = self.__staroid._api_put(
160160
"orgs/{}/vc/{}/instance/{}/pause".format(
161-
self.__staroid.get_org(),
161+
self.__staroid.get_account(),
162162
self.__cluster.id(),
163163
ns.id()
164164
)
@@ -180,7 +180,7 @@ def shell_start(self, instance_name):
180180

181181
r = self.__staroid._api_post(
182182
"orgs/{}/vc/{}/instance/{}/shell".format(
183-
self.__staroid.get_org(),
183+
self.__staroid.get_account(),
184184
self.__cluster.id(),
185185
ns.id()
186186
)
@@ -201,7 +201,7 @@ def shell_stop(self, instance_name):
201201

202202
r = self.__staroid._api_delete(
203203
"orgs/{}/vc/{}/instance/{}/shell".format(
204-
self.__staroid.get_org(),
204+
self.__staroid.get_account(),
205205
self.__cluster.id(),
206206
ns.id()
207207
)

staroid/staroid.py

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,58 @@ def provider(self):
1919
def id(self):
2020
return int(self.__json["id"])
2121

22+
class User:
23+
def __init__(self, json):
24+
self.__json = json
25+
26+
def name(self):
27+
return self.__json["name"]
28+
29+
def provider(self):
30+
return self.__json["provider"]
31+
32+
def principal(self):
33+
return self.__json["principal"]
34+
35+
2236
class Staroid:
2337
"""Staroid client object"""
2438

25-
def __init__(self, access_token=None, org=None, config_path="~/.staroid/config.yaml"):
39+
def __init__(self, access_token=None, account=None, config_path="~/.staroid/config.yaml"):
2640
self.__api_addr = "https://staroid.com/api"
41+
self.__access_token = None
42+
self.__account = None
43+
44+
# 1. set from configs
2745
self.__read_config(config_path)
2846

47+
# 2. set from env
48+
if "STAROID_ACCESS_TOKEN" in os.environ:
49+
self.__access_token = os.environ["STAROID_ACCESS_TOKEN"]
50+
51+
if "STAROID_ACCOUNT" in os.environ:
52+
self.__account = os.environ["STAROID_ACCOUNT"]
53+
54+
# 3. set from args
2955
if access_token != None:
3056
self.__access_token = access_token
3157

32-
if org != None:
33-
self.__org = org
58+
if account != None:
59+
self.__account = account
60+
61+
# if account is not set, set default user account
62+
if self.__account == None:
63+
user = self.get_user()
64+
if user != None:
65+
self.__account = "{}/{}".format(user.provider(), user.principal())
3466

3567
def __read_config(self, config_path):
3668
try:
3769
with open(config_path, "r") as f:
3870
logging.info("Read configuration from " + config_path)
3971
data = yaml.load(f, Loader=yaml.FullLoader)
4072
self.__access_token = data.get("access_token", None)
41-
self.__org = data.get("default_org", None)
73+
self.__account = data.get("account", None)
4274
except EnvironmentError:
4375
pass
4476

@@ -53,14 +85,26 @@ def namespace(self, cluster):
5385
def get_access_token(self):
5486
return self.__access_token
5587

56-
def get_org(self):
57-
return self.__org
88+
def get_account(self):
89+
return self.__account
5890

59-
def with_org(self, org):
60-
self.__org = org
91+
def with_account(self, account):
92+
self.__account = account
6193
return self
6294

63-
def get_all_orgs(self):
95+
def get_user(self):
96+
"read user information"
97+
if self.__access_token == None:
98+
return None
99+
100+
r = self._api_get("auth/user")
101+
if r.status_code == 200:
102+
js = json.loads(r.text)
103+
return User(js)
104+
else:
105+
return None
106+
107+
def get_all_accounts(self):
64108
r = self._api_get("orgs/")
65109
if r.status_code == 200:
66110
org_object_list = json.loads(r.text)
@@ -70,7 +114,7 @@ def get_all_orgs(self):
70114

71115
return org_list
72116
else:
73-
logging.error("Can't get orgs")
117+
logging.error("Can't get accounts")
74118
return None
75119

76120
def __get_request_url(self, path):

tests/test_cluster.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ def integration_test_ready():
1010
class TestCluster(unittest.TestCase):
1111
@unittest.skipUnless(integration_test_ready(), "Integration test environment is not configured")
1212
def test_initialize(self):
13-
s = Staroid(access_token=os.environ["STAROID_ACCESS_TOKEN"], org=os.environ["STAROID_ACCOUNT"])
14-
all_orgs = s.get_all_orgs()
13+
s = Staroid(access_token=os.environ["STAROID_ACCESS_TOKEN"], account=os.environ["STAROID_ACCOUNT"])
14+
s.get_all_accounts()
1515

1616
@unittest.skipUnless(integration_test_ready(), "Integration test environment is not configured")
1717
def test_crud(self):
1818
# given
19-
s = Staroid(access_token=os.environ["STAROID_ACCESS_TOKEN"], org=os.environ["STAROID_ACCOUNT"])
20-
all_orgs = s.get_all_orgs()
19+
s = Staroid(access_token=os.environ["STAROID_ACCESS_TOKEN"], account=os.environ["STAROID_ACCOUNT"])
20+
all_accounts = s.get_all_accounts()
2121
clusters_before = s.cluster().get_all()
2222

2323
# when create

tests/test_namespace.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ class TestCluster(unittest.TestCase):
2020
@unittest.skipUnless(integration_test_ready(), "Integration test environment is not configured")
2121
def test_crud_namespace(self):
2222
# given
23-
s = Staroid(access_token=os.environ["STAROID_ACCESS_TOKEN"], org=os.environ["STAROID_ACCOUNT"])
24-
all_orgs = s.get_all_orgs()
23+
s = Staroid(access_token=os.environ["STAROID_ACCESS_TOKEN"], account=os.environ["STAROID_ACCOUNT"])
2524
c = s.cluster().create("staroid-python it-test-namespace")
2625

2726
# when create a namespace

tests/test_staroid.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,59 @@
11
import unittest
22
import tempfile
3+
import os
34

45
from staroid import Staroid
56

7+
def integration_test_ready():
8+
return "STAROID_ACCESS_TOKEN" in os.environ and "STAROID_ACCOUNT" in os.environ
9+
610
class TestStaroid(unittest.TestCase):
711
def test_initialize(self):
812
s = Staroid()
913

1014
def test_read_config(self):
15+
# unset env
16+
at = None
17+
ac = None
18+
if "STAROID_ACCESS_TOKEN" in os.environ:
19+
at = os.environ["STAROID_ACCESS_TOKEN"]
20+
del os.environ["STAROID_ACCESS_TOKEN"]
21+
if "STAROID_ACCOUNT" in os.environ:
22+
ac = os.environ["STAROID_ACCOUNT"]
23+
del os.environ["STAROID_ACCOUNT"]
24+
1125
# given
1226
fp = tempfile.NamedTemporaryFile()
13-
fp.write(b"access_token: abc\ndefault_org: GITHUB/user1")
27+
fp.write(b"access_token: abc\naccount: GITHUB/user1")
1428
fp.flush()
1529

1630
# when
1731
s = Staroid(config_path=fp.name)
1832

1933
# then
2034
self.assertEqual("abc", s.get_access_token())
21-
self.assertEqual("GITHUB/user1", s.get_org())
35+
self.assertEqual("GITHUB/user1", s.get_account())
36+
37+
# restore env
38+
if at != None:
39+
os.environ["STAROID_ACCESS_TOKEN"] = at
40+
if ac != None:
41+
os.environ["STAROID_ACCOUNT"] = ac
42+
43+
@unittest.skipUnless(integration_test_ready(), "Integration test environment is not configured")
44+
def test_read_default_account(self):
45+
# given access_token is set but account is not set
46+
ac = None
47+
if "STAROID_ACCOUNT" in os.environ:
48+
ac = os.environ["STAROID_ACCOUNT"]
49+
del os.environ["STAROID_ACCOUNT"]
50+
51+
# when
52+
s = Staroid()
53+
54+
# then
55+
self.assertNotEqual(None, s.get_account())
56+
57+
# restore env
58+
if ac != None:
59+
os.environ["STAROID_ACCOUNT"] = ac

0 commit comments

Comments
 (0)