aboutsummaryrefslogtreecommitdiff
path: root/phpipam_api/resources.py
blob: 7d79166d416b23832c0a1666f5e49dc98b5513f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#
# phpipam/resources.py
# (c) 2021 Jonas Gunz <himself@jonasgunz.de>
# License: MIT
#
from .backend import PhpipamBackend

# Custom functions are defined here
resource_types = {
    'sections' : {
        'getSubnets':{
            'method':'GET',
            'request':'/sections/{section_id}/subnets',
        }
    },
    'subnets' : {
        'search':{
            'method':'GET',
            'request':'/subnets/search/{search}'
        },
        'getIP':{
            'method':'GET',
            'request':'/subnets/{subnet_id}/addresses/{ip}/'
        },
        'getAddresses':{
            'method':'GET',
            'request':'/subnets/{subnet_id}/addresses/'
        },
    },
    'addresses' : {
        'getByIP':{
            'method':'GET',
            'request':'/addresses/{ip}/{subnet_id}/'
        },
        'getByTag':{
            'method':'GET',
            'request':'/addresses/tags/{tag_id}/addresses/'
        },
        'search':{
            'method':'GET',
            'request':'/addresses/search/{ip}/'
        },
        'getFirstFree':{
            'method':'GET',
            'request':'/addresses/first_free/{subnet_id}/'
        },
        'getTags':{
            'method':'GET',
            'request':'/addresses/tags/'
        },
        'getTag':{
            'method':'GET',
            'request':'/addresses/tags/{tag_id}/'
        },
        'createFirstFree':{
            'method':'POST',
            'request':'/addresses/first_free/{subnet_id}/'
        }
    },
    'vlan':{},
    'l2domains':{},
    'vrf':{},
    'devices' : {
        'getAddresses':{
            'method':'GET',
            'request':'/devices/{device_id}/addresses/'
        },
        'getSubnets':{
            'method':'GET',
            'request':'/devices/{device_id}/subnets/'
        }
    },
    'prefix':{},
}

class InvalidResourceException(Exception):
    pass

class InvalidResourceOperationException(Exception):
    pass

class InvalidResourceOperationArgumentException(Exception):
    pass

class PhpipamResourceFunction:
    def __init__(self, backend, resource, function):
        if not function in resource_types[resource]:
            raise InvalidResourceOperationException(f'Operation {function} is not defined for {resource}.')

        self._backend = backend
        self._resource = resource
        self._function = resource_types[resource][function]
        self._name = function

    def __call__(self, **kwargs):
        if 'data' in kwargs:
            data = kwargs['data']
        else:
            data = {}
        try:
            return self._backend.request( self._function['method'], self._function['request'].format(**kwargs), data=data )
        except KeyError as e:
            raise InvalidResourceOperationArgumentException( f'{self._resource}.{self._name}: Missing arguments: {e.args}' )


class PhpipamResource:
    def __init__(self, backend, resource):
        if not resource in resource_types:
            raise InvalidResourceException(f'Invalid resource "{resource}"')

        self._type = resource
        self._backend = backend

    def __getattr__(self, attr):
        return PhpipamResourceFunction(self._backend, self._type, attr)

    # Functions every ObjectType shares

    def get(self):
        """List of all objects"""
        return self._backend.request('GET', f'/{self._type}')

    def byID(self, object_id):
        """object identified by object_id : str"""
        return self._backend.request('GET', f'/{self._type}/{object_id}')

    def create(self, object_id, data):
        return self._backend.request('POST', f'/{self._type}/{object_id}', data=data)

    def edit(self, object_id, data):
        return self._backend.request('PATCH', f'/{self._type}/{object_id}', data=data)

    def delete(self, object_id):
        return self._backend.request('DELETE', f'/{self._type}/{object_id}')