Skip to content

Commit

Permalink
W-15805907: assign_permission_set_licenses accepts the api_names in e…
Browse files Browse the repository at this point in the history
…ither developerName or the PermissionSetLicenseKey (#3798)

To handle the compatibility issues and ensure smooth functionality for
both `get_available_permission_set_licenses` and
`assign_permission_set_licenses`, you can make the following
adjustments:

1. **Revert `get_available_permission_set_licenses` to Return
`PermissionSetLicenseKey`:**
   - This will maintain backward compatibility.

2. **Update `assign_permission_set_licenses` to Accept Both
`DeveloperName` and `PermissionSetLicenseKey`:**
- Modify the function to handle input as either `DeveloperName` or
`PermissionSetLicenseKey`, ensuring it can correctly interpret and
process both formats. This adjustment will resolve issues with names
like `alpha.beta` where the `DeveloperName` is `alpha_beta`. Accepts
api_name as `alpha_beta` or `alpha.beta` and assigns them correctly

---------

Co-authored-by: James Estevez <jestevez@salesforce.com>
  • Loading branch information
lakshmi2506 and jstvz committed Jun 14, 2024
1 parent 2f8a94e commit 8da8c67
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
4 changes: 2 additions & 2 deletions cumulusci/tasks/preflight/licenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ def _run_task(self):
class GetAvailablePermissionSetLicenses(BaseSalesforceApiTask):
def _run_task(self):
self.return_values = [
result["DeveloperName"]
result["PermissionSetLicenseKey"]
for result in self.sf.query(
"SELECT DeveloperName FROM PermissionSetLicense"
"SELECT PermissionSetLicenseKey FROM PermissionSetLicense"
)["records"]
]
licenses = "\n".join(self.return_values)
Expand Down
6 changes: 3 additions & 3 deletions cumulusci/tasks/preflight/tests/test_licenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ def test_psl_preflight(self):
task._init_api.return_value.query.return_value = {
"totalSize": 2,
"records": [
{"DeveloperName": "TEST1"},
{"DeveloperName": "TEST2"},
{"PermissionSetLicenseKey": "TEST1"},
{"PermissionSetLicenseKey": "TEST2"},
],
}
task()

task._init_api.return_value.query.assert_called_once_with(
"SELECT DeveloperName FROM PermissionSetLicense"
"SELECT PermissionSetLicenseKey FROM PermissionSetLicense"
)
assert task.return_values == ["TEST1", "TEST2"]

Expand Down
27 changes: 25 additions & 2 deletions cumulusci/tasks/salesforce/users/permsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def _process_composite_results(self, api_results):

class AssignPermissionSetLicenses(AssignPermissionSets):
task_docs = """
Assigns Permission Set Licenses whose Developer Names are in ``api_names`` to either the default org user or the user whose Alias is ``user_alias``. This task skips assigning Permission Set Licenses that are already assigned.
Assigns Permission Set Licenses whose Developer Names or PermissionSetLicenseKey are in ``api_names`` to either the default org user or the user whose Alias is ``user_alias``. This task skips assigning Permission Set Licenses that are already assigned.
Permission Set Licenses are usually associated with a Permission Set, and assigning the Permission Set usually assigns the associated Permission Set License automatically. However, in non-namespaced developer scratch orgs, assigning the associated Permission Set may not automatically assign the Permission Set License, and this task will ensure the Permission Set Licenses are assigned.
"""
Expand All @@ -174,12 +174,35 @@ class AssignPermissionSetLicenses(AssignPermissionSets):
}

permission_name = "PermissionSetLicense"
permission_name_field = "DeveloperName"
permission_name_field = ["DeveloperName", "PermissionSetLicenseKey"]
permission_label = "Permission Set License"
assignment_name = "PermissionSetLicenseAssign"
assignment_lookup = "PermissionSetLicenseId"
assignment_child_relationship = "PermissionSetLicenseAssignments"

def _get_perm_ids(self):
perms_by_ids = {}
api_names = "', '".join(self.options["api_names"])
perms = self.sf.query(
f"SELECT Id,{self.permission_name_field[0]},{self.permission_name_field[1]} FROM {self.permission_name} WHERE {self.permission_name_field[0]} IN ('{api_names}') OR {self.permission_name_field[1]} IN ('{api_names}')"
)
for p in perms["records"]:
if p[self.permission_name_field[0]] in self.options["api_names"]:
perms_by_ids[p["Id"]] = p[self.permission_name_field[0]]
else:
perms_by_ids[p["Id"]] = p[self.permission_name_field[1]]

missing_perms = [
api_name
for api_name in self.options["api_names"]
if api_name not in perms_by_ids.values()
]
if missing_perms:
raise CumulusCIException(
f"The following {self.permission_label}s were not found: {', '.join(missing_perms)}."
)
return perms_by_ids


class AssignPermissionSetGroups(AssignPermissionSets):
task_docs = """
Expand Down
24 changes: 16 additions & 8 deletions cumulusci/tasks/salesforce/users/tests/test_permsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,19 +447,21 @@ def test_create_permsetlicense(self):
)
responses.add(
method="GET",
url=f"{task.org_config.instance_url}/services/data/v{CURRENT_SF_API_VERSION}/query/?q=SELECT+Id%2CDeveloperName+FROM+PermissionSetLicense+WHERE+DeveloperName+IN+%28%27PermSetLicense1%27%2C+%27PermSetLicense2%27%29",
url=f"{task.org_config.instance_url}/services/data/v{CURRENT_SF_API_VERSION}/query/?q=SELECT+Id%2CDeveloperName%2CPermissionSetLicenseKey+FROM+PermissionSetLicense+WHERE+DeveloperName+IN+%28%27PermSetLicense1%27%2C+%27PermSetLicense2%27%29+OR+PermissionSetLicenseKey+IN+%28%27PermSetLicense1%27%2C+%27PermSetLicense2%27%29",
status=200,
json={
"done": True,
"totalSize": 1,
"totalSize": 2,
"records": [
{
"Id": "0PL000000000000",
"DeveloperName": "PermSetLicense1",
"PermissionSetLicenseKey": "PermSetLicense1",
},
{
"Id": "0PL000000000001",
"DeveloperName": "PermSetLicense2",
"PermissionSetLicenseKey": "PermSetLicense1",
},
],
},
Expand Down Expand Up @@ -516,19 +518,21 @@ def test_create_permsetlicense__no_assignments(self):
)
responses.add(
method="GET",
url=f"{task.org_config.instance_url}/services/data/v{CURRENT_SF_API_VERSION}/query/?q=SELECT+Id%2CDeveloperName+FROM+PermissionSetLicense+WHERE+DeveloperName+IN+%28%27PermSetLicense1%27%2C+%27PermSetLicense2%27%29",
url=f"{task.org_config.instance_url}/services/data/v{CURRENT_SF_API_VERSION}/query/?q=SELECT+Id%2CDeveloperName%2CPermissionSetLicenseKey+FROM+PermissionSetLicense+WHERE+DeveloperName+IN+%28%27PermSetLicense1%27%2C+%27PermSetLicense2%27%29+OR+PermissionSetLicenseKey+IN+%28%27PermSetLicense1%27%2C+%27PermSetLicense2%27%29",
status=200,
json={
"done": True,
"totalSize": 1,
"totalSize": 2,
"records": [
{
"Id": "0PL000000000000",
"DeveloperName": "PermSetLicense1",
"PermissionSetLicenseKey": "PermSet.License1",
},
{
"Id": "0PL000000000001",
"DeveloperName": "PermSetLicense2",
"PermissionSetLicenseKey": "PermSet.License2",
},
],
},
Expand Down Expand Up @@ -597,23 +601,26 @@ def test_create_permsetlicense__alias(self):
)
responses.add(
method="GET",
url=f"{task.org_config.instance_url}/services/data/v{CURRENT_SF_API_VERSION}/query/?q=SELECT+Id%2CDeveloperName+FROM+PermissionSetLicense+WHERE+DeveloperName+IN+%28%27PermSetLicense1%27%2C+%27PermSetLicense2%27%29",
url=f"{task.org_config.instance_url}/services/data/v{CURRENT_SF_API_VERSION}/query/?q=SELECT+Id%2CDeveloperName%2CPermissionSetLicenseKey+FROM+PermissionSetLicense+WHERE+DeveloperName+IN+%28%27PermSetLicense1%27%2C+%27PermSetLicense2%27%29+OR+PermissionSetLicenseKey+IN+%28%27PermSetLicense1%27%2C+%27PermSetLicense2%27%29",
status=200,
json={
"done": True,
"totalSize": 1,
"totalSize": 2,
"records": [
{
"Id": "0PL000000000000",
"DeveloperName": "PermSetLicense1",
"PermissionSetLicenseKey": "PermSetLicense1",
},
{
"Id": "0PL000000000001",
"DeveloperName": "PermSetLicense2",
"PermissionSetLicenseKey": "PermSetLicense2",
},
],
},
)

responses.add(
method="POST",
url=f"{task.org_config.instance_url}/services/data/v{CURRENT_SF_API_VERSION}/sobjects/PermissionSetLicenseAssign/",
Expand Down Expand Up @@ -698,7 +705,7 @@ def test_create_permsetlicense_raises(self):
)
responses.add(
method="GET",
url=f"{task.org_config.instance_url}/services/data/v{CURRENT_SF_API_VERSION}/query/?q=SELECT+Id%2CDeveloperName+FROM+PermissionSetLicense+WHERE+DeveloperName+IN+%28%27PermSetLicense1%27%2C+%27PermSetLicense2%27%2C+%27PermSetLicense3%27%29",
url=f"{task.org_config.instance_url}/services/data/v{CURRENT_SF_API_VERSION}/query/?q=SELECT+Id%2CDeveloperName%2CPermissionSetLicenseKey+FROM+PermissionSetLicense+WHERE+DeveloperName+IN+%28%27PermSetLicense1%27%2C+%27PermSetLicense2%27%2C+%27PermSetLicense3%27%29+OR+PermissionSetLicenseKey+IN+%28%27PermSetLicense1%27%2C+%27PermSetLicense2%27%2C+%27PermSetLicense3%27%29",
status=200,
json={
"done": True,
Expand All @@ -707,15 +714,16 @@ def test_create_permsetlicense_raises(self):
{
"Id": "0PL000000000000",
"DeveloperName": "PermSetLicense1",
"PermissionSetLicenseKey": "PermSetLicense1",
},
{
"Id": "0PL000000000001",
"DeveloperName": "PermSetLicense2",
"PermissionSetLicenseKey": "PermSetLicense2",
},
],
},
)

with pytest.raises(CumulusCIException):
task()

Expand Down

0 comments on commit 8da8c67

Please sign in to comment.