-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathdata_source_tc_dcdb_orders.go
156 lines (131 loc) · 4.11 KB
/
data_source_tc_dcdb_orders.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
package dcdb
import (
"context"
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"
dcdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dcdb/v20180411"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func DataSourceTencentCloudDcdbOrders() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudDcdbOrdersRead,
Schema: map[string]*schema.Schema{
"deal_names": {
Required: true,
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "List of long order numbers to be queried, which are returned for the APIs for creating, renewing, or scaling instances.",
},
"deals": {
Computed: true,
Type: schema.TypeList,
Description: "Order information list.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"deal_name": {
Type: schema.TypeString,
Computed: true,
Description: "Order number.",
},
"owner_uin": {
Type: schema.TypeString,
Computed: true,
Description: "Account.",
},
"count": {
Type: schema.TypeInt,
Computed: true,
Description: "Number of items.",
},
"flow_id": {
Type: schema.TypeInt,
Computed: true,
Description: "ID of the associated process, which can be used to query the process execution status.",
},
"instance_ids": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Computed: true,
Description: "The ID of the created instance, which is required only for the order that creates an instance.Note: This field may return null, indicating that no valid values can be obtained.",
},
"pay_mode": {
Type: schema.TypeInt,
Computed: true,
Description: "Payment mode. Valid values: 0 (postpaid), 1 (prepaid).",
},
},
},
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
},
}
}
func dataSourceTencentCloudDcdbOrdersRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_dcdb_orders.read")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
paramMap := make(map[string]interface{})
if v, ok := d.GetOk("deal_names"); ok {
dealNamesSet := v.(*schema.Set).List()
paramMap["DealNames"] = helper.InterfacesStringsPoint(dealNamesSet)
}
service := DcdbService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
var deals []*dcdb.Deal
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
result, e := service.DescribeDcdbOrdersByFilter(ctx, paramMap)
if e != nil {
return tccommon.RetryError(e)
}
deals = result
return nil
})
if err != nil {
return err
}
ids := make([]string, 0, len(deals))
tmpList := make([]map[string]interface{}, 0, len(deals))
if deals != nil {
for _, deal := range deals {
dealMap := map[string]interface{}{}
if deal.DealName != nil {
dealMap["deal_name"] = deal.DealName
}
if deal.OwnerUin != nil {
dealMap["owner_uin"] = deal.OwnerUin
}
if deal.Count != nil {
dealMap["count"] = deal.Count
}
if deal.FlowId != nil {
dealMap["flow_id"] = deal.FlowId
}
if deal.InstanceIds != nil {
dealMap["instance_ids"] = deal.InstanceIds
}
if deal.PayMode != nil {
dealMap["pay_mode"] = deal.PayMode
}
ids = append(ids, *deal.DealName)
tmpList = append(tmpList, dealMap)
}
_ = d.Set("deals", tmpList)
}
d.SetId(helper.DataResourceIdsHash(ids))
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := tccommon.WriteToFile(output.(string), tmpList); e != nil {
return e
}
}
return nil
}