summaryrefslogtreecommitdiff
path: root/troveclient/v1/metadata.py
blob: 8288a5b5e2027a1ac585026c011a5fa739bc18c7 (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
# Copyright 2014 Rackspace Hosting
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import json
from troveclient import base


class MetadataResource(base.Resource):
    def __getitem__(self, item):
        return self.__dict__[item]

    def __contains__(self, item):
        if item in self.__dict__:
                return True
        else:
            return False


class Metadata(base.Manager):

    resource_class = MetadataResource

    def list(self, instance_id):
        return self._get('/instances/%s/metadata' % instance_id, 'metadata')

    def show(self, instance_id, key):
        return self._get('/instances/%s/metadata/%s' % (instance_id, key),
                         'metadata')

    def create(self, instance_id, key, value):
        body = {
            'metadata': {
                'value': self._parse_value(value)
            }
        }
        return self._create(
            '/instances/%s/metadata/%s' % (instance_id, key), body, 'metadata')

    def update(self, instance_id, key, newkey, value):
        body = {
            'metadata': {
                'key': newkey,
                'value': self._parse_value(value)
            }
        }
        return self._update(
            '/instances/%s/metadata/%s' % (instance_id, key), body)

    def edit(self, instance_id, key, value):
        body = {
            'metadata': {
                'value': self._parse_value(value)
            }
        }
        return self._edit(
            '/instances/%s/metadata/%s' % (instance_id, key), body)

    def delete(self, instance_id, key):
        return self._delete('/instances/%s/metadata/%s' % (instance_id, key))

    @staticmethod
    def _parse_value(value):
        """This method is used to parse if a string was passed to any of the
        methods we should first try to deserialize it using json.loads.  This
        is needed to facilitate users passing serialized structures from the
        cli.

        :param value: A value of type dict, list, tuple, int, float, str

        :returns value:
        """
        # NOTE(imsplitbit): if you give _parse_value invalid json you get
        # the string passed back to you.
        if isinstance(value, str):
            try:
                value = json.loads(value)
            except ValueError:
                # the value passed in was a string but not json
                pass

        return value