|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of address template groups. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_address_template_groups" "name" { |
| 8 | + name = "test" |
| 9 | +} |
| 10 | +``` |
| 11 | +*/ |
| 12 | +package tencentcloud |
| 13 | + |
| 14 | +import ( |
| 15 | + "context" |
| 16 | + "log" |
| 17 | + |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 19 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 20 | + vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" |
| 21 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 22 | +) |
| 23 | + |
| 24 | +func dataSourceTencentCloudAddressTemplateGroups() *schema.Resource { |
| 25 | + return &schema.Resource{ |
| 26 | + Read: dataSourceTencentCloudAddressTemplateGroupsRead, |
| 27 | + |
| 28 | + Schema: map[string]*schema.Schema{ |
| 29 | + "name": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Optional: true, |
| 32 | + Description: "Name of the address template group to query.", |
| 33 | + }, |
| 34 | + "id": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Optional: true, |
| 37 | + Description: "Id of the address template group to query.", |
| 38 | + }, |
| 39 | + "result_output_file": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Optional: true, |
| 42 | + Description: "Used to save results.", |
| 43 | + }, |
| 44 | + |
| 45 | + // Computed values |
| 46 | + "group_list": { |
| 47 | + Type: schema.TypeList, |
| 48 | + Computed: true, |
| 49 | + Description: "Information list of the dedicated address template groups.", |
| 50 | + Elem: &schema.Resource{ |
| 51 | + Schema: map[string]*schema.Schema{ |
| 52 | + "id": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + Description: "Id of the address template group.", |
| 56 | + }, |
| 57 | + "name": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Computed: true, |
| 60 | + Description: "Name of address template group.", |
| 61 | + }, |
| 62 | + "template_ids": { |
| 63 | + Type: schema.TypeList, |
| 64 | + Elem: &schema.Schema{ |
| 65 | + Type: schema.TypeString, |
| 66 | + }, |
| 67 | + Computed: true, |
| 68 | + Description: "ID set of the address template.", |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func dataSourceTencentCloudAddressTemplateGroupsRead(d *schema.ResourceData, meta interface{}) error { |
| 78 | + defer logElapsed("data_source.tencentcloud_address_template_groups.read")() |
| 79 | + |
| 80 | + logId := getLogId(contextNil) |
| 81 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 82 | + |
| 83 | + var name, templateId string |
| 84 | + var filters = make([]*vpc.Filter, 0) |
| 85 | + if v, ok := d.GetOk("name"); ok { |
| 86 | + name = v.(string) |
| 87 | + filters = append(filters, &vpc.Filter{Name: helper.String("address-template-group-name"), Values: []*string{&name}}) |
| 88 | + } |
| 89 | + |
| 90 | + if v, ok := d.GetOk("id"); ok { |
| 91 | + templateId = v.(string) |
| 92 | + filters = append(filters, &vpc.Filter{Name: helper.String("address-template-group-id"), Values: []*string{&templateId}}) |
| 93 | + } |
| 94 | + |
| 95 | + vpcService := VpcService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 96 | + var outErr, inErr error |
| 97 | + groups, outErr := vpcService.DescribeAddressTemplateGroups(ctx, filters) |
| 98 | + if outErr != nil { |
| 99 | + outErr = resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 100 | + groups, inErr = vpcService.DescribeAddressTemplateGroups(ctx, filters) |
| 101 | + if inErr != nil { |
| 102 | + return retryError(inErr) |
| 103 | + } |
| 104 | + return nil |
| 105 | + }) |
| 106 | + } |
| 107 | + |
| 108 | + if outErr != nil { |
| 109 | + return outErr |
| 110 | + } |
| 111 | + |
| 112 | + ids := make([]string, 0, len(groups)) |
| 113 | + templateGroupList := make([]map[string]interface{}, 0, len(groups)) |
| 114 | + for _, ins := range groups { |
| 115 | + mapping := map[string]interface{}{ |
| 116 | + "id": ins.AddressTemplateGroupId, |
| 117 | + "name": ins.AddressTemplateGroupName, |
| 118 | + "template_ids": ins.AddressTemplateIdSet, |
| 119 | + } |
| 120 | + templateGroupList = append(templateGroupList, mapping) |
| 121 | + ids = append(ids, *ins.AddressTemplateGroupId) |
| 122 | + } |
| 123 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 124 | + if e := d.Set("group_list", templateGroupList); e != nil { |
| 125 | + log.Printf("[CRITAL]%s provider set address template group list fail, reason:%s\n", logId, e) |
| 126 | + return e |
| 127 | + } |
| 128 | + |
| 129 | + output, ok := d.GetOk("result_output_file") |
| 130 | + if ok && output.(string) != "" { |
| 131 | + if e := writeToFile(output.(string), templateGroupList); e != nil { |
| 132 | + return e |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return nil |
| 137 | + |
| 138 | +} |
0 commit comments