Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Samples For Cloud Resource Manager #54

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Style and syntax fixes
  • Loading branch information
elibixby committed Jul 17, 2015
commit 02d76f8d01e928445b92b7d5f02968c690267380
33 changes: 23 additions & 10 deletions resourcemanager/create_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import argparse
import json


def create_project(name, id, **labels):
return client.projects().create(body={
'projectId': pid_candidate,
Expand All @@ -13,29 +14,41 @@ def create_project(name, id, **labels):
}).execute()


def run(name, id=None, **labels):
project = None
def run(name, id=None, **labels):
project = None
if id is None:
while project is None:
id = "{}-{}-{}".format(*rw.random_words(count=2), random.randint(100, 999))[:30]
id = "{}-{}-{}".format(*rw.random_words(count=2),
random.randint(100, 999))[:30]
try:
project = create_project(client, name, id, **labels)
except HttpError as e:
code, uri, reason = str(e).parse('<HttpError %s when requesting %s returned "%s">')
if not reason=="Requested entity already exists":
code, uri, reason = str(e).parse(
'<HttpError %s when requesting %s returned "%s">')
if not reason == "Requested entity already exists":
raise e
else:
project = create_project(client, name, id, **labels)

return wait_for_active(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 have a memorable string generated for you')
parser.add_argument('--labels', type=json.loads, help='Json formatted dictionary of labels to apply to the 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()
args = parser.parse_args()
if args.labels:
run(args.name, id=args.id, **args.labels)
else:
Expand Down
11 changes: 6 additions & 5 deletions resourcemanager/delete_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ def run(id):
if project['lifecycleState'] == 'DELETE_REQUESTED':
print("Project {} successfully deleted".format(id))
else:
print("Project {} was not scheduled for deletion: \n
print("""Project {} was not scheduled for deletion:
either the project is associated with a billing account,
or is not currently active".format(id))


or is not currently active""".format(id))


parser = argparse.ArgumentParser(description='Delete a Google Cloud Project')
parser.add_argument('--id', type=str, required=True, help='Unique Id of the project to delete')
parser.add_argument('--id',
type=str,
required=True,
help='Unique Id of the project to delete')

if __name__ == '__main__':
args = parser.parse_args()
Expand Down
11 changes: 7 additions & 4 deletions resourcemanager/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
import httplib2
from time import sleep


def build_client():
return build('cloudresourcemanager',
'v1beta1',
credentials=GoogleCredentials.get_application_default()
http=httplib2.Http(timeout=90)) #Long timeout for create requests
credentials=GoogleCredentials.get_application_default(),
# Use long timeout for create requests
http=httplib2.Http(timeout=90))


def wait_for_active(project):
timeout=1
timeout = 1
while project['lifecycleState'] != 'ACTIVE':
sleep(timeout)
timeout=timeout*2
timeout = timeout*2
project = client.projects().get(projectId=project_id).execute()
return project