Skip to content

Commit 345ae81

Browse files
feat(iotda): add a new datasource to get list of custom authentications (#6451)
1 parent 91cd54c commit 345ae81

File tree

4 files changed

+334
-0
lines changed

4 files changed

+334
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
subcategory: "IoT Device Access (IoTDA)"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_iotda_custom_authentications"
5+
description: |-
6+
Use this data source to get the list of IoTDA custom authentications.
7+
---
8+
9+
# huaweicloud_iotda_custom_authentications
10+
11+
Use this data source to get the list of IoTDA custom authentications.
12+
13+
-> When accessing an IoTDA **standard** or **enterprise** edition instance, you need to specify the IoTDA service
14+
endpoint in `provider` block.
15+
You can login to the IoTDA console, choose the instance **Overview** and click **Access Details**
16+
to view the HTTPS application access address. An example of the access address might be
17+
**9bc34xxxxx.st1.iotda-app.ap-southeast-1.myhuaweicloud.com**, then you need to configure the
18+
`provider` block as follows:
19+
20+
```hcl
21+
provider "huaweicloud" {
22+
endpoints = {
23+
iotda = "https://9bc34xxxxx.st1.iotda-app.ap-southeast-1.myhuaweicloud.com"
24+
}
25+
}
26+
```
27+
28+
## Example Usage
29+
30+
```hcl
31+
data "huaweicloud_iotda_custom_authentications" "test" {}
32+
```
33+
34+
## Argument Reference
35+
36+
The following arguments are supported:
37+
38+
* `region` - (Optional, String) Specifies the region in which to query the resource.
39+
If omitted, the provider-level region will be used.
40+
41+
* `authorizer_name` - (Optional, String) Specifies the name of the custom authentication.
42+
43+
## Attribute Reference
44+
45+
In addition to all arguments above, the following attributes are exported:
46+
47+
* `id` - The data source ID.
48+
49+
* `authorizers` - The list of the custom authentications.
50+
The [authorizers](#iotda_authorizers) structure is documented below.
51+
52+
<a name="iotda_authorizers"></a>
53+
The `authorizers` block supports:
54+
55+
* `authorizer_id` - The ID of the custom authentication.
56+
57+
* `authorizer_name` - The name of the custom authentication.
58+
59+
* `func_name` - The name of the function associated with the custom authentication.
60+
61+
* `func_urn` - The URN of the function associated with the custom authentication.
62+
63+
* `signing_enable` - Whether to enable signature authentication.
64+
65+
* `default_authorizer` - Whether the custom authentication is the default authentication mode.
66+
67+
* `status` - Whether to enable the custom authentication mode.
68+
69+
* `cache_enable` - Whether to enable the cache function.
70+
71+
* `create_time` - The creation time of the custom authentication.
72+
The format is **yyyyMMdd'T'HHmmss'Z'**. e.g. **20151212T121212Z**.
73+
74+
* `update_time` - The latest update time of the custom authentication.
75+
The format is **yyyyMMdd'T'HHmmss'Z'**. e.g. **20151212T121212Z**.

huaweicloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ func Provider() *schema.Provider {
10021002
"huaweicloud_iotda_device_binding_groups": iotda.DataSourceDeviceBindingGroups(),
10031003
"huaweicloud_iotda_amqps": iotda.DataSourceAMQPQueues(),
10041004
"huaweicloud_iotda_batchtasks": iotda.DataSourceBatchTasks(),
1005+
"huaweicloud_iotda_custom_authentications": iotda.DataSourceCustomAuthentications(),
10051006
"huaweicloud_iotda_dataforwarding_rules": iotda.DataSourceDataForwardingRules(),
10061007
"huaweicloud_iotda_data_flow_control_policies": iotda.DataSourceDataFlowControlPolicies(),
10071008
"huaweicloud_iotda_data_backlog_policies": iotda.DataSourceDataBacklogPolicies(),
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package iotda
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
9+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
10+
)
11+
12+
func TestAccDataSourceCustomAuthentications_basic(t *testing.T) {
13+
var (
14+
dataSourceName = "data.huaweicloud_iotda_custom_authentications.test"
15+
dc = acceptance.InitDataSourceCheck(dataSourceName)
16+
name = acceptance.RandomAccResourceName()
17+
)
18+
19+
resource.ParallelTest(t, resource.TestCase{
20+
PreCheck: func() {
21+
acceptance.TestAccPreCheck(t)
22+
acceptance.TestAccPreCheckHWIOTDAAccessAddress(t)
23+
},
24+
ProviderFactories: acceptance.TestAccProviderFactories,
25+
Steps: []resource.TestStep{
26+
{
27+
Config: testAccDataSourceCustomAuthentications_basic(name),
28+
Check: resource.ComposeTestCheckFunc(
29+
dc.CheckResourceExists(),
30+
resource.TestCheckResourceAttrSet(dataSourceName, "authorizers.#"),
31+
resource.TestCheckResourceAttrSet(dataSourceName, "authorizers.0.authorizer_id"),
32+
resource.TestCheckResourceAttrSet(dataSourceName, "authorizers.0.authorizer_name"),
33+
resource.TestCheckResourceAttrSet(dataSourceName, "authorizers.0.func_name"),
34+
resource.TestCheckResourceAttrSet(dataSourceName, "authorizers.0.func_urn"),
35+
resource.TestCheckResourceAttrSet(dataSourceName, "authorizers.0.signing_enable"),
36+
resource.TestCheckResourceAttrSet(dataSourceName, "authorizers.0.default_authorizer"),
37+
resource.TestCheckResourceAttrSet(dataSourceName, "authorizers.0.status"),
38+
resource.TestCheckResourceAttrSet(dataSourceName, "authorizers.0.cache_enable"),
39+
resource.TestCheckResourceAttrSet(dataSourceName, "authorizers.0.create_time"),
40+
resource.TestCheckResourceAttrSet(dataSourceName, "authorizers.0.update_time"),
41+
42+
resource.TestCheckOutput("authorizer_name_filter_useful", "true"),
43+
resource.TestCheckOutput("not_found_validation_pass", "true"),
44+
),
45+
},
46+
},
47+
})
48+
}
49+
50+
func testAccDataSourceCustomAuthentications_basic(name string) string {
51+
return fmt.Sprintf(`
52+
%[1]s
53+
54+
data "huaweicloud_iotda_custom_authentications" "test" {
55+
depends_on = [huaweicloud_iotda_custom_authentication.test]
56+
}
57+
58+
locals {
59+
authorizer_name = data.huaweicloud_iotda_custom_authentications.test.authorizers[0].authorizer_name
60+
}
61+
62+
data "huaweicloud_iotda_custom_authentications" "authorizer_name_filter" {
63+
authorizer_name = local.authorizer_name
64+
}
65+
66+
output "authorizer_name_filter_useful" {
67+
value = length(data.huaweicloud_iotda_custom_authentications.authorizer_name_filter.authorizers) > 0 && alltrue(
68+
[
69+
for v in data.huaweicloud_iotda_custom_authentications.authorizer_name_filter.authorizers[*].authorizer_name :
70+
v == local.authorizer_name
71+
]
72+
)
73+
}
74+
75+
data "huaweicloud_iotda_custom_authentications" "not_found" {
76+
authorizer_name = "resource_not_found"
77+
}
78+
79+
output "not_found_validation_pass" {
80+
value = length(data.huaweicloud_iotda_custom_authentications.not_found.authorizers) == 0
81+
}
82+
`, testAccCustomAuthentication_basic(name))
83+
}
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
package iotda
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/hashicorp/go-multierror"
9+
"github.com/hashicorp/go-uuid"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
13+
"github.com/chnsz/golangsdk"
14+
15+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
16+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
17+
)
18+
19+
// @API IoTDA GET /v5/iot/{project_id}/device-authorizers
20+
func DataSourceCustomAuthentications() *schema.Resource {
21+
return &schema.Resource{
22+
ReadContext: dataSourceCustomAuthenticationsRead,
23+
24+
Schema: map[string]*schema.Schema{
25+
"region": {
26+
Type: schema.TypeString,
27+
Optional: true,
28+
Computed: true,
29+
},
30+
"authorizer_name": {
31+
Type: schema.TypeString,
32+
Optional: true,
33+
},
34+
"authorizers": {
35+
Type: schema.TypeList,
36+
Computed: true,
37+
Elem: &schema.Resource{
38+
Schema: map[string]*schema.Schema{
39+
"authorizer_id": {
40+
Type: schema.TypeString,
41+
Computed: true,
42+
},
43+
"authorizer_name": {
44+
Type: schema.TypeString,
45+
Computed: true,
46+
},
47+
"func_name": {
48+
Type: schema.TypeString,
49+
Computed: true,
50+
},
51+
"func_urn": {
52+
Type: schema.TypeString,
53+
Computed: true,
54+
},
55+
"signing_enable": {
56+
Type: schema.TypeBool,
57+
Computed: true,
58+
},
59+
"default_authorizer": {
60+
Type: schema.TypeBool,
61+
Computed: true,
62+
},
63+
"status": {
64+
Type: schema.TypeString,
65+
Computed: true,
66+
},
67+
"cache_enable": {
68+
Type: schema.TypeBool,
69+
Computed: true,
70+
},
71+
"create_time": {
72+
Type: schema.TypeString,
73+
Computed: true,
74+
},
75+
"update_time": {
76+
Type: schema.TypeString,
77+
Computed: true,
78+
},
79+
},
80+
},
81+
},
82+
},
83+
}
84+
}
85+
86+
func buildCustomAuthenticationsQueryParams(d *schema.ResourceData) string {
87+
queryParams := ""
88+
89+
if v, ok := d.GetOk("authorizer_name"); ok {
90+
queryParams = fmt.Sprintf("%s&authorizer_name=%v", queryParams, v)
91+
}
92+
93+
return queryParams
94+
}
95+
96+
func dataSourceCustomAuthenticationsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
97+
var (
98+
cfg = meta.(*config.Config)
99+
region = cfg.GetRegion(d)
100+
isDerived = WithDerivedAuth(cfg, region)
101+
httpUrl = "v5/iot/{project_id}/device-authorizers?limit=50"
102+
allAuthorizers []interface{}
103+
offset = 0
104+
)
105+
106+
client, err := cfg.NewServiceClientWithDerivedAuth("iotda", region, isDerived)
107+
if err != nil {
108+
return diag.Errorf("error creating IoTDA client: %s", err)
109+
}
110+
111+
listPath := client.Endpoint + httpUrl
112+
listPath = strings.ReplaceAll(listPath, "{project_id}", client.ProjectID)
113+
listPath += buildCustomAuthenticationsQueryParams(d)
114+
115+
listOpt := golangsdk.RequestOpts{
116+
KeepResponseBody: true,
117+
}
118+
for {
119+
requestPathWithOffset := fmt.Sprintf("%s&offset=%d", listPath, offset)
120+
listResp, err := client.Request("GET", requestPathWithOffset, &listOpt)
121+
if err != nil {
122+
return diag.Errorf("error retrieving IoTDA custom authentications: %s", err)
123+
}
124+
125+
listRespBody, err := utils.FlattenResponse(listResp)
126+
if err != nil {
127+
return diag.FromErr(err)
128+
}
129+
130+
authorizers := utils.PathSearch("authorizers", listRespBody, make([]interface{}, 0)).([]interface{})
131+
if len(authorizers) == 0 {
132+
break
133+
}
134+
135+
allAuthorizers = append(allAuthorizers, authorizers...)
136+
offset += len(authorizers)
137+
}
138+
139+
dataSourceId, err := uuid.GenerateUUID()
140+
if err != nil {
141+
return diag.Errorf("unable to generate ID: %s", err)
142+
}
143+
144+
d.SetId(dataSourceId)
145+
146+
mErr := multierror.Append(nil,
147+
d.Set("region", region),
148+
d.Set("authorizers", flattenListCustomAuthentications(allAuthorizers)),
149+
)
150+
151+
return diag.FromErr(mErr.ErrorOrNil())
152+
}
153+
154+
func flattenListCustomAuthentications(authorizers []interface{}) []interface{} {
155+
if len(authorizers) == 0 {
156+
return nil
157+
}
158+
159+
result := make([]interface{}, 0, len(authorizers))
160+
for _, v := range authorizers {
161+
result = append(result, map[string]interface{}{
162+
"authorizer_id": utils.PathSearch("authorizer_id", v, nil),
163+
"authorizer_name": utils.PathSearch("authorizer_name", v, nil),
164+
"func_name": utils.PathSearch("func_name", v, nil),
165+
"func_urn": utils.PathSearch("func_urn", v, nil),
166+
"signing_enable": utils.PathSearch("signing_enable", v, nil),
167+
"default_authorizer": utils.PathSearch("default_authorizer", v, nil),
168+
"status": utils.PathSearch("status", v, nil),
169+
"cache_enable": utils.PathSearch("cache_enable", v, nil),
170+
"create_time": utils.PathSearch("create_time", v, nil),
171+
"update_time": utils.PathSearch("update_time", v, nil),
172+
})
173+
}
174+
return result
175+
}

0 commit comments

Comments
 (0)