Completed
Push — master ( 062836...3184f1 )
by Edward
06:10 queued 02:54
created

status_text_to_code()   B

Complexity

Conditions 6

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
dl 0
loc 18
rs 8
c 0
b 0
f 0
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 socket
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", None)  # aka slack 'grey'
28
    elif status == 1:
29
        return ("Up", "#00ad52")  # aka slack 'good'
30
    elif status == 2:
31
        return ("Down", "#eb0000")  # aka slack 'danger'
32
    elif status == 3:
33
        return ("Warning", "#e89e0e")  # aka slack 'warning'
34
    elif status == 14:
35
        return ("Critical", "#eb0000")  # aka slack 'danger'
36
37
38
def status_text_to_code(status):
39
    """
40
    Takes an Solarwinds Orion status text and translates it
41
    to an Orion code.
42
    """
43
44
    if status == "Up":
45
        return 1
46
    elif status == "Down":
47
        return 2
48
    elif status == "Warning":
49
        return 3
50
    elif status == "Critical":
51
        return 14
52
    elif status == "Unknown":
53
        return 0
54
    else:
55
        raise ValueError("No matching status text for: {}".format(status))
56
57
58
def send_user_error(message):
59
    """
60
    Prints an user error message.
61
    """
62
    print(message)
63
64
65
def discovery_status_to_text(status):
66
    """
67
    Convert a Discovery Status code into meaningful text.
68
69
    Args:
70
       status: Staus code from Orion.
71
72
    Returns:
73
       String: Human text for status code.
74
    """
75
    discovery_statuses = {"0": 'Unknown',
76
                          "1": 'InProgress',
77
                          "2": 'Finished',
78
                          "3": 'Error',
79
                          "4": "NotScheduled",
80
                          "5": "Scheduled",
81
                          "6": "NotCompleted",
82
                          "7": "Canceling",
83
                          "8": "ReadyForImport"}
84
    return discovery_statuses[status]
85
86
87
def is_ip(ip_address):
88
    """
89
    Check if an valid IP address using socket.inet_pton.
90
91
    Args:
92
       ip_address: a string to check
93
94
    Returns:
95
       bool: True if an IP address, False if not.
96
    """
97
    if "." in ip_address:
98
        family = socket.AF_INET
99
    elif ":" in ip_address:
100
        family = socket.AF_INET6
101
    else:
102
        return False
103
104
    try:
105
        socket.inet_pton(family, ip_address)
106
    except socket.error:
107
        return False
108
    else:
109
        return True
110
111
112
def only_one(*args):
113
    """
114
    Only return True, if only one arg is evaluates to True.
115
    """
116
117
    bools = [bool(v) for v in args]
118
    if all(bools):
119
        return False
120
121
    return reduce(operator.xor, bools, False)
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'reduce'
Loading history...
122