Completed
Push — master ( f1a13c...e53aa3 )
by Tomaz
20s
created

is_ip()   B

Complexity

Conditions 5

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
dl 0
loc 23
rs 8.2508
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 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
    """
69
    Check if an valid IP address using socket.inet_pton.
70
71
    Args:
72
       ip_address: a string to check
73
74
    Returns:
75
       bool: True if an IP address, False if not.
76
    """
77
    if "." in ip_address:
78
        family = socket.AF_INET
79
    elif ":" in ip_address:
80
        family = socket.AF_INET6
81
    else:
82
        return False
83
84
    try:
85
        socket.inet_pton(family, ip_address)
86
    except socket.error:
87
        return False
88
    else:
89
        return True
90
91
92
def only_one(*args):
93
    """
94
    Only return True, if only one arg is evaluates to True.
95
    """
96
97
    bools = [bool(v) for v in args]
98
    if all(bools):
99
        return False
100
101
    return reduce(operator.xor, bools, False)
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'reduce'
Loading history...
102