File: modify_attribute.py

package info (click to toggle)
python-pykmip 0.10.0-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,796 kB
  • sloc: python: 102,456; makefile: 33; sh: 12
file content (512 lines) | stat: -rw-r--r-- 18,416 bytes parent folder | download | duplicates (4)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# Copyright (c) 2019 The Johns Hopkins University/Applied Physics Laboratory
# 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 six

from kmip.core import enums
from kmip.core import exceptions
from kmip.core import objects
from kmip.core import primitives
from kmip.core import utils
from kmip.core.messages.payloads import base


class ModifyAttributeRequestPayload(base.RequestPayload):
    """
    A request payload for the ModifyAttribute operation.

    Attributes:
        unique_identifier: The unique ID of the object on which attribute
            deletion should be performed.
        attribute: The attribute value to be modified by index. Used in KMIP
            1.0 - 1.4.
        current_attribute: The current attribute to be modified on the
            specified object. Used in KMIP 2.0+.
        new_attribute: The new attribute value to set on the specified object.
            Used in KMIP 2.0+.
    """

    def __init__(self,
                 unique_identifier=None,
                 attribute=None,
                 current_attribute=None,
                 new_attribute=None):
        """
        Construct a ModifyAttribute request payload.

        Args:
            unique_identifier (string): The unique ID of the object on which
                attribute modification should be performed. Optional, defaults
                to None.
            attribute (struct): An Attribute object containing the attribute
                name and index identifying the existing attribute, and the new
                attribute value to replace the original attribute. Optional,
                defaults to None. Used in KMIP 1.0 - 1.4. Required for
                read/write.
            current_attribute (struct): A CurrentAttribute object containing
                the original attribute value on the specified object. Optional,
                defaults to None. Used in KMIP 2.0+.
            new_attribute (struct): A NewAttribute object containing the new
                attribute value to set on the specified object. Optional,
                defaults to None. Used in KMIP 2.0+. Required for read/write.
        """
        super(ModifyAttributeRequestPayload, self).__init__()

        self._unique_identifier = None
        self._attribute = None
        self._current_attribute = None
        self._new_attribute = None

        self.unique_identifier = unique_identifier
        self.attribute = attribute
        self.current_attribute = current_attribute
        self.new_attribute = new_attribute

    @property
    def unique_identifier(self):
        if self._unique_identifier:
            return self._unique_identifier.value
        return None

    @unique_identifier.setter
    def unique_identifier(self, value):
        if value is None:
            self._unique_identifier = None
        elif isinstance(value, six.string_types):
            self._unique_identifier = primitives.TextString(
                value=value,
                tag=enums.Tags.UNIQUE_IDENTIFIER
            )
        else:
            raise TypeError("The unique identifier must be a string.")

    @property
    def attribute(self):
        if self._attribute:
            return self._attribute
        return None

    @attribute.setter
    def attribute(self, value):
        if value is None:
            self._attribute = None
        elif isinstance(value, objects.Attribute):
            self._attribute = value
        else:
            raise TypeError("The attribute must be an Attribute object.")

    @property
    def current_attribute(self):
        if self._current_attribute:
            return self._current_attribute
        return None

    @current_attribute.setter
    def current_attribute(self, value):
        if value is None:
            self._current_attribute = None
        elif isinstance(value, objects.CurrentAttribute):
            self._current_attribute = value
        else:
            raise TypeError(
                "The current attribute must be a CurrentAttribute object."
            )

    @property
    def new_attribute(self):
        if self._new_attribute:
            return self._new_attribute
        return None

    @new_attribute.setter
    def new_attribute(self, value):
        if value is None:
            self._new_attribute = None
        elif isinstance(value, objects.NewAttribute):
            self._new_attribute = value
        else:
            raise TypeError(
                "The new attribute must be a NewAttribute object."
            )

    def read(self, input_buffer, kmip_version=enums.KMIPVersion.KMIP_1_0):
        """
        Read the data encoding the ModifyAttribute request payload and decode
        it into its constituent part.

        Args:
            input_buffer (stream): A data stream containing encoded object
                data, supporting a read method; usually a BytearrayStream
                object.
            kmip_version (KMIPVersion): An enumeration defining the KMIP
                version with which the object will be decoded. Optional,
                defaults to KMIP 1.0.

        Raises:
            InvalidKmipEncoding: Raised if fields are missing from the
                encoding.
        """
        super(ModifyAttributeRequestPayload, self).read(
            input_buffer,
            kmip_version=kmip_version
        )
        local_buffer = utils.BytearrayStream(input_buffer.read(self.length))

        if self.is_tag_next(enums.Tags.UNIQUE_IDENTIFIER, local_buffer):
            self._unique_identifier = primitives.TextString(
                tag=enums.Tags.UNIQUE_IDENTIFIER
            )
            self._unique_identifier.read(
                local_buffer,
                kmip_version=kmip_version
            )
        else:
            self._unique_identifier = None

        if kmip_version < enums.KMIPVersion.KMIP_2_0:
            if self.is_tag_next(enums.Tags.ATTRIBUTE, local_buffer):
                self._attribute = objects.Attribute()
                self._attribute.read(local_buffer, kmip_version=kmip_version)
            else:
                raise exceptions.InvalidKmipEncoding(
                    "The ModifyAttribute request payload encoding is missing "
                    "the attribute field."
                )
        else:
            if self.is_tag_next(enums.Tags.CURRENT_ATTRIBUTE, local_buffer):
                self._current_attribute = objects.CurrentAttribute()
                self._current_attribute.read(
                    local_buffer,
                    kmip_version=kmip_version
                )
            else:
                self._current_attribute = None

            if self.is_tag_next(enums.Tags.NEW_ATTRIBUTE, local_buffer):
                self._new_attribute = objects.NewAttribute()
                self._new_attribute.read(
                    local_buffer,
                    kmip_version=kmip_version
                )
            else:
                raise exceptions.InvalidKmipEncoding(
                    "The ModifyAttribute request payload encoding is missing "
                    "the new attribute field."
                )

        self.is_oversized(local_buffer)

    def write(self, output_buffer, kmip_version=enums.KMIPVersion.KMIP_1_0):
        """
        Write the data encoding the ModifyAttribute request payload to a
        stream.

        Args:
            output_buffer (stream): A data stream in which to encode object
                data, supporting a write method; usually a BytearrayStream
                object.
            kmip_version (KMIPVersion): An enumeration defining the KMIP
                version with which the object will be encoded. Optional,
                defaults to KMIP 1.0.

        Raises:
            InvalidField: Raised if a required field is missing from the
                payload object.
        """
        local_buffer = utils.BytearrayStream()

        if self._unique_identifier:
            self._unique_identifier.write(
                local_buffer,
                kmip_version=kmip_version
            )

        if kmip_version < enums.KMIPVersion.KMIP_2_0:
            if self._attribute:
                self._attribute.write(
                    local_buffer,
                    kmip_version=kmip_version
                )
            else:
                raise exceptions.InvalidField(
                    "The ModifyAttribute request payload is missing the "
                    "attribute field."
                )
        else:
            if self._current_attribute:
                self._current_attribute.write(
                    local_buffer,
                    kmip_version=kmip_version
                )

            if self._new_attribute:
                self._new_attribute.write(
                    local_buffer,
                    kmip_version=kmip_version
                )
            else:
                raise exceptions.InvalidField(
                    "The ModifyAttribute request payload is missing the new "
                    "attribute field."
                )

        self.length = local_buffer.length()
        super(ModifyAttributeRequestPayload, self).write(
            output_buffer,
            kmip_version=kmip_version
        )
        output_buffer.write(local_buffer.buffer)

    def __repr__(self):
        args = [
            "unique_identifier='{}'".format(self.unique_identifier),
            "attribute={}".format(
                repr(self.attribute) if self.attribute else None
            ),
            "current_attribute={}".format(
                repr(self.current_attribute) if self.current_attribute
                else None
            ),
            "new_attribute={}".format(
                repr(self.new_attribute) if self.new_attribute else None
            )
        ]
        return "ModifyAttributeRequestPayload({})".format(", ".join(args))

    def __str__(self):
        return str(
            {
                "unique_identifier": self.unique_identifier,
                "attribute": str(self.attribute) if self.attribute else None,
                "current_attribute": str(
                    self.current_attribute
                ) if self.current_attribute else None,
                "new_attribute": str(
                    self.new_attribute
                ) if self.new_attribute else None
            }
        )

    def __eq__(self, other):
        if isinstance(other, ModifyAttributeRequestPayload):
            if self.unique_identifier != other.unique_identifier:
                return False
            elif self.attribute != other.attribute:
                return False
            elif self.current_attribute != other.current_attribute:
                return False
            elif self.new_attribute != other.new_attribute:
                return False
            else:
                return True
        else:
            return NotImplemented

    def __ne__(self, other):
        if isinstance(other, ModifyAttributeRequestPayload):
            return not self.__eq__(other)
        else:
            return NotImplemented


class ModifyAttributeResponsePayload(base.ResponsePayload):
    """
    A response payload for the ModifyAttribute operation.

    Attributes:
        unique_identifier: The unique ID of the object on which the attribute
            was set.
        attribute: The newly modified attribute. Used in KMIP 1.0 - 1.4.
    """

    def __init__(self, unique_identifier=None, attribute=None):
        """
        Construct a ModifyAttribute response payload.

        Args:
            unique_identifier (string): The unique ID of the object on
                which the attribute was set. Defaults to None. Required for
                read/write.
            attribute (struct): An Attribute object representing the newly
                modified attribute. Optional, defaults to None. Used in KMIP
                1.0 - 1.4. Required for read/write.
        """
        super(ModifyAttributeResponsePayload, self).__init__()

        self._unique_identifier = None
        self._attribute = None

        self.unique_identifier = unique_identifier
        self.attribute = attribute

    @property
    def unique_identifier(self):
        if self._unique_identifier:
            return self._unique_identifier.value
        return None

    @unique_identifier.setter
    def unique_identifier(self, value):
        if value is None:
            self._unique_identifier = None
        elif isinstance(value, six.string_types):
            self._unique_identifier = primitives.TextString(
                value=value,
                tag=enums.Tags.UNIQUE_IDENTIFIER
            )
        else:
            raise TypeError("The unique identifier must be a string.")

    @property
    def attribute(self):
        if self._attribute:
            return self._attribute
        return None

    @attribute.setter
    def attribute(self, value):
        if value is None:
            self._attribute = None
        elif isinstance(value, objects.Attribute):
            self._attribute = value
        else:
            raise TypeError("The attribute must be an Attribute object.")

    def read(self, input_buffer, kmip_version=enums.KMIPVersion.KMIP_1_0):
        """
        Read the data encoding the ModifyAttribute response payload and decode
        it into its constituent parts.

        Args:
            input_buffer (stream): A data stream containing encoded object
                data, supporting a read method; usually a BytearrayStream
                object.
            kmip_version (enum): A KMIPVersion enumeration defining the KMIP
                version with which the object will be decoded. Optional,
                defaults to KMIP 1.0.

        Raises:
            InvalidKmipEncoding: Raised if any required fields are missing
                from the encoding.
        """
        super(ModifyAttributeResponsePayload, self).read(
            input_buffer,
            kmip_version=kmip_version
        )
        local_buffer = utils.BytearrayStream(input_buffer.read(self.length))

        if self.is_tag_next(enums.Tags.UNIQUE_IDENTIFIER, local_buffer):
            self._unique_identifier = primitives.TextString(
                tag=enums.Tags.UNIQUE_IDENTIFIER
            )
            self._unique_identifier.read(
                local_buffer,
                kmip_version=kmip_version
            )
        else:
            raise exceptions.InvalidKmipEncoding(
                "The ModifyAttribute response payload encoding is missing the "
                "unique identifier field."
            )

        if kmip_version < enums.KMIPVersion.KMIP_2_0:
            if self.is_tag_next(enums.Tags.ATTRIBUTE, local_buffer):
                self._attribute = objects.Attribute()
                self._attribute.read(local_buffer, kmip_version=kmip_version)
            else:
                raise exceptions.InvalidKmipEncoding(
                    "The ModifyAttribute response payload encoding is missing "
                    "the attribute field."
                )

        self.is_oversized(local_buffer)

    def write(self, output_buffer, kmip_version=enums.KMIPVersion.KMIP_1_0):
        """
        Write the data encoding the ModifyAttribute response payload to a
        buffer.

        Args:
            output_buffer (buffer): A data buffer in which to encode object
                data, supporting a write method; usually a BytearrayStream
                object.
            kmip_version (enum): A KMIPVersion enumeration defining the KMIP
                version with which the object will be encoded. Optional,
                defaults to KMIP 1.0.

        Raises:
            InvalidField: Raised if a required field is missing from the
                payload object.
        """
        local_buffer = utils.BytearrayStream()

        if self._unique_identifier:
            self._unique_identifier.write(
                local_buffer,
                kmip_version=kmip_version
            )
        else:
            raise exceptions.InvalidField(
                "The ModifyAttribute response payload is missing the unique "
                "identifier field."
            )

        if kmip_version < enums.KMIPVersion.KMIP_2_0:
            if self._attribute:
                self._attribute.write(local_buffer, kmip_version=kmip_version)
            else:
                raise exceptions.InvalidField(
                    "The ModifyAttribute response payload is missing the "
                    "attribute field."
                )

        self.length = local_buffer.length()
        super(ModifyAttributeResponsePayload, self).write(
            output_buffer,
            kmip_version=kmip_version
        )
        output_buffer.write(local_buffer.buffer)

    def __repr__(self):
        args = [
            "unique_identifier='{}'".format(self.unique_identifier),
            "attribute={}".format(
                repr(self.attribute) if self.attribute else None
            )
        ]
        return "ModifyAttributeResponsePayload({})".format(", ".join(args))

    def __str__(self):
        return str(
            {
                "unique_identifier": self.unique_identifier,
                "attribute": str(self.attribute) if self.attribute else None
            }
        )

    def __eq__(self, other):
        if isinstance(other, ModifyAttributeResponsePayload):
            if self.unique_identifier != other.unique_identifier:
                return False
            elif self.attribute != other.attribute:
                return False
            else:
                return True
        return NotImplemented

    def __ne__(self, other):
        if isinstance(other, ModifyAttributeResponsePayload):
            return not self.__eq__(other)
        return NotImplemented