diff --git a/.changelog/3362.txt b/.changelog/3362.txt new file mode 100644 index 0000000000..8aff7808c0 --- /dev/null +++ b/.changelog/3362.txt @@ -0,0 +1,11 @@ +```release-note:new-data-source +tencentcloud_mqtt_instances +``` + +```release-note:new-data-source +tencentcloud_mqtt_instance_detail +``` + +```release-note:new-data-source +tencentcloud_mqtt_topics +``` \ No newline at end of file diff --git a/tencentcloud/provider.go b/tencentcloud/provider.go index d05dca4693..b8e61c439d 100644 --- a/tencentcloud/provider.go +++ b/tencentcloud/provider.go @@ -1238,6 +1238,9 @@ func Provider() *schema.Provider { "tencentcloud_cdwpg_log": cdwpg.DataSourceTencentCloudCdwpgLog(), "tencentcloud_cdwpg_nodes": cdwpg.DataSourceTencentCloudCdwpgNodes(), "tencentcloud_mqtt_registration_code": mqtt.DataSourceTencentCloudMqttRegistrationCode(), + "tencentcloud_mqtt_instances": mqtt.DataSourceTencentCloudMqttInstances(), + "tencentcloud_mqtt_instance_detail": mqtt.DataSourceTencentCloudMqttInstanceDetail(), + "tencentcloud_mqtt_topics": mqtt.DataSourceTencentCloudMqttTopics(), }, ResourcesMap: map[string]*schema.Resource{ diff --git a/tencentcloud/provider.md b/tencentcloud/provider.md index cd73c877e0..0f5fa5b9df 100644 --- a/tencentcloud/provider.md +++ b/tencentcloud/provider.md @@ -2319,6 +2319,9 @@ tencentcloud_tcss_image_registry TDMQ for MQTT(MQTT) Data Source tencentcloud_mqtt_registration_code +tencentcloud_mqtt_instances +tencentcloud_mqtt_instance_detail +tencentcloud_mqtt_topics Resource tencentcloud_mqtt_instance tencentcloud_mqtt_instance_public_endpoint diff --git a/tencentcloud/services/mqtt/data_source_tc_mqtt_instance_detail.go b/tencentcloud/services/mqtt/data_source_tc_mqtt_instance_detail.go new file mode 100644 index 0000000000..dd84ea2bc0 --- /dev/null +++ b/tencentcloud/services/mqtt/data_source_tc_mqtt_instance_detail.go @@ -0,0 +1,309 @@ +package mqtt + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + mqttv20240516 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mqtt/v20240516" + + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" +) + +func DataSourceTencentCloudMqttInstanceDetail() *schema.Resource { + return &schema.Resource{ + Read: dataSourceTencentCloudMqttInstanceDetailRead, + Schema: map[string]*schema.Schema{ + "instance_id": { + Type: schema.TypeString, + Required: true, + Description: "Instance ID.", + }, + + // computed + "instance_type": { + Type: schema.TypeString, + Computed: true, + Description: "Instance ID.", + }, + + "instance_name": { + Type: schema.TypeString, + Computed: true, + Description: "Instance type. BASIC- Basic Edition; PRO- professional edition; PLATINUM- Platinum version.", + }, + + "topic_num": { + Type: schema.TypeInt, + Computed: true, + Description: "Topic num.", + }, + + "topic_num_limit": { + Type: schema.TypeInt, + Computed: true, + Description: "Maximum number of instance topics.", + }, + + "tps_limit": { + Type: schema.TypeInt, + Computed: true, + Description: "Elastic TPS current limit value.", + }, + + "created_time": { + Type: schema.TypeInt, + Computed: true, + Description: "Creation time, millisecond timestamp.", + }, + + "remark": { + Type: schema.TypeString, + Computed: true, + Description: "Remark.", + }, + + "instance_status": { + Type: schema.TypeString, + Computed: true, + Description: "Instance status. RUNNING- In operation; MAINTAINING- Under Maintenance; ABNORMAL- abnormal; OVERDUE- Arrears of fees; DESTROYED- Deleted; CREATING- Creating in progress; MODIFYING- In the process of transformation; CREATE_FAILURE- Creation failed; MODIFY_FAILURE- Transformation failed; DELETING- deleting.", + }, + + "sku_code": { + Type: schema.TypeString, + Computed: true, + Description: "Product specifications.", + }, + + "max_subscription_per_client": { + Type: schema.TypeInt, + Computed: true, + Description: "Maximum number of subscriptions per client.", + }, + + "authorization_policy_limit": { + Type: schema.TypeInt, + Computed: true, + Description: "Limit on the number of authorization rules.", + }, + + "client_num_limit": { + Type: schema.TypeInt, + Computed: true, + Description: "Number of client connections online.", + }, + + "device_certificate_provision_type": { + Type: schema.TypeString, + Computed: true, + Description: "Client certificate registration method: JITP: Automatic Registration; API: Manually register through API.", + }, + + "automatic_activation": { + Type: schema.TypeBool, + Computed: true, + Description: "Is it automatically activated when registering device certificates automatically.", + }, + + "renew_flag": { + Type: schema.TypeInt, + Computed: true, + Description: "Whether to renew automatically. Only the annual and monthly package cluster is effective. 1: Automatic renewal; 0: Non automatic renewal.", + }, + + "pay_mode": { + Type: schema.TypeString, + Computed: true, + Description: "Billing mode, POSTPAID, pay as you go PREPAID, annual and monthly package.", + }, + + "expiry_time": { + Type: schema.TypeInt, + Computed: true, + Description: "Expiration time, millisecond level timestamp.", + }, + + "destroy_time": { + Type: schema.TypeInt, + Computed: true, + Description: "Pre destruction time, millisecond timestamp.", + }, + + "x509_mode": { + Type: schema.TypeString, + Computed: true, + Description: "TLS, Unidirectional authentication mTLS, bidirectional authentication BYOC; One machine, one certificate.", + }, + + "max_ca_num": { + Type: schema.TypeInt, + Computed: true, + Description: "Maximum Ca quota.", + }, + + "registration_code": { + Type: schema.TypeString, + Computed: true, + Description: "Certificate registration code.", + }, + + "max_subscription": { + Type: schema.TypeInt, + Computed: true, + Description: "Maximum number of subscriptions in the cluster.", + }, + + "authorization_policy": { + Type: schema.TypeBool, + Computed: true, + Description: "Authorization Policy Switch.", + }, + + "result_output_file": { + Type: schema.TypeString, + Optional: true, + Description: "Used to save results.", + }, + }, + } +} + +func dataSourceTencentCloudMqttInstanceDetailRead(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("data_source.tencentcloud_mqtt_instance_detail.read")() + defer tccommon.InconsistentCheck(d, meta)() + + var ( + logId = tccommon.GetLogId(nil) + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) + service = MqttService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} + instanceId string + ) + + paramMap := make(map[string]interface{}) + if v, ok := d.GetOk("instance_id"); ok { + paramMap["InstanceId"] = helper.String(v.(string)) + instanceId = v.(string) + } + + var respData *mqttv20240516.DescribeInstanceResponseParams + reqErr := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { + result, e := service.DescribeMqttInstanceDetailByFilter(ctx, paramMap) + if e != nil { + return tccommon.RetryError(e) + } + + respData = result + return nil + }) + + if reqErr != nil { + return reqErr + } + + if respData.InstanceType != nil { + _ = d.Set("instance_type", respData.InstanceType) + } + + if respData.InstanceId != nil { + _ = d.Set("instance_id", respData.InstanceId) + } + + if respData.InstanceName != nil { + _ = d.Set("instance_name", respData.InstanceName) + } + + if respData.TopicNum != nil { + _ = d.Set("topic_num", respData.TopicNum) + } + + if respData.TopicNumLimit != nil { + _ = d.Set("topic_num_limit", respData.TopicNumLimit) + } + + if respData.TpsLimit != nil { + _ = d.Set("tps_limit", respData.TpsLimit) + } + + if respData.CreatedTime != nil { + _ = d.Set("created_time", respData.CreatedTime) + } + + if respData.Remark != nil { + _ = d.Set("remark", respData.Remark) + } + + if respData.InstanceStatus != nil { + _ = d.Set("instance_status", respData.InstanceStatus) + } + + if respData.SkuCode != nil { + _ = d.Set("sku_code", respData.SkuCode) + } + + if respData.MaxSubscriptionPerClient != nil { + _ = d.Set("max_subscription_per_client", respData.MaxSubscriptionPerClient) + } + + if respData.AuthorizationPolicyLimit != nil { + _ = d.Set("authorization_policy_limit", respData.AuthorizationPolicyLimit) + } + + if respData.ClientNumLimit != nil { + _ = d.Set("client_num_limit", respData.ClientNumLimit) + } + + if respData.DeviceCertificateProvisionType != nil { + _ = d.Set("device_certificate_provision_type", respData.DeviceCertificateProvisionType) + } + + if respData.AutomaticActivation != nil { + _ = d.Set("automatic_activation", respData.AutomaticActivation) + } + + if respData.RenewFlag != nil { + _ = d.Set("renew_flag", respData.RenewFlag) + } + + if respData.PayMode != nil { + _ = d.Set("pay_mode", respData.PayMode) + } + + if respData.ExpiryTime != nil { + _ = d.Set("expiry_time", respData.ExpiryTime) + } + + if respData.DestroyTime != nil { + _ = d.Set("destroy_time", respData.DestroyTime) + } + + if respData.X509Mode != nil { + _ = d.Set("x509_mode", respData.X509Mode) + } + + if respData.MaxCaNum != nil { + _ = d.Set("max_ca_num", respData.MaxCaNum) + } + + if respData.RegistrationCode != nil { + _ = d.Set("registration_code", respData.RegistrationCode) + } + + if respData.MaxSubscription != nil { + _ = d.Set("max_subscription", respData.MaxSubscription) + } + + if respData.AuthorizationPolicy != nil { + _ = d.Set("authorization_policy", respData.AuthorizationPolicy) + } + + d.SetId(instanceId) + output, ok := d.GetOk("result_output_file") + if ok && output.(string) != "" { + if e := tccommon.WriteToFile(output.(string), d); e != nil { + return e + } + } + + return nil +} diff --git a/tencentcloud/services/mqtt/data_source_tc_mqtt_instance_detail.md b/tencentcloud/services/mqtt/data_source_tc_mqtt_instance_detail.md new file mode 100644 index 0000000000..ce68e18399 --- /dev/null +++ b/tencentcloud/services/mqtt/data_source_tc_mqtt_instance_detail.md @@ -0,0 +1,9 @@ +Use this data source to query detailed information of MQTT instance detail + +Example Usage + +```hcl +data "tencentcloud_mqtt_instance_detail" "example" { + instance_id = "mqtt-kngmpg9p" +} +``` diff --git a/tencentcloud/services/mqtt/data_source_tc_mqtt_instance_detail_test.go b/tencentcloud/services/mqtt/data_source_tc_mqtt_instance_detail_test.go new file mode 100644 index 0000000000..97f8018ce0 --- /dev/null +++ b/tencentcloud/services/mqtt/data_source_tc_mqtt_instance_detail_test.go @@ -0,0 +1,32 @@ +package mqtt_test + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest" +) + +func TestAccTencentCloudMqttInstanceDetailDataSource_basic(t *testing.T) { + t.Parallel() + resource.Test(t, resource.TestCase{ + PreCheck: func() { + tcacctest.AccPreCheck(t) + }, + Providers: tcacctest.AccProviders, + Steps: []resource.TestStep{{ + Config: testAccMqttInstanceDetailDataSource, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.tencentcloud_mqtt_instance_detail.example", "id"), + resource.TestCheckResourceAttrSet("data.tencentcloud_mqtt_instance_detail.example", "instance_id"), + ), + }}, + }) +} + +const testAccMqttInstanceDetailDataSource = ` +data "tencentcloud_mqtt_instance_detail" "example" { + instance_id = "mqtt-kngmpg9p" +} +` diff --git a/tencentcloud/services/mqtt/data_source_tc_mqtt_instances.go b/tencentcloud/services/mqtt/data_source_tc_mqtt_instances.go new file mode 100644 index 0000000000..8fd0de7f91 --- /dev/null +++ b/tencentcloud/services/mqtt/data_source_tc_mqtt_instances.go @@ -0,0 +1,356 @@ +package mqtt + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + mqttv20240516 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mqtt/v20240516" + + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" +) + +func DataSourceTencentCloudMqttInstances() *schema.Resource { + return &schema.Resource{ + Read: dataSourceTencentCloudMqttInstancesRead, + Schema: map[string]*schema.Schema{ + "filters": { + Type: schema.TypeList, + Optional: true, + Description: "Query criteria list, supporting the following fields: InstanceName: cluster name, fuzzy search, InstanceId: cluster ID, precise search, InstanceStatus: cluster status search (RUNNING - Running, CREATING - Creating, MODIFYING - Changing, DELETING - Deleting).", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + Description: "Filter name.", + }, + "values": { + Type: schema.TypeSet, + Required: true, + Description: "Values.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + + "tag_filters": { + Type: schema.TypeList, + Optional: true, + Description: "Tag filters.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "tag_key": { + Type: schema.TypeString, + Optional: true, + Description: "Tag key.", + }, + "tag_values": { + Type: schema.TypeSet, + Optional: true, + Description: "Tag values.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + + "data": { + Type: schema.TypeList, + Computed: true, + Description: "Instance list.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "instance_id": { + Type: schema.TypeString, + Computed: true, + Description: "Instacen ID.", + }, + "instance_name": { + Type: schema.TypeString, + Computed: true, + Description: "Instacen name.", + }, + "version": { + Type: schema.TypeString, + Computed: true, + Description: "Instacen version.", + }, + "instance_type": { + Type: schema.TypeString, + Computed: true, + Description: "Instance type. BASIC- Basic Edition; PRO- professional edition; PLATINUM- Platinum version.", + }, + "instance_status": { + Type: schema.TypeString, + Computed: true, + Description: "Instance status. RUNNING- In operation; MAINTAINING- Under Maintenance; ABNORMAL- abnormal; OVERDUE- Arrears of fees; DESTROYED- Deleted; CREATING- Creating in progress; MODIFYING- In the process of transformation; CREATE_FAILURE- Creation failed; MODIFY_FAILURE- Transformation failed; DELETING- deleting.", + }, + "topic_num_limit": { + Type: schema.TypeInt, + Computed: true, + Description: "Maximum number of instance topics.", + }, + "remark": { + Type: schema.TypeString, + Computed: true, + Description: "Remark.", + }, + "topic_num": { + Type: schema.TypeInt, + Computed: true, + Description: "Topic num.", + }, + "sku_code": { + Type: schema.TypeString, + Computed: true, + Description: "Product specifications.", + }, + "tps_limit": { + Type: schema.TypeInt, + Computed: true, + Description: "Elastic TPS current limit value.", + }, + "create_time": { + Type: schema.TypeInt, + Computed: true, + Description: "Creation time, millisecond timestamp.", + }, + "max_subscription_per_client": { + Type: schema.TypeInt, + Computed: true, + Description: "Maximum number of subscriptions per client.", + }, + "client_num_limit": { + Type: schema.TypeInt, + Computed: true, + Description: "Number of client connections online.", + }, + "renew_flag": { + Type: schema.TypeInt, + Computed: true, + Description: "Whether to renew automatically. Only the annual and monthly package cluster is effective. 1: Automatic renewal; 0: Non automatic renewal.", + }, + "pay_mode": { + Type: schema.TypeString, + Computed: true, + Description: "Billing mode, POSTPAID, pay as you go PREPAID, annual and monthly package.", + }, + "expiry_time": { + Type: schema.TypeInt, + Computed: true, + Description: "Expiration time, millisecond level timestamp.", + }, + "destroy_time": { + Type: schema.TypeInt, + Computed: true, + Description: "Pre destruction time, millisecond timestamp.", + }, + "authorization_policy_limit": { + Type: schema.TypeInt, + Computed: true, + Description: "Limit on the number of authorization rules.", + }, + "max_ca_num": { + Type: schema.TypeInt, + Computed: true, + Description: "Maximum CA quota.", + }, + "max_subscription": { + Type: schema.TypeInt, + Computed: true, + Description: "Maximum number of subscriptions.", + }, + }, + }, + }, + + "result_output_file": { + Type: schema.TypeString, + Optional: true, + Description: "Used to save results.", + }, + }, + } +} + +func dataSourceTencentCloudMqttInstancesRead(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("data_source.tencentcloud_mqtt_instances.read")() + defer tccommon.InconsistentCheck(d, meta)() + + var ( + logId = tccommon.GetLogId(nil) + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) + service = MqttService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} + ) + + paramMap := make(map[string]interface{}) + if v, ok := d.GetOk("filters"); ok { + filtersSet := v.([]interface{}) + tmpSet := make([]*mqttv20240516.Filter, 0, len(filtersSet)) + for _, item := range filtersSet { + filtersMap := item.(map[string]interface{}) + filter := mqttv20240516.Filter{} + if v, ok := filtersMap["name"].(string); ok && v != "" { + filter.Name = helper.String(v) + } + + if v, ok := filtersMap["values"]; ok { + valuesSet := v.(*schema.Set).List() + for i := range valuesSet { + values := valuesSet[i].(string) + filter.Values = append(filter.Values, helper.String(values)) + } + } + + tmpSet = append(tmpSet, &filter) + } + + paramMap["Filters"] = tmpSet + } + + if v, ok := d.GetOk("tag_filters"); ok { + tagFiltersSet := v.([]interface{}) + tmpSet := make([]*mqttv20240516.TagFilter, 0, len(tagFiltersSet)) + for _, item := range tagFiltersSet { + tagFiltersMap := item.(map[string]interface{}) + tagFilter := mqttv20240516.TagFilter{} + if v, ok := tagFiltersMap["tag_key"].(string); ok && v != "" { + tagFilter.TagKey = helper.String(v) + } + + if v, ok := tagFiltersMap["tag_values"]; ok { + tagValuesSet := v.(*schema.Set).List() + for i := range tagValuesSet { + tagValues := tagValuesSet[i].(string) + tagFilter.TagValues = append(tagFilter.TagValues, helper.String(tagValues)) + } + } + + tmpSet = append(tmpSet, &tagFilter) + } + + paramMap["TagFilters"] = tmpSet + } + + var respData []*mqttv20240516.MQTTInstanceItem + reqErr := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { + result, e := service.DescribeMqttInstancesByFilter(ctx, paramMap) + if e != nil { + return tccommon.RetryError(e) + } + + respData = result + return nil + }) + + if reqErr != nil { + return reqErr + } + + dataList := make([]map[string]interface{}, 0, len(respData)) + if respData != nil { + for _, data := range respData { + dataMap := map[string]interface{}{} + if data.InstanceId != nil { + dataMap["instance_id"] = data.InstanceId + } + + if data.InstanceName != nil { + dataMap["instance_name"] = data.InstanceName + } + + if data.Version != nil { + dataMap["version"] = data.Version + } + + if data.InstanceType != nil { + dataMap["instance_type"] = data.InstanceType + } + + if data.InstanceStatus != nil { + dataMap["instance_status"] = data.InstanceStatus + } + + if data.TopicNumLimit != nil { + dataMap["topic_num_limit"] = data.TopicNumLimit + } + + if data.Remark != nil { + dataMap["remark"] = data.Remark + } + + if data.TopicNum != nil { + dataMap["topic_num"] = data.TopicNum + } + + if data.SkuCode != nil { + dataMap["sku_code"] = data.SkuCode + } + + if data.TpsLimit != nil { + dataMap["tps_limit"] = data.TpsLimit + } + + if data.CreateTime != nil { + dataMap["create_time"] = data.CreateTime + } + + if data.MaxSubscriptionPerClient != nil { + dataMap["max_subscription_per_client"] = data.MaxSubscriptionPerClient + } + + if data.ClientNumLimit != nil { + dataMap["client_num_limit"] = data.ClientNumLimit + } + + if data.RenewFlag != nil { + dataMap["renew_flag"] = data.RenewFlag + } + + if data.PayMode != nil { + dataMap["pay_mode"] = data.PayMode + } + + if data.ExpiryTime != nil { + dataMap["expiry_time"] = data.ExpiryTime + } + + if data.DestroyTime != nil { + dataMap["destroy_time"] = data.DestroyTime + } + + if data.AuthorizationPolicyLimit != nil { + dataMap["authorization_policy_limit"] = data.AuthorizationPolicyLimit + } + + if data.MaxCaNum != nil { + dataMap["max_ca_num"] = data.MaxCaNum + } + + if data.MaxSubscription != nil { + dataMap["max_subscription"] = data.MaxSubscription + } + + dataList = append(dataList, dataMap) + } + + _ = d.Set("data", dataList) + } + + d.SetId(helper.BuildToken()) + output, ok := d.GetOk("result_output_file") + if ok && output.(string) != "" { + if e := tccommon.WriteToFile(output.(string), dataList); e != nil { + return e + } + } + + return nil +} diff --git a/tencentcloud/services/mqtt/data_source_tc_mqtt_instances.md b/tencentcloud/services/mqtt/data_source_tc_mqtt_instances.md new file mode 100644 index 0000000000..47e3dadb51 --- /dev/null +++ b/tencentcloud/services/mqtt/data_source_tc_mqtt_instances.md @@ -0,0 +1,30 @@ +Use this data source to query detailed information of MQTT instances + +Example Usage + +Query all mqtt instances + +```hcl +data "tencentcloud_mqtt_instances" "example" {} +``` + +Query mqtt instances by filters + +```hcl +data "tencentcloud_mqtt_instances" "example" { + filters { + name = "InstanceId" + values = ["mqtt-kngmpg9p"] + } + + filters { + name = "InstanceName" + values = ["tf-example"] + } + + filters { + name = "InstanceStatus" + values = ["RUNNING"] + } +} +``` diff --git a/tencentcloud/services/mqtt/data_source_tc_mqtt_instances_test.go b/tencentcloud/services/mqtt/data_source_tc_mqtt_instances_test.go new file mode 100644 index 0000000000..68c1fcafc9 --- /dev/null +++ b/tencentcloud/services/mqtt/data_source_tc_mqtt_instances_test.go @@ -0,0 +1,29 @@ +package mqtt_test + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest" +) + +func TestAccTencentCloudMqttInstancesDataSource_basic(t *testing.T) { + t.Parallel() + resource.Test(t, resource.TestCase{ + PreCheck: func() { + tcacctest.AccPreCheck(t) + }, + Providers: tcacctest.AccProviders, + Steps: []resource.TestStep{{ + Config: testAccMqttInstancesDataSource, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.tencentcloud_mqtt_instances.example", "id"), + ), + }}, + }) +} + +const testAccMqttInstancesDataSource = ` +data "tencentcloud_mqtt_instances" "example" {} +` diff --git a/tencentcloud/services/mqtt/data_source_tc_mqtt_topics.go b/tencentcloud/services/mqtt/data_source_tc_mqtt_topics.go new file mode 100644 index 0000000000..be6eda2de2 --- /dev/null +++ b/tencentcloud/services/mqtt/data_source_tc_mqtt_topics.go @@ -0,0 +1,122 @@ +package mqtt + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + mqttv20240516 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mqtt/v20240516" + + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" +) + +func DataSourceTencentCloudMqttTopics() *schema.Resource { + return &schema.Resource{ + Read: dataSourceTencentCloudMqttTopicsRead, + Schema: map[string]*schema.Schema{ + "instance_id": { + Type: schema.TypeString, + Required: true, + Description: "Instance ID.", + }, + + // computed + "data": { + Type: schema.TypeList, + Computed: true, + Description: "Topic list.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "instance_id": { + Type: schema.TypeString, + Required: true, + Description: "Instance ID.", + }, + "topic": { + Type: schema.TypeString, + Required: true, + Description: "Topic.", + }, + "remark": { + Type: schema.TypeString, + Required: true, + Description: "Remark.", + }, + }, + }, + }, + + "result_output_file": { + Type: schema.TypeString, + Optional: true, + Description: "Used to save results.", + }, + }, + } +} + +func dataSourceTencentCloudMqttTopicsRead(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("data_source.tencentcloud_mqtt_topics.read")() + defer tccommon.InconsistentCheck(d, meta)() + + var ( + logId = tccommon.GetLogId(nil) + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) + service = MqttService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} + instanceId string + ) + + paramMap := make(map[string]interface{}) + if v, ok := d.GetOk("instance_id"); ok { + paramMap["InstanceId"] = helper.String(v.(string)) + instanceId = v.(string) + } + + var respData []*mqttv20240516.MQTTTopicItem + reqErr := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { + result, e := service.DescribeMqttTopicsByFilter(ctx, paramMap) + if e != nil { + return tccommon.RetryError(e) + } + + respData = result + return nil + }) + + if reqErr != nil { + return reqErr + } + + dataList := make([]map[string]interface{}, 0, len(respData)) + if respData != nil { + for _, data := range respData { + dataMap := map[string]interface{}{} + if data.InstanceId != nil { + dataMap["instance_id"] = data.InstanceId + } + + if data.Topic != nil { + dataMap["topic"] = data.Topic + } + + if data.Remark != nil { + dataMap["remark"] = data.Remark + } + + dataList = append(dataList, dataMap) + } + + _ = d.Set("data", dataList) + } + + d.SetId(instanceId) + output, ok := d.GetOk("result_output_file") + if ok && output.(string) != "" { + if e := tccommon.WriteToFile(output.(string), dataList); e != nil { + return e + } + } + + return nil +} diff --git a/tencentcloud/services/mqtt/data_source_tc_mqtt_topics.md b/tencentcloud/services/mqtt/data_source_tc_mqtt_topics.md new file mode 100644 index 0000000000..e445dc1498 --- /dev/null +++ b/tencentcloud/services/mqtt/data_source_tc_mqtt_topics.md @@ -0,0 +1,9 @@ +Use this data source to query detailed information of MQTT topics + +Example Usage + +```hcl +data "tencentcloud_mqtt_topics" "example" { + instance_id = "mqtt-g4qgr3gx" +} +``` diff --git a/tencentcloud/services/mqtt/data_source_tc_mqtt_topics_test.go b/tencentcloud/services/mqtt/data_source_tc_mqtt_topics_test.go new file mode 100644 index 0000000000..50cb56c963 --- /dev/null +++ b/tencentcloud/services/mqtt/data_source_tc_mqtt_topics_test.go @@ -0,0 +1,32 @@ +package mqtt_test + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest" +) + +func TestAccTencentCloudMqttTopicsDataSource_basic(t *testing.T) { + t.Parallel() + resource.Test(t, resource.TestCase{ + PreCheck: func() { + tcacctest.AccPreCheck(t) + }, + Providers: tcacctest.AccProviders, + Steps: []resource.TestStep{{ + Config: testAccMqttTopicsDataSource, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.tencentcloud_mqtt_topics.example", "id"), + resource.TestCheckResourceAttrSet("data.tencentcloud_mqtt_topics.example", "instance_id"), + ), + }}, + }) +} + +const testAccMqttTopicsDataSource = ` +data "tencentcloud_mqtt_topics" "example" { + instance_id = "mqtt-g4qgr3gx" +} +` diff --git a/tencentcloud/services/mqtt/service_tencentcloud_mqtt.go b/tencentcloud/services/mqtt/service_tencentcloud_mqtt.go index b7fac22d9f..75c4b6de50 100644 --- a/tencentcloud/services/mqtt/service_tencentcloud_mqtt.go +++ b/tencentcloud/services/mqtt/service_tencentcloud_mqtt.go @@ -461,3 +461,147 @@ func (me *MqttService) DescribeMqttAuthorizationPolicyById(ctx context.Context, ret = response.Response return } + +func (me *MqttService) DescribeMqttInstancesByFilter(ctx context.Context, param map[string]interface{}) (ret []*mqttv20240516.MQTTInstanceItem, errRet error) { + var ( + logId = tccommon.GetLogId(ctx) + request = mqttv20240516.NewDescribeInstanceListRequest() + ) + + defer func() { + if errRet != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error()) + } + }() + + for k, v := range param { + if k == "Filters" { + request.Filters = v.([]*mqttv20240516.Filter) + } + + if k == "TagFilters" { + request.TagFilters = v.([]*mqttv20240516.TagFilter) + } + } + + ratelimit.Check(request.GetAction()) + + var ( + offset int64 = 0 + limit int64 = 100 + ) + + for { + request.Offset = &offset + request.Limit = &limit + response, err := me.client.UseMqttV20240516Client().DescribeInstanceList(request) + if err != nil { + errRet = err + return + } + + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString()) + + if response == nil || len(response.Response.Data) < 1 { + break + } + + ret = append(ret, response.Response.Data...) + if len(response.Response.Data) < int(limit) { + break + } + + offset += limit + } + + return +} + +func (me *MqttService) DescribeMqttInstanceDetailByFilter(ctx context.Context, param map[string]interface{}) (ret *mqttv20240516.DescribeInstanceResponseParams, errRet error) { + var ( + logId = tccommon.GetLogId(ctx) + request = mqttv20240516.NewDescribeInstanceRequest() + ) + + defer func() { + if errRet != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error()) + } + }() + + for k, v := range param { + if k == "InstanceId" { + request.InstanceId = v.(*string) + } + } + + ratelimit.Check(request.GetAction()) + response, err := me.client.UseMqttV20240516Client().DescribeInstance(request) + if err != nil { + errRet = err + return + } + + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString()) + + if response == nil || response.Response == nil { + return + } + + ret = response.Response + return +} + +func (me *MqttService) DescribeMqttTopicsByFilter(ctx context.Context, param map[string]interface{}) (ret []*mqttv20240516.MQTTTopicItem, errRet error) { + var ( + logId = tccommon.GetLogId(ctx) + request = mqttv20240516.NewDescribeTopicListRequest() + ) + + defer func() { + if errRet != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error()) + } + }() + + for k, v := range param { + if k == "InstanceId" { + request.InstanceId = v.(*string) + } + + if k == "Filters" { + request.Filters = v.([]*mqttv20240516.Filter) + } + } + + ratelimit.Check(request.GetAction()) + + var ( + offset int64 = 0 + limit int64 = 100 + ) + for { + request.Offset = &offset + request.Limit = &limit + response, err := me.client.UseMqttV20240516Client().DescribeTopicList(request) + if err != nil { + errRet = err + return + } + + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString()) + + if response == nil || len(response.Response.Data) < 1 { + break + } + + ret = append(ret, response.Response.Data...) + if len(response.Response.Data) < int(limit) { + break + } + + offset += limit + } + + return +} diff --git a/website/docs/d/mqtt_instance_detail.html.markdown b/website/docs/d/mqtt_instance_detail.html.markdown new file mode 100644 index 0000000000..0ce22b4c73 --- /dev/null +++ b/website/docs/d/mqtt_instance_detail.html.markdown @@ -0,0 +1,57 @@ +--- +subcategory: "TDMQ for MQTT(MQTT)" +layout: "tencentcloud" +page_title: "TencentCloud: tencentcloud_mqtt_instance_detail" +sidebar_current: "docs-tencentcloud-datasource-mqtt_instance_detail" +description: |- + Use this data source to query detailed information of MQTT instance detail +--- + +# tencentcloud_mqtt_instance_detail + +Use this data source to query detailed information of MQTT instance detail + +## Example Usage + +```hcl +data "tencentcloud_mqtt_instance_detail" "example" { + instance_id = "mqtt-kngmpg9p" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `instance_id` - (Required, String) Instance ID. +* `result_output_file` - (Optional, String) Used to save results. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `authorization_policy_limit` - Limit on the number of authorization rules. +* `authorization_policy` - Authorization Policy Switch. +* `automatic_activation` - Is it automatically activated when registering device certificates automatically. +* `client_num_limit` - Number of client connections online. +* `created_time` - Creation time, millisecond timestamp. +* `destroy_time` - Pre destruction time, millisecond timestamp. +* `device_certificate_provision_type` - Client certificate registration method: JITP: Automatic Registration; API: Manually register through API. +* `expiry_time` - Expiration time, millisecond level timestamp. +* `instance_name` - Instance type. BASIC- Basic Edition; PRO- professional edition; PLATINUM- Platinum version. +* `instance_status` - Instance status. RUNNING- In operation; MAINTAINING- Under Maintenance; ABNORMAL- abnormal; OVERDUE- Arrears of fees; DESTROYED- Deleted; CREATING- Creating in progress; MODIFYING- In the process of transformation; CREATE_FAILURE- Creation failed; MODIFY_FAILURE- Transformation failed; DELETING- deleting. +* `instance_type` - Instance ID. +* `max_ca_num` - Maximum Ca quota. +* `max_subscription_per_client` - Maximum number of subscriptions per client. +* `max_subscription` - Maximum number of subscriptions in the cluster. +* `pay_mode` - Billing mode, POSTPAID, pay as you go PREPAID, annual and monthly package. +* `registration_code` - Certificate registration code. +* `remark` - Remark. +* `renew_flag` - Whether to renew automatically. Only the annual and monthly package cluster is effective. 1: Automatic renewal; 0: Non automatic renewal. +* `sku_code` - Product specifications. +* `topic_num_limit` - Maximum number of instance topics. +* `topic_num` - Topic num. +* `tps_limit` - Elastic TPS current limit value. +* `x509_mode` - TLS, Unidirectional authentication mTLS, bidirectional authentication BYOC; One machine, one certificate. + + diff --git a/website/docs/d/mqtt_instances.html.markdown b/website/docs/d/mqtt_instances.html.markdown new file mode 100644 index 0000000000..9c822f55fe --- /dev/null +++ b/website/docs/d/mqtt_instances.html.markdown @@ -0,0 +1,87 @@ +--- +subcategory: "TDMQ for MQTT(MQTT)" +layout: "tencentcloud" +page_title: "TencentCloud: tencentcloud_mqtt_instances" +sidebar_current: "docs-tencentcloud-datasource-mqtt_instances" +description: |- + Use this data source to query detailed information of MQTT instances +--- + +# tencentcloud_mqtt_instances + +Use this data source to query detailed information of MQTT instances + +## Example Usage + +### Query all mqtt instances + +```hcl +data "tencentcloud_mqtt_instances" "example" {} +``` + +### Query mqtt instances by filters + +```hcl +data "tencentcloud_mqtt_instances" "example" { + filters { + name = "InstanceId" + values = ["mqtt-kngmpg9p"] + } + + filters { + name = "InstanceName" + values = ["tf-example"] + } + + filters { + name = "InstanceStatus" + values = ["RUNNING"] + } +} +``` + +## Argument Reference + +The following arguments are supported: + +* `filters` - (Optional, List) Query criteria list, supporting the following fields: InstanceName: cluster name, fuzzy search, InstanceId: cluster ID, precise search, InstanceStatus: cluster status search (RUNNING - Running, CREATING - Creating, MODIFYING - Changing, DELETING - Deleting). +* `result_output_file` - (Optional, String) Used to save results. +* `tag_filters` - (Optional, List) Tag filters. + +The `filters` object supports the following: + +* `name` - (Required, String) Filter name. +* `values` - (Required, Set) Values. + +The `tag_filters` object supports the following: + +* `tag_key` - (Optional, String) Tag key. +* `tag_values` - (Optional, Set) Tag values. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `data` - Instance list. + * `authorization_policy_limit` - Limit on the number of authorization rules. + * `client_num_limit` - Number of client connections online. + * `create_time` - Creation time, millisecond timestamp. + * `destroy_time` - Pre destruction time, millisecond timestamp. + * `expiry_time` - Expiration time, millisecond level timestamp. + * `instance_id` - Instacen ID. + * `instance_name` - Instacen name. + * `instance_status` - Instance status. RUNNING- In operation; MAINTAINING- Under Maintenance; ABNORMAL- abnormal; OVERDUE- Arrears of fees; DESTROYED- Deleted; CREATING- Creating in progress; MODIFYING- In the process of transformation; CREATE_FAILURE- Creation failed; MODIFY_FAILURE- Transformation failed; DELETING- deleting. + * `instance_type` - Instance type. BASIC- Basic Edition; PRO- professional edition; PLATINUM- Platinum version. + * `max_ca_num` - Maximum CA quota. + * `max_subscription_per_client` - Maximum number of subscriptions per client. + * `max_subscription` - Maximum number of subscriptions. + * `pay_mode` - Billing mode, POSTPAID, pay as you go PREPAID, annual and monthly package. + * `remark` - Remark. + * `renew_flag` - Whether to renew automatically. Only the annual and monthly package cluster is effective. 1: Automatic renewal; 0: Non automatic renewal. + * `sku_code` - Product specifications. + * `topic_num_limit` - Maximum number of instance topics. + * `topic_num` - Topic num. + * `tps_limit` - Elastic TPS current limit value. + * `version` - Instacen version. + + diff --git a/website/docs/d/mqtt_topics.html.markdown b/website/docs/d/mqtt_topics.html.markdown new file mode 100644 index 0000000000..d7f5cd1e81 --- /dev/null +++ b/website/docs/d/mqtt_topics.html.markdown @@ -0,0 +1,35 @@ +--- +subcategory: "TDMQ for MQTT(MQTT)" +layout: "tencentcloud" +page_title: "TencentCloud: tencentcloud_mqtt_topics" +sidebar_current: "docs-tencentcloud-datasource-mqtt_topics" +description: |- + Use this data source to query detailed information of MQTT topics +--- + +# tencentcloud_mqtt_topics + +Use this data source to query detailed information of MQTT topics + +## Example Usage + +```hcl +data "tencentcloud_mqtt_topics" "example" { + instance_id = "mqtt-g4qgr3gx" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `instance_id` - (Required, String) Instance ID. +* `result_output_file` - (Optional, String) Used to save results. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `data` - Topic list. + + diff --git a/website/tencentcloud.erb b/website/tencentcloud.erb index 5b69b00cb5..dcf5a83b49 100644 --- a/website/tencentcloud.erb +++ b/website/tencentcloud.erb @@ -4057,6 +4057,15 @@