Skip to content

Commit

Permalink
wcaOnPrem: raise a 503 with feature_not_released_yet for playbook ExpGen
Browse files Browse the repository at this point in the history
Raise an err 503 with `feature_not_released_yet` code when the client tries
to access the ExpGen endpoint of a OnPrem server.
  • Loading branch information
goneri committed May 28, 2024
1 parent e2d8e12 commit 1c4c4db
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
6 changes: 6 additions & 0 deletions ansible_wisdom/ai/api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ class ServiceUnavailable(BaseWisdomAPIException):
default_detail = 'An error occurred attempting to complete the request.'


class FeatureNotReleasedYet(BaseWisdomAPIException):
status_code = 503
default_code = 'feature_not_released_yet'
default_detail = 'The service in not available with your offer.'


class InternalServerError(BaseWisdomAPIException):
status_code = 500
default_code = 'internal_server'
Expand Down
9 changes: 9 additions & 0 deletions ansible_wisdom/ai/api/model_client/wca_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from requests.auth import HTTPBasicAuth
from requests.exceptions import HTTPError

from ansible_ai_connect.ai.api.exceptions import FeatureNotReleasedYet
from ansible_ai_connect.ai.api.formatter import (
get_task_names_from_prompt,
strip_task_preamble_from_multi_task_prompt,
Expand Down Expand Up @@ -581,3 +582,11 @@ def self_test(self) -> HealthCheckSummary:
)

return summary

def generate_playbook(
self, request, text: str = "", create_outline: bool = False, outline: str = ""
) -> tuple[str, str]:
raise FeatureNotReleasedYet

def explain_playbook(self, request, content: str) -> str:
raise FeatureNotReleasedYet
36 changes: 36 additions & 0 deletions ansible_wisdom/ai/api/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,14 @@
DataSource,
)
from ansible_ai_connect.ai.api.utils import segment_analytics_telemetry
from ansible_ai_connect.main.tests.test_views import create_user_with_provider
from ansible_ai_connect.organizations.models import Organization
from ansible_ai_connect.test_utils import (
WisdomAppsBackendMocking,
WisdomLogAwareMixin,
WisdomServiceLogAwareTestCase,
)
from ansible_ai_connect.users.constants import USER_SOCIAL_AUTH_PROVIDER_OIDC

DEFAULT_SUGGESTION_ID = uuid.uuid4()

Expand Down Expand Up @@ -2744,3 +2746,37 @@ def test_service_unavailable(self, invoke):
with self.assertRaises(Exception):
r = self.client.post(reverse('generations'), payload, format='json')
self.assertEqual(r.status_code, HTTPStatus.SERVICE_UNAVAILABLE)


@modify_settings()
@override_settings(WCA_SECRET_BACKEND_TYPE='dummy')
@override_settings(WCA_SECRET_DUMMY_SECRETS='1981:valid')
@override_settings(ANSIBLE_AI_MODEL_MESH_API_TYPE="wca-onprem")
@override_settings(ANSIBLE_WCA_USERNAME='bo')
@override_settings(ANSIBLE_AI_MODEL_MESH_API_KEY='my-secret-key')
class TestWcaOnprem503(WisdomAppsBackendMocking):

def setUp(self):
super().setUp()
self.username = 'u' + "".join(random.choices(string.digits, k=5))
self.user = create_user_with_provider(USER_SOCIAL_AUTH_PROVIDER_OIDC, rh_org_id=1981)
self.user.rh_user_has_seat = True
self.user.rh_org_has_subscription = True
self.user.organization.is_subscription_check_should_be_bypassed = True
self.user.organization.save()
self.user.save()

def tearDown(self):
Organization.objects.filter(id=1981).delete()
self.user.delete()
super().tearDown()

@override_settings(ANSIBLE_AI_ENABLE_TECH_PREVIEW=False)
def test_feature_not_enabled_yet(self):
payload = {
"content": "Install Wordpress on a RHEL9",
"explanationId": str(uuid.uuid4()),
}
self.client.force_login(user=self.user)
r = self.client.post(reverse('explanations'), payload)
self.assertEqual(r.status_code, 503)
3 changes: 2 additions & 1 deletion ansible_wisdom/main/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@
from ansible_ai_connect.users.tests.test_users import create_user


def create_user_with_provider(user_provider):
def create_user_with_provider(user_provider, **kwargs):
return create_user(
username='test_user_name',
password='test_passwords',
provider=user_provider,
external_username='anexternalusername',
**kwargs
)


Expand Down

0 comments on commit 1c4c4db

Please sign in to comment.