github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/lb.go (about)

     1  /*
     2   * Copyright 2019 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     3   */
     4  
     5  package govcd
     6  
     7  import (
     8  	"fmt"
     9  	"path"
    10  	"strings"
    11  )
    12  
    13  // extractNsxObjectIdFromPath parses proxied NSX API response Location header and
    14  // extracts ID for newly created object from it. The object ID is the last element in path.
    15  // It expects the path to have at least one "/" to be a valid path and cleans up the trailing slash
    16  // if there is one.
    17  //
    18  // Sample locationPath from API: /network/edges/edge-3/loadbalancer/config/monitors/monitor-5
    19  // Expected ID to be returned: monitor-5
    20  func extractNsxObjectIdFromPath(locationPath string) (string, error) {
    21  	if locationPath == "" {
    22  		return "", fmt.Errorf("unable to get ID from empty path")
    23  	}
    24  
    25  	cleanPath := path.Clean(locationPath) // Removes trailing slash if there is one
    26  	splitPath := strings.Split(cleanPath, "/")
    27  
    28  	if len(splitPath) < 2 {
    29  		return "", fmt.Errorf("path does not contain url path: %s", splitPath)
    30  	}
    31  
    32  	objectID := splitPath[len(splitPath)-1]
    33  
    34  	return objectID, nil
    35  }