Skip to content

Commit

Permalink
Add IoT Pre-provisioning hook Request and Reponse structs (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
karl-dau authored Mar 2, 2023
1 parent 0d45ea2 commit 3dedc32
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
19 changes: 19 additions & 0 deletions events/iot_preprovision_hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package events

// IoTPreProvisionHookRequest contains the request parameters for the IoT Pre-Provisioning Hook.
// See https://docs.aws.amazon.com/iot/latest/developerguide/pre-provisioning-hook.html
type IoTPreProvisionHookRequest struct {
ClaimCertificateID string `json:"claimCertificateId"`
CertificateID string `json:"certificateId"`
CertificatePEM string `json:"certificatePem"`
TemplateARN string `json:"templateArn"`
ClientID string `json:"clientId"`
Parameters map[string]string `json:"parameters"`
}

// IoTPreProvisionHookResponse contains the response parameters for the IoT Pre-Provisioning Hook.
// See https://docs.aws.amazon.com/iot/latest/developerguide/pre-provisioning-hook.html
type IoTPreProvisionHookResponse struct {
AllowProvisioning bool `json:"allowProvisioning"`
ParameterOverrides map[string]string `json:"parameterOverrides"`
}
63 changes: 63 additions & 0 deletions events/iot_preprovision_hook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package events

import (
"encoding/json"
"io/ioutil" //nolint: staticcheck
"testing"

"github.com/aws/aws-lambda-go/events/test"
)

func TestIoTPreProvisionHookRequest(t *testing.T) {

// read json from file
inputJSON, err := ioutil.ReadFile("./testdata/iot-preprovision-hook-request.json")
if err != nil {
t.Errorf("could not open test file. details: %v", err)
}

// de-serialize into Go object
var inputEvent IoTPreProvisionHookRequest
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
t.Errorf("could not unmarshal event. details: %v", err)
}

// serialize to json
outputJSON, err := json.Marshal(inputEvent)
if err != nil {
t.Errorf("could not marshal event. details: %v", err)
}

test.AssertJsonsEqual(t, inputJSON, outputJSON)
}

func TestIoTPreProvisionHookRequestMalformedJson(t *testing.T) {
test.TestMalformedJson(t, IoTPreProvisionHookRequest{})
}

func TestIoTPreProvisionHookResponseMarshaling(t *testing.T) {

// read json from file
inputJSON, err := ioutil.ReadFile("./testdata/iot-preprovision-hook-response.json")
if err != nil {
t.Errorf("could not open test file. details: %v", err)
}

// de-serialize into Go object
var inputEvent IoTPreProvisionHookResponse
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
t.Errorf("could not unmarshal event. details: %v", err)
}

// serialize to json
outputJSON, err := json.Marshal(inputEvent)
if err != nil {
t.Errorf("could not marshal event. details: %v", err)
}

test.AssertJsonsEqual(t, inputJSON, outputJSON)
}

func TestIoTPreProvisionHookResponseMalformedJson(t *testing.T) {
test.TestMalformedJson(t, IoTPreProvisionHookResponse{})
}
11 changes: 11 additions & 0 deletions events/testdata/iot-preprovision-hook-request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"claimCertificateId" : "string",
"certificateId" : "string",
"certificatePem" : "string",
"templateArn" : "arn:aws:iot:us-east-1:1234567890:provisioningtemplate/MyTemplate",
"clientId" : "221a6d10-9c7f-42f1-9153-e52e6fc869c1",
"parameters" : {
"param1" : "parma1value",
"param2" : "param2value"
}
}
7 changes: 7 additions & 0 deletions events/testdata/iot-preprovision-hook-response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"allowProvisioning": true,
"parameterOverrides" : {
"Key1": "newCustomValue1",
"Key2": "newCustomValue2"
}
}

0 comments on commit 3dedc32

Please sign in to comment.