Completed
Push — master ( 486e1a...9be8a7 )
by Jaspar
02:33 queued 36s
created

gvm.protocols.gmpv208.entities.entities   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 7

1 Function

Rating   Name   Duplication   Size   Complexity  
B get_entity_type_from_string() 0 30 7
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2021 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
# pylint:  disable=redefined-builtin
20
# MAYBE we should change filter to filter_string (everywhere)
21
22
from enum import Enum
23
from typing import Optional
24
from gvm.errors import InvalidArgument
25
26
27
class EntityType(Enum):
28
    """Enum for entity types"""
29
30
    ALERT = "alert"
31
    ASSET = "asset"
32
    AUDIT = "audit"
33
    CERT_BUND_ADV = "cert_bund_adv"
34
    CPE = "cpe"
35
    CREDENTIAL = "credential"
36
    CVE = "cve"
37
    DFN_CERT_ADV = "dfn_cert_adv"
38
    FILTER = "filter"
39
    GROUP = "group"
40
    HOST = "host"
41
    INFO = "info"
42
    NOTE = "note"
43
    NVT = "nvt"
44
    OPERATING_SYSTEM = "os"
45
    OVALDEF = "ovaldef"
46
    OVERRIDE = "override"
47
    PERMISSION = "permission"
48
    POLICY = "policy"
49
    PORT_LIST = "port_list"
50
    REPORT = "report"
51
    REPORT_FORMAT = "report_format"
52
    RESULT = "result"
53
    ROLE = "role"
54
    SCAN_CONFIG = "config"
55
    SCANNER = "scanner"
56
    SCHEDULE = "schedule"
57
    TAG = "tag"
58
    TARGET = "target"
59
    TASK = "task"
60
    TICKET = "ticket"
61
    TLS_CERTIFICATE = "tls_certificate"
62
    USER = "user"
63
    VULNERABILITY = "vuln"
64
65
66
def get_entity_type_from_string(
67
    entity_type: Optional[str],
68
) -> Optional[EntityType]:
69
    """Convert a entity type string to an actual EntityType instance
70
71
    Arguments:
72
        entity_type: Entity type string to convert to a EntityType
73
    """
74
    if not entity_type:
75
        return None
76
77
    if entity_type == 'vuln':
78
        return EntityType.VULNERABILITY
79
80
    if entity_type == 'os':
81
        return EntityType.OPERATING_SYSTEM
82
83
    if entity_type == 'config':
84
        return EntityType.SCAN_CONFIG
85
86
    if entity_type == 'tls_certificate':
87
        return EntityType.TLS_CERTIFICATE
88
89
    try:
90
        return EntityType[entity_type.upper()]
91
    except KeyError:
92
        raise InvalidArgument(
93
            argument='entity_type',
94
            function=get_entity_type_from_string.__name__,
95
        ) from None
96