|
| 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