|
| 1 | +/* |
| 2 | +Use this data source to query Publish Subscribe resources for the specific SQL Server instance. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_sqlserver_publish_subscribe" "example" { |
| 8 | + publish_instance_id = tencentcloud_sqlserver_instance.publish_instance.id |
| 9 | + subscribe_instance_id = tencentcloud_sqlserver_instance.subscribe_instance.id |
| 10 | + publish_subscribe_name = "example" |
| 11 | + delete_subscribe_db = false |
| 12 | + database_tuples { |
| 13 | + publish_database = tencentcloud_sqlserver_db.test_publish_subscribe.name |
| 14 | + } |
| 15 | +} |
| 16 | +``` |
| 17 | +*/ |
| 18 | +package tencentcloud |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "log" |
| 23 | + |
| 24 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 25 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 26 | +) |
| 27 | + |
| 28 | +func dataSourceTencentSqlserverPublishSubscribes() *schema.Resource { |
| 29 | + return &schema.Resource{ |
| 30 | + Read: dataSourceTencentSqlserverPublishSubscribesRead, |
| 31 | + Schema: map[string]*schema.Schema{ |
| 32 | + "result_output_file": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Optional: true, |
| 35 | + Description: "Used to store results.", |
| 36 | + }, |
| 37 | + "instance_id": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Required: true, |
| 40 | + Description: "ID of the SQL Server instance.", |
| 41 | + }, |
| 42 | + "pub_or_sub_instance_id": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Optional: true, |
| 45 | + Description: "The subscribe/publish instance ID is related to whether the `instance_id` is a publish instance or a subscribe instance. when `instance_id` is a publish instance, this field is filtered according to the subscribe instance ID; when `instance_id` is a subscribe instance, this field is filtering according to the publish instance ID.", |
| 46 | + }, |
| 47 | + "pub_or_sub_instance_ip": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Optional: true, |
| 50 | + Description: "The intranet IP of the subscribe/publish instance is related to whether the `instance_id` is a publish instance or a subscribe instance. when `instance_id` is a publish instance, this field is filtered according to the intranet IP of the subscribe instance; when `instance_id` is a subscribe instance, this field is based on the publish instance intranet IP filter.", |
| 51 | + }, |
| 52 | + "publish_subscribe_id": { |
| 53 | + Type: schema.TypeInt, |
| 54 | + Optional: true, |
| 55 | + Description: "The id of the Publish and Subscribe in the SQLServer instance.", |
| 56 | + }, |
| 57 | + "publish_subscribe_name": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Optional: true, |
| 60 | + Description: "The name of the Publish and Subscribe in the SQLServer instance.", |
| 61 | + }, |
| 62 | + "publish_database": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Optional: true, |
| 65 | + Description: "Publish the database.", |
| 66 | + }, |
| 67 | + "subscribe_database": { |
| 68 | + Type: schema.TypeString, |
| 69 | + Optional: true, |
| 70 | + Description: "Subscribe to the database.", |
| 71 | + }, |
| 72 | + "publish_subscribe_list": { |
| 73 | + Type: schema.TypeList, |
| 74 | + Computed: true, |
| 75 | + Description: "Publish and subscribe list. Each element contains the following attributes.", |
| 76 | + Elem: &schema.Resource{ |
| 77 | + Schema: map[string]*schema.Schema{ |
| 78 | + "publish_subscribe_id": { |
| 79 | + Type: schema.TypeInt, |
| 80 | + Computed: true, |
| 81 | + Description: "The id of the Publish and Subscribe in the SQLServer instance.", |
| 82 | + }, |
| 83 | + "publish_subscribe_name": { |
| 84 | + Type: schema.TypeString, |
| 85 | + Computed: true, |
| 86 | + Description: "The name of the Publish and Subscribe in the SQLServer instance.", |
| 87 | + }, |
| 88 | + "publish_instance_id": { |
| 89 | + Type: schema.TypeString, |
| 90 | + Computed: true, |
| 91 | + Description: "Publish the instance ID in the SQLServer instance.", |
| 92 | + }, |
| 93 | + "publish_instance_name": { |
| 94 | + Type: schema.TypeString, |
| 95 | + Computed: true, |
| 96 | + Description: "Publish the instance name in the SQLServer instance.", |
| 97 | + }, |
| 98 | + "publish_instance_ip": { |
| 99 | + Type: schema.TypeString, |
| 100 | + Computed: true, |
| 101 | + Description: "Publish the instance IP in the SQLServer instance.", |
| 102 | + }, |
| 103 | + "subscribe_instance_id": { |
| 104 | + Type: schema.TypeString, |
| 105 | + Computed: true, |
| 106 | + Description: "Subscribe the instance ID in the SQLServer instance.", |
| 107 | + }, |
| 108 | + "subscribe_instance_name": { |
| 109 | + Type: schema.TypeString, |
| 110 | + Computed: true, |
| 111 | + Description: "Subscribe the instance name in the SQLServer instance.", |
| 112 | + }, |
| 113 | + "subscribe_instance_ip": { |
| 114 | + Type: schema.TypeString, |
| 115 | + Computed: true, |
| 116 | + Description: "Subscribe the instance IP in the SQLServer instance.", |
| 117 | + }, |
| 118 | + "database_tuples": { |
| 119 | + Type: schema.TypeSet, |
| 120 | + Computed: true, |
| 121 | + Description: "Database Publish and Publish relationship list.", |
| 122 | + Elem: &schema.Resource{ |
| 123 | + Schema: map[string]*schema.Schema{ |
| 124 | + "publish_database": { |
| 125 | + Type: schema.TypeString, |
| 126 | + Computed: true, |
| 127 | + Description: "Publish the database.", |
| 128 | + }, |
| 129 | + "subscribe_database": { |
| 130 | + Type: schema.TypeString, |
| 131 | + Computed: true, |
| 132 | + Description: "Subscribe to the database.", |
| 133 | + }, |
| 134 | + "last_sync_time": { |
| 135 | + Type: schema.TypeString, |
| 136 | + Computed: true, |
| 137 | + Description: "Last sync time.", |
| 138 | + }, |
| 139 | + "status": { |
| 140 | + Type: schema.TypeString, |
| 141 | + Computed: true, |
| 142 | + Description: "Publish and subscribe status between databases `running`, `success`, `fail`, `unknow`.", |
| 143 | + }, |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + }, |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +func dataSourceTencentSqlserverPublishSubscribesRead(d *schema.ResourceData, meta interface{}) error { |
| 155 | + defer logElapsed("data_source.tencentcloud_sqlserver_publish_subscribes.read")() |
| 156 | + |
| 157 | + logId := getLogId(contextNil) |
| 158 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 159 | + sqlserverService := SqlserverService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 160 | + paramMap := make(map[string]interface{}) |
| 161 | + paramMap["instanceId"] = d.Get("instance_id").(string) |
| 162 | + if v, ok := d.GetOk("pub_or_sub_instance_id"); ok { |
| 163 | + paramMap["pubOrSubInstanceId"] = v.(string) |
| 164 | + } |
| 165 | + if v, ok := d.GetOk("pub_or_sub_instance_ip"); ok { |
| 166 | + paramMap["pubOrSubInstanceIp"] = v.(string) |
| 167 | + } |
| 168 | + if v, ok := d.GetOk("publish_subscribe_name"); ok { |
| 169 | + paramMap["publishSubscribeName"] = v.(string) |
| 170 | + } |
| 171 | + if v, ok := d.GetOk("publish_subscribe_id"); ok { |
| 172 | + id := v.(int) |
| 173 | + paramMap["publishSubscribeId"] = *helper.IntUint64(id) |
| 174 | + } else { |
| 175 | + paramMap["publishSubscribeId"] = *helper.IntUint64(0) |
| 176 | + } |
| 177 | + if v, ok := d.GetOk("publish_database"); ok { |
| 178 | + paramMap["publishDBName"] = v.(string) |
| 179 | + paramMap["subscribeDBName"] = v.(string) |
| 180 | + } |
| 181 | + |
| 182 | + publishSubscribes, err := sqlserverService.DescribeSqlserverPublishSubscribes(ctx, paramMap) |
| 183 | + if err != nil { |
| 184 | + return err |
| 185 | + } |
| 186 | + instanceList := make([]map[string]interface{}, 0, len(publishSubscribes)) |
| 187 | + ids := make([]string, 0, len(publishSubscribes)) |
| 188 | + |
| 189 | + for _, publishSubscribe := range publishSubscribes { |
| 190 | + var databaseTupleStatus []map[string]interface{} |
| 191 | + for _, inst := range publishSubscribe.DatabaseTupleSet { |
| 192 | + databaseTuple := map[string]interface{}{ |
| 193 | + "publish_database": inst.PublishDatabase, |
| 194 | + "subscribe_database": inst.SubscribeDatabase, |
| 195 | + "last_sync_time": inst.LastSyncTime, |
| 196 | + "status": inst.Status, |
| 197 | + } |
| 198 | + databaseTupleStatus = append(databaseTupleStatus, databaseTuple) |
| 199 | + } |
| 200 | + instance := map[string]interface{}{ |
| 201 | + "publish_subscribe_id": publishSubscribe.Id, |
| 202 | + "publish_subscribe_name": publishSubscribe.Name, |
| 203 | + "publish_instance_id": publishSubscribe.PublishInstanceId, |
| 204 | + "publish_instance_ip": publishSubscribe.PublishInstanceIp, |
| 205 | + "publish_instance_name": publishSubscribe.PublishInstanceName, |
| 206 | + "subscribe_instance_id": publishSubscribe.SubscribeInstanceId, |
| 207 | + "subscribe_instance_ip": publishSubscribe.SubscribeInstanceIp, |
| 208 | + "subscribe_instance_name": publishSubscribe.SubscribeInstanceName, |
| 209 | + "database_tuples": databaseTupleStatus, |
| 210 | + } |
| 211 | + resourceId := *publishSubscribe.PublishInstanceId + FILED_SP + *publishSubscribe.SubscribeInstanceId |
| 212 | + instanceList = append(instanceList, instance) |
| 213 | + ids = append(ids, resourceId) |
| 214 | + } |
| 215 | + |
| 216 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 217 | + if err = d.Set("publish_subscribe_list", instanceList); err != nil { |
| 218 | + log.Printf("[CRITAL]%s provider set sql server publish and subscribe list fail, reason:%s ", logId, err.Error()) |
| 219 | + return err |
| 220 | + } |
| 221 | + |
| 222 | + output, ok := d.GetOk("result_output_file") |
| 223 | + if ok && output.(string) != "" { |
| 224 | + if err := writeToFile(output.(string), instanceList); err != nil { |
| 225 | + return err |
| 226 | + } |
| 227 | + } |
| 228 | + return nil |
| 229 | +} |
0 commit comments