# -*- coding: utf-8 -*-
from navmazing import NavigateToAttribute
from widgetastic.widget import Text, TextInput
from widgetastic_manageiq import MultiBoxSelect
from widgetastic_patternfly import Button, Input
from . import ControlExplorerView
from utils.appliance import Navigatable
from utils.appliance.implementations.ui import navigator, navigate_to, CFMENavigateStep
from utils.pretty import Pretty
from utils.update import Updateable
[docs]class NewPolicyProfileView(PolicyProfileFormCommon):
add_button = Button("Add")
@property
def is_displayed(self):
return (
self.in_control_explorer and
self.title.text == "Adding a New Policy Profile" and
self.policy_profiles.tree.currently_selected == ["All Policy Profiles"]
)
[docs]class EditPolicyProfileView(PolicyProfileFormCommon):
save_button = Button("Save")
@property
def is_displayed(self):
return (
self.in_control_explorer and
self.title.text == 'Editing Policy Profile "{}"'.format(
self.context["object"].description) and
self.policy_profiles.tree.currently_selected == [
"All Policy Profiles",
self.context["object"].description
]
)
[docs]class PolicyProfileDetailsView(ControlExplorerView):
title = Text("#explorer_title_text")
@property
def is_displayed(self):
return (
self.in_control_explorer and
self.title.text == 'Policy Profile "{}"'.format(self.context["object"].description)
)
[docs]class PolicyProfilesAllView(ControlExplorerView):
title = Text("#explorer_title_text")
@property
def is_displayed(self):
return (
self.in_control_explorer and
self.title.text == "All Policy Profiles"
)
[docs]class PolicyProfile(Updateable, Navigatable, Pretty):
def __init__(self, description, policies=None, notes=None, appliance=None):
Navigatable.__init__(self, appliance=appliance)
self.description = description
self.notes = notes
self.policies = policies
[docs] def create(self):
view = navigate_to(self, "Add")
view.fill({
"description": self.description,
"notes": self.notes,
"policies": self.prepared_policies
})
view.add_button.click()
view = self.create_view(PolicyProfileDetailsView)
assert view.is_displayed
view.flash.assert_no_error()
view.flash.assert_message('Policy Profile "{}" was added'.format(self.description))
@property
def prepared_policies(self):
if self.policies is not None:
return ["{} {}: {}".format(
policy.PRETTY, policy.TYPE, policy.description) for policy in self.policies]
else:
return None
[docs] def update(self, updates):
"""Update this Policy Profile in UI.
Args:
updates: Provided by update() context manager.
cancel: Whether to cancel the update (default False).
"""
view = navigate_to(self, "Edit")
changed = view.fill(updates)
if changed:
view.save_button.click()
else:
view.cancel_button.click()
for attr, value in updates.items():
setattr(self, attr, value)
view = self.create_view(PolicyProfileDetailsView)
assert view.is_displayed
view.flash.assert_no_error()
if changed:
view.flash.assert_message(
'Policy Profile "{}" was saved'.format(
updates.get("description", self.description)))
else:
view.flash.assert_message(
'Edit of Policy Profile "{}" was cancelled by the user'.format(self.description))
[docs] def delete(self, cancel=False):
"""Delete this Policy Profile in UI.
Args:
cancel: Whether to cancel the deletion (default False).
"""
view = navigate_to(self, "Details")
view.configuration.item_select("Remove this Policy Profile", handle_alert=not cancel)
if cancel:
assert view.is_displayed
view.flash.assert_no_error()
else:
view = self.create_view(PolicyProfilesAllView)
assert view.is_displayed
view.flash.assert_no_error()
view.flash.assert_message(
'Policy Profile "{}": Delete successful'.format(self.description))
@property
def exists(self):
"""Check existence of this Policy Profile.
Returns: :py:class:`bool` signalizing the presence of the Policy Profile in database.
"""
miq_sets = self.appliance.db.client["miq_sets"]
return self.appliance.db.client.session\
.query(miq_sets.description)\
.filter(
miq_sets.description == self.description and miq_sets.set_type == "MiqPolicySet")\
.count() > 0
@navigator.register(PolicyProfile, "Add")
[docs]class PolicyProfileNew(CFMENavigateStep):
VIEW = NewPolicyProfileView
prerequisite = NavigateToAttribute("appliance.server", "ControlExplorer")
[docs] def step(self):
self.view.policy_profiles.tree.click_path("All Policy Profiles")
self.view.configuration.item_select("Add a New Policy Profile")
@navigator.register(PolicyProfile, "Edit")
[docs]class PolicyProfileEdit(CFMENavigateStep):
VIEW = EditPolicyProfileView
prerequisite = NavigateToAttribute("appliance.server", "ControlExplorer")
[docs] def step(self):
self.view.policy_profiles.tree.click_path("All Policy Profiles", self.obj.description)
self.view.configuration.item_select("Edit this Policy Profile")
@navigator.register(PolicyProfile, "Details")
[docs]class PolicyProfileDetails(CFMENavigateStep):
VIEW = PolicyProfileDetailsView
prerequisite = NavigateToAttribute("appliance.server", "ControlExplorer")
[docs] def step(self):
self.view.policy_profiles.tree.click_path("All Policy Profiles", self.obj.description)