Greengrass Implement device_definition_version APIs (#5211)
This commit is contained in:
parent
a2c2c06243
commit
f8bbf98176
@ -101,17 +101,21 @@ class FakeDeviceDefinitionVersion(BaseModel):
|
||||
self.arn = f"arn:aws:greengrass:{region_name}:{get_account_id()}:/greengrass/definition/devices/{self.device_definition_id}/versions/{self.version}"
|
||||
self.created_at_datetime = datetime.utcnow()
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
def to_dict(self, include_detail=False):
|
||||
obj = {
|
||||
"Arn": self.arn,
|
||||
"CreationTimestamp": iso_8601_datetime_with_milliseconds(
|
||||
self.created_at_datetime
|
||||
),
|
||||
"Definition": {"Devices": self.devices},
|
||||
"Id": self.device_definition_id,
|
||||
"Version": self.version,
|
||||
}
|
||||
|
||||
if include_detail:
|
||||
obj["Definition"] = {"Devices": self.devices}
|
||||
|
||||
return obj
|
||||
|
||||
|
||||
class GreengrassBackend(BaseBackend):
|
||||
def __init__(self, region_name, account_id):
|
||||
@ -240,6 +244,12 @@ class GreengrassBackend(BaseBackend):
|
||||
|
||||
return device_ver
|
||||
|
||||
def list_device_definition_versions(self, device_definition_id):
|
||||
|
||||
if device_definition_id not in self.device_definitions:
|
||||
raise IdNotFoundException("That devices definition does not exist.")
|
||||
return self.device_definition_versions[device_definition_id].values()
|
||||
|
||||
def get_device_definition(self, device_definition_id):
|
||||
|
||||
if device_definition_id not in self.device_definitions:
|
||||
@ -262,5 +272,24 @@ class GreengrassBackend(BaseBackend):
|
||||
raise IdNotFoundException("That devices definition does not exist.")
|
||||
self.device_definitions[device_definition_id].name = name
|
||||
|
||||
def get_device_definition_version(
|
||||
self, device_definition_id, device_definition_version_id
|
||||
):
|
||||
|
||||
if device_definition_id not in self.device_definitions:
|
||||
raise IdNotFoundException("That devices definition does not exist.")
|
||||
|
||||
if (
|
||||
device_definition_version_id
|
||||
not in self.device_definition_versions[device_definition_id]
|
||||
):
|
||||
raise VersionNotFoundException(
|
||||
f"Version {device_definition_version_id} of Device List Definition {device_definition_id} does not exist."
|
||||
)
|
||||
|
||||
return self.device_definition_versions[device_definition_id][
|
||||
device_definition_version_id
|
||||
]
|
||||
|
||||
|
||||
greengrass_backends = BackendDict(GreengrassBackend, "greengrass")
|
||||
|
@ -152,6 +152,9 @@ class GreengrassResponse(BaseResponse):
|
||||
if self.method == "POST":
|
||||
return self.create_device_definition_version()
|
||||
|
||||
if self.method == "GET":
|
||||
return self.list_device_definition_versions()
|
||||
|
||||
def create_device_definition_version(self):
|
||||
|
||||
device_definition_id = self.path.split("/")[-2]
|
||||
@ -162,6 +165,20 @@ class GreengrassResponse(BaseResponse):
|
||||
)
|
||||
return 201, {"status": 201}, json.dumps(res.to_dict())
|
||||
|
||||
def list_device_definition_versions(self):
|
||||
|
||||
device_definition_id = self.path.split("/")[-2]
|
||||
res = self.greengrass_backend.list_device_definition_versions(
|
||||
device_definition_id
|
||||
)
|
||||
return (
|
||||
200,
|
||||
{"status": 200},
|
||||
json.dumps(
|
||||
{"Versions": [device_def_ver.to_dict() for device_def_ver in res]}
|
||||
),
|
||||
)
|
||||
|
||||
def device_definition(self, request, full_url, headers):
|
||||
self.setup_class(request, full_url, headers)
|
||||
|
||||
@ -197,3 +214,18 @@ class GreengrassResponse(BaseResponse):
|
||||
device_definition_id=device_definition_id, name=name
|
||||
)
|
||||
return 200, {"status": 200}, json.dumps({})
|
||||
|
||||
def device_definition_version(self, request, full_url, headers):
|
||||
self.setup_class(request, full_url, headers)
|
||||
|
||||
if self.method == "GET":
|
||||
return self.get_device_definition_version()
|
||||
|
||||
def get_device_definition_version(self):
|
||||
device_definition_id = self.path.split("/")[-3]
|
||||
device_definition_version_id = self.path.split("/")[-1]
|
||||
res = self.greengrass_backend.get_device_definition_version(
|
||||
device_definition_id=device_definition_id,
|
||||
device_definition_version_id=device_definition_version_id,
|
||||
)
|
||||
return 200, {"status": 200}, json.dumps(res.to_dict(include_detail=True))
|
||||
|
@ -16,4 +16,5 @@ url_paths = {
|
||||
"{0}/greengrass/definition/devices$": response.device_definitions,
|
||||
"{0}/greengrass/definition/devices/(?P<definition_id>[^/]+)/?$": response.device_definition,
|
||||
"{0}/greengrass/definition/devices/(?P<definition_id>[^/]+)/versions$": response.device_definition_versions,
|
||||
"{0}/greengrass/definition/devices/(?P<definition_id>[^/]+)/versions/(?P<definition_version_id>[^/]+)/?$": response.device_definition_version,
|
||||
}
|
||||
|
@ -307,3 +307,119 @@ def test_update_device_definition_with_invalid_id():
|
||||
"That devices definition does not exist."
|
||||
)
|
||||
ex.value.response["Error"]["Code"].should.equal("IdNotFoundException")
|
||||
|
||||
|
||||
@freezegun.freeze_time("2022-06-01 12:00:00")
|
||||
@mock_greengrass
|
||||
def test_list_device_definition_versions():
|
||||
|
||||
client = boto3.client("greengrass", region_name="ap-northeast-1")
|
||||
devices = [
|
||||
{
|
||||
"CertificateArn": f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:cert/36ed61be9c6271ae8da174e29d0e033c06af149d7b21672f3800fe322044554d",
|
||||
"Id": "123",
|
||||
"SyncShadow": True,
|
||||
"ThingArn": f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/v1Thing",
|
||||
}
|
||||
]
|
||||
|
||||
initial_version = {"Devices": devices}
|
||||
create_res = client.create_device_definition(
|
||||
InitialVersion=initial_version, Name="TestDevice"
|
||||
)
|
||||
device_def_id = create_res["Id"]
|
||||
|
||||
device_def_vers_res = client.list_device_definition_versions(
|
||||
DeviceDefinitionId=device_def_id
|
||||
)
|
||||
|
||||
device_def_vers_res.should.have.key("Versions")
|
||||
device_def_ver = device_def_vers_res["Versions"][0]
|
||||
device_def_ver.should.have.key("Arn")
|
||||
device_def_ver.should.have.key("CreationTimestamp")
|
||||
|
||||
if not TEST_SERVER_MODE:
|
||||
device_def_ver["CreationTimestamp"].should.equal("2022-06-01T12:00:00.000Z")
|
||||
device_def_ver.should.have.key("Id").equals(device_def_id)
|
||||
device_def_ver.should.have.key("Version")
|
||||
|
||||
|
||||
@freezegun.freeze_time("2022-06-01 12:00:00")
|
||||
@mock_greengrass
|
||||
def test_get_device_definition_version():
|
||||
|
||||
client = boto3.client("greengrass", region_name="ap-northeast-1")
|
||||
devices = [
|
||||
{
|
||||
"CertificateArn": f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:cert/36ed61be9c6271ae8da174e29d0e033c06af149d7b21672f3800fe322044554d",
|
||||
"Id": "123",
|
||||
"SyncShadow": True,
|
||||
"ThingArn": f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/v1Thing",
|
||||
}
|
||||
]
|
||||
|
||||
initial_version = {"Devices": devices}
|
||||
create_res = client.create_device_definition(
|
||||
InitialVersion=initial_version, Name="TestDevice"
|
||||
)
|
||||
|
||||
device_def_id = create_res["Id"]
|
||||
device_def_ver_id = create_res["LatestVersion"]
|
||||
|
||||
core_def_ver_res = client.get_device_definition_version(
|
||||
DeviceDefinitionId=device_def_id, DeviceDefinitionVersionId=device_def_ver_id
|
||||
)
|
||||
|
||||
core_def_ver_res.should.have.key("Arn")
|
||||
core_def_ver_res.should.have.key("CreationTimestamp")
|
||||
core_def_ver_res.should.have.key("Definition").should.equal(initial_version)
|
||||
if not TEST_SERVER_MODE:
|
||||
core_def_ver_res["CreationTimestamp"].should.equal("2022-06-01T12:00:00.000Z")
|
||||
core_def_ver_res.should.have.key("Id").equals(device_def_id)
|
||||
core_def_ver_res.should.have.key("Version")
|
||||
|
||||
|
||||
@mock_greengrass
|
||||
def test_get_device_definition_version_with_invalid_id():
|
||||
|
||||
client = boto3.client("greengrass", region_name="ap-northeast-1")
|
||||
with pytest.raises(ClientError) as ex:
|
||||
client.get_device_definition_version(
|
||||
DeviceDefinitionId="fe2392e9-e67f-4308-af1b-ff94a128b231",
|
||||
DeviceDefinitionVersionId="cd2ea6dc-6634-4e89-8441-8003500435f9",
|
||||
)
|
||||
ex.value.response["Error"]["Message"].should.equal(
|
||||
"That devices definition does not exist."
|
||||
)
|
||||
ex.value.response["Error"]["Code"].should.equal("IdNotFoundException")
|
||||
|
||||
|
||||
@mock_greengrass
|
||||
def test_get_device_definition_version_with_invalid_version_id():
|
||||
|
||||
client = boto3.client("greengrass", region_name="ap-northeast-1")
|
||||
devices = [
|
||||
{
|
||||
"CertificateArn": f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:cert/36ed61be9c6271ae8da174e29d0e033c06af149d7b21672f3800fe322044554d",
|
||||
"Id": "123",
|
||||
"SyncShadow": True,
|
||||
"ThingArn": f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/v1Thing",
|
||||
}
|
||||
]
|
||||
|
||||
initial_version = {"Devices": devices}
|
||||
create_res = client.create_device_definition(
|
||||
InitialVersion=initial_version, Name="TestDevice"
|
||||
)
|
||||
|
||||
device_def_id = create_res["Id"]
|
||||
invalid_device_version_id = "6fbffc21-989e-4d29-a793-a42f450a78c6"
|
||||
with pytest.raises(ClientError) as ex:
|
||||
client.get_device_definition_version(
|
||||
DeviceDefinitionId=device_def_id,
|
||||
DeviceDefinitionVersionId=invalid_device_version_id,
|
||||
)
|
||||
ex.value.response["Error"]["Message"].should.equal(
|
||||
f"Version {invalid_device_version_id} of Device List Definition {device_def_id} does not exist."
|
||||
)
|
||||
ex.value.response["Error"]["Code"].should.equal("VersionNotFoundException")
|
||||
|
Loading…
Reference in New Issue
Block a user