|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of chdfs access_groups |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_chdfs_access_groups" "access_groups" { |
| 8 | + vpc_id = "vpc-pewdpc0d" |
| 9 | +} |
| 10 | +``` |
| 11 | +*/ |
| 12 | +package tencentcloud |
| 13 | + |
| 14 | +import ( |
| 15 | + "context" |
| 16 | + |
| 17 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 19 | + chdfs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/chdfs/v20201112" |
| 20 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 21 | +) |
| 22 | + |
| 23 | +func dataSourceTencentCloudChdfsAccessGroups() *schema.Resource { |
| 24 | + return &schema.Resource{ |
| 25 | + Read: dataSourceTencentCloudChdfsAccessGroupsRead, |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "vpc_id": { |
| 28 | + Optional: true, |
| 29 | + Type: schema.TypeString, |
| 30 | + Description: "get groups belongs to the vpc id, must set but only can use one of VpcId and OwnerUin to get the groups.", |
| 31 | + }, |
| 32 | + |
| 33 | + "owner_uin": { |
| 34 | + Optional: true, |
| 35 | + Type: schema.TypeInt, |
| 36 | + Description: "get groups belongs to the owner uin, must set but only can use one of VpcId and OwnerUin to get the groups.", |
| 37 | + }, |
| 38 | + |
| 39 | + "access_groups": { |
| 40 | + Computed: true, |
| 41 | + Type: schema.TypeList, |
| 42 | + Description: "access group list.", |
| 43 | + Elem: &schema.Resource{ |
| 44 | + Schema: map[string]*schema.Schema{ |
| 45 | + "access_group_id": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Computed: true, |
| 48 | + Description: "access group id.", |
| 49 | + }, |
| 50 | + "access_group_name": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + Description: "access group name.", |
| 54 | + }, |
| 55 | + "description": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Computed: true, |
| 58 | + Description: "access group description.", |
| 59 | + }, |
| 60 | + "create_time": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Computed: true, |
| 63 | + Description: "create time.", |
| 64 | + }, |
| 65 | + "vpc_type": { |
| 66 | + Type: schema.TypeInt, |
| 67 | + Computed: true, |
| 68 | + Description: "vpc network type(1:CVM, 2:BM 1.0).", |
| 69 | + }, |
| 70 | + "vpc_id": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Computed: true, |
| 73 | + Description: "VPC ID.", |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + |
| 79 | + "result_output_file": { |
| 80 | + Type: schema.TypeString, |
| 81 | + Optional: true, |
| 82 | + Description: "Used to save results.", |
| 83 | + }, |
| 84 | + }, |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func dataSourceTencentCloudChdfsAccessGroupsRead(d *schema.ResourceData, meta interface{}) error { |
| 89 | + defer logElapsed("data_source.tencentcloud_chdfs_access_groups.read")() |
| 90 | + defer inconsistentCheck(d, meta)() |
| 91 | + |
| 92 | + logId := getLogId(contextNil) |
| 93 | + |
| 94 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 95 | + |
| 96 | + paramMap := make(map[string]interface{}) |
| 97 | + if v, ok := d.GetOk("vpc_id"); ok { |
| 98 | + paramMap["vpc_id"] = helper.String(v.(string)) |
| 99 | + } |
| 100 | + |
| 101 | + if v, ok := d.GetOkExists("owner_uin"); ok { |
| 102 | + paramMap["owner_uin"] = helper.IntUint64(v.(int)) |
| 103 | + } |
| 104 | + |
| 105 | + service := ChdfsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 106 | + |
| 107 | + var accessGroups []*chdfs.AccessGroup |
| 108 | + |
| 109 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 110 | + result, e := service.DescribeChdfsAccessGroupsByFilter(ctx, paramMap) |
| 111 | + if e != nil { |
| 112 | + return retryError(e) |
| 113 | + } |
| 114 | + accessGroups = result |
| 115 | + return nil |
| 116 | + }) |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + |
| 121 | + ids := make([]string, 0, len(accessGroups)) |
| 122 | + tmpList := make([]map[string]interface{}, 0, len(accessGroups)) |
| 123 | + |
| 124 | + if accessGroups != nil { |
| 125 | + for _, accessGroup := range accessGroups { |
| 126 | + accessGroupMap := map[string]interface{}{} |
| 127 | + |
| 128 | + if accessGroup.AccessGroupId != nil { |
| 129 | + accessGroupMap["access_group_id"] = accessGroup.AccessGroupId |
| 130 | + } |
| 131 | + |
| 132 | + if accessGroup.AccessGroupName != nil { |
| 133 | + accessGroupMap["access_group_name"] = accessGroup.AccessGroupName |
| 134 | + } |
| 135 | + |
| 136 | + if accessGroup.Description != nil { |
| 137 | + accessGroupMap["description"] = accessGroup.Description |
| 138 | + } |
| 139 | + |
| 140 | + if accessGroup.CreateTime != nil { |
| 141 | + accessGroupMap["create_time"] = accessGroup.CreateTime |
| 142 | + } |
| 143 | + |
| 144 | + if accessGroup.VpcType != nil { |
| 145 | + accessGroupMap["vpc_type"] = accessGroup.VpcType |
| 146 | + } |
| 147 | + |
| 148 | + if accessGroup.VpcId != nil { |
| 149 | + accessGroupMap["vpc_id"] = accessGroup.VpcId |
| 150 | + } |
| 151 | + |
| 152 | + ids = append(ids, *accessGroup.AccessGroupId) |
| 153 | + tmpList = append(tmpList, accessGroupMap) |
| 154 | + } |
| 155 | + |
| 156 | + _ = d.Set("access_groups", tmpList) |
| 157 | + } |
| 158 | + |
| 159 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 160 | + output, ok := d.GetOk("result_output_file") |
| 161 | + if ok && output.(string) != "" { |
| 162 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 163 | + return e |
| 164 | + } |
| 165 | + } |
| 166 | + return nil |
| 167 | +} |
0 commit comments