Skip to content

Commit 5bba67b

Browse files
authored
feat(iot): add iot hub and device datasource (#1262)
1 parent 7a04fe0 commit 5bba67b

9 files changed

+2267
-0
lines changed

docs/data-sources/iot_device.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
layout: "scaleway"
3+
page_title: "Scaleway: scaleway_iot_device"
4+
description: |-
5+
Gets information about an IOT Device.
6+
---
7+
8+
# scaleway_iot_device
9+
10+
Gets information about an IOT Device.
11+
12+
## Example Usage
13+
14+
```hcl
15+
# Get info by name
16+
data "scaleway_iot_device" "my_device" {
17+
name = "foobar"
18+
}
19+
20+
# Get info by name and hub_id
21+
data "scaleway_iot_device" "my_device" {
22+
name = "foobar"
23+
hub_id = "11111111-1111-1111-1111-111111111111"
24+
}
25+
26+
# Get info by device ID
27+
data "scaleway_iot_device" "my_device" {
28+
device_id = "11111111-1111-1111-1111-111111111111"
29+
}
30+
31+
```
32+
33+
## Argument Reference
34+
35+
- `name` - (Optional) The name of the Hub.
36+
Only one of the `name` and `device_id` should be specified.
37+
38+
- `hub_id` - (Optional) The hub ID.
39+
40+
- `device_id` - (Optional) The device ID.
41+
Only one of the `name` and `device_id` should be specified.
42+
43+
- `region` - (Default to [provider](../index.md) `region`) The [region](../guides/regions_and_zones.md#zones) in which the hub exists.

docs/data-sources/iot_hub.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
layout: "scaleway"
3+
page_title: "Scaleway: scaleway_iot_hub"
4+
description: |-
5+
Gets information about an IOT Hub.
6+
---
7+
8+
# scaleway_iot_hub
9+
10+
Gets information about an IOT Hub.
11+
12+
## Example Usage
13+
14+
```hcl
15+
# Get info by name
16+
data "scaleway_iot_hub" "my_hub" {
17+
name = "foobar"
18+
}
19+
20+
# Get info by hub ID
21+
data "scaleway_iot_hub" "my_hub" {
22+
hub_id = "11111111-1111-1111-1111-111111111111"
23+
}
24+
25+
```
26+
27+
## Argument Reference
28+
29+
- `name` - (Optional) The name of the Hub.
30+
Only one of the `name` and `hub_id` should be specified.
31+
32+
- `hub_id` - (Optional) The Hub ID.
33+
Only one of the `name` and `hub_id` should be specified.
34+
35+
- `region` - (Default to [provider](../index.md) `region`) The [region](../guides/regions_and_zones.md#zones) in which the hub exists.
36+
37+
- `project_id` - (Default to [provider](../index.md) `project_id`)

scaleway/data_source_iot_device.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package scaleway
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/scaleway/scaleway-sdk-go/api/iot/v1"
9+
)
10+
11+
func dataSourceScalewayIotDevice() *schema.Resource {
12+
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayIotDevice().Schema)
13+
14+
addOptionalFieldsToSchema(dsSchema, "name", "region")
15+
16+
dsSchema["name"].ConflictsWith = []string{"device_id"}
17+
dsSchema["hub_id"].Optional = true
18+
dsSchema["device_id"] = &schema.Schema{
19+
Type: schema.TypeString,
20+
Optional: true,
21+
Description: "The ID of the IOT Device",
22+
ConflictsWith: []string{"name"},
23+
ValidateFunc: validationUUIDorUUIDWithLocality(),
24+
}
25+
26+
return &schema.Resource{
27+
ReadContext: dataSourceScalewayIotDeviceRead,
28+
Schema: dsSchema,
29+
}
30+
}
31+
32+
func dataSourceScalewayIotDeviceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
33+
api, region, err := iotAPIWithRegion(d, meta)
34+
if err != nil {
35+
return diag.FromErr(err)
36+
}
37+
38+
deviceID, ok := d.GetOk("device_id")
39+
if !ok {
40+
hubID, hubIDExists := d.GetOk("hub_id")
41+
if hubIDExists {
42+
_, hubID, err = parseRegionalID(hubID.(string))
43+
if err != nil {
44+
return diag.FromErr(err)
45+
}
46+
}
47+
res, err := api.ListDevices(&iot.ListDevicesRequest{
48+
Region: region,
49+
Name: expandStringPtr(d.Get("name")),
50+
HubID: expandStringPtr(hubID),
51+
})
52+
if err != nil {
53+
return diag.FromErr(err)
54+
}
55+
for _, device := range res.Devices {
56+
if device.Name == d.Get("name").(string) {
57+
if deviceID != "" {
58+
return diag.Errorf("more than 1 device with the same name %s", d.Get("name"))
59+
}
60+
deviceID = device.ID
61+
}
62+
}
63+
if deviceID == "" {
64+
return diag.Errorf("no device found with the name %s", d.Get("name"))
65+
}
66+
}
67+
68+
regionalID := datasourceNewRegionalizedID(deviceID, region)
69+
d.SetId(regionalID)
70+
err = d.Set("device_id", regionalID)
71+
if err != nil {
72+
return diag.FromErr(err)
73+
}
74+
diags := resourceScalewayIotDeviceRead(ctx, d, meta)
75+
if diags != nil {
76+
return diags
77+
}
78+
if d.Id() == "" {
79+
return diag.Errorf("IOT Device not found (%s)", regionalID)
80+
}
81+
return nil
82+
}
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package scaleway
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccScalewayDataSourceIotDevice_Basic(t *testing.T) {
10+
tt := NewTestTools(t)
11+
defer tt.Cleanup()
12+
resource.ParallelTest(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheck(t) },
14+
ProviderFactories: tt.ProviderFactories,
15+
CheckDestroy: testAccCheckScalewayIotHubDestroy(tt),
16+
Steps: []resource.TestStep{
17+
{
18+
Config: `
19+
resource "scaleway_iot_device" "test" {
20+
name = "test_iot_device_datasource"
21+
hub_id = scaleway_iot_hub.test.id
22+
}
23+
24+
resource "scaleway_iot_hub" "test" {
25+
name = "test_iot_hub_datasource"
26+
product_plan = "plan_shared"
27+
}
28+
29+
data "scaleway_iot_device" "by_name" {
30+
name = scaleway_iot_device.test.name
31+
}
32+
33+
data "scaleway_iot_device" "by_name_and_hub" {
34+
name = scaleway_iot_device.test.name
35+
hub_id = scaleway_iot_hub.test.id
36+
}
37+
38+
data "scaleway_iot_device" "by_id" {
39+
device_id = scaleway_iot_device.test.id
40+
}
41+
`,
42+
Check: resource.ComposeTestCheckFunc(
43+
testAccCheckScalewayIotDeviceExists(tt, "scaleway_iot_device.test"),
44+
45+
resource.TestCheckResourceAttr("data.scaleway_iot_device.by_name", "name", "test_iot_device_datasource"),
46+
resource.TestCheckResourceAttrSet("data.scaleway_iot_device.by_name", "id"),
47+
48+
resource.TestCheckResourceAttr("data.scaleway_iot_device.by_name_and_hub", "name", "test_iot_device_datasource"),
49+
resource.TestCheckResourceAttrSet("data.scaleway_iot_device.by_name_and_hub", "id"),
50+
51+
resource.TestCheckResourceAttr("data.scaleway_iot_device.by_id", "name", "test_iot_device_datasource"),
52+
resource.TestCheckResourceAttrSet("data.scaleway_iot_device.by_id", "id"),
53+
),
54+
},
55+
},
56+
})
57+
}

scaleway/data_source_iot_hub.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package scaleway
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/scaleway/scaleway-sdk-go/api/iot/v1"
9+
"github.com/scaleway/scaleway-sdk-go/scw"
10+
)
11+
12+
func dataSourceScalewayIotHub() *schema.Resource {
13+
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayIotHub().Schema)
14+
15+
addOptionalFieldsToSchema(dsSchema, "name", "region")
16+
17+
dsSchema["name"].ConflictsWith = []string{"hub_id"}
18+
dsSchema["hub_id"] = &schema.Schema{
19+
Type: schema.TypeString,
20+
Optional: true,
21+
Description: "The ID of the IOT Hub",
22+
ConflictsWith: []string{"name"},
23+
ValidateFunc: validationUUIDorUUIDWithLocality(),
24+
}
25+
26+
return &schema.Resource{
27+
ReadContext: dataSourceScalewayIotHubRead,
28+
Schema: dsSchema,
29+
}
30+
}
31+
32+
func dataSourceScalewayIotHubRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
33+
api, region, err := iotAPIWithRegion(d, meta)
34+
if err != nil {
35+
return diag.FromErr(err)
36+
}
37+
38+
hubID, ok := d.GetOk("hub_id")
39+
if !ok {
40+
res, err := api.ListHubs(&iot.ListHubsRequest{
41+
Region: region,
42+
ProjectID: expandStringPtr(d.Get("project_id")),
43+
Name: expandStringPtr(d.Get("name")),
44+
}, scw.WithContext(ctx))
45+
if err != nil {
46+
return diag.FromErr(err)
47+
}
48+
for _, hub := range res.Hubs {
49+
if hub.Name == d.Get("name").(string) {
50+
if hubID != "" {
51+
return diag.Errorf("more than 1 hub with the same name %s", d.Get("name"))
52+
}
53+
hubID = hub.ID
54+
}
55+
}
56+
if hubID == "" {
57+
return diag.Errorf("no hub found with the name %s", d.Get("name"))
58+
}
59+
}
60+
61+
regionalID := datasourceNewRegionalizedID(hubID, region)
62+
d.SetId(regionalID)
63+
err = d.Set("hub_id", regionalID)
64+
if err != nil {
65+
return diag.FromErr(err)
66+
}
67+
diags := resourceScalewayIotHubRead(ctx, d, meta)
68+
if diags != nil {
69+
return diags
70+
}
71+
if d.Id() == "" {
72+
return diag.Errorf("IOT Hub not found (%s)", regionalID)
73+
}
74+
return nil
75+
}

scaleway/data_source_iot_hub_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package scaleway
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccScalewayDataSourceIotHub_Basic(t *testing.T) {
10+
tt := NewTestTools(t)
11+
defer tt.Cleanup()
12+
resource.ParallelTest(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheck(t) },
14+
ProviderFactories: tt.ProviderFactories,
15+
CheckDestroy: testAccCheckScalewayIotHubDestroy(tt),
16+
Steps: []resource.TestStep{
17+
{
18+
Config: `
19+
resource "scaleway_iot_hub" "test" {
20+
name = "test_iot_hub_datasource"
21+
product_plan = "plan_shared"
22+
}
23+
24+
data "scaleway_iot_hub" "by_name" {
25+
name = scaleway_iot_hub.test.name
26+
}
27+
28+
data "scaleway_iot_hub" "by_id" {
29+
hub_id = scaleway_iot_hub.test.id
30+
}
31+
`,
32+
Check: resource.ComposeTestCheckFunc(
33+
testAccCheckScalewayIotHubExists(tt, "scaleway_iot_hub.test"),
34+
35+
resource.TestCheckResourceAttr("data.scaleway_iot_hub.by_name", "name", "test_iot_hub_datasource"),
36+
resource.TestCheckResourceAttrSet("data.scaleway_iot_hub.by_name", "id"),
37+
38+
resource.TestCheckResourceAttr("data.scaleway_iot_hub.by_id", "name", "test_iot_hub_datasource"),
39+
resource.TestCheckResourceAttrSet("data.scaleway_iot_hub.by_id", "id"),
40+
),
41+
},
42+
},
43+
})
44+
}

scaleway/provider.go

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
122122
"scaleway_instance_server": dataSourceScalewayInstanceServer(),
123123
"scaleway_instance_image": dataSourceScalewayInstanceImage(),
124124
"scaleway_instance_volume": dataSourceScalewayInstanceVolume(),
125+
"scaleway_iot_hub": dataSourceScalewayIotHub(),
126+
"scaleway_iot_device": dataSourceScalewayIotDevice(),
125127
"scaleway_k8s_cluster": dataSourceScalewayK8SCluster(),
126128
"scaleway_k8s_pool": dataSourceScalewayK8SPool(),
127129
"scaleway_lb": dataSourceScalewayLb(),

0 commit comments

Comments
 (0)