-
Notifications
You must be signed in to change notification settings - Fork 149
/
types.go
160 lines (139 loc) · 5.27 KB
/
types.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
package client
import (
"errors"
"reflect"
"strings"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
"github.com/go-logr/logr"
)
var ErrInvalidArgs = errors.New("invalid args")
// log fields
const (
LogFieldAPI = "api"
LogFieldRequestID = "requestID"
LogFieldInstanceID = "instanceID"
LogFieldSecondaryIPCount = "secondaryIPCount"
LogFieldENIID = "eni"
LogFieldIPs = "ips"
LogFieldEIPID = "eip"
LogFieldPrivateIP = "privateIP"
LogFieldVSwitchID = "vSwitchID"
LogFieldSgID = "securityGroupID"
LogFieldResourceGroupID = "resourceGroupID"
)
const (
eniNamePrefix = "eni-cni-"
eniDescription = "interface create by terway"
maxSinglePageSize = 500
)
// status for eni
const (
ENIStatusInUse string = "InUse"
ENIStatusAvailable string = "Available"
)
const (
ENITypePrimary string = "Primary"
ENITypeSecondary string = "Secondary"
ENITypeTrunk string = "Trunk"
ENITypeMember string = "Member"
)
const (
ENITrafficModeRDMA string = "HighPerformance"
)
const EIPInstanceTypeNetworkInterface = "NetworkInterface"
// NetworkInterface openAPI result for ecs.CreateNetworkInterfaceResponse and ecs.NetworkInterfaceSet
type NetworkInterface struct {
Status string `json:"status,omitempty"`
MacAddress string `json:"mac_address,omitempty"`
NetworkInterfaceID string `json:"network_interface_id,omitempty"`
VSwitchID string `json:"v_switch_id,omitempty"`
PrivateIPAddress string `json:"private_ip_address,omitempty"`
PrivateIPSets []ecs.PrivateIpSet `json:"private_ip_sets"`
ZoneID string `json:"zone_id,omitempty"`
SecurityGroupIDs []string `json:"security_group_ids,omitempty"`
ResourceGroupID string `json:"resource_group_id,omitempty"`
IPv6Set []ecs.Ipv6Set `json:"ipv6_set,omitempty"`
Tags []ecs.Tag `json:"tags,omitempty"`
// fields for DescribeNetworkInterface
Type string `json:"type,omitempty"`
InstanceID string `json:"instance_id,omitempty"`
TrunkNetworkInterfaceID string `json:"trunk_network_interface_id,omitempty"`
NetworkInterfaceTrafficMode string `json:"network_interface_traffic_mode"`
DeviceIndex int `json:"device_index,omitempty"`
CreationTime string `json:"creation_time,omitempty"`
}
func FromCreateResp(in *ecs.CreateNetworkInterfaceResponse) *NetworkInterface {
return &NetworkInterface{
Status: in.Status,
MacAddress: in.MacAddress,
NetworkInterfaceID: in.NetworkInterfaceId,
VSwitchID: in.VSwitchId,
PrivateIPAddress: in.PrivateIpAddress,
PrivateIPSets: in.PrivateIpSets.PrivateIpSet,
ZoneID: in.ZoneId,
SecurityGroupIDs: in.SecurityGroupIds.SecurityGroupId,
IPv6Set: in.Ipv6Sets.Ipv6Set,
Tags: in.Tags.Tag,
Type: in.Type,
ResourceGroupID: in.ResourceGroupId,
}
}
func FromDescribeResp(in *ecs.NetworkInterfaceSet) *NetworkInterface {
ins := in.InstanceId
if in.InstanceId == "" {
ins = in.Attachment.InstanceId
}
return &NetworkInterface{
Status: in.Status,
MacAddress: in.MacAddress,
NetworkInterfaceID: in.NetworkInterfaceId,
InstanceID: ins,
VSwitchID: in.VSwitchId,
PrivateIPAddress: in.PrivateIpAddress,
ZoneID: in.ZoneId,
SecurityGroupIDs: in.SecurityGroupIds.SecurityGroupId,
IPv6Set: in.Ipv6Sets.Ipv6Set,
PrivateIPSets: in.PrivateIpSets.PrivateIpSet,
Tags: in.Tags.Tag,
TrunkNetworkInterfaceID: in.Attachment.TrunkNetworkInterfaceId,
NetworkInterfaceTrafficMode: in.NetworkInterfaceTrafficMode,
DeviceIndex: in.Attachment.DeviceIndex,
Type: in.Type,
CreationTime: in.CreationTime,
}
}
// LogFields function enhances the provided logger with key-value pairs extracted from the fields of the given object.
//
// Parameters:
// l - The original logr.Logger instance to be augmented with object field information.
// obj - An arbitrary object whose fields will be inspected for logging. Must be of a struct type.
//
// Return Value:
// Returns an updated logr.Logger instance that includes key-value pairs for non-empty, non-zero fields of the input object.
// The original logger `l` is modified in place, and the returned logger is a reference to the same instance.
func LogFields(l logr.Logger, obj any) logr.Logger {
r := l
t := reflect.TypeOf(obj)
realObj := obj
if t.Kind() == reflect.Ptr {
t = t.Elem()
objValue := reflect.ValueOf(obj).Elem()
realObj = objValue.Interface()
}
if t.Kind() == reflect.Struct {
r.WithValues(LogFieldAPI, strings.TrimSuffix(t.Name(), "Request"))
}
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
tagValue := field.Tag.Get("name")
if tagValue == "" {
continue
}
fieldValue := reflect.ValueOf(realObj).FieldByName(field.Name)
if !fieldValue.IsValid() || fieldValue.IsZero() {
continue
}
r = r.WithValues(field.Name, fieldValue.Interface())
}
return r
}