-
Notifications
You must be signed in to change notification settings - Fork 133
/
data_source_tc_api_gateway_customer_domains.go
158 lines (146 loc) · 4.73 KB
/
data_source_tc_api_gateway_customer_domains.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
package apigateway
import (
"context"
"log"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
apigateway "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apigateway/v20180808"
)
func DataSourceTencentCloudAPIGatewayCustomerDomains() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudAPIGatewayCustomerDomainRead,
Schema: map[string]*schema.Schema{
"service_id": {
Type: schema.TypeString,
Required: true,
Description: "The service ID.",
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
//Computed
"list": {
Type: schema.TypeList,
Computed: true,
Description: "Service custom domain name list.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"domain_name": {
Type: schema.TypeString,
Computed: true,
Description: "Domain name.",
},
"is_status_on": {
Type: schema.TypeBool,
Computed: true,
Description: "Domain name resolution status. Valid values: `true`, `false`. `true` means normal parsing, `false` means parsing failed.",
},
"certificate_id": {
Type: schema.TypeString,
Computed: true,
Description: "The certificate ID.",
},
"is_default_mapping": {
Type: schema.TypeBool,
Computed: true,
Description: "Whether to use default path mapping. Valid values: `true`, `false`. `true` means to use default path mapping, `false` means to use custom path mapping.",
},
"protocol": {
Type: schema.TypeString,
Computed: true,
Description: "Custom domain name agreement type.",
},
"net_type": {
Type: schema.TypeString,
Computed: true,
Description: "Network type.",
},
"path_mappings": {
Type: schema.TypeList,
Computed: true,
Description: "Domain name mapping path and environment list.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"path": {
Type: schema.TypeString,
Computed: true,
Description: "The domain mapping path.",
},
"environment": {
Type: schema.TypeString,
Computed: true,
Description: "Release environment.",
},
},
},
},
},
},
},
},
}
}
func dataSourceTencentCloudAPIGatewayCustomerDomainRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_api_gateway_customer_domains.read")
var (
logId = tccommon.GetLogId(tccommon.ContextNil)
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
apiGatewayService = APIGatewayService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
serviceId = d.Get("service_id").(string)
infos []*apigateway.DomainSetList
list []map[string]interface{}
err error
)
if err = resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
infos, err = apiGatewayService.DescribeServiceSubDomains(ctx, serviceId)
if err != nil {
return tccommon.RetryError(err, tccommon.InternalError)
}
return nil
}); err != nil {
return err
}
for _, info := range infos {
var (
pathMapping []map[string]interface{}
status bool
)
if !*info.IsDefaultMapping && *info.DomainName != "" {
var mappings *apigateway.ServiceSubDomainMappings
mappings, err = apiGatewayService.DescribeServiceSubDomainMappings(ctx, serviceId, *info.DomainName)
if err != nil {
return err
}
for _, v := range mappings.PathMappingSet {
pathMapping = append(pathMapping, map[string]interface{}{
"path": v.Path,
"environment": v.Environment,
})
}
}
if *info.Status == 1 {
status = true
}
list = append(list, map[string]interface{}{
"domain_name": info.DomainName,
"is_status_on": status,
"certificate_id": info.CertificateId,
"is_default_mapping": info.IsDefaultMapping,
"protocol": info.Protocol,
"net_type": info.NetType,
"path_mappings": pathMapping,
})
}
if err = d.Set("list", list); err != nil {
log.Printf("[CRITAL]%s provider set list fail, reason:%s", logId, err.Error())
return err
}
d.SetId(serviceId)
if output, ok := d.GetOk("result_output_file"); ok && output.(string) != "" {
return tccommon.WriteToFile(output.(string), list)
}
return nil
}