Skip to content

Commit

Permalink
Merge pull request RedHatInsights#1457 from astrozzc/flip_flag
Browse files Browse the repository at this point in the history
Add internal endpoint to set tenant ready flag
  • Loading branch information
astrozzc authored Jan 23, 2025
2 parents 5a0a337 + d90db23 commit 318ea33
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
89 changes: 89 additions & 0 deletions rbac/internal/specs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,95 @@
}
}
},
"/api/utils/set_tenant_ready/": {
"get": {
"tags": [
"Set tenant ready"
],
"summary": "View count of tenants with ready flag false",
"description": "View count of tenants with ready flag false.",
"operationId": "ViewTenantReadyFalse",
"responses": {
"200": {
"description": "Total of tenants not set to be ready."
},
"405": {
"description": "Invalid method.",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"500": {
"description": "Unexpected Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
},
"post": {
"tags": [
"Set tenant ready"
],
"summary": "Set ready flag of tenants to true",
"description": "Set ready flag of tenants to true.",
"operationId": "SetTenantReady",
"parameters": [
{
"name": "max_expected",
"in": "query",
"required": true,
"description": "Defines the max number of not ready tenants to be updated.",
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "0 tenant with ready flag equal to false."
},
"400":{
"description": "Invalid request, must supply the 'max_expected' query parameter.",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"405": {
"description": "Invalid method.",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"500": {
"description": "Unexpected Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
},
"/openapi.json": {
"get": {
"tags": [
Expand Down
1 change: 1 addition & 0 deletions rbac/internal/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
path("api/cars/expire/", views.car_expiry),
path("api/sentry_debug/", views.trigger_error),
path("api/utils/sync_schemas/", views.sync_schemas),
path("api/utils/set_tenant_ready/", views.set_tenant_ready),
path("api/utils/populate_tenant_account_id/", views.populate_tenant_account_id),
path("api/utils/invalid_default_admin_groups/", views.invalid_default_admin_groups),
path("api/utils/ocm_performance/", views.ocm_performance),
Expand Down
34 changes: 34 additions & 0 deletions rbac/internal/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,40 @@ def car_expiry(request):
return HttpResponse('Invalid method, only "POST" is allowed.', status=405)


def set_tenant_ready(request):
"""View/set Tenant with ready flag true.
GET /_private/api/utils/set_tenant_ready/
POST /_private/api/utils/set_tenant_ready/?max_expected=1234
"""
tenant_qs = Tenant.objects.exclude(tenant_name="public").filter(ready=False)
if request.method == "GET":
tenant_count = tenant_qs.count()
return HttpResponse(f"Total of {tenant_count} tenants not set to be ready.", status=200)

if request.method == "POST":
if not destructive_ok("api"):
return HttpResponse("Destructive operations disallowed.", status=400)
logger.info("Setting flag ready to true for tenants.")
max_expected = request.GET.get("max_expected")
if not max_expected:
return HttpResponse("Please specify a max_expected value.", status=400)
with transaction.atomic():
prev_count = tenant_qs.count()
if prev_count > int(max_expected):
return HttpResponse(
f"Total of {prev_count} tenants exceeds max_expected of {max_expected}.",
status=400,
)
tenant_qs.update(ready=True)
return HttpResponse(
f"Total of {prev_count} tenants has been updated. "
f"{tenant_qs.count()} tenant with ready flag equal to false.",
status=200,
)
return HttpResponse('Invalid method, only "POST" is allowed.', status=405)


def populate_tenant_account_id(request):
"""View method for populating Tenant#account_id values.
Expand Down
19 changes: 19 additions & 0 deletions tests/internal/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,25 @@ def test_populate_tenant_account_id(self, populate_mock):
"Tenant objects account_id values being updated in background worker.",
)

@override_settings(INTERNAL_DESTRUCTIVE_API_OK_UNTIL=valid_destructive_time())
def test_setting_ready_flag_for_tenants(self):
"""Test that we can get the total of not ready tenants and set them to true."""
Tenant.objects.create(tenant_name="acct_not_ready", org_id="1234")
response = self.client.get(f"/_private/api/utils/set_tenant_ready/", **self.request.META)

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.content.decode(), "Total of 1 tenants not set to be ready.")

response = self.client.post(f"/_private/api/utils/set_tenant_ready/", **self.request.META)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

response = self.client.post(f"/_private/api/utils/set_tenant_ready/?max_expected=2", **self.request.META)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
response.content.decode(), "Total of 1 tenants has been updated. 0 tenant with ready flag equal to false."
)
self.assertEqual(Tenant.objects.filter(ready=False).count(), 0)

@patch("api.tasks.populate_tenant_account_id_in_worker.delay")
def test_populate_tenant_account_id_get_failure(self, populate_mock):
"""Test that we get a bad request for not using POST method."""
Expand Down

0 comments on commit 318ea33

Please sign in to comment.