1
\$\begingroup\$

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:

  1. if both vifs down, call create
  2. if both vifs available, take them down and up one-by-one
  3. if one down and one available, bring first up the down one, then take down and bring up the available one
  4. if only one down, get it up, then create the second one
  5. 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")
}
```
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.