summaryrefslogtreecommitdiff
path: root/ick2/store.py
blob: 83a008b6861e30c11e80f3016a1ebd0af973b721 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Copyright (C) 2019  Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import copy
import json
import uuid


import requests


import ick2


class StoreInterface:  # pragma: no cover

    def create(self, token, obj):
        raise NotImplementedError()

    def update(self, token, rid, obj, revision):
        raise NotImplementedError()

    def show(self, token, rid):
        raise NotImplementedError()

    def delete(self, token, rid):
        raise NotImplementedError()

    def search(self, token, cond):
        raise NotImplementedError()


class MemoryStore(StoreInterface):

    def __init__(self):
        self._objs = {}

    def search(self, token, cond):
        return list(self._objs.keys())

    def _new_id(self):
        return str(uuid.uuid4())

    def _set(self, rid, rev, obj):
        self._objs[rid] = (rev, copy.deepcopy(obj))

    def create(self, token, obj):
        rid = self._new_id()
        rev = self._new_id()
        self._set(rid, rev, obj)
        return rid, rev

    def show(self, token, rid):
        if rid not in self._objs:
            raise ick2.NotFound('unknown.type', rid)
        rev, obj = self._objs[rid]
        return copy.deepcopy(obj), rev

    def update(self, token, rid, obj, revision):
        old_obj, old_rev = self.show(token, rid)
        if old_rev != revision:
            raise ick2.Conflict(rid, old_rev, revision)
        new_rev = self._new_id()
        self._set(rid, new_rev, obj)
        return new_rev

    def delete(self, token, rid):
        del self._objs[rid]


class MuckStore(StoreInterface):  # pragma: no cover

    def __init__(self, muck_url):
        self._url = muck_url
        ick2.log.log('info', msg_text='MuckStore created', muck_url=muck_url)

    def _request(self, func, path, token, headers=None, body=None):
        url = '{}{}'.format(self._url, path)
        if headers is None:
            headers = {}
        headers['Authorization'] = 'Bearer {}'.format(token)
        r = func(url, headers=headers, data=body)
        ick2.log.log(
            'trace', msg_text='Accessing Muck',
            func=repr(func), url=url, path=path, headers=headers, data=body,
            status=r.status_code, text=r.text)
        if not r.ok:
            raise ick2.StoreError(r.text)
        return r

    def search(self, token, cond):
        headers = {
            'Content-Type': 'application/json',
        }
        cond = {
            'cond': [
                {
                    'where': 'meta',
                    'field': 'id',
                    'op': '>=',
                    'pattern': '',
                }
            ],
        }
        body = json.dumps(cond)
        r = self._request(
            requests.get, '/search', token, headers=headers, body=body)
        obj = r.json()
        return obj['resources']

    def create(self, token, obj):
        headers = {
            'Content-Type': 'application/json',
        }
        body = json.dumps(obj)
        r = self._request(
            requests.post, '/res', token, headers=headers, body=body)
        rid = r.headers['Muck-Id']
        rev = r.headers['Muck-Revision']
        return rid, rev

    def show(self, token, rid):
        headers = {
            'Muck-Id': rid,
        }
        r = self._request(requests.get, '/res', token, headers=headers)
        rev = r.headers['Muck-Revision']
        as_dict = r.json()
        return as_dict, rev

    def update(self, token, rid, obj, revision):
        headers = {
            'Content-Type': 'application/json',
            'Muck-Id': rid,
            'Muck-Revision': revision,
        }
        body = json.dumps(obj)
        r = self._request(
            requests.put, '/res', token, headers=headers, body=body)
        rev = r.headers['Muck-Revision']
        return rev

    def delete(self, token, rid):
        headers = {
            'Muck-Id': rid,
        }
        self._request(requests.delete, '/res', token, headers=headers)