Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 17 additions & 22 deletions internal/agent/f5/f5os/f5os.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (f *F5OS) newRequest(method, path string, body any) *http.Request {
}

// apiCall performs an API call to the F5OS device using the provided request.
func (f *F5OS) apiCall(req *http.Request, v any) error {
func (f *F5OS) apiCall(req *http.Request, response any, expected ...int) error {
// Set the Authorization header
if f.token.Valid() {
req.Header.Set("X-Auth-Token", string(f.token))
Expand Down Expand Up @@ -122,6 +122,14 @@ func (f *F5OS) apiCall(req *http.Request, v any) error {
"url": req.URL.Redacted(),
"status": resp.StatusCode,
}).Debug("F5OS API call")

for _, e := range expected {
if resp.StatusCode == e {
log.Debugf("Expected status code %d for %s", e, req.URL.Redacted())
return nil
}
}

if resp.StatusCode < 200 || resp.StatusCode >= 300 {
body, _ := io.ReadAll(resp.Body)
return retry.RetryableError(fmt.Errorf("unexpected status code for %s %d: %s",
Expand All @@ -137,7 +145,7 @@ func (f *F5OS) apiCall(req *http.Request, v any) error {
}
}

if err = json.NewDecoder(resp.Body).Decode(v); err != nil {
if err = json.NewDecoder(resp.Body).Decode(response); err != nil {
if errors.Is(err, io.EOF) {
// If the body is empty, we can return nil
return nil
Expand Down Expand Up @@ -348,7 +356,9 @@ func (f *F5OS) DeleteVLAN(segmentId int) error {

path := fmt.Sprintf("api/data/openconfig-vlan:vlans/vlan=%d", segmentId)
req := f.newRequest("DELETE", path, nil)
if err := f.apiCall(req, nil); err != nil {

// We expect a 404 if the VLAN does not exist, so we tolerate that error
if err := f.apiCall(req, nil, http.StatusNotFound); err != nil {
return fmt.Errorf("error deleting VLAN %d on %s: %w", segmentId, f.uri.Host, err)
}
return nil
Expand All @@ -361,26 +371,11 @@ func (f *F5OS) DeleteSelfIP(_ string) error {
func (f *F5OS) DeleteInterfaceVlan(segmentID int) error {
path := "api/data/openconfig-interfaces:interfaces/interface=" +
config.Global.Agent.PhysicalInterface +
"/openconfig-if-aggregate:aggregation/openconfig-vlan:switched-vlan/config/trunk-vlans"

var vlans trunkVlans
if err := f.apiCall(f.newRequest("GET", path, nil), &vlans); err != nil {
return fmt.Errorf("error fetching trunk VLANs: %w", err)
}

b := make([]int, 0, len(vlans.OpenconfigVlanTrunkVlans)-1)
for _, v := range vlans.OpenconfigVlanTrunkVlans {
if v != segmentID {
b = append(b, v)
}
}
if len(b) == len(vlans.OpenconfigVlanTrunkVlans) {
return nil // nothing to delete
}
vlans.OpenconfigVlanTrunkVlans = b
"/openconfig-if-aggregate:aggregation/openconfig-vlan:switched-vlan/config/trunk-vlans" +
"=" + fmt.Sprintf("%d", segmentID)

req := f.newRequest("PUT", path, vlans)
if err := f.apiCall(req, nil); err != nil {
req := f.newRequest("DELETE", path, nil)
if err := f.apiCall(req, nil, http.StatusNotFound); err != nil {
return fmt.Errorf("error deleting interface VLAN %d on %s: %w", segmentID, f.uri.Host, err)
}
return nil
Expand Down
Loading