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

marketplace: change api call to return array of customer ids (PROJQUAY-7129) #1

Closed
wants to merge 1 commit into from
Closed
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
31 changes: 17 additions & 14 deletions util/marketplace.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ def get_account_number(self, user):
email = user.email
account_number = entitlements.get_web_customer_id(user.id)
if account_number is None:
account_number = self.lookup_customer_id(email)
if account_number:
account_numbers = self.lookup_customer_id(email)
if account_numbers:
# store in database for next lookup
entitlements.save_web_customer_id(user, account_number)
for account_number in account_numbers:
entitlements.save_web_customer_id(user, account_number)
account_number = account_numbers[0]
return account_number

def lookup_customer_id(self, email):
"""
Send request to internal api for customer id (ebs acc number)
Send request to internal api for customer id (web customer id)
"""
request_body_dict = {
"by": {"emailStartsWith": email},
Expand Down Expand Up @@ -68,14 +70,15 @@ def lookup_customer_id(self, email):
if not info:
logger.debug("request to %s did not return any data", self.user_endpoint)
return None

customer_ids = []
for account in info:
if account["accountRelationships"][0]["account"]["type"] == "person":
customer_id = account["accountRelationships"][0]["account"].get("id")
# convert str response from api to int value
if customer_id.isdigit():
customer_id = int(customer_id)
return customer_id
return None
customer_id = account["accountRelationships"][0]["account"].get("id")
# convert str response from api to int value
if customer_id.isdigit():
customer_id = int(customer_id)
customer_ids.append(customer_id)
return customer_ids


class RedHatSubscriptionApi(object):
Expand Down Expand Up @@ -326,11 +329,11 @@ class FakeUserApi(RedHatUserApi):

def lookup_customer_id(self, email):
if email == TEST_USER["email"]:
return TEST_USER["account_number"]
return [TEST_USER["account_number"]]
if email == FREE_USER["email"]:
return FREE_USER["account_number"]
return [FREE_USER["account_number"]]
if email == STRIPE_USER["email"]:
return STRIPE_USER["account_number"]
return [STRIPE_USER["account_number"]]
return None


Expand Down
48 changes: 1 addition & 47 deletions util/test/test_marketplace.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,48 +53,6 @@
},
]

mocked_organization_only_response = [
{
"id": "12345678",
"accountRelationships": [
{
"emails": [{"address": "example@example.com", "status": "enabled"}],
"accountId": "fakeid",
"startDate": "2022-09-19T04:18:19.228Z",
"id": "fakeid",
"account": {
"id": "222222222",
"cdhPartyNumber": "11111111",
"ebsAccountNumber": "102030",
"name": "Red Hat",
"displayName": "Red Hat",
"status": "enabled",
"type": "organization",
},
}
],
},
{
"id": "87654321",
"accountRelationships": [
{
"emails": [{"address": "example@example.com", "status": "enabled"}],
"accountId": "fakeid",
"startDate": "2022-09-20T14:31:09.974Z",
"id": "fakeid",
"account": {
"id": "fakeid",
"cdhPartyNumber": "0000000",
"ebsAccountNumber": "1234567",
"name": "Test Org",
"status": "enabled",
"type": "organization",
},
}
],
},
]

mocked_subscription_response = [
{
"id": 1,
Expand Down Expand Up @@ -219,11 +177,7 @@ def test_user_lookup(self, requests_mock):
requests_mock.return_value.content = json.dumps(mocked_user_service_response)

customer_id = user_api.lookup_customer_id("example@example.com")
assert customer_id == 000000000

requests_mock.return_value.content = json.dumps(mocked_organization_only_response)
customer_id = user_api.lookup_customer_id("example@example.com")
assert customer_id is None
assert customer_id == [222222222, 00000000]

@patch("requests.request")
def test_subscription_lookup(self, requests_mock):
Expand Down
52 changes: 27 additions & 25 deletions workers/reconciliationworker.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,33 +53,34 @@ def _perform_reconciliation(self, user_api, marketplace_api):
)

# check against user api
customer_id = user_api.lookup_customer_id(email)
logger.debug("Found %s number for %s", str(customer_id), email)

if model_customer_id is None and customer_id:
logger.debug("Saving new customer id %s for %s", customer_id, user.username)
entitlements.save_web_customer_id(user, customer_id)
elif model_customer_id != customer_id:
# what is in the database differs from the service
# take the service and store in the database instead
if customer_id:
logger.debug(
"Reconciled differing ids for %s, changing from %s to %s",
user.username,
model_customer_id,
customer_id,
)
entitlements.update_web_customer_id(user, customer_id)
else:
customer_ids = user_api.lookup_customer_id(email)
if customer_ids is None:
logger.debug("No web customer ids found for %s", email)
if model_customer_id:
# user does not have a web customer id from api and should be removed from table
logger.debug(
"Removing conflicting id %s for %s", model_customer_id, user.username
)
entitlements.remove_web_customer_id(user, model_customer_id)
elif customer_id is None:
logger.debug("User %s does not have an account number", user.username)
continue

logger.debug("Found %s number for %s", str(customer_ids), email)

for customer_id in customer_ids:
if model_customer_id is None and customer_id:
logger.debug("Saving new customer id %s for %s", customer_id, user.username)
entitlements.save_web_customer_id(user, customer_id)
elif model_customer_id != customer_id:
# what is in the database differs from the service
# take the service and store in the database instead
logger.debug(
"Reconciled differing ids for %s, changing from %s to %s",
user.username,
model_customer_id,
customer_ids,
)
entitlements.update_web_customer_id(user, customer_id)

# check if we need to create a subscription for customer in RH marketplace
try:
stripe_customer = stripe.Customer.retrieve(user.stripe_id)
Expand All @@ -95,11 +96,12 @@ def _perform_reconciliation(self, user_api, marketplace_api):
if plan is None:
continue
if plan.get("rh_sku") == sku_id:
subscription = marketplace_api.lookup_subscription(customer_id, sku_id)
if subscription is None:
logger.debug("Found %s to create for %s", sku_id, user.username)
marketplace_api.create_entitlement(customer_id, sku_id)
break
for customer_id in customer_ids:
subscription = marketplace_api.lookup_subscription(customer_id, sku_id)
if subscription is None:
logger.debug("Found %s to create for %s", sku_id, user.username)
marketplace_api.create_entitlement(customer_id, sku_id)
break
else:
logger.debug("User %s does not have a stripe subscription", user.username)

Expand Down
2 changes: 1 addition & 1 deletion workers/test/test_reconciliationworker.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_reconcile_different_ids(initialized_db):

new_id = model.entitlements.get_web_customer_id(test_user.id)
assert new_id != 12345
assert new_id == marketplace_users.lookup_customer_id(test_user.email)
assert [new_id] == marketplace_users.lookup_customer_id(test_user.email)

# make sure it will remove account numbers from db that do not belong
with patch.object(marketplace_users, "lookup_customer_id") as mock:
Expand Down
Loading