forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_alicloud_cas_certificates.go
162 lines (146 loc) · 3.77 KB
/
data_source_alicloud_cas_certificates.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package alicloud
import (
"regexp"
"strconv"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/cas"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
func dataSourceAlicloudCasCertificates() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlicloudCascertsRead,
Schema: map[string]*schema.Schema{
"name_regex": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"output_file": {
Type: schema.TypeString,
Optional: true,
},
"certificates": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"common": {
Type: schema.TypeString,
Computed: true,
},
"finger_print": {
Type: schema.TypeString,
Computed: true,
},
"issuer": {
Type: schema.TypeString,
Computed: true,
},
"org_name": {
Type: schema.TypeString,
Computed: true,
},
"province": {
Type: schema.TypeString,
Computed: true,
},
"city": {
Type: schema.TypeString,
Computed: true,
},
"country": {
Type: schema.TypeString,
Computed: true,
},
"start_date": {
Type: schema.TypeString,
Computed: true,
},
"end_date": {
Type: schema.TypeString,
Computed: true,
},
"sans": {
Type: schema.TypeString,
Computed: true,
},
"expired": {
Type: schema.TypeBool,
Computed: true,
},
"buy_in_aliyun": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
},
}
}
func dataSourceAlicloudCascertsRead(d *schema.ResourceData, meta interface{}) error {
var allcerts []cas.Certificate
client := meta.(*connectivity.AliyunClient)
args := cas.CreateDescribeUserCertificateListRequest()
args.ShowSize = requests.NewInteger(PageSizeLarge)
for i := 1; ; i++ {
args.CurrentPage = requests.NewInteger(i)
raw, err := client.WithCasClient(func(casClient *cas.Client) (interface{}, error) {
return casClient.DescribeUserCertificateList(args)
})
if err != nil {
return WrapError(err)
}
res, _ := raw.(*cas.DescribeUserCertificateListResponse)
allcerts = append(allcerts, res.CertificateList...)
if len(res.CertificateList) < PageSizeLarge {
break
}
}
var s []map[string]interface{}
var ids []string
for _, cert := range allcerts {
if v, ok := d.GetOk("name_regex"); ok && v.(string) != "" {
r := regexp.MustCompile(v.(string))
if !r.MatchString(cert.Name) {
continue
}
}
mapping := map[string]interface{}{
"id": cert.Id,
"name": cert.Name,
"common": cert.Common,
"finger_print": cert.Fingerprint,
"issuer": cert.Issuer,
"org_name": cert.OrgName,
"province": cert.Province,
"city": cert.City,
"country": cert.Country,
"start_date": cert.StartDate,
"end_date": cert.EndDate,
"sans": cert.Sans,
"expired": cert.Expired,
"buy_in_aliyun": cert.BuyInAliyun,
}
s = append(s, mapping)
ids = append(ids, strconv.Itoa(cert.Id))
}
d.SetId(dataResourceIdHash(ids))
if err := d.Set("certificates", 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
}