Skip to content

Commit 416320d

Browse files
committed
Added requirements, fixed syntax
1 parent 02d76f8 commit 416320d

4 files changed

Lines changed: 40 additions & 25 deletions

File tree

resourcemanager/create_project.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,48 @@
1-
from utils import build_client, wait_for_active
2-
from googleapiclient.errors import HttpError
3-
import httplib2
4-
import random
51
import argparse
62
import json
3+
import random
4+
5+
from googleapiclient.errors import HttpError
6+
from random_words import RandomWords
7+
from utils import build_client, wait_for_active
8+
79

10+
rw = RandomWords()
811

9-
def create_project(name, id, **labels):
10-
return client.projects().create(body={
11-
'projectId': pid_candidate,
12-
'name': name
12+
13+
def create_project(client, name, id, **labels):
14+
return client.projects().create(
15+
body={
16+
'projectId': id,
17+
'name': name,
1318
'labels': labels
14-
}).execute()
19+
}
20+
).execute()
1521

1622

17-
def run(name, id=None, **labels):
18-
project = None
23+
def run(name, id=None, **labels):
24+
client = build_client()
25+
project = None
1926
if id is None:
2027
while project is None:
21-
id = "{}-{}-{}".format(*rw.random_words(count=2),
28+
words = rw.random_words(count=2)
29+
id = "{}-{}-{}".format(words[0],
30+
words[1],
2231
random.randint(100, 999))[:30]
2332
try:
2433
project = create_project(client, name, id, **labels)
2534
except HttpError as e:
2635
code, uri, reason = str(e).parse(
27-
'<HttpError %s when requesting %s returned "%s">')
36+
'<HttpError %s when requesting %s returned "%s">')
2837
if not reason == "Requested entity already exists":
2938
raise e
3039
else:
3140
project = create_project(client, name, id, **labels)
3241

33-
return wait_for_active(project)
42+
return wait_for_active(client, project)
3443

3544

36-
parser = argparse.ArgumentParser(description = 'Create a Google Cloud Project')
45+
parser = argparse.ArgumentParser(description='Create a Google Cloud Project')
3746
parser.add_argument('--name',
3847
type=str,
3948
help='Human readable name of the project',
@@ -47,9 +56,9 @@ def run(name, id=None, **labels):
4756
type=json.loads,
4857
help='Json formatted dictionary of labels')
4958

50-
if __name__=='__main__':
59+
if __name__ == '__main__':
5160
args = parser.parse_args()
52-
if args.labels:
53-
run(args.name, id=args.id, **args.labels)
54-
else:
55-
run(args.name, id=args.id)
61+
if args.labels:
62+
run(args.name, id=args.id, **args.labels)
63+
else:
64+
run(args.name, id=args.id)

resourcemanager/delete_project.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from utils import build_client
21
import argparse
32

3+
from utils import build_client
4+
45

56
def run(id):
67
client = build_client()

resourcemanager/requirements.txt

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

resourcemanager/utils.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
from time import sleep
2+
13
from googleapiclient.discovery import build
2-
from oauth2client.client import GoogleCredentials
34
import httplib2
4-
from time import sleep
5+
from oauth2client.client import GoogleCredentials
56

67

78
def build_client():
@@ -12,10 +13,12 @@ def build_client():
1213
http=httplib2.Http(timeout=90))
1314

1415

15-
def wait_for_active(project):
16+
def wait_for_active(client, project):
1617
timeout = 1
1718
while project['lifecycleState'] != 'ACTIVE':
1819
sleep(timeout)
1920
timeout = timeout*2
20-
project = client.projects().get(projectId=project_id).execute()
21+
project = client.projects().get(
22+
projectId=project['projectId']
23+
).execute()
2124
return project

0 commit comments

Comments
 (0)