Skip to content

Commit 5805cd7

Browse files
committed
samples-wide factoring start
1 parent bb3b98a commit 5805cd7

10 files changed

+78
-60
lines changed

samples/basic_discovery.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0.
33

4-
import time, json
5-
from awscrt import io, http
6-
from awscrt.mqtt import QoS
74
from awsiot.greengrass_discovery import DiscoveryClient
85
from awsiot import mqtt_connection_builder
6+
from awscrt import io, http
7+
from awscrt.mqtt import QoS
8+
import time, json
99

1010
allowed_actions = ['both', 'publish', 'subscribe']
1111

@@ -14,10 +14,11 @@
1414

1515
def parse_sample_input():
1616
parser = argparse.ArgumentParser(
17-
description="Greengrass basic discovery",
17+
description="Greengrass Basic Discovery",
1818
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
1919
)
2020

21+
# Connection / TLS
2122
parser.add_argument("--cert", required=True, dest="input_cert",
2223
help="Path to the certificate file to use during mTLS connection establishment")
2324
parser.add_argument("--key", required=True, dest="input_key",
@@ -36,7 +37,7 @@ def parse_sample_input():
3637
parser.add_argument("--mode", default='both', dest="input_mode",
3738
help=f"The operation mode (optional, default='both').\nModes:{allowed_actions}")
3839

39-
# Proxy (optional)
40+
# Proxy
4041
parser.add_argument("--proxy-host", dest="input_proxy_host", help="HTTP proxy host")
4142
parser.add_argument("--proxy-port", type=int, default=0, dest="input_proxy_port", help="HTTP proxy port")
4243

@@ -46,10 +47,12 @@ def parse_sample_input():
4647

4748
return parser.parse_args()
4849

50+
# args contains all the parsed commandline arguments used by the sample
4951
args = parse_sample_input()
5052

5153
# --------------------------------- ARGUMENT PARSING END -----------------------------------------
5254

55+
5356
tls_options = io.TlsContextOptions.create_client_with_mtls_from_path(args.input_cert, args.input_key)
5457
if (args.input_ca is not None):
5558
tls_options.override_default_trust_store_from_path(None, args.input_ca)

samples/fleet_provisioning_basic.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0.
33

4-
from awscrt import mqtt5, mqtt_request_response
54
from awsiot import iotidentity, mqtt5_client_builder
5+
from awscrt import mqtt5, mqtt_request_response
66
from concurrent.futures import Future
7+
import json, uuid
8+
9+
# --------------------------------- ARGUMENT PARSING -----------------------------------------
710
import argparse
8-
import json
9-
import uuid
11+
12+
# --------------------------------- ARGUMENT PARSING END -----------------------------------------
1013

1114

1215
if __name__ == '__main__':

samples/fleet_provisioning_csr.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0.
33

4-
from awscrt import mqtt5, mqtt_request_response
54
from awsiot import iotidentity, mqtt5_client_builder
5+
from awscrt import mqtt5, mqtt_request_response
66
from concurrent.futures import Future
7+
import json, uuid
8+
9+
# --------------------------------- ARGUMENT PARSING -----------------------------------------
710
import argparse
8-
import json
9-
import uuid
11+
12+
# --------------------------------- ARGUMENT PARSING END -----------------------------------------
1013

1114

1215
if __name__ == '__main__':

samples/ipc_greengrass.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919
2020
"""
2121

22-
import json
23-
import time
24-
import os
25-
import sys
22+
import json, time, os, sys
2623

2724
import awsiot.greengrasscoreipc
2825
import awsiot.greengrasscoreipc.model as model

samples/jobs.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0.
33

4-
from awscrt import mqtt5, mqtt_request_response
54
from awsiot import iotjobs, mqtt5_client_builder
5+
from awscrt import mqtt5, mqtt_request_response
66
import boto3
77
from concurrent.futures import Future
88
from dataclasses import dataclass
99
from typing import Optional
10-
import awsiot
10+
import awsiot, sys
11+
12+
# --------------------------------- ARGUMENT PARSING -----------------------------------------
1113
import argparse
12-
import sys
14+
15+
# --------------------------------- ARGUMENT PARSING END -----------------------------------------
1316

1417

1518
@dataclass

samples/mqtt5_custom_authorizer_connect.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0.
33

4+
from awsiot import mqtt5_client_builder
5+
from awscrt import mqtt5
6+
from concurrent.futures import Future
7+
48
# --------------------------------- ARGUMENT PARSING -----------------------------------------
59
import argparse, uuid
610

711
def parse_sample_input():
812
parser = argparse.ArgumentParser(
9-
description="MQTT5 pub/sub sample (mTLS).",
13+
description="MQTT5 Custom Authorizer Sample.",
1014
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
1115
)
1216

17+
# Connection
1318
parser.add_argument("--endpoint", required=True, dest="input_endpoint", help="IoT endpoint hostname")
14-
parser.add_argument("--ca", dest="input_ca", help="Path to optional CA bundle (PEM)")
19+
parser.add_argument("--ca_file", dest="input_ca", help="Path to optional CA bundle (PEM)")
20+
parser.add_argument("--use_websockets", dest="input_use_websockets",
21+
action="store_const", const=True, default=None,
22+
help="Use WebSockets instead of direct TLS")
1523

24+
# Custom Auth
1625
parser.add_argument("--custom_auth_username", dest="input_custom_auth_username",
1726
help="The name to send when connecting through the custom authorizer (optional)")
1827
parser.add_argument("--custom_auth_authorizer_name", dest="input_custom_authorizer_name",
@@ -25,10 +34,6 @@ def parse_sample_input():
2534
help="Key used to extract the custom authorizer token (optional)")
2635
parser.add_argument("--custom_auth_token_value", dest="input_custom_authorizer_token_value",
2736
help="The opaque token value for the custom authorizer (optional)")
28-
29-
parser.add_argument("--use_websockets", dest="input_use_websockets",
30-
action="store_const", const=True, default=None,
31-
help="Use WebSockets instead of direct TLS")
3237

3338
# Misc
3439
parser.add_argument("--client-id", dest="input_clientId",
@@ -47,13 +52,11 @@ def parse_sample_input():
4752

4853
return args
4954

55+
# args contains all the parsed commandline arguments used by the sample
5056
args = parse_sample_input()
5157

5258
# --------------------------------- ARGUMENT PARSING END -----------------------------------------
5359

54-
from awsiot import mqtt5_client_builder
55-
from awscrt import mqtt5
56-
from concurrent.futures import Future
5760

5861
TIMEOUT = 100
5962

samples/mqtt5_pkcs11_connect.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0.
33

4+
from awsiot import mqtt5_client_builder
5+
from awscrt import mqtt5, io
6+
from concurrent.futures import Future
7+
48
# --------------------------------- ARGUMENT PARSING -----------------------------------------
59
import argparse, uuid
610

711
def parse_sample_input():
812
parser = argparse.ArgumentParser(
9-
description="MQTT5 pub/sub sample (mTLS).",
13+
description="MQTT5 PKCS11 Sample.",
1014
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
1115
)
1216

@@ -15,7 +19,14 @@ def parse_sample_input():
1519
parser.add_argument("--port", type=int, default=8883, dest="input_port", help="Port (8883 mTLS, 443 ALPN)")
1620
parser.add_argument("--cert", required=True, dest="input_cert",
1721
help="Path to the certificate file to use during mTLS connection establishment")
18-
parser.add_argument("--ca", dest="input_ca", help="Path to optional CA bundle (PEM)")
22+
parser.add_argument("--ca_file", dest="input_ca", help="Path to optional CA bundle (PEM)")
23+
24+
# PKCS11
25+
parser.add_argument("--pkcs11_lib", required=True, dest="input_pkcs11_lib_path", help="Path to PKCS#11 Library")
26+
parser.add_argument("--pin", required=True, dest="input_pkcs11_user_pin", help="User PIN for logging into PKCS#11 token")
27+
parser.add_argument("--token_label", dest="input_pkcs11_token_label", help="Label of the PKCS#11 token to use (optional).")
28+
parser.add_argument("--slot_id", dest="input_pkcs11_slot_id", help="Slot ID containing the PKCS#11 token to use (optional).")
29+
parser.add_argument("--key_label", dest="input_pkcs11_key_label", help="Label of private key on the PKCS#11 token (optional).")
1930

2031
# Messaging
2132
parser.add_argument("--topic", default="test/topic", dest="input_topic", help="Topic")
@@ -30,22 +41,14 @@ def parse_sample_input():
3041
# Misc
3142
parser.add_argument("--client-id", dest="input_clientId",
3243
default=f"test-{uuid.uuid4().hex[:8]}", help="Client ID")
33-
34-
parser.add_argument("--pkcs11_lib", required=True, dest="input_pkcs11_lib_path", help="Path to PKCS#11 Library")
35-
parser.add_argument("--pin", required=True, dest="input_pkcs11_user_pin", help="User PIN for logging into PKCS#11 token")
36-
parser.add_argument("--token_label", dest="input_pkcs11_token_label", help="Label of the PKCS#11 token to use (optional).")
37-
parser.add_argument("--slot_id", dest="input_pkcs11_slot_id", help="Slot ID containing the PKCS#11 token to use (optional).")
38-
parser.add_argument("--key_label", dest="input_pkcs11_key_label", help="Label of private key on the PKCS#11 token (optional).")
3944

4045
return parser.parse_args()
4146

47+
# args contains all the parsed commandline arguments used by the sample
4248
args = parse_sample_input()
4349

4450
# --------------------------------- ARGUMENT PARSING END -----------------------------------------
4551

46-
from awscrt import mqtt5, io
47-
from awsiot import mqtt5_client_builder
48-
from concurrent.futures import Future
4952

5053
TIMEOUT = 100
5154

samples/mqtt5_pubsub.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0.
33

4+
from awsiot import mqtt5_client_builder
5+
from awscrt import mqtt5, http
6+
import threading, time, json
7+
from concurrent.futures import Future
8+
49
# --------------------------------- ARGUMENT PARSING -----------------------------------------
510
import argparse, uuid
611

712
def parse_sample_input():
813
parser = argparse.ArgumentParser(
9-
description="MQTT5 pub/sub sample (mTLS).",
14+
description="MQTT5 pub/sub Sample (mTLS).",
1015
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
1116
)
1217

@@ -17,15 +22,15 @@ def parse_sample_input():
1722
help="Path to the certificate file to use during mTLS connection establishment")
1823
parser.add_argument("--key", required=True, dest="input_key",
1924
help="Path to the private key file to use during mTLS connection establishment")
20-
parser.add_argument("--ca", dest="input_ca", help="Path to optional CA bundle (PEM)")
25+
parser.add_argument("--ca_file", dest="input_ca", help="Path to optional CA bundle (PEM)")
2126

2227
# Messaging
2328
parser.add_argument("--topic", default="test/topic", dest="input_topic", help="Topic")
2429
parser.add_argument("--message", default="Hello from mqtt5 sample", dest="input_message", help="Message payload")
2530
parser.add_argument("--count", default=5, dest="input_count",
2631
help="Messages to publish (0 = infinite)")
2732

28-
# Proxy (optional)
33+
# Proxy
2934
parser.add_argument("--proxy-host", dest="input_proxy_host", help="HTTP proxy host")
3035
parser.add_argument("--proxy-port", type=int, default=0, dest="input_proxy_port", help="HTTP proxy port")
3136

@@ -35,14 +40,11 @@ def parse_sample_input():
3540

3641
return parser.parse_args()
3742

43+
# args contains all the parsed commandline arguments used by the sample
3844
args = parse_sample_input()
3945

4046
# --------------------------------- ARGUMENT PARSING END -----------------------------------------
4147

42-
from awsiot import mqtt5_client_builder
43-
from awscrt import mqtt5, http
44-
import threading, time, json
45-
from concurrent.futures import Future
4648

4749
TIMEOUT = 100
4850
topic_filter = "test/topic"

samples/mqtt5_shared_subscription.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0.
33

4+
from awsiot import mqtt5_client_builder
5+
from awscrt import mqtt5
6+
from concurrent.futures import Future
7+
import time, json
8+
49
# --------------------------------- ARGUMENT PARSING -----------------------------------------
510
import argparse, uuid
611

712
def parse_sample_input():
813
parser = argparse.ArgumentParser(
9-
description="MQTT5 pub/sub sample (mTLS).",
14+
description="MQTT5 Shared Subscription Sample.",
1015
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
1116
)
1217

@@ -17,13 +22,15 @@ def parse_sample_input():
1722
help="Path to the certificate file to use during mTLS connection establishment")
1823
parser.add_argument("--key", required=True, dest="input_key",
1924
help="Path to the private key file to use during mTLS connection establishment")
20-
parser.add_argument("--ca", dest="input_ca", help="Path to optional CA bundle (PEM)")
25+
parser.add_argument("--ca_file", dest="input_ca", help="Path to optional CA bundle (PEM)")
2126

2227
# Messaging
2328
parser.add_argument("--topic", default="test/topic", dest="input_topic", help="Topic")
2429
parser.add_argument("--message", default="Hello from mqtt5 sample", dest="input_message", help="Message payload")
2530
parser.add_argument("--count", default=5, dest="input_count",
2631
help="Messages to publish (0 = infinite)")
32+
parser.add_argument("--group_identifier", default="python-sample", dest="input_group_identifier",
33+
help="The group identifier to use in the shared subscription (optional, default='python-sample').")
2734

2835
# Proxy (optional)
2936
parser.add_argument("--proxy-host", dest="input_proxy_host", help="HTTP proxy host")
@@ -32,22 +39,14 @@ def parse_sample_input():
3239
# Misc
3340
parser.add_argument("--client-id", dest="input_clientId", default=f"test-{uuid.uuid4().hex[:8]}",
3441
help="Client ID to use for MQTT5 connection (optional, default=None). Note that '1', '2', and '3' will be added for to the given clientIDs since this sample uses 3 clients.")
35-
36-
parser.add_argument("--group_identifier", default="python-sample", dest="input_group_identifier",
37-
help="The group identifier to use in the shared subscription (optional, default='python-sample').")
3842

3943
return parser.parse_args()
4044

45+
# args contains all the parsed commandline arguments used by the sample
4146
args = parse_sample_input()
4247

4348
# --------------------------------- ARGUMENT PARSING END -----------------------------------------
4449

45-
from awscrt import mqtt5
46-
from awsiot import mqtt5_client_builder
47-
import threading
48-
from concurrent.futures import Future
49-
import time
50-
import json
5150

5251
# For the purposes of this sample, we need to associate certain variables with a particular MQTT5 client
5352
# and to do so we use this class to hold all the data for a particular client used in the sample.

samples/shadow.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0.
33

4-
from awscrt import mqtt5, mqtt_request_response, io
54
from awsiot import iotshadow, mqtt5_client_builder
5+
from awscrt import mqtt5, mqtt_request_response
66
from concurrent.futures import Future
77
from dataclasses import dataclass
88
from typing import Optional
9-
import awsiot
9+
import awsiot, json, sys
10+
11+
# --------------------------------- ARGUMENT PARSING -----------------------------------------
1012
import argparse
11-
import json
12-
import sys
13+
# --------------------------------- ARGUMENT PARSING END -----------------------------------------
14+
1315

1416

1517
@dataclass

0 commit comments

Comments
 (0)