Skip to content

Commit e6c6a35

Browse files
Lichard TormanClément Decoodtremyleone
authored
feat(iot): Add IoT Routes resource (#510)
Co-authored-by: Clément Decoodt <[email protected]> Co-authored-by: Rémy Léone <[email protected]>
1 parent 25fa0bb commit e6c6a35

6 files changed

+2881
-0
lines changed

docs/resources/iot_routes.md

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
---
2+
layout: "scaleway"
3+
page_title: "Scaleway: scaleway_iot_routes"
4+
description: |-
5+
Manages Scaleway IoT Routes.
6+
---
7+
8+
# scaleway_iot_routes
9+
10+
-> **Note:** This terraform resource is currently in beta and might include breaking change in future releases.
11+
12+
Creates and manages Scaleway IoT Routes. For more information, see the following:
13+
14+
- [API documentation](https://developers.scaleway.com/en/products/iot/api).
15+
- [Product documentation](https://www.scaleway.com/en/docs/scaleway-iothub-route/)
16+
17+
## Examples
18+
19+
### Database Route
20+
21+
```hcl
22+
resource "scaleway_iot_route" "main" {
23+
name = "default"
24+
hub_id = scaleway_iot_hub.main.id
25+
topic = "#"
26+
database {
27+
query = <<-EOT
28+
INSERT INTO measurements(
29+
push_time,
30+
report_time,
31+
station_id,
32+
temperature,
33+
humidity
34+
) VALUES (
35+
NOW(),
36+
TIMESTAMP 'epoch' + (($PAYLOAD::jsonb->'last_reported')::integer * INTERVAL '1 second'),
37+
($PAYLOAD::jsonb->'station_id')::uuid,
38+
($PAYLOAD::jsonb->'temperature')::decimal,
39+
($PAYLOAD::jsonb->'humidity'):decimal:
40+
);
41+
EOT
42+
host = scaleway_rdb_instance.iot.endpoint_ip
43+
port = scaleway_rdb_instance.iot.endpoint_port
44+
dbname = "rdb"
45+
username = scaleway_rdb_instance.iot.user_name
46+
password = scaleway_rdb_instance.iot.password
47+
}
48+
}
49+
50+
resource "scaleway_iot_hub" "main" {
51+
name = "main"
52+
product_plan = "plan_shared"
53+
}
54+
55+
resource "scaleway_rdb_instance" "iot" {
56+
name = "iot"
57+
node_type = "db-dev-s"
58+
engine = "PostgreSQL-12"
59+
user_name = "root"
60+
password = "T3stP4ssw0rdD0N0tUs3!"
61+
}
62+
```
63+
64+
### S3 Route
65+
66+
```hcl
67+
resource "scaleway_iot_route" "main" {
68+
name = "main"
69+
hub_id = scaleway_iot_hub.main.id
70+
topic = "#"
71+
s3 {
72+
bucket_region = scaleway_object_bucket.main.region
73+
bucket_name = scaleway_object_bucket.main.name
74+
object_prefix = "foo"
75+
strategy = "per_topic"
76+
}
77+
}
78+
79+
resource "scaleway_iot_hub" "main" {
80+
name = "main"
81+
product_plan = "plan_shared"
82+
}
83+
84+
resource "scaleway_object_bucket" "main" {
85+
region = "fr-par"
86+
name = "my_awesome-bucket"
87+
}
88+
```
89+
90+
### Rest Route
91+
92+
```hcl
93+
resource "scaleway_iot_route" "main" {
94+
name = "main"
95+
hub_id = scaleway_iot_hub.main.id
96+
topic = "#"
97+
rest {
98+
verb = "get"
99+
uri = "http://scaleway.com"
100+
headers = {
101+
X-awesome-header = "my-awesome-value"
102+
}
103+
}
104+
}
105+
106+
resource "scaleway_iot_hub" "main" {
107+
name = "main"
108+
product_plan = "plan_shared"
109+
}
110+
```
111+
112+
## Arguments Reference
113+
114+
~> **Important:** Updates to any value will recreate the IoT Route.
115+
116+
The following arguments are supported:
117+
118+
- `name` - (Required) The name of the IoT Route you want to create (e.g. `my-route`).
119+
120+
- `hub_id` - (Required) The hub ID to which the Route will be attached to.
121+
122+
- `topic` - (Required) The topic the Route subscribes to, wildcards allowed (e.g. `thelab/+/temperature/#`).
123+
124+
- `database` - (Optional) Configuration block for the database routes. See [product documentation](https://www.scaleway.com/en/docs/scaleway-iothub-route/#-Database-Route) for a better understanding of the parameters.
125+
- `query` - (Required) The SQL query that will be executed when receiving a message ($TOPIC and $PAYLOAD variables are available, see documentation, e.g. `INSERT INTO mytable(date, topic, value) VALUES (NOW(), $TOPIC, $PAYLOAD)`).
126+
- `host` - (Required) The database hostname. Can be an IP or a FQDN.
127+
- `port` - (Required) The database port (e.g. `5432`)
128+
- `dbname` - (Required) The database name (e.g. `measurements`).
129+
- `username` - (Required) The database username.
130+
- `password` - (Required) The database password.
131+
132+
- `rest` (Optional) - Configuration block for the rest routes. See [product documentation](https://www.scaleway.com/en/docs/scaleway-iothub-route/#-REST-Route) for a better understanding of the parameters.
133+
- `verb` - (Required) The HTTP Verb used to call Rest URI (e.g. `post`).
134+
- `uri` - (Required) The URI of the Rest endpoint (e.g. `https://internal.mycompany.com/ingest/mqttdata`).
135+
- `headers` - (Required) a map of the extra headers to send with the HTTP call (e.g. `X-Header = Value`).
136+
137+
- `s3` (Optional) - Configuration block for the S3 routes. See [product documentation](https://www.scaleway.com/en/docs/scaleway-iothub-route/#-Scaleway-Object-Storage-Route) for a better understanding of the parameters.
138+
- `bucket_region` (Required) - The region of the S3 route's destination bucket (e.g. `fr-par`).
139+
- `bucket_name` (Required) - The name of the S3 route's destination bucket (e.g. `my-object-storage`).
140+
- `object_prefix` (Required) - The string to prefix object names with (e.g. `mykeyprefix-`).
141+
- `strategy` (Required) - How the S3 route's objects will be created (e.g. `per_topic`). See [documentation](https://www.scaleway.com/en/docs/scaleway-iothub-route/#-Messages-Store-Strategies) for behaviour details.
142+
143+
## Attributes Reference
144+
145+
In addition to all arguments above, the following attributes are exported:
146+
147+
- `id` - The ID of the Route.
148+
- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions) in which the Route is attached to.
149+
- `created_at` - The date and time the Route was created.
150+
151+
152+
## Import
153+
154+
IoT Routes can be imported using the `{region}/{id}`, e.g.
155+
156+
```bash
157+
$ terraform import scaleway_iot_route.route01 fr-par/11111111-1111-1111-1111-111111111111
158+
```
159+

scaleway/helpers_iot.go

+11
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,14 @@ func waitIotHub(iotAPI *iot.API, region scw.Region, hubID string, desiredStates
4242

4343
return fmt.Errorf("hub %s has state %s, wants one of %+q", hubID, hub.Status, desiredStates)
4444
}
45+
46+
func extractRestHeaders(d *schema.ResourceData, key string) map[string]string {
47+
stringMap := map[string]string{}
48+
49+
data := d.Get(key).(map[string]interface{})
50+
51+
for k, v := range data {
52+
stringMap[k] = v.(string)
53+
}
54+
return stringMap
55+
}

scaleway/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
7777
"scaleway_instance_private_nic": resourceScalewayInstancePrivateNIC(),
7878
"scaleway_iot_hub": resourceScalewayIotHub(),
7979
"scaleway_iot_device": resourceScalewayIotDevice(),
80+
"scaleway_iot_route": resourceScalewayIotRoute(),
8081
"scaleway_k8s_cluster": resourceScalewayK8SCluster(),
8182
"scaleway_k8s_pool": resourceScalewayK8SPool(),
8283
"scaleway_lb": resourceScalewayLb(),

0 commit comments

Comments
 (0)