/
data_source_alicloud_ga_additional_certificates.go
163 lines (154 loc) · 4.39 KB
/
data_source_alicloud_ga_additional_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
163
package alicloud
import (
"fmt"
"time"
"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceAlicloudGaAdditionalCertificates() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlicloudGaAdditionalCertificatesRead,
Schema: map[string]*schema.Schema{
"accelerator_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"ids": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"listener_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"output_file": {
Type: schema.TypeString,
Optional: true,
},
"certificates": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"accelerator_id": {
Type: schema.TypeString,
Computed: true,
},
"certificate_id": {
Type: schema.TypeString,
Computed: true,
},
"id": {
Type: schema.TypeString,
Computed: true,
},
"domain": {
Type: schema.TypeString,
Computed: true,
},
"listener_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceAlicloudGaAdditionalCertificatesRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
action := "ListListenerCertificates"
request := make(map[string]interface{})
request["AcceleratorId"] = d.Get("accelerator_id")
request["ListenerId"] = d.Get("listener_id")
request["RegionId"] = client.RegionId
request["Role"] = "additional"
request["MaxResults"] = PageSizeLarge
var objects []map[string]interface{}
idsMap := make(map[string]string)
if v, ok := d.GetOk("ids"); ok {
for _, vv := range v.([]interface{}) {
if vv == nil {
continue
}
idsMap[vv.(string)] = vv.(string)
}
}
var response map[string]interface{}
conn, err := client.NewGaplusClient()
if err != nil {
return WrapError(err)
}
for {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-11-20"), StringPointer("AK"), nil, request, &runtime)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
addDebug(action, response, request)
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_ga_additional_certificates", action, AlibabaCloudSdkGoERROR)
}
resp, err := jsonpath.Get("$.Certificates", response)
if err != nil {
return WrapErrorf(err, FailedGetAttributeMsg, action, "$.Certificates", response)
}
result, _ := resp.([]interface{})
for _, v := range result {
item := v.(map[string]interface{})
if len(idsMap) > 0 {
if _, ok := idsMap[fmt.Sprint(request["AcceleratorId"], ":", request["ListenerId"], ":", item["Domain"])]; !ok {
continue
}
}
objects = append(objects, item)
}
if nextToken, ok := response["NextToken"].(string); ok && nextToken != "" {
request["NextToken"] = nextToken
} else {
break
}
}
ids := make([]string, 0)
s := make([]map[string]interface{}, 0)
for _, object := range objects {
mapping := map[string]interface{}{
"accelerator_id": request["AcceleratorId"],
"certificate_id": object["CertificateId"],
"id": fmt.Sprint(request["AcceleratorId"], ":", request["ListenerId"], ":", object["Domain"]),
"domain": fmt.Sprint(object["Domain"]),
"listener_id": request["ListenerId"],
}
ids = append(ids, fmt.Sprint(mapping["id"]))
s = append(s, mapping)
}
d.SetId(dataResourceIdHash(ids))
if err := d.Set("ids", ids); err != nil {
return WrapError(err)
}
if err := d.Set("certificates", s); err != nil {
return WrapError(err)
}
if output, ok := d.GetOk("output_file"); ok && output.(string) != "" {
writeToFile(output.(string), s)
}
return nil
}