This repository has been archived by the owner on Aug 25, 2020. It is now read-only.
forked from aliyun/terraform-provider-alicloud
/
data_source_alicloud_nas_protocols.go
103 lines (95 loc) · 2.68 KB
/
data_source_alicloud_nas_protocols.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package alicloud
import (
"strings"
"github.com/aliyun/alibaba-cloud-sdk-go/services/nas"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
func dataSourceAlicloudNasProtocols() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlicloudNasProtocolsRead,
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateAllowedStringValue([]string{
"Capacity",
"Performance",
}),
},
"zone_id": {
Type: schema.TypeString,
Optional: true,
},
"output_file": {
Type: schema.TypeString,
Optional: true,
},
"protocols": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func dataSourceAlicloudNasProtocolsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
request := nas.CreateDescribeZonesRequest()
var nasProtocol []nas.DescribeZonesProtocol
for {
raw, err := client.WithNasClient(func(nasClient *nas.Client) (interface{}, error) {
return nasClient.DescribeZones(request)
})
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_nas_protocols", request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw)
response, _ := raw.(*nas.DescribeZonesResponse)
if len(response.Zones.Zone) < 1 {
break
}
for _, val := range response.Zones.Zone {
if v, ok := d.GetOk("zone_id"); ok && val.ZoneId != Trim(v.(string)) {
continue
}
if v, ok := d.GetOk("type"); ok {
if Trim(v.(string)) == "Performance" {
if len(val.Performance.Protocol) == 0 {
continue
} else {
nasProtocol = append(nasProtocol, val.Performance)
}
}
if Trim(v.(string)) == "Capacity" {
if len(val.Capacity.Protocol) == 0 {
continue
} else {
nasProtocol = append(nasProtocol, val.Capacity)
}
}
}
}
break
}
return nasProtocolsDescriptionAttributes(d, nasProtocol)
}
func nasProtocolsDescriptionAttributes(d *schema.ResourceData, nasProtocol []nas.DescribeZonesProtocol) error {
var s []string
var ids []string
for _, val := range nasProtocol {
for _, protocol := range val.Protocol {
s = append(s, strings.ToUpper(protocol))
ids = append(ids, protocol)
}
}
d.SetId(dataResourceIdHash(ids))
if err := d.Set("protocols", s); err != nil {
return WrapError(err)
}
// create a json file in current directory and write data source to it.
if output, ok := d.GetOk("output_file"); ok && output.(string) != "" {
writeToFile(output.(string), s)
}
return nil
}