/
resource_gslb_site_stats.go
154 lines (134 loc) · 4.39 KB
/
resource_gslb_site_stats.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
package thunder
import (
"context"
edpt "github.com/a10networks/terraform-provider-thunder/thunder/axapi/endpoint"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceGslbSiteStats() *schema.Resource {
return &schema.Resource{
Description: "`thunder_gslb_site_stats`: Statistics for the object site\n\n__PLACEHOLDER__",
ReadContext: resourceGslbSiteStatsRead,
Schema: map[string]*schema.Schema{
"ip_server_list": {
Type: schema.TypeList, Optional: true, Description: "",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip_server_name": {
Type: schema.TypeString, Required: true, Description: "Specify the real server name",
},
"stats": {
Type: schema.TypeList, MaxItems: 1, Optional: true, Description: "",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"hits": {
Type: schema.TypeInt, Optional: true, Description: "Number of times the IP was selected",
},
"recent": {
Type: schema.TypeInt, Optional: true, Description: "Recent hits",
},
},
},
},
},
},
},
"site_name": {
Type: schema.TypeString, Required: true, Description: "Specify GSLB site name",
},
"stats": {
Type: schema.TypeList, MaxItems: 1, Optional: true, Description: "",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"hits": {
Type: schema.TypeInt, Optional: true, Description: "Number of times the site was selected",
},
},
},
},
},
}
}
func resourceGslbSiteStatsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(Thunder)
logger := client.log
logger.Println("resourceGslbSiteStatsRead()")
var diags diag.Diagnostics
if client.Host != "" {
obj := dataToEndpointGslbSiteStats(d)
res, err := obj.Get(client.Token, client.Host, d.Id(), logger)
d.SetId(obj.GetId())
logger.Println(res)
GslbSiteStatsIpServerList := setSliceGslbSiteStatsIpServerList(res)
d.Set("ip_server_list", GslbSiteStatsIpServerList)
GslbSiteStatsStats := setObjectGslbSiteStatsStats(res)
d.Set("stats", GslbSiteStatsStats)
if err != nil {
return diag.FromErr(err)
}
}
return diags
}
func setSliceGslbSiteStatsIpServerList(d edpt.DataGslbSiteStats) []map[string]interface{} {
result := []map[string]interface{}{}
for _, item := range d.DtGslbSiteStats.IpServerList {
in := make(map[string]interface{})
in["ip_server_name"] = item.IpServerName
in["stats"] = setObjectGslbSiteStatsIpServerListStats(item.Stats)
result = append(result, in)
}
return result
}
func setObjectGslbSiteStatsIpServerListStats(d edpt.GslbSiteStatsIpServerListStats) []map[string]interface{} {
result := []map[string]interface{}{}
in := make(map[string]interface{})
in["hits"] = d.Hits
in["recent"] = d.Recent
result = append(result, in)
return result
}
func setObjectGslbSiteStatsStats(ret edpt.DataGslbSiteStats) []interface{} {
return []interface{}{
map[string]interface{}{
"hits": ret.DtGslbSiteStats.Stats.Hits,
},
}
}
func getSliceGslbSiteStatsIpServerList(d []interface{}) []edpt.GslbSiteStatsIpServerList {
count1 := len(d)
ret := make([]edpt.GslbSiteStatsIpServerList, 0, count1)
for _, item := range d {
in := item.(map[string]interface{})
var oi edpt.GslbSiteStatsIpServerList
oi.IpServerName = in["ip_server_name"].(string)
oi.Stats = getObjectGslbSiteStatsIpServerListStats(in["stats"].([]interface{}))
ret = append(ret, oi)
}
return ret
}
func getObjectGslbSiteStatsIpServerListStats(d []interface{}) edpt.GslbSiteStatsIpServerListStats {
count1 := len(d)
var ret edpt.GslbSiteStatsIpServerListStats
if count1 > 0 {
in := d[0].(map[string]interface{})
ret.Hits = in["hits"].(int)
ret.Recent = in["recent"].(int)
}
return ret
}
func getObjectGslbSiteStatsStats(d []interface{}) edpt.GslbSiteStatsStats {
count1 := len(d)
var ret edpt.GslbSiteStatsStats
if count1 > 0 {
in := d[0].(map[string]interface{})
ret.Hits = in["hits"].(int)
}
return ret
}
func dataToEndpointGslbSiteStats(d *schema.ResourceData) edpt.GslbSiteStats {
var ret edpt.GslbSiteStats
ret.IpServerList = getSliceGslbSiteStatsIpServerList(d.Get("ip_server_list").([]interface{}))
ret.SiteName = d.Get("site_name").(string)
ret.Stats = getObjectGslbSiteStatsStats(d.Get("stats").([]interface{}))
return ret
}