|
| 1 | +package mqtt |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + mqttv20240516 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mqtt/v20240516" |
| 11 | + |
| 12 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 13 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 14 | +) |
| 15 | + |
| 16 | +func ResourceTencentCloudMqttHttpAuthenticator() *schema.Resource { |
| 17 | + return &schema.Resource{ |
| 18 | + Create: resourceTencentCloudMqttHttpAuthenticatorCreate, |
| 19 | + Read: resourceTencentCloudMqttHttpAuthenticatorRead, |
| 20 | + Update: resourceTencentCloudMqttHttpAuthenticatorUpdate, |
| 21 | + Delete: resourceTencentCloudMqttHttpAuthenticatorDelete, |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "instance_id": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Required: true, |
| 26 | + Description: "Instance ID.", |
| 27 | + }, |
| 28 | + |
| 29 | + "endpoint": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Required: true, |
| 32 | + Description: "JWKS endpoint.", |
| 33 | + }, |
| 34 | + |
| 35 | + "concurrency": { |
| 36 | + Type: schema.TypeInt, |
| 37 | + Optional: true, |
| 38 | + Description: "Maximum concurrent connections, default 8, range: 1-20.", |
| 39 | + }, |
| 40 | + |
| 41 | + "method": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Optional: true, |
| 44 | + Description: "Network request method Get or Post, default Post.", |
| 45 | + }, |
| 46 | + |
| 47 | + "remark": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Optional: true, |
| 50 | + Description: "Remark.", |
| 51 | + }, |
| 52 | + |
| 53 | + "connect_timeout": { |
| 54 | + Type: schema.TypeInt, |
| 55 | + Optional: true, |
| 56 | + Description: "Connection timeout, unit: seconds, range: 1-30.", |
| 57 | + }, |
| 58 | + |
| 59 | + "read_timeout": { |
| 60 | + Type: schema.TypeInt, |
| 61 | + Optional: true, |
| 62 | + Description: "Request timeout, unit: seconds, range: 1-30.", |
| 63 | + }, |
| 64 | + |
| 65 | + "header": { |
| 66 | + Type: schema.TypeList, |
| 67 | + Optional: true, |
| 68 | + Description: "Forwarding request header.", |
| 69 | + Elem: &schema.Resource{ |
| 70 | + Schema: map[string]*schema.Schema{ |
| 71 | + "key": { |
| 72 | + Type: schema.TypeString, |
| 73 | + Required: true, |
| 74 | + Description: "Header key.", |
| 75 | + }, |
| 76 | + "value": { |
| 77 | + Type: schema.TypeString, |
| 78 | + Required: true, |
| 79 | + Description: "Header value.", |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + |
| 85 | + "body": { |
| 86 | + Type: schema.TypeList, |
| 87 | + Optional: true, |
| 88 | + Description: "Forwarding request body.", |
| 89 | + Elem: &schema.Resource{ |
| 90 | + Schema: map[string]*schema.Schema{ |
| 91 | + "key": { |
| 92 | + Type: schema.TypeString, |
| 93 | + Required: true, |
| 94 | + Description: "Body key.", |
| 95 | + }, |
| 96 | + "value": { |
| 97 | + Type: schema.TypeString, |
| 98 | + Required: true, |
| 99 | + Description: "Body key.", |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func resourceTencentCloudMqttHttpAuthenticatorCreate(d *schema.ResourceData, meta interface{}) error { |
| 109 | + defer tccommon.LogElapsed("resource.tencentcloud_mqtt_http_authenticator.create")() |
| 110 | + defer tccommon.InconsistentCheck(d, meta)() |
| 111 | + |
| 112 | + var ( |
| 113 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 114 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 115 | + request = mqttv20240516.NewCreateHttpAuthenticatorRequest() |
| 116 | + instanceId string |
| 117 | + ) |
| 118 | + |
| 119 | + if v, ok := d.GetOk("instance_id"); ok { |
| 120 | + request.InstanceId = helper.String(v.(string)) |
| 121 | + instanceId = v.(string) |
| 122 | + } |
| 123 | + |
| 124 | + if v, ok := d.GetOk("endpoint"); ok { |
| 125 | + request.Endpoint = helper.String(v.(string)) |
| 126 | + } |
| 127 | + |
| 128 | + if v, ok := d.GetOkExists("concurrency"); ok { |
| 129 | + request.Concurrency = helper.IntInt64(v.(int)) |
| 130 | + } |
| 131 | + |
| 132 | + if v, ok := d.GetOk("method"); ok { |
| 133 | + request.Method = helper.String(v.(string)) |
| 134 | + } |
| 135 | + |
| 136 | + if v, ok := d.GetOk("remark"); ok { |
| 137 | + request.Remark = helper.String(v.(string)) |
| 138 | + } |
| 139 | + |
| 140 | + if v, ok := d.GetOkExists("connect_timeout"); ok { |
| 141 | + request.ConnectTimeout = helper.IntInt64(v.(int)) |
| 142 | + } |
| 143 | + |
| 144 | + if v, ok := d.GetOkExists("read_timeout"); ok { |
| 145 | + request.ReadTimeout = helper.IntInt64(v.(int)) |
| 146 | + } |
| 147 | + |
| 148 | + if v, ok := d.GetOk("header"); ok { |
| 149 | + for _, item := range v.([]interface{}) { |
| 150 | + headerMap := item.(map[string]interface{}) |
| 151 | + headerItem := mqttv20240516.HeaderItem{} |
| 152 | + if v, ok := headerMap["key"].(string); ok && v != "" { |
| 153 | + headerItem.Key = helper.String(v) |
| 154 | + } |
| 155 | + |
| 156 | + if v, ok := headerMap["value"].(string); ok && v != "" { |
| 157 | + headerItem.Value = helper.String(v) |
| 158 | + } |
| 159 | + |
| 160 | + request.Header = append(request.Header, &headerItem) |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + if v, ok := d.GetOk("body"); ok { |
| 165 | + for _, item := range v.([]interface{}) { |
| 166 | + bodyMap := item.(map[string]interface{}) |
| 167 | + bodyItem := mqttv20240516.BodyItem{} |
| 168 | + if v, ok := bodyMap["key"].(string); ok && v != "" { |
| 169 | + bodyItem.Key = helper.String(v) |
| 170 | + } |
| 171 | + |
| 172 | + if v, ok := bodyMap["value"].(string); ok && v != "" { |
| 173 | + bodyItem.Value = helper.String(v) |
| 174 | + } |
| 175 | + |
| 176 | + request.Body = append(request.Body, &bodyItem) |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 181 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseMqttV20240516Client().CreateHttpAuthenticatorWithContext(ctx, request) |
| 182 | + if e != nil { |
| 183 | + return tccommon.RetryError(e) |
| 184 | + } else { |
| 185 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 186 | + } |
| 187 | + |
| 188 | + if result == nil || result.Response == nil { |
| 189 | + return resource.NonRetryableError(fmt.Errorf("Create mqtt http authenticator failed, Response is nil.")) |
| 190 | + } |
| 191 | + |
| 192 | + return nil |
| 193 | + }) |
| 194 | + |
| 195 | + if reqErr != nil { |
| 196 | + log.Printf("[CRITAL]%s create mqtt http authenticator failed, reason:%+v", logId, reqErr) |
| 197 | + return reqErr |
| 198 | + } |
| 199 | + |
| 200 | + d.SetId(instanceId) |
| 201 | + |
| 202 | + return resourceTencentCloudMqttHttpAuthenticatorRead(d, meta) |
| 203 | +} |
| 204 | + |
| 205 | +func resourceTencentCloudMqttHttpAuthenticatorRead(d *schema.ResourceData, meta interface{}) error { |
| 206 | + defer tccommon.LogElapsed("resource.tencentcloud_mqtt_http_authenticator.read")() |
| 207 | + defer tccommon.InconsistentCheck(d, meta)() |
| 208 | + |
| 209 | + var ( |
| 210 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 211 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 212 | + service = MqttService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 213 | + instanceId = d.Id() |
| 214 | + ) |
| 215 | + |
| 216 | + respData, err := service.DescribeMqttHttpAuthenticatorById(ctx, instanceId) |
| 217 | + if err != nil { |
| 218 | + return err |
| 219 | + } |
| 220 | + |
| 221 | + if respData == nil { |
| 222 | + d.SetId("") |
| 223 | + log.Printf("[WARN]%s resource `mqtt_http_authenticator` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 224 | + return nil |
| 225 | + } |
| 226 | + |
| 227 | + return nil |
| 228 | +} |
| 229 | + |
| 230 | +func resourceTencentCloudMqttHttpAuthenticatorUpdate(d *schema.ResourceData, meta interface{}) error { |
| 231 | + defer tccommon.LogElapsed("resource.tencentcloud_mqtt_http_authenticator.update")() |
| 232 | + defer tccommon.InconsistentCheck(d, meta)() |
| 233 | + |
| 234 | + var ( |
| 235 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 236 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 237 | + instanceId = d.Id() |
| 238 | + ) |
| 239 | + |
| 240 | + request := mqttv20240516.NewModifyHttpAuthenticatorRequest() |
| 241 | + request.InstanceId = &instanceId |
| 242 | + |
| 243 | + if v, ok := d.GetOk("endpoint"); ok { |
| 244 | + request.Endpoint = helper.String(v.(string)) |
| 245 | + } |
| 246 | + |
| 247 | + if v, ok := d.GetOkExists("concurrency"); ok { |
| 248 | + request.Concurrency = helper.IntInt64(v.(int)) |
| 249 | + } |
| 250 | + |
| 251 | + if v, ok := d.GetOkExists("connect_timeout"); ok { |
| 252 | + request.ConnectTimeout = helper.IntInt64(v.(int)) |
| 253 | + } |
| 254 | + |
| 255 | + if v, ok := d.GetOkExists("read_timeout"); ok { |
| 256 | + request.ReadTimeout = helper.IntInt64(v.(int)) |
| 257 | + } |
| 258 | + |
| 259 | + if v, ok := d.GetOk("remark"); ok { |
| 260 | + request.Remark = helper.String(v.(string)) |
| 261 | + } |
| 262 | + |
| 263 | + if v, ok := d.GetOk("method"); ok { |
| 264 | + request.Method = helper.String(v.(string)) |
| 265 | + } |
| 266 | + |
| 267 | + if v, ok := d.GetOk("header"); ok { |
| 268 | + for _, item := range v.([]interface{}) { |
| 269 | + headerMap := item.(map[string]interface{}) |
| 270 | + headerItem := mqttv20240516.HeaderItem{} |
| 271 | + if v, ok := headerMap["key"].(string); ok && v != "" { |
| 272 | + headerItem.Key = helper.String(v) |
| 273 | + } |
| 274 | + |
| 275 | + if v, ok := headerMap["value"].(string); ok && v != "" { |
| 276 | + headerItem.Value = helper.String(v) |
| 277 | + } |
| 278 | + |
| 279 | + request.Header = append(request.Header, &headerItem) |
| 280 | + } |
| 281 | + } |
| 282 | + |
| 283 | + if v, ok := d.GetOk("body"); ok { |
| 284 | + for _, item := range v.([]interface{}) { |
| 285 | + bodyMap := item.(map[string]interface{}) |
| 286 | + bodyItem := mqttv20240516.BodyItem{} |
| 287 | + if v, ok := bodyMap["key"].(string); ok && v != "" { |
| 288 | + bodyItem.Key = helper.String(v) |
| 289 | + } |
| 290 | + |
| 291 | + if v, ok := bodyMap["value"].(string); ok && v != "" { |
| 292 | + bodyItem.Value = helper.String(v) |
| 293 | + } |
| 294 | + |
| 295 | + request.Body = append(request.Body, &bodyItem) |
| 296 | + } |
| 297 | + } |
| 298 | + |
| 299 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 300 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseMqttV20240516Client().ModifyHttpAuthenticatorWithContext(ctx, request) |
| 301 | + if e != nil { |
| 302 | + return tccommon.RetryError(e) |
| 303 | + } else { |
| 304 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 305 | + } |
| 306 | + |
| 307 | + return nil |
| 308 | + }) |
| 309 | + |
| 310 | + if reqErr != nil { |
| 311 | + log.Printf("[CRITAL]%s update mqtt http authenticator failed, reason:%+v", logId, reqErr) |
| 312 | + return reqErr |
| 313 | + } |
| 314 | + |
| 315 | + return resourceTencentCloudMqttHttpAuthenticatorRead(d, meta) |
| 316 | +} |
| 317 | + |
| 318 | +func resourceTencentCloudMqttHttpAuthenticatorDelete(d *schema.ResourceData, meta interface{}) error { |
| 319 | + defer tccommon.LogElapsed("resource.tencentcloud_mqtt_http_authenticator.delete")() |
| 320 | + defer tccommon.InconsistentCheck(d, meta)() |
| 321 | + |
| 322 | + var ( |
| 323 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 324 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 325 | + request = mqttv20240516.NewDeleteAuthenticatorRequest() |
| 326 | + instanceId = d.Id() |
| 327 | + ) |
| 328 | + |
| 329 | + request.InstanceId = &instanceId |
| 330 | + request.Type = helper.String("HTTP") |
| 331 | + |
| 332 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 333 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseMqttV20240516Client().DeleteAuthenticatorWithContext(ctx, request) |
| 334 | + if e != nil { |
| 335 | + return tccommon.RetryError(e) |
| 336 | + } else { |
| 337 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 338 | + } |
| 339 | + |
| 340 | + return nil |
| 341 | + }) |
| 342 | + |
| 343 | + if reqErr != nil { |
| 344 | + log.Printf("[CRITAL]%s delete mqtt http authenticator failed, reason:%+v", logId, reqErr) |
| 345 | + return reqErr |
| 346 | + } |
| 347 | + |
| 348 | + return nil |
| 349 | +} |
0 commit comments