Skip to content

Commit

Permalink
Service Account operations
Browse files Browse the repository at this point in the history
 - added update and delete operations
 - updated extensions and platform collections to include the delete and update operations tasks
  • Loading branch information
kidynamit committed Jun 3, 2019
1 parent 71ec705 commit 27fe3f8
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 8 deletions.
20 changes: 20 additions & 0 deletions tesserarius/extensions/serviceaccount.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ def create(ctx):
sa = ExtensionsServiceAccount.create_obj()
sa.create(ctx)


@task
def update(ctx):
'''
Updates an IAM GCloud Service Account on rehive-services
'''
sa = ExtensionsServiceAccount.create_obj()
sa.update(ctx)


@task
def delete(ctx):
'''
an IAM GCloud Service Account on rehive-services
'''
sa = ExtensionsServiceAccount.create_obj()
sa.delete(ctx)

collection = Collection("serviceaccount")
collection.add_task(create, "create")
collection.add_task(update, "update")
collection.add_task(delete, "delete")
# collection.add_task(authorize_serviceaccount, "auth")
25 changes: 23 additions & 2 deletions tesserarius/platform/serviceaccount.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,33 @@ def create_obj(project="platform"):


@task
def create(context):
def create(ctx):
'''
Creates an IAM GCloud Service Account on rehive-core
'''
sa = PlatformServiceAccount.create_obj()
sa.create(context)
sa.create(ctx)


@task
def update(ctx):
'''
Updates an IAM GCloud Service Account on rehive-core
'''
sa = PlatformServiceAccount.create_obj()
sa.update(ctx)


@task
def delete(ctx):
'''
an IAM GCloud Service Account on rehive-core
'''
sa = PlatformServiceAccount.create_obj()
sa.delete(ctx)

collection = Collection("serviceaccount")
collection.add_task(create, "create")
collection.add_task(update, "update")
collection.add_task(delete, "delete")
# collection.add_task(authorize_serviceaccount, "auth")
63 changes: 57 additions & 6 deletions tesserarius/serviceaccount.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,16 @@ def _check_name(self):
raise ServiceAccountValidationError("Invalid account name.")


def get_emailaddress(self):
self.emailaddress = "{name}@{project_id}" \
".iam.gserviceaccount.com".format(
name=self.name, project_id=self.project_id)
return self.emailaddress


def create(self, ctx):
'''
Creates an IAM GCloud Service Account on rehive-services
Creates an IAM GCloud Service Account
'''
print("Creating service account '{name}' ... ".format(name=self.name),
end="")
Expand All @@ -83,11 +90,8 @@ def create(self, ctx):
description=self.description,
project_id=self.project_id),
echo=False,out_stream=tout(), err_stream=terr())
self.get_emailaddress()
self.created = True
self.emailaddress = "{name}@{project_id}" \
".iam.gserviceaccount.com".format(
name=self.name, project_id=self.project_id)

print("SUCCESS!")
except (Failure, UnexpectedExit,):
self.emailaddress = None
Expand All @@ -97,8 +101,55 @@ def create(self, ctx):
print("FAILED! [serviceaccount has already been created]'")


def update(self, ctx):
'''
Updates an IAM GCloud Service Account
'''
print("Updating service account '{name}' ... ".format(name=self.name),
end="")
self.get_emailaddress()
command = "gcloud alpha iam service-accounts update {emailaddress}" \
" --display-name \"{display_name}\"" \
" --description \"{description}\"" \
" --verbosity debug " \
" --project {project_id}"

try:
result = ctx.run(command.format(
emailaddress=self.emailaddress,
display_name=self.display_name,
description=self.description,
project_id=self.project_id),
echo=False,out_stream=tout(), err_stream=terr())
self.get_emailaddress()
print("SUCCESS!")
except (Failure, UnexpectedExit,):
self.emailaddress = None
print("FAILED! [serviceaccount can't be updated]'")


def delete(self, ctx):
'''
Deletes an IAM GCloud Service Account
'''
print("Deleting service account '{name}' ... ".format(name=self.name),
end="")
self.get_emailaddress()
command = "gcloud alpha iam service-accounts delete {emailaddress}" \
" --verbosity debug " \
" --project {project_id}"

try:
result = ctx.run(command.format(
emailaddress=self.emailaddress,
project_id=self.project_id),
echo=False,out_stream=tout(), err_stream=terr())
self.get_emailaddress()
print("SUCCESS!")
except (Failure, UnexpectedExit,):
self.emailaddress = None
print("FAILED! [serviceaccount can't be deleted]'")

# TODO Check for errrors on output

@staticmethod
def create_obj(project):
Expand Down

0 comments on commit 27fe3f8

Please sign in to comment.