Passed
Pull Request — master (#352)
by Vinicius
03:45
created

002_reset_spf_attribute.main()   A

Complexity

Conditions 3

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nop 0
dl 0
loc 17
rs 9.8
c 0
b 0
f 0
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
from napps.kytos.mef_eline.controllers import ELineController
4
import os
5
import sys
6
7
8
def reset_primary_constraints_spf_attr(controller: ELineController, value: str) -> int:
9
    """Reset primary_constraints.spf_attribute."""
10
    db = controller.db
11
    return db.evcs.update_many(
12
        {"primary_constraints.spf_attribute": "hop"},
13
        {"$set": {"primary_constraints.spf_attribute": value}},
14
    ).modified_count
15
16
17
def reset_secondary_constraints_spf_attr(
18
    controller: ELineController, value: str
19
) -> int:
20
    """Reset secondary_constraints.spf_attribute."""
21
    db = controller.db
22
    return db.evcs.update_many(
23
        {"secondary_constraints.spf_attribute": "hop"},
24
        {"$set": {"secondary_constraints.spf_attribute": value}},
25
    ).modified_count
26
27
28
def main() -> None:
29
    """Main function."""
30
    controller = ELineController()
31
    value = os.getenv("SPF_ATTRIBUTE")
32
    expected_values = {"hop", "priority", "delay"}
33
    if not value or value not in expected_values:
34
        print(
35
            f"'SPF_ATTRIBUTE' env: '{value}', "
36
            f"expected one of these values: {expected_values}.\n"
37
            "Please, set the SPF_ATTRIBUTE env var."
38
        )
39
        sys.exit(1)
40
41
    count = reset_primary_constraints_spf_attr(controller, value)
42
    print(f"Updated {count} primary_constraints spf_attribute as {value}")
43
    count = reset_secondary_constraints_spf_attr(controller, value)
44
    print(f"Updated {count} secondary_constraints spf_attribute as {value}")
45
46
47
if __name__ == "__main__":
48
    main()
49