问题
Can this logic not be simplified more, the code works, but it has become too much of a spaghetti. The requirements are that there should always be one connection if possible in available
state, but I need to take all the available
states down and re-create them.
these paths I have to cover:
- if both vifs down, call create
- if both vifs available, take them down and up one-by-one
- if one down and one available, bring first up the down one, then take down and bring up the available one
- if only one down, get it up, then create the second one
- if only one up, create the second one, then delete and re-create the first one
var vifsMap = make(map[string][]*directconnect.VirtualInterface)
var vifsStateAvailable, vifsStateDown []*directconnect.VirtualInterface
for _, item := range convertToDirectconnect(lagPair, amz.dcLags) {
vis, err := amz.dcRead.DescribeVirtualInterfaces(&directconnect.DescribeVirtualInterfacesInput{ConnectionId: item.LagId})
if err != nil {
return err
}
for _, vi := range vis.VirtualInterfaces {
if *vi.OwnerAccount == cfg.RemoteAccountID {
if aws.StringValue(vi.VirtualInterfaceState) == directconnect.VirtualInterfaceStateAvailable {
vifsStateAvailable = append(vifsStateAvailable, vi)
} else if aws.StringValue(vi.VirtualInterfaceState) == directconnect.VirtualInterfaceStateDown {
vifsStateDown = append(vifsStateDown, vi)
}
}
}
}
vifsMap["available"] = vifsStateAvailable
vifsMap["down"] = vifsStateDown
if len(vifsStateDown) > 1 {
fmt.Println("contains 2 down elements")
} else if len(vifsStateAvailable) > 1 {
fmt.Println("contains 2 up elements")
} else if len(vifsStateDown) == 1 && len(vifsStateAvailable) == 1 {
fmt.Println("contains 1 vif down and 1 up")
} else if len(vifsStateAvailable) == 1 && len(vifsStateDown) == 1 {
fmt.Println("contains 1 vif up and 1 down")
} else if len(vifsStateDown) == 1 && len(vifsStateAvailable) == 0 {
fmt.Println("contains 1 down only")
} else if len(vifsStateAvailable) == 1 && len(vifsStateDown) == 0 {
fmt.Println("contains 1 up only")
}
来源:https://stackoverflow.com/questions/61213337/simplifying-decision-tree-when-working-with-map-which-has-two-keys-and-value-as