Skip to content

Commit

Permalink
Kotlin Add AWS IoT Core Kotlin code examples (#6151)
Browse files Browse the repository at this point in the history
  • Loading branch information
scmacdon authored Feb 26, 2024
1 parent 054979d commit 869b7dd
Show file tree
Hide file tree
Showing 7 changed files with 934 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .doc_gen/metadata/iot-data_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ iot-data-plane_GetThingShadow:
synopsis: get the shadow for an &IoT; thing.
category:
languages:
Kotlin:
versions:
- sdk_version: 1
github: kotlin/services/iot
sdkguide:
excerpts:
- description:
snippet_tags:
- iot.kotlin.get.shadow.writer.main
Java:
versions:
- sdk_version: 2
Expand All @@ -21,6 +30,15 @@ iot-data-plane_UpdateThingShadow:
synopsis: update the shadow for an &IoT; thing.
category:
languages:
Kotlin:
versions:
- sdk_version: 1
github: kotlin/services/iot
sdkguide:
excerpts:
- description:
snippet_tags:
- iot.kotlin.update.shadow.thing.main
Java:
versions:
- sdk_version: 2
Expand Down
126 changes: 126 additions & 0 deletions .doc_gen/metadata/iot_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ iot_Hello:
synopsis: get started using &IoT;.
category: Hello
languages:
Kotlin:
versions:
- sdk_version: 1
github: kotlin/services/iot
sdkguide:
excerpts:
- description:
snippet_tags:
- iot.kotlin.hello_iot.main
Java:
versions:
- sdk_version: 2
Expand All @@ -22,6 +31,15 @@ iot_DescribeEndpoint:
synopsis: get &IoT; endpoint information.
category:
languages:
Kotlin:
versions:
- sdk_version: 1
github: kotlin/services/iot
sdkguide:
excerpts:
- description:
snippet_tags:
- iot.kotlin.describe.endpoint.main
Java:
versions:
- sdk_version: 2
Expand Down Expand Up @@ -62,6 +80,15 @@ iot_ListCertificates:
synopsis: list your &IoT; certificates.
category:
languages:
Kotlin:
versions:
- sdk_version: 1
github: kotlin/services/iot
sdkguide:
excerpts:
- description:
snippet_tags:
- iot.kotlin.list.certs.main
Java:
versions:
- sdk_version: 2
Expand All @@ -78,6 +105,15 @@ iot_CreateCertificate:
synopsis: create an &IoT; certificate.
category:
languages:
Kotlin:
versions:
- sdk_version: 1
github: kotlin/services/iot
sdkguide:
excerpts:
- description:
snippet_tags:
- iot.java2.create.cert.main
Java:
versions:
- sdk_version: 2
Expand All @@ -94,6 +130,15 @@ iot_DeleteCertificate:
synopsis: delete an &IoT; certificate.
category:
languages:
Kotlin:
versions:
- sdk_version: 1
github: kotlin/services/iot
sdkguide:
excerpts:
- description:
snippet_tags:
- iot.kotlin.delete.cert.main
Java:
versions:
- sdk_version: 2
Expand All @@ -110,6 +155,15 @@ iot_SearchIndex:
synopsis: query the &IoT; search index.
category:
languages:
Kotlin:
versions:
- sdk_version: 1
github: kotlin/services/iot
sdkguide:
excerpts:
- description:
snippet_tags:
- iot.kotlin.search.thing.main
Java:
versions:
- sdk_version: 2
Expand All @@ -126,6 +180,15 @@ iot_DeleteThing:
synopsis: delete an &IoT; thing.
category:
languages:
Kotlin:
versions:
- sdk_version: 1
github: kotlin/services/iot
sdkguide:
excerpts:
- description:
snippet_tags:
- iot.kotlin.delete.thing.main
Java:
versions:
- sdk_version: 2
Expand All @@ -142,6 +205,15 @@ iot_DescribeThing:
synopsis: describe an &IoT; thing.
category:
languages:
Kotlin:
versions:
- sdk_version: 1
github: kotlin/services/iot
sdkguide:
excerpts:
- description:
snippet_tags:
- iot.kotlin.describe.thing.main
Java:
versions:
- sdk_version: 2
Expand All @@ -158,6 +230,15 @@ iot_AttachThingPrincipal:
synopsis: attach an &IoT; certificate.
category:
languages:
Kotlin:
versions:
- sdk_version: 1
github: kotlin/services/iot
sdkguide:
excerpts:
- description:
snippet_tags:
- iot.kotlin.attach.thing.main
Java:
versions:
- sdk_version: 2
Expand All @@ -174,6 +255,15 @@ iot_DetachThingPrincipal:
synopsis: detach an &IoT; certificate.
category:
languages:
Kotlin:
versions:
- sdk_version: 1
github: kotlin/services/iot
sdkguide:
excerpts:
- description:
snippet_tags:
- iot.kotlin.detach.thing.main
Java:
versions:
- sdk_version: 2
Expand All @@ -190,6 +280,15 @@ iot_UpdateThing:
synopsis: update an &IoT; thing.
category:
languages:
Kotlin:
versions:
- sdk_version: 1
github: kotlin/services/iot
sdkguide:
excerpts:
- description:
snippet_tags:
- iot.kotlin.update.thing.main
Java:
versions:
- sdk_version: 2
Expand All @@ -206,6 +305,15 @@ iot_CreateRule:
synopsis: create an &IoT; rule.
category:
languages:
Kotlin:
versions:
- sdk_version: 1
github: kotlin/services/iot
sdkguide:
excerpts:
- description:
snippet_tags:
- iot.kotlin.create.rule.main
Java:
versions:
- sdk_version: 2
Expand All @@ -222,6 +330,15 @@ iot_CreateThing:
synopsis: create an &IoT; thing.
category:
languages:
Kotlin:
versions:
- sdk_version: 1
github: kotlin/services/iot
sdkguide:
excerpts:
- description:
snippet_tags:
- iot.kotlin.create.thing.main
Java:
versions:
- sdk_version: 2
Expand All @@ -238,6 +355,15 @@ iot_Scenario:
synopsis: working with &IoT; device management use cases using &IoT; SDK
category: Scenarios
languages:
Kotlin:
versions:
- sdk_version: 1
github: kotlin/services/iot
sdkguide:
excerpts:
- description:
snippet_tags:
- iot.kotlin.scenario.main
Java:
versions:
- sdk_version: 2
Expand Down
81 changes: 81 additions & 0 deletions kotlin/services/iot/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# AWS IoT Core code examples for the SDK for Kotlin

## Overview
This README discusses how to run and test the AWS SDK for Kotlin examples for AWS IoT Core.

AWS IoT Core enables secure two-way communication between internet-connected devices and AWS services with device gateway and device SDK capabilities.

## ⚠️ Important
* The SDK for Kotlin examples perform AWS operations for the account and AWS Region for which you've specified credentials. Running these examples might incur charges on your account. For details about the charges you can expect for a given service and API operation, see [AWS Pricing](https://aws.amazon.com/pricing/).
* Running the tests might result in charges to your AWS account.
* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).

## Code examples

The credential provider used in all code examples is the default credential provider. For more information, see [Using credentials](https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/credential-providers.html).

### Get started

- [Hello AWS IoT Core](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/kotlin/services/iot/src/main/kotlin/com/example/iot/HelloIoT.kt) (`ListThings`)


### Single action

The following examples use the **IotClient** object:

- [Attach a certificate to an AWS IoT thing](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/kotlin/services/iot/src/main/kotlin/com/example/iot/IotScenario.kt) (`AttachThingPrincipal`)
- [Create an AWS IoT thing](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/kotlin/services/iot/src/main/kotlin/com/example/iot/IotScenario.kt) (`CreateThing`)
- [Create an AWS IoT certificate](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/kotlin/services/iot/src/main/kotlin/com/example/iot/IotScenario.kt) (`CreateKeysAndCertificate`)
- [Create an AWS IoT rule](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/kotlin/services/iot/src/main/kotlin/com/example/iot/IotScenario.kt) (`CreateTopicRule`)
- [Delete an AWS IoT certificate](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/kotlin/services/iot/src/main/kotlin/com/example/iot/IotScenario.kt) (`DeleteCertificate`)
- [Delete an AWS IoT thing](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/kotlin/services/iot/src/main/kotlin/com/example/iot/IotScenario.kt) (`DeleteThing`)
- [Detach a certificate from an AWS IoT thing](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/kotlin/services/iot/src/main/kotlin/com/example/iot/IotScenario.kt) (`DetachThingPrincipal`)
- [Get information about an endpoint](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/kotlin/services/iot/src/main/kotlin/com/example/iot/IotScenario.kt) (`DescribeEndpoint`)
- [List your AWS IoT certificates](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/kotlin/services/iot/src/main/kotlin/com/example/iot/IotScenario.kt) (`ListCertificates`)
- [Query the AWS IoT search index.](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/kotlin/services/iot/src/main/kotlin/com/example/iot/IotScenario.kt) (`SearchIndex`)
- [Update an AWS IoT thing](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/kotlin/services/iot/src/main/kotlin/com/example/iot/IotScenario.kt) (`UpdateThing`)

The following examples use the **IotDataPlaneClient** object:

- [Get the shadow for the specified thing](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/kotlin/services/iot/src/main/kotlin/com/example/iot/IotScenario.kt) (`GetThingShadow`)
- [Update the shadow for the specified thing](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/kotlin/services/iot/src/main/kotlin/com/example/iot/IotScenario.kt) (`UpdateThingShadow`)

### Scenarios

Code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

* [Perform device management use cases ](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/kotlin/services/iot/src/main/kotlin/com/example/iot/IotScenario.kt)

## Running the AWS IoT Kotlin files

Some of these examples perform *destructive* operations on AWS resources, such as deleting an AWS IoT Thing. **Be very careful** when running an operation that deletes or modifies AWS resources in your account. We recommend creating separate test-only resources when experimenting with these examples.

To run these examples, set up your development environment. For more information,
see [Get started with the SDK for Kotlin](https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/get-started.html).


## Testing the AWS IoT Kotlin files

You can test the Kotlin code examples for AWS IoT by running a test file named **IoTTests**. This file uses JUnit 5 to run the JUnit tests and is located in the **src/test/kotlin** folder. For more information, see [https://junit.org/junit5/](https://junit.org/junit5/).

You can run the JUnit tests from an IDE, such as IntelliJ, or from the command line. As each test runs, you can view messages that inform you if the various tests succeed or fail. For example, the following message informs you that the Test passed.

Test 2 passed

**WARNING**: _Running these JUnit tests manipulates real Amazon resources and might incur charges on your account._

### Properties file
Before running the JUnit tests, you must define values in the **config.properties** file located in the **resources** folder. This file contains values that are required to run the JUnit tests. For example, you define a tableId used in the tests. If you do not define all values, the JUnit tests fail.

Define these values to successfully run the JUnit tests:

- **roleARN** - The ARN of an IAM role that has permission to work with AWS IOT.
- **snsAction** - An ARN of an SNS topic.

## Additional resources
* [Developer Guide - AWS SDK for Koltin](https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/home.html).
* [Developer Guide - AWS IoT Core](https://docs.aws.amazon.com/iot/latest/developerguide/iot-gs.html).
* [Interface IotClient](https://sdk.amazonaws.com/kotlin/api/latest/iot/aws.sdk.kotlin.services.iot/-iot-client/index.html).

Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0
52 changes: 52 additions & 0 deletions kotlin/services/iot/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.9.0"
application
}

group = "me.scmacdon"
version = "1.0-SNAPSHOT"

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

buildscript {
repositories {
maven("https://plugins.gradle.org/m2/")
}
dependencies {
classpath("org.jlleitschuh.gradle:ktlint-gradle:10.3.0")
}
}

repositories {
mavenCentral()
}
apply(plugin = "org.jlleitschuh.gradle.ktlint")
dependencies {
implementation("aws.sdk.kotlin:iot:1.0.30")
implementation("aws.sdk.kotlin:iotdataplane:1.0.30")
implementation("aws.sdk.kotlin:secretsmanager:1.0.30")
implementation("aws.smithy.kotlin:http-client-engine-okhttp:0.30.0")
implementation("aws.smithy.kotlin:http-client-engine-crt:0.30.0")
implementation("com.google.code.gson:gson:2.10")
testImplementation("org.junit.jupiter:junit-jupiter:5.9.2")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
}
tasks.withType<KotlinCompile>() {
kotlinOptions.jvmTarget = "17"
}

tasks.test {
useJUnitPlatform()
testLogging {
events("passed", "skipped", "failed")
}

// Define the test source set
testClassesDirs += files("build/classes/kotlin/test")
classpath += files("build/classes/kotlin/main", "build/resources/main")
}
Loading

0 comments on commit 869b7dd

Please sign in to comment.