Skip to content

Commit f3f7670

Browse files
committed
Initial commit for adding CRM samples
1 parent 34e0721 commit f3f7670

4 files changed

Lines changed: 82 additions & 0 deletions

File tree

resourcemanager/__init__.py

Whitespace-only changes.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from utils import build_client, wait_for_active
2+
from googleapiclient.errors import HttpError
3+
import httplib2
4+
import random
5+
import argparse
6+
import json
7+
8+
def create_project(name, id, **labels):
9+
return client.projects().create(body={
10+
'projectId': pid_candidate,
11+
'name': name
12+
'labels': labels
13+
}).execute()
14+
15+
16+
def run(name, id=None, **labels):
17+
project = None
18+
if id is None:
19+
while project is None:
20+
id = "{}-{}-{}".format(*rw.random_words(count=2), random.randint(100, 999))[:30]
21+
try:
22+
project = create_project(client, name, id, **labels)
23+
except HttpError as e:
24+
code, uri, reason = str(e).parse('<HttpError %s when requesting %s returned "%s">')
25+
if not reason=="Requested entity already exists":
26+
raise e
27+
else:
28+
project = create_project(client, name, id, **labels)
29+
30+
return wait_for_active(project)
31+
32+
parser = argparse.ArgumentParser(description='Create a Google Cloud Project')
33+
parser.add_argument('--name', type=str, help='Human readable name of the project', required=True)
34+
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 have a memorable string generated for you')
35+
parser.add_argument('--labels', type=json.loads, help='Json formatted dictionary of labels to apply to the project')
36+
37+
if __name__=='__main__':
38+
args = parser.parse_args()
39+
if args.labels:
40+
run(args.name, id=args.id, **args.labels)
41+
else:
42+
run(args.name, id=args.id)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from utils import build_client
2+
import argparse
3+
4+
5+
def run(id):
6+
client = build_client()
7+
project = client.projects().delete(projectId=id)
8+
if project['lifecycleState'] == 'DELETE_REQUESTED':
9+
print("Project {} successfully deleted".format(id))
10+
else:
11+
print("Project {} was not scheduled for deletion: \n
12+
either the project is associated with a billing account,
13+
or is not currently active".format(id))
14+
15+
16+
17+
18+
parser = argparse.ArgumentParser(description='Delete a Google Cloud Project')
19+
parser.add_argument('--id', type=str, required=True, help='Unique Id of the project to delete')
20+
21+
if __name__ == '__main__':
22+
args = parser.parse_args()
23+
run(args.id)

resourcemanager/samples/utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from googleapiclient.discovery import build
2+
from oauth2client.client import GoogleCredentials
3+
import httplib2
4+
5+
def build_client():
6+
return build('cloudresourcemanager',
7+
'v1beta1',
8+
credentials=GoogleCredentials.get_application_default()
9+
http=httplib2.Http(timeout=90) #Long timeout for create requests
10+
11+
def wait_for_active(project):
12+
timeout=1
13+
while project['lifecycleState'] != 'ACTIVE':
14+
sleep(timeout)
15+
timeout=timeout*2
16+
project = client.projects().get(projectId=project_id).execute()
17+
return project

0 commit comments

Comments
 (0)