1- from utils import build_client , wait_for_active
2- from googleapiclient .errors import HttpError
3- import httplib2
4- import random
51import argparse
62import 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' )
3746parser .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 )
0 commit comments