Completed
Pull Request — master (#487)
by
unknown
02:56
created

status_code_to_text()   B

Complexity

Conditions 6

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
c 1
b 0
f 0
dl 0
loc 16
rs 8
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
import re
17
import operator
18
19
20
def status_code_to_text(status):
21
    """
22
    Takes an Solarwinds Orion status code and translates it to
23
    human text and also a colour that can be used in Slack.
24
    """
25
26
    if status == 0:
27
        return ("Unknown", "grey")  # aka slack 'grey'
28
    elif status == 1:
29
        return ("Up", "good")  # slack 'good'
30
    elif status == 2:
31
        return ("Down", "#7CD197")  # slack 'danger'
32
    elif status == 3:
33
        return ("Warning", "warning")  # slack 'warning'
34
    elif status == 14:
35
        return ("Critical", "#7CD197")  # slack 'danger'
36
37
38
def send_user_error(message):
39
    """
40
    Prints an user error message.
41
    """
42
    print(message)
43
44
45
def discovery_status_to_text(status):
46
    """
47
    Convert a Discovery Status code into meaningful text.
48
49
    Args:
50
       status: Staus code from Orion.
51
52
    Returns:
53
       String: Human text for status code.
54
    """
55
    discovery_statuses = {"0": 'Unknown',
56
                          "1": 'InProgress',
57
                          "2": 'Finished',
58
                          "3": 'Error',
59
                          "4": "NotScheduled",
60
                          "5": "Scheduled",
61
                          "6": "NotCompleted",
62
                          "7": "Canceling",
63
                          "8": "ReadyForImport"}
64
    return discovery_statuses[status]
65
66
67
def is_ip(ip_address):
68
    v4_pattern = re.compile(
69
        "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")  # noqa: ignore=E501
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (123/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Bug introduced by
A suspicious escape sequence \. was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
70
71
    is_ipv4 = v4_pattern.match(ip_address)
72
73
    v6_pattern = re.compile("^(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}$")
74
    is_ipv6 = v6_pattern.match(ip_address)
75
76
    if is_ipv4 or is_ipv6:
77
        return True
78
    else:
79
        return False
80
81
82
def only_one(*args):
83
    """
84
    Only return True, if only one arg is evaluates to True.
85
    """
86
87
    bools = [bool(v) for v in args]
88
    if all(bools):
89
        return False
90
91
    return reduce(operator.xor, bools, False)
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'reduce'
Loading history...
92