Merge pull request #2393 from cm-iwata/fix_device_shadow_ver_conflict
fix #2392 Add validation for device shadow version
This commit is contained in:
		
						commit
						c81de6e381
					
				| @ -21,3 +21,11 @@ class InvalidRequestException(IoTDataPlaneClientError): | |||||||
|         super(InvalidRequestException, self).__init__( |         super(InvalidRequestException, self).__init__( | ||||||
|             "InvalidRequestException", message |             "InvalidRequestException", message | ||||||
|         ) |         ) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | class ConflictException(IoTDataPlaneClientError): | ||||||
|  |     def __init__(self, message): | ||||||
|  |         self.code = 409 | ||||||
|  |         super(ConflictException, self).__init__( | ||||||
|  |             "ConflictException", message | ||||||
|  |         ) | ||||||
|  | |||||||
| @ -6,6 +6,7 @@ import jsondiff | |||||||
| from moto.core import BaseBackend, BaseModel | from moto.core import BaseBackend, BaseModel | ||||||
| from moto.iot import iot_backends | from moto.iot import iot_backends | ||||||
| from .exceptions import ( | from .exceptions import ( | ||||||
|  |     ConflictException, | ||||||
|     ResourceNotFoundException, |     ResourceNotFoundException, | ||||||
|     InvalidRequestException |     InvalidRequestException | ||||||
| ) | ) | ||||||
| @ -161,6 +162,8 @@ class IoTDataPlaneBackend(BaseBackend): | |||||||
|         if any(_ for _ in payload['state'].keys() if _ not in ['desired', 'reported']): |         if any(_ for _ in payload['state'].keys() if _ not in ['desired', 'reported']): | ||||||
|             raise InvalidRequestException('State contains an invalid node') |             raise InvalidRequestException('State contains an invalid node') | ||||||
| 
 | 
 | ||||||
|  |         if 'version' in payload and thing.thing_shadow.version != payload['version']: | ||||||
|  |                 raise ConflictException('Version conflict') | ||||||
|         new_shadow = FakeShadow.create_from_previous_version(thing.thing_shadow, payload) |         new_shadow = FakeShadow.create_from_previous_version(thing.thing_shadow, payload) | ||||||
|         thing.thing_shadow = new_shadow |         thing.thing_shadow = new_shadow | ||||||
|         return thing.thing_shadow |         return thing.thing_shadow | ||||||
|  | |||||||
| @ -86,6 +86,12 @@ def test_update(): | |||||||
|     payload.should.have.key('version').which.should.equal(2) |     payload.should.have.key('version').which.should.equal(2) | ||||||
|     payload.should.have.key('timestamp') |     payload.should.have.key('timestamp') | ||||||
| 
 | 
 | ||||||
|  |     raw_payload = b'{"state": {"desired": {"led": "on"}}, "version": 1}' | ||||||
|  |     with assert_raises(ClientError) as ex: | ||||||
|  |         client.update_thing_shadow(thingName=name, payload=raw_payload) | ||||||
|  |     ex.exception.response['ResponseMetadata']['HTTPStatusCode'].should.equal(409) | ||||||
|  |     ex.exception.response['Error']['Message'].should.equal('Version conflict') | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| @mock_iotdata | @mock_iotdata | ||||||
| def test_publish(): | def test_publish(): | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user