Skip to content

Commit

Permalink
✨ oum: replace ocm-org-ids option with query
Browse files Browse the repository at this point in the history
  • Loading branch information
chassing committed Nov 21, 2024
1 parent a80c5cc commit 2c53fdd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
10 changes: 1 addition & 9 deletions reconcile/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3356,29 +3356,21 @@ def status_page_maintenances(ctx):
required=False,
envvar="OCM_ENV",
)
@click.option(
"--ocm-org-ids",
help="A comma seperated list of OCM organization IDs the integration should operator on. If none is specified, all organizations are considered.",
required=False,
envvar="OCM_ORG_IDS",
)
@click.option(
"--group-provider",
help="A group provider spec is the form of <provider-name>:<provider-type>:<provider-args>.",
required=False,
multiple=True,
)
@click.pass_context
def ocm_standalone_user_management(ctx, ocm_env, ocm_org_ids, group_provider):
def ocm_standalone_user_management(ctx, ocm_env, group_provider):
from reconcile.oum.base import OCMUserManagementIntegrationParams
from reconcile.oum.standalone import OCMStandaloneUserManagementIntegration

ocm_organization_ids = set(ocm_org_ids.split(",")) if ocm_org_ids else None
run_class_integration(
OCMStandaloneUserManagementIntegration(
OCMUserManagementIntegrationParams(
ocm_environment=ocm_env,
ocm_organization_ids=ocm_organization_ids,
group_provider_specs=group_provider,
),
),
Expand Down
25 changes: 20 additions & 5 deletions reconcile/oum/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
query as ocm_environment_query,
)
from reconcile.gql_definitions.fragments.ocm_environment import OCMEnvironment
from reconcile.gql_definitions.oum.organizations import (
OpenShiftClusterManagerV1,
)
from reconcile.gql_definitions.oum.organizations import (
query as ocm_orgs_query,
)
from reconcile.oum.metrics import (
OCMUserManagementOrganizationActionCounter as ReconcileActionCounter,
)
Expand All @@ -35,6 +41,7 @@
gql,
metrics,
)
from reconcile.utils.disabled_integrations import integration_is_enabled
from reconcile.utils.ocm.base import (
CAPABILITY_MANAGE_CLUSTER_ADMIN,
OCMClusterGroupId,
Expand All @@ -56,7 +63,6 @@

class OCMUserManagementIntegrationParams(PydanticRunParams):
ocm_environment: str | None = None
ocm_organization_ids: set[str] | None = None
group_provider_specs: list[str]


Expand Down Expand Up @@ -87,7 +93,7 @@ def get_ocm_environments(self) -> list[OCMEnvironment]:

@abstractmethod
def get_user_mgmt_config_for_ocm_env(
self, ocm_env: OCMEnvironment, org_ids: set[str] | None
self, ocm_env: OCMEnvironment
) -> dict[str, OrganizationUserManagementConfiguration]:
"""
Discover cluster user mgmt configurations in the given OCM environment.
Expand All @@ -101,9 +107,7 @@ def reconcile_ocm_environment(self, dry_run: bool, ocm_env: OCMEnvironment) -> N
Processes user management configuration for all OCM organizations
within the given OCM environment.
"""
org_configs = self.get_user_mgmt_config_for_ocm_env(
ocm_env, self.params.ocm_organization_ids
)
org_configs = self.get_user_mgmt_config_for_ocm_env(ocm_env)

ocm_api = init_ocm_base_client(ocm_env, self.secret_reader)
for org_id, org_config in org_configs.items():
Expand Down Expand Up @@ -385,3 +389,14 @@ def get_group_providers(
else:
raise ValueError(f"unknown group member provider type {provider_type}")
return providers


def get_ocm_orgs_from_env(
env_name: str, int_name: str
) -> list[OpenShiftClusterManagerV1]:
orgs = ocm_orgs_query(gql.get_api().query).organizations
return [
org
for org in orgs or []
if integration_is_enabled(int_name, org) and org.environment.name == env_name
]
8 changes: 5 additions & 3 deletions reconcile/oum/standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from datetime import timedelta

from reconcile.gql_definitions.fragments.ocm_environment import OCMEnvironment
from reconcile.oum.base import OCMUserManagementIntegration
from reconcile.oum.base import OCMUserManagementIntegration, get_ocm_orgs_from_env
from reconcile.oum.labelset import build_cluster_config_from_labels
from reconcile.oum.models import (
ClusterError,
Expand Down Expand Up @@ -35,12 +35,14 @@ def name(self) -> str:
return "ocm-standalone-user-management"

def get_user_mgmt_config_for_ocm_env(
self, ocm_env: OCMEnvironment, org_ids: set[str] | None
self, ocm_env: OCMEnvironment
) -> dict[str, OrganizationUserManagementConfiguration]:
ocm_api = init_ocm_base_client(ocm_env, self.secret_reader)
clusters_by_org = discover_clusters(
ocm_api=ocm_api,
org_ids=org_ids,
org_ids={
org.org_id for org in get_ocm_orgs_from_env(ocm_env.name, self.name)
},
)
configs: dict[str, OrganizationUserManagementConfiguration] = {}
for org_id, org_clusters in clusters_by_org.items():
Expand Down
2 changes: 1 addition & 1 deletion reconcile/test/ocm/oum/test_oum_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ def test_build_spec_from_config_cluster_admin_with_capability(

class MockOCMUserManagementIntegration(OCMUserManagementIntegration):
def get_user_mgmt_config_for_ocm_env(
self, ocm_env: OCMEnvironment, org_ids: set[str] | None
self, ocm_env: OCMEnvironment
) -> dict[str, OrganizationUserManagementConfiguration]:
return {}

Expand Down

0 comments on commit 2c53fdd

Please sign in to comment.