/
resource_ip_rib_oper.go
158 lines (146 loc) · 4.57 KB
/
resource_ip_rib_oper.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 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 resourceIpRibOper() *schema.Resource {
return &schema.Resource{
Description: "`thunder_ip_rib_oper`: Operational Status for the object rib\n\n__PLACEHOLDER__",
ReadContext: resourceIpRibOperRead,
Schema: map[string]*schema.Schema{
"oper": {
Type: schema.TypeList, MaxItems: 1, Optional: true, Description: "",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString, Optional: true, Description: "",
},
"total": {
Type: schema.TypeInt, Optional: true, Description: "",
},
"total_paths": {
Type: schema.TypeInt, Optional: true, Description: "",
},
"limit": {
Type: schema.TypeInt, Optional: true, Description: "",
},
"ipv4_routes": {
Type: schema.TypeList, Optional: true, Description: "",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"prefix": {
Type: schema.TypeString, Optional: true, Description: "",
},
"prefixlen": {
Type: schema.TypeInt, Optional: true, Description: "",
},
"type": {
Type: schema.TypeString, Optional: true, Description: "",
},
"subtype": {
Type: schema.TypeString, Optional: true, Description: "",
},
"distance": {
Type: schema.TypeInt, Optional: true, Description: "",
},
"metric": {
Type: schema.TypeInt, Optional: true, Description: "",
},
"nexthop": {
Type: schema.TypeString, Optional: true, Description: "",
},
"interface": {
Type: schema.TypeString, Optional: true, Description: "",
},
},
},
},
},
},
},
},
}
}
func resourceIpRibOperRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(Thunder)
logger := client.log
logger.Println("resourceIpRibOperRead()")
var diags diag.Diagnostics
if client.Host != "" {
obj := dataToEndpointIpRibOper(d)
res, err := obj.Get(client.Token, client.Host, d.Id(), logger)
d.SetId(obj.GetId())
logger.Println(res)
IpRibOperOper := setObjectIpRibOperOper(res)
d.Set("oper", IpRibOperOper)
if err != nil {
return diag.FromErr(err)
}
}
return diags
}
func setObjectIpRibOperOper(ret edpt.DataIpRibOper) []interface{} {
return []interface{}{
map[string]interface{}{
"description": ret.DtIpRibOper.Oper.Description,
"total": ret.DtIpRibOper.Oper.Total,
"total_paths": ret.DtIpRibOper.Oper.TotalPaths,
"limit": ret.DtIpRibOper.Oper.Limit,
"ipv4_routes": setSliceIpRibOperOperIpv4Routes(ret.DtIpRibOper.Oper.Ipv4Routes),
},
}
}
func setSliceIpRibOperOperIpv4Routes(d []edpt.IpRibOperOperIpv4Routes) []map[string]interface{} {
result := []map[string]interface{}{}
for _, item := range d {
in := make(map[string]interface{})
in["prefix"] = item.Prefix
in["prefixlen"] = item.Prefixlen
in["type"] = item.Type
in["subtype"] = item.Subtype
in["distance"] = item.Distance
in["metric"] = item.Metric
in["nexthop"] = item.Nexthop
in["interface"] = item.Interface
result = append(result, in)
}
return result
}
func getObjectIpRibOperOper(d []interface{}) edpt.IpRibOperOper {
count1 := len(d)
var ret edpt.IpRibOperOper
if count1 > 0 {
in := d[0].(map[string]interface{})
ret.Description = in["description"].(string)
ret.Total = in["total"].(int)
ret.TotalPaths = in["total_paths"].(int)
ret.Limit = in["limit"].(int)
ret.Ipv4Routes = getSliceIpRibOperOperIpv4Routes(in["ipv4_routes"].([]interface{}))
}
return ret
}
func getSliceIpRibOperOperIpv4Routes(d []interface{}) []edpt.IpRibOperOperIpv4Routes {
count1 := len(d)
ret := make([]edpt.IpRibOperOperIpv4Routes, 0, count1)
for _, item := range d {
in := item.(map[string]interface{})
var oi edpt.IpRibOperOperIpv4Routes
oi.Prefix = in["prefix"].(string)
oi.Prefixlen = in["prefixlen"].(int)
oi.Type = in["type"].(string)
oi.Subtype = in["subtype"].(string)
oi.Distance = in["distance"].(int)
oi.Metric = in["metric"].(int)
oi.Nexthop = in["nexthop"].(string)
oi.Interface = in["interface"].(string)
ret = append(ret, oi)
}
return ret
}
func dataToEndpointIpRibOper(d *schema.ResourceData) edpt.IpRibOper {
var ret edpt.IpRibOper
ret.Oper = getObjectIpRibOperOper(d.Get("oper").([]interface{}))
return ret
}