|
| 1 | +package mongodb |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + mongodb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb/v20190725" |
| 9 | + |
| 10 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 11 | +) |
| 12 | + |
| 13 | +func DataSourceTencentCloudMongodbInstanceUrls() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Read: dataSourceTencentCloudMongodbInstanceUrlsRead, |
| 16 | + Schema: map[string]*schema.Schema{ |
| 17 | + "instance_id": { |
| 18 | + Type: schema.TypeString, |
| 19 | + Required: true, |
| 20 | + Description: "Instance id.", |
| 21 | + }, |
| 22 | + |
| 23 | + "urls": { |
| 24 | + Type: schema.TypeList, |
| 25 | + Computed: true, |
| 26 | + Description: "Example connection string access address in the form of an instance URI. Contains: URI type and connection string address.", |
| 27 | + Elem: &schema.Resource{ |
| 28 | + Schema: map[string]*schema.Schema{ |
| 29 | + "url_type": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Required: true, |
| 32 | + Description: "Refers to the URI category, including:\n" + |
| 33 | + " - CLUSTER_ALL: Refers to the main node connected to the library instance through this URI, which can be read and write;\n" + |
| 34 | + " - CLUSTER_READ_READONLY: Refers to the read-only node connected to the instance through this URI;\n" + |
| 35 | + " - CLUSTER_READ_SECONDARY: Refers to connecting the instance slave node through this URI;\n" + |
| 36 | + " - CLUSTER_READ_SECONDARY_AND_READONLY: Refers to the read-only slave node connected to the instance through this URI;\n" + |
| 37 | + " - CLUSTER_PRIMARY_AND_SECONDARY: This URI connects the instance master node and slave node;\n" + |
| 38 | + " - MONGOS_ALL: means that each Mongos node is connected through this URI and can be read and write;\n" + |
| 39 | + " - MONGOS_READ_READONLY: Refers to the read-only node connected to Mongos through this URI;\n" + |
| 40 | + " - MONGOS_READ_SECONDARY: Refers to the slave node connected to Mongos through this URI;\n" + |
| 41 | + " - MONGOS_READ_PRIMARY_AND_SECONDARY: refers to the connection between the master node and slave node of Mongos through this URI;\n" + |
| 42 | + " - MONGOS_READ_SECONDARY_AND_READONLY: refers to the connection between Mongos slave node and read-only node through this URI.", |
| 43 | + }, |
| 44 | + "address": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Required: true, |
| 47 | + Description: "Example connection string access address in the form of an instance URI.", |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + |
| 53 | + "result_output_file": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Optional: true, |
| 56 | + Description: "Used to save results.", |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func dataSourceTencentCloudMongodbInstanceUrlsRead(d *schema.ResourceData, meta interface{}) error { |
| 63 | + defer tccommon.LogElapsed("data_source.tencentcloud_mongodb_instance_urls.read")() |
| 64 | + defer tccommon.InconsistentCheck(d, meta)() |
| 65 | + |
| 66 | + logId := tccommon.GetLogId(nil) |
| 67 | + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 68 | + |
| 69 | + service := MongodbService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 70 | + |
| 71 | + var ( |
| 72 | + instanceId string |
| 73 | + ) |
| 74 | + if v, ok := d.GetOk("instance_id"); ok { |
| 75 | + instanceId = v.(string) |
| 76 | + } |
| 77 | + |
| 78 | + var respData []*mongodb.DbURL |
| 79 | + err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 80 | + result, e := service.DescribeMongodbInstanceUrls(ctx, instanceId) |
| 81 | + if e != nil { |
| 82 | + return tccommon.RetryError(e) |
| 83 | + } |
| 84 | + respData = result |
| 85 | + return nil |
| 86 | + }) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + urlsList := make([]map[string]interface{}, 0, len(respData)) |
| 92 | + if respData != nil { |
| 93 | + for _, urls := range respData { |
| 94 | + urlsMap := map[string]interface{}{} |
| 95 | + |
| 96 | + if urls.URLType != nil { |
| 97 | + urlsMap["url_type"] = urls.URLType |
| 98 | + } |
| 99 | + |
| 100 | + if urls.Address != nil { |
| 101 | + urlsMap["address"] = urls.Address |
| 102 | + } |
| 103 | + |
| 104 | + urlsList = append(urlsList, urlsMap) |
| 105 | + } |
| 106 | + |
| 107 | + _ = d.Set("urls", urlsList) |
| 108 | + } |
| 109 | + |
| 110 | + d.SetId(instanceId) |
| 111 | + |
| 112 | + output, ok := d.GetOk("result_output_file") |
| 113 | + if ok && output.(string) != "" { |
| 114 | + if e := tccommon.WriteToFile(output.(string), urlsList); e != nil { |
| 115 | + return e |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return nil |
| 120 | +} |
0 commit comments