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

fix(refactor): refactor code #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 8 additions & 4 deletions cirrus/google_cloud/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def get_primary_service_account(self, proxy_group_id):
if service_account_id_for_user in service_account_emails:
primary_email = service_account_emails[service_account_id_for_user]

return self.get_service_account(primary_email)
return self.get_service_account(primary_email).json()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are breaking changes. did you make sure you fixed every spot where these functions gets called in fence?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this refactoring may be better off waiting until we've done more implementation, since it might be a headache to fix all the spots in fence that rely on this. I'd almost suggest we wait to do this cirrus cleanup/refactor until later

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed all the code called to that function


def get_project_organization(self):
"""
Expand Down Expand Up @@ -507,7 +507,7 @@ def get_service_account(self, account):

response = self._authed_request("GET", api_url)

return response.json()
return response

def get_service_account_type(self, account):
"""
Expand All @@ -519,8 +519,12 @@ def get_service_account_type(self, account):
Returns:
String: type of service account
"""
service_account = self.get_service_account(account)
email_domain = service_account['email'].split("@")[-1]
response = self.get_service_account(account)
if response.status_code != 200:
# TODO: Need to have better handle non-200 status code
return None
service_account = response.json()
email_domain = service_account.get('email', '').split("@")[-1]
for (domain, sa_type) in GOOGLE_SERVICE_ACCOUNT_DOMAIN_TYPE_MAPPING:
if domain in email_domain:
return sa_type
Expand Down
15 changes: 13 additions & 2 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
from cirrus.google_cloud.utils import get_valid_service_account_id_for_user


class MockResponse:
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code

def json(self):
return self.json_data


def get_test_cloud_manager():
project_id = "test_project"
manager = GoogleCloudManager(project_id)
Expand Down Expand Up @@ -85,13 +94,15 @@ def mock_get_service_accounts_from_group(


def mock_get_service_account(test_cloud_manager, primary_service_account):

test_cloud_manager.get_service_account = MagicMock()
test_cloud_manager.get_service_account.return_value = {

test_cloud_manager.get_service_account.return_value = MockResponse({
"name": "",
"projectId": "",
"uniqueId": "",
"email": primary_service_account,
"displayName": "",
"etag": "",
"oauth2ClientId": "",
}
}, 200)
2 changes: 1 addition & 1 deletion test/test_google_cloud_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def test_get_service_account_valid(test_cloud_manager):
200, {"uniqueId": "123"})

# Call #
service_account = test_cloud_manager.get_service_account("123")
service_account = test_cloud_manager.get_service_account("123").json()

# Test #
assert service_account["uniqueId"] == "123"
Expand Down