|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of chdfs file_systems |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_chdfs_file_systems" "file_systems" {} |
| 8 | +``` |
| 9 | +*/ |
| 10 | +package tencentcloud |
| 11 | + |
| 12 | +import ( |
| 13 | + "context" |
| 14 | + |
| 15 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 16 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 17 | + chdfs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/chdfs/v20201112" |
| 18 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 19 | +) |
| 20 | + |
| 21 | +func dataSourceTencentCloudChdfsFileSystems() *schema.Resource { |
| 22 | + return &schema.Resource{ |
| 23 | + Read: dataSourceTencentCloudChdfsFileSystemsRead, |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + "file_systems": { |
| 26 | + Computed: true, |
| 27 | + Type: schema.TypeList, |
| 28 | + Description: "file system list.", |
| 29 | + Elem: &schema.Resource{ |
| 30 | + Schema: map[string]*schema.Schema{ |
| 31 | + "app_id": { |
| 32 | + Type: schema.TypeInt, |
| 33 | + Computed: true, |
| 34 | + Description: "appid of the user.", |
| 35 | + }, |
| 36 | + "file_system_name": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Computed: true, |
| 39 | + Description: "file system name.", |
| 40 | + }, |
| 41 | + "description": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Computed: true, |
| 44 | + Description: "desc of the file system.", |
| 45 | + }, |
| 46 | + "region": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Computed: true, |
| 49 | + Description: "region of the file system.", |
| 50 | + }, |
| 51 | + "file_system_id": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Computed: true, |
| 54 | + Description: "file system id.", |
| 55 | + }, |
| 56 | + "create_time": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + Description: "create time.", |
| 60 | + }, |
| 61 | + "block_size": { |
| 62 | + Type: schema.TypeInt, |
| 63 | + Computed: true, |
| 64 | + Description: "block size of the file system(byte).", |
| 65 | + }, |
| 66 | + "capacity_quota": { |
| 67 | + Type: schema.TypeInt, |
| 68 | + Computed: true, |
| 69 | + Description: "capacity of the file system(byte).", |
| 70 | + }, |
| 71 | + "status": { |
| 72 | + Type: schema.TypeInt, |
| 73 | + Computed: true, |
| 74 | + Description: "status of the file system(1: creating create success 3: create failed).", |
| 75 | + }, |
| 76 | + "super_users": { |
| 77 | + Type: schema.TypeSet, |
| 78 | + Elem: &schema.Schema{ |
| 79 | + Type: schema.TypeString, |
| 80 | + }, |
| 81 | + Computed: true, |
| 82 | + Description: "super users of the file system.", |
| 83 | + }, |
| 84 | + "posix_acl": { |
| 85 | + Type: schema.TypeBool, |
| 86 | + Computed: true, |
| 87 | + Description: "check POSIX ACL or not.", |
| 88 | + }, |
| 89 | + "enable_ranger": { |
| 90 | + Type: schema.TypeBool, |
| 91 | + Computed: true, |
| 92 | + Description: "check the ranger address or not.", |
| 93 | + }, |
| 94 | + "ranger_service_addresses": { |
| 95 | + Type: schema.TypeSet, |
| 96 | + Elem: &schema.Schema{ |
| 97 | + Type: schema.TypeString, |
| 98 | + }, |
| 99 | + Computed: true, |
| 100 | + Description: "ranger address list.", |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + |
| 106 | + "result_output_file": { |
| 107 | + Type: schema.TypeString, |
| 108 | + Optional: true, |
| 109 | + Description: "Used to save results.", |
| 110 | + }, |
| 111 | + }, |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func dataSourceTencentCloudChdfsFileSystemsRead(d *schema.ResourceData, meta interface{}) error { |
| 116 | + defer logElapsed("data_source.tencentcloud_chdfs_file_systems.read")() |
| 117 | + defer inconsistentCheck(d, meta)() |
| 118 | + |
| 119 | + logId := getLogId(contextNil) |
| 120 | + |
| 121 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 122 | + |
| 123 | + service := ChdfsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 124 | + |
| 125 | + var fileSystems []*chdfs.FileSystem |
| 126 | + |
| 127 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 128 | + result, e := service.DescribeChdfsFileSystems(ctx) |
| 129 | + if e != nil { |
| 130 | + return retryError(e) |
| 131 | + } |
| 132 | + fileSystems = result |
| 133 | + return nil |
| 134 | + }) |
| 135 | + if err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + |
| 139 | + ids := make([]string, 0, len(fileSystems)) |
| 140 | + tmpList := make([]map[string]interface{}, 0, len(fileSystems)) |
| 141 | + |
| 142 | + if fileSystems != nil { |
| 143 | + for _, fileSystem := range fileSystems { |
| 144 | + fileSystemMap := map[string]interface{}{} |
| 145 | + |
| 146 | + if fileSystem.AppId != nil { |
| 147 | + fileSystemMap["app_id"] = fileSystem.AppId |
| 148 | + } |
| 149 | + |
| 150 | + if fileSystem.FileSystemName != nil { |
| 151 | + fileSystemMap["file_system_name"] = fileSystem.FileSystemName |
| 152 | + } |
| 153 | + |
| 154 | + if fileSystem.Description != nil { |
| 155 | + fileSystemMap["description"] = fileSystem.Description |
| 156 | + } |
| 157 | + |
| 158 | + if fileSystem.Region != nil { |
| 159 | + fileSystemMap["region"] = fileSystem.Region |
| 160 | + } |
| 161 | + |
| 162 | + if fileSystem.FileSystemId != nil { |
| 163 | + fileSystemMap["file_system_id"] = fileSystem.FileSystemId |
| 164 | + } |
| 165 | + |
| 166 | + if fileSystem.CreateTime != nil { |
| 167 | + fileSystemMap["create_time"] = fileSystem.CreateTime |
| 168 | + } |
| 169 | + |
| 170 | + if fileSystem.BlockSize != nil { |
| 171 | + fileSystemMap["block_size"] = fileSystem.BlockSize |
| 172 | + } |
| 173 | + |
| 174 | + if fileSystem.CapacityQuota != nil { |
| 175 | + fileSystemMap["capacity_quota"] = fileSystem.CapacityQuota |
| 176 | + } |
| 177 | + |
| 178 | + if fileSystem.Status != nil { |
| 179 | + fileSystemMap["status"] = fileSystem.Status |
| 180 | + } |
| 181 | + |
| 182 | + if fileSystem.SuperUsers != nil { |
| 183 | + fileSystemMap["super_users"] = fileSystem.SuperUsers |
| 184 | + } |
| 185 | + |
| 186 | + if fileSystem.PosixAcl != nil { |
| 187 | + fileSystemMap["posix_acl"] = fileSystem.PosixAcl |
| 188 | + } |
| 189 | + |
| 190 | + if fileSystem.EnableRanger != nil { |
| 191 | + fileSystemMap["enable_ranger"] = fileSystem.EnableRanger |
| 192 | + } |
| 193 | + |
| 194 | + if fileSystem.RangerServiceAddresses != nil { |
| 195 | + fileSystemMap["ranger_service_addresses"] = fileSystem.RangerServiceAddresses |
| 196 | + } |
| 197 | + |
| 198 | + ids = append(ids, *fileSystem.FileSystemId) |
| 199 | + tmpList = append(tmpList, fileSystemMap) |
| 200 | + } |
| 201 | + |
| 202 | + _ = d.Set("file_systems", tmpList) |
| 203 | + } |
| 204 | + |
| 205 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 206 | + output, ok := d.GetOk("result_output_file") |
| 207 | + if ok && output.(string) != "" { |
| 208 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 209 | + return e |
| 210 | + } |
| 211 | + } |
| 212 | + return nil |
| 213 | +} |
0 commit comments