Skip to content

Commit

Permalink
UI: Add support for creating OpenShift credential with username and p…
Browse files Browse the repository at this point in the history
…assword
  • Loading branch information
mirekdlugosz committed Jan 8, 2025
1 parent c329b43 commit 850f843
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 5 deletions.
9 changes: 8 additions & 1 deletion camayoc/tests/qpc/ui/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
CREDENTIAL_TYPE_MAP = {
SatelliteCredentialFormDTO: CredentialTypes.SATELLITE,
VCenterCredentialFormDTO: CredentialTypes.VCENTER,
OpenShiftCredentialFormDTO: CredentialTypes.OPENSHIFT,
AnsibleCredentialFormDTO: CredentialTypes.ANSIBLE,
RHACSCredentialFormDTO: CredentialTypes.RHACS,
}
Expand Down Expand Up @@ -69,6 +68,14 @@ def create_credential_dto(credential_type, data_provider):
)
data_provider.mark_for_cleanup(Credential(name=credential_form.credential_name))
return credential
elif issubclass(credential_type, get_args(OpenShiftCredentialFormDTO)):
form_factory_cls = getattr(data_factories, f"{credential_type.__name__}Factory")
credential_form = form_factory_cls()
credential = data_factories.AddCredentialDTOFactory(
credential_type=CredentialTypes.OPENSHIFT, credential_form=credential_form
)
data_provider.mark_for_cleanup(Credential(name=credential_form.credential_name))
return credential

credential = data_factories.AddCredentialDTOFactory(
credential_type=CREDENTIAL_TYPE_MAP.get(credential_type)
Expand Down
35 changes: 34 additions & 1 deletion camayoc/types/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from camayoc.ui.enums import CredentialTypes
from camayoc.ui.enums import NetworkCredentialAuthenticationTypes
from camayoc.ui.enums import NetworkCredentialBecomeMethods
from camayoc.ui.enums import OpenShiftCredentialAuthenticationTypes
from camayoc.ui.enums import SourceConnectionTypes
from camayoc.ui.enums import SourceTypes

Expand Down Expand Up @@ -196,9 +197,35 @@ def to_model(self):


@frozen
class OpenShiftCredentialFormDTO:
class PlainOpenShiftCredentialFormDTO:
credential_name: str
username: str
password: str
authentication_type: OpenShiftCredentialAuthenticationTypes = (
OpenShiftCredentialAuthenticationTypes.USERNAME_AND_PASSWORD
)

@classmethod
def from_model(cls, model: Credential):
return cls(credential_name=model.name, username=model.username, password=model.password)

def to_model(self):
model = Credential(
cred_type="openshift",
name=self.credential_name,
username=self.username,
password=self.password,
)
return model


@frozen
class TokenOpenShiftCredentialFormDTO:
credential_name: str
token: str
authentication_type: OpenShiftCredentialAuthenticationTypes = (
OpenShiftCredentialAuthenticationTypes.TOKEN
)

@classmethod
def from_model(cls, model: Credential):
Expand All @@ -213,6 +240,12 @@ def to_model(self):
return model


OpenShiftCredentialFormDTO = Union[
PlainOpenShiftCredentialFormDTO,
TokenOpenShiftCredentialFormDTO,
]


@frozen
class AnsibleCredentialFormDTO:
credential_name: str
Expand Down
25 changes: 22 additions & 3 deletions camayoc/ui/data_factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,22 @@
from camayoc.types.ui import OpenShiftCredentialFormDTO
from camayoc.types.ui import OpenShiftSourceFormDTO
from camayoc.types.ui import PlainNetworkCredentialFormDTO
from camayoc.types.ui import PlainOpenShiftCredentialFormDTO
from camayoc.types.ui import RHACSCredentialFormDTO
from camayoc.types.ui import RHACSSourceFormDTO
from camayoc.types.ui import SatelliteCredentialFormDTO
from camayoc.types.ui import SatelliteSourceFormDTO
from camayoc.types.ui import SourceFormDTO
from camayoc.types.ui import SSHNetworkCredentialFormDTO
from camayoc.types.ui import TokenOpenShiftCredentialFormDTO
from camayoc.types.ui import TriggerScanDTO
from camayoc.types.ui import VCenterCredentialFormDTO
from camayoc.types.ui import VCenterSourceFormDTO

from .enums import CredentialTypes
from .enums import NetworkCredentialAuthenticationTypes
from .enums import NetworkCredentialBecomeMethods
from .enums import OpenShiftCredentialAuthenticationTypes
from .enums import SourceConnectionTypes
from .enums import SourceTypes

Expand All @@ -46,7 +49,7 @@ def get_model_class(self):
setattr(self, "original_model", self.model)

if get_origin(self.original_model) != Union:
msg = "Meta.model must be an Union in Factory inheriting from " "UnionDTOFactory"
msg = "Meta.model must be an Union in Factory inheriting from UnionDTOFactory"
raise factory.errors.FactoryError(msg)

faker = factory.Faker._get_faker()
Expand Down Expand Up @@ -141,12 +144,28 @@ class Meta:
password = factory.Faker("password")


class OpenShiftCredentialFormDTOFactory(factory.Factory):
class PlainOpenShiftCredentialFormDTOFactory(factory.Factory):
class Meta:
model = OpenShiftCredentialFormDTO
model = PlainOpenShiftCredentialFormDTO

credential_name = factory.Faker("text", max_nb_chars=56)
username = factory.Faker("user_name")
password = factory.Faker("password")
authentication_type = OpenShiftCredentialAuthenticationTypes.USERNAME_AND_PASSWORD


class TokenOpenShiftCredentialFormDTOFactory(factory.Factory):
class Meta:
model = TokenOpenShiftCredentialFormDTO

credential_name = factory.Faker("text", max_nb_chars=56)
token = factory.Faker("password")
authentication_type = OpenShiftCredentialAuthenticationTypes.TOKEN


class OpenShiftCredentialFormDTOFactory(UnionDTOFactory):
class Meta:
model = OpenShiftCredentialFormDTO


class AnsibleCredentialFormDTOFactory(factory.Factory):
Expand Down
5 changes: 5 additions & 0 deletions camayoc/ui/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class NetworkCredentialBecomeMethods(LowercasedStrEnum):
RUNAS = auto()


class OpenShiftCredentialAuthenticationTypes(StrEnum):
USERNAME_AND_PASSWORD = "Username and Password"
TOKEN = "Token"


class SourceTypes(StrEnum):
NETWORK_RANGE = "network"
SATELLITE = "satellite"
Expand Down
3 changes: 3 additions & 0 deletions camayoc/ui/models/pages/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ def fill(self, data: VCenterCredentialFormDTO):

class OpenShiftCredentialForm(CredentialForm):
class FormDefinition:
authentication_type = SelectField("button[data-ouia-component-id=auth_type]")
credential_name = InputField("input[data-ouia-component-id=cred_name]")
username = InputField("input[data-ouia-component-id=username]")
password = InputField("input[data-ouia-component-id=password]")
token = InputField("input[data-ouia-component-id=auth_token]")

@overload
Expand Down

0 comments on commit 850f843

Please sign in to comment.