forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_project.py
More file actions
67 lines (56 loc) · 2.09 KB
/
Copy pathcreate_project.py
File metadata and controls
67 lines (56 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import argparse
import json
import logging
import random
from googleapiclient.errors import HttpError
from parse import *
from random_words import RandomWords
from utils import build_client, wait_for_active
rw = RandomWords()
def create_project(client, name, id, **labels):
return client.projects().create(
body={
'projectId': id,
'name': name,
'labels': labels
}
).execute()
def run(name, id=None, **labels):
client = build_client()
project = None
if id is None:
while project is None:
words = rw.random_words(count=2)
id = "{}-{}-{}".format(words[0],
words[1],
random.randint(100, 999))[:30]
try:
project = create_project(client, name, id, **labels)
except HttpError as e:
code, uri, reason = parse('<HttpError {} when requesting {} returned "{}">',
str(e))
if reason == "Requested entity already exists":
logging.info("Project ID {} is taken".format(id))
else:
raise e
project = create_project(client, name, id, **labels)
return wait_for_active(client, project)
parser = argparse.ArgumentParser(description='Create a Google Cloud Project')
parser.add_argument('--name',
type=str,
help='Human readable name of the project',
required=True)
parser.add_argument('--id',
type=str,
help="""Unique ID of the project. Max 30 Characters.
Only hyphens, digits, and lower case letters.
Leave blank to use a generated string""")
parser.add_argument('--labels',
type=json.loads,
help='Json formatted dictionary of labels')
if __name__ == '__main__':
args = parser.parse_args()
if args.labels:
run(args.name, id=args.id, **args.labels)
else:
run(args.name, id=args.id)