Skip to content

Commit

Permalink
[IMP] Added search filter for deployed applications.
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGigSolutions authored and tarteo committed May 14, 2024
1 parent 4f0dfd3 commit 2d776ed
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
26 changes: 25 additions & 1 deletion argocd_deployer/models/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class Application(models.Model):
application_set_id = fields.Many2one(
"argocd.application.set",
)
is_deployed = fields.Boolean(compute="_compute_is_deployed")
is_deployed = fields.Boolean(
compute="_compute_is_deployed", search="_search_is_deployed"
)
is_application_set_deployed = fields.Boolean(
string="Is App. Set deployed", related="application_set_id.is_deployed"
)
Expand Down Expand Up @@ -98,6 +100,28 @@ def _compute_is_deployed(self):
)
app.is_deployed = os.path.isfile(path)

def _search_is_deployed(self, operator, value):
if operator not in ["=", "!="]:
raise NotImplementedError("Operator not supported. Use '=' or '!='.")
if not isinstance(value, bool):
raise NotImplementedError("Value must be boolean")

# TODO: When there are many apps, this will be pretty slow. If so,
# may need to come up with a better search method.
if operator == "=" and value or operator == "!=" and not value:
apps = (
self.env["argocd.application"]
.search([])
.filtered(lambda a: a.is_deployed)
)
else:
apps = (
self.env["argocd.application"]
.search([])
.filtered(lambda a: not a.is_deployed)
)
return [("id", "in", apps.ids)]

def _render_description(self):
self.ensure_one()
return self.env["ir.qweb"]._render(
Expand Down
33 changes: 33 additions & 0 deletions argocd_deployer/tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,36 @@ def test_immediate_destroy(self):
mock_repository, "Removed `test-set`", ["application_set.yaml"]
)
mock_remote.push.assert_called_once()

def test_search_is_deployed(self):
app2 = self.env["argocd.application"].create(
{
"name": "myapp2",
"template_id": self.env.ref(
"argocd_deployer.demo_curq_basis_application_template"
).id,
"tag_ids": [
self.env.ref(
"argocd_deployer.demo_matomo_server_application_tag"
).id,
],
}
)
result = self.env["argocd.application"].search([("is_deployed", "=", False)])
self.assertEqual(self.app | app2, result)
result = self.env["argocd.application"].search([("is_deployed", "!=", True)])
self.assertEqual(self.app | app2, result)

with patch(
"odoo.addons.argocd_deployer.models.application.Application.is_deployed",
return_value=True,
):
result = self.env["argocd.application"].search([("is_deployed", "=", True)])
self.assertEqual(self.app | app2, result)
result = self.env["argocd.application"].search(
[("is_deployed", "!=", False)]
)
self.assertEqual(self.app | app2, result)

with self.assertRaisesRegex(NotImplementedError, "Operator not supported."):
self.env["argocd.application"].search([("is_deployed", "in", [True])])
10 changes: 10 additions & 0 deletions argocd_deployer/views/application_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@
</field>
</record>

<record id="application_search" model="ir.ui.view">
<field name="model">argocd.application</field>
<field name="arch" type="xml">
<search string="Applications">
<filter name="deployed_applications" string="Deployed Apps" domain="[('is_deployed', '=', True)]"/>
<filter name="not_deployed_applications" string="Not Deployed Apps" domain="[('is_deployed', '!=', True)]"/>
</search>
</field>
</record>

<record id="application_tree" model="ir.ui.view">
<field name="model">argocd.application</field>
<field name="arch" type="xml">
Expand Down

0 comments on commit 2d776ed

Please sign in to comment.