Skip to content

Commit 147491b

Browse files
committed
refactor:drop _get_missing_env_properites, add _get_missing_session_attributes
1 parent c71c67d commit 147491b

2 files changed

Lines changed: 33 additions & 58 deletions

File tree

src/pybritive/britive_cli.py

Lines changed: 32 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -465,38 +465,20 @@ def list_environments(self):
465465
data.append(row)
466466
self.print(data, ignore_silent=True)
467467

468-
# temporary fix till the new API is updated to return `profileEnvironmentProperties`
469-
def _get_missing_env_properties(
470-
self, app_id: str, app_type: str, env_id: str, profile_id: str, from_cache_command: bool
471-
) -> dict:
472-
if app_type.lower() == 'kubernetes' and (from_cache_command or self.config.auto_refresh_kube_config()):
473-
if not self.listed_profiles:
474-
self.listed_profiles = self.b.my_access.list_profiles()
475-
return next(
476-
(
477-
env['profileEnvironmentProperties']
478-
for app in self.listed_profiles
479-
if app['appContainerId'] == app_id
480-
for profile in app.get('profiles', [])
481-
if profile['profileId'] == profile_id
482-
for env in profile.get('environments', [])
483-
if env['environmentId'] == env_id
484-
),
485-
{},
486-
)
487-
return {}
488-
489-
def _fetch_session_attributes_for_profiles(self, accesses):
490-
session_attributes = {}
491-
unique_profile_ids = {profile_id for _, _, profile_id in accesses}
492-
for profile_id in unique_profile_ids:
493-
try:
494-
session_attrs = self.b.application_management.profiles.session_attributes.list(profile_id=profile_id)
495-
session_attributes[profile_id] = session_attrs
496-
except Exception:
497-
session_attributes[profile_id] = {}
498-
499-
return session_attributes
468+
# temporary fix till the new API is updated to return `sessionAttributes`
469+
def _get_missing_session_attributes(self, app_id: str, profile_id: str) -> dict:
470+
if not self.listed_profiles:
471+
self.listed_profiles = self.b.my_access.list_profiles()
472+
return next(
473+
(
474+
profile['sessionAttributes']
475+
for app in self.listed_profiles
476+
if app['appContainerId'] == app_id
477+
for profile in app.get('profiles', [])
478+
if profile['profileId'] == profile_id
479+
),
480+
[],
481+
)
500482

501483
def _set_available_profiles(self, from_cache_command=False, profile_type: Optional[str] = None):
502484
if not self.available_profiles:
@@ -515,31 +497,29 @@ def _set_available_profiles(self, from_cache_command=False, profile_type: Option
515497
accesses = [
516498
([a['appContainerId'], a['environmentId'], a['papId']]) for a in access_data.get('accesses', [])
517499
]
518-
session_attributes = self._fetch_session_attributes_for_profiles(accesses)
519500
access_output = []
520501
for app_id, env_id, profile_id in accesses:
521502
app = apps[app_id]
522503
env = envs[env_id]
523504
profile = profiles[profile_id]
524505
row = {
525-
'app_name': app['catalogAppDisplayName'],
506+
'2_part_profile_format_allowed': app['requiresHierarchicalModel'],
507+
'app_description': app['appDescription'],
526508
'app_id': app_id,
509+
'app_name': app['catalogAppDisplayName'],
527510
'app_type': app['catalogAppName'],
528-
'app_description': app['appDescription'],
529-
'env_name': env['environmentName'],
511+
'env_description': env['environmentDescription'],
530512
'env_id': env_id,
513+
'env_name': env['environmentName'],
514+
'env_properties': env['profileEnvironmentProperties'],
531515
'env_short_name': env['alternateEnvironmentName'],
532-
'env_description': env['environmentDescription'],
533-
'profile_name': profile['papName'],
534-
'profile_id': profile_id,
535516
'profile_allows_console': app.get('consoleAccess', False),
536517
'profile_allows_programmatic': app.get('programmaticAccess', False),
537518
'profile_description': profile['papDescription'],
538-
'session_attributes': session_attributes.get(profile_id, []),
539-
'2_part_profile_format_allowed': app['requiresHierarchicalModel'],
540-
'env_properties': env['profileEnvironmentProperties']
541-
or self._get_missing_env_properties(
542-
app_id, app['catalogAppName'], env_id, profile_id, from_cache_command
519+
'profile_id': profile_id,
520+
'profile_name': profile['papName'],
521+
'session_attributes': profile.get(
522+
'sessionAttributes', self._get_missing_session_attributes(app_id, profile_id)
543523
),
544524
}
545525
if row not in access_output:
@@ -551,25 +531,23 @@ def _set_available_profiles(self, from_cache_command=False, profile_type: Option
551531
else:
552532
profiles = self.b.my_resources.list(size=resource_limit)
553533
profiles = profiles['data']
554-
session_attributes = self._fetch_session_attributes_for_profiles(accesses)
555534
for item in profiles:
556535
row = {
557-
'app_name': None,
536+
'2_part_profile_format_allowed': False,
537+
'app_description': None,
558538
'app_id': None,
539+
'app_name': None,
559540
'app_type': 'Resources',
560-
'app_description': None,
561-
'env_name': item['resourceName'],
541+
'env_description': None,
562542
'env_id': item['resourceId'],
543+
'env_name': item['resourceName'],
544+
'env_properties': item.get('resourceLabels', {}),
563545
'env_short_name': item['resourceName'],
564-
'env_description': None,
565-
'profile_name': item['profileName'],
566-
'profile_id': item['profileId'],
567546
'profile_allows_console': False,
568547
'profile_allows_programmatic': True,
569-
'session_attributes': session_attributes.get(profile_id, []),
570548
'profile_description': None,
571-
'2_part_profile_format_allowed': False,
572-
'env_properties': item.get('resourceLabels', {}),
549+
'profile_id': item['profileId'],
550+
'profile_name': item['profileName'],
573551
}
574552
data.append(row)
575553
self.available_profiles = data

src/pybritive/helpers/kube_config_builder.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ def parse_profiles(profiles, aliases):
7878
escaped_profile_str = f'{app}/{env}/{pro}'.lower()
7979
alias = aliases.get(escaped_profile_str, None)
8080
assigned_aliases.append(alias)
81-
keys_to_remove = ['id', 'attributeSchemaId', 'transitive', 'sessionAttributeType']
82-
[list(map(attr.pop, keys_to_remove)) for attr in profile['session_attributes']]
8381

8482
cluster_names[env_profile] = {
8583
'apps': [],
@@ -140,7 +138,6 @@ def build_tenant_config(tenant, cluster_names, username, cli: BritiveCli):
140138

141139
cert = details['cert']
142140
url = details['url']
143-
session_attributes = details['session_attributes']
144141

145142
if not valid_cert(cert=cert, profile=details['profile'], cli=cli):
146143
continue
@@ -168,7 +165,7 @@ def build_tenant_config(tenant, cluster_names, username, cli: BritiveCli):
168165
'context': {
169166
'cluster': f'{tenant}-{name}',
170167
'user': username,
171-
**{attr['mappingName']: attr['attributeValue'] for attr in session_attributes},
168+
**{attr['mappingName']: attr['attributeValue'] for attr in details['session_attributes']},
172169
},
173170
}
174171
)

0 commit comments

Comments
 (0)