github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/nsxt_network_context_profile.go (about) 1 package govcd 2 3 import ( 4 "fmt" 5 "net/url" 6 7 "github.com/vmware/go-vcloud-director/v2/types/v56" 8 ) 9 10 // GetAllNetworkContextProfiles retrieves a slice of types.NsxtNetworkContextProfile 11 // This function requires at least a filter value for 'context_id' which can be one of: 12 // * Org VDC ID - to get Network Context Profiles scoped for VDC 13 // * Network provider ID - to get Network Context Profiles scoped for attached NSX-T environment 14 // * VDC Group ID - to get Network Context Profiles scoped for attached NSX-T environment 15 func GetAllNetworkContextProfiles(client *Client, queryParameters url.Values) ([]*types.NsxtNetworkContextProfile, error) { 16 endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointNetworkContextProfiles 17 apiVersion, err := client.getOpenApiHighestElevatedVersion(endpoint) 18 if err != nil { 19 return nil, err 20 } 21 22 urlRef, err := client.OpenApiBuildEndpoint(endpoint) 23 if err != nil { 24 return nil, err 25 } 26 27 typeResponses := []*types.NsxtNetworkContextProfile{} 28 err = client.OpenApiGetAllItems(apiVersion, urlRef, queryParameters, &typeResponses, nil) 29 if err != nil { 30 return nil, err 31 } 32 33 return typeResponses, nil 34 } 35 36 // GetNetworkContextProfilesByScopeAndName retrieves a single NSX-T Network Context Profile by name 37 // and context ID. All fields - name, scope and contextId are mandatory 38 // 39 // contextId is mandatory and can be one off: 40 // * Org VDC ID - to get Network Context Profiles scoped for VDC 41 // * Network provider ID - to get Network Context Profiles scoped for attached NSX-T environment 42 // * VDC Group ID - to get Network Context Profiles scoped for attached NSX-T environment 43 // 44 // scope can be one off: 45 // * SYSTEM 46 // * PROVIDER 47 // * TENANT 48 func GetNetworkContextProfilesByNameScopeAndContext(client *Client, name, scope, contextId string) (*types.NsxtNetworkContextProfile, error) { 49 if name == "" || contextId == "" || scope == "" { 50 return nil, fmt.Errorf("error - 'name', 'scope' and 'contextId' must be specified") 51 } 52 53 queryParams := copyOrNewUrlValues(nil) 54 queryParams.Add("filter", fmt.Sprintf("name==%s", name)) 55 queryParams = queryParameterFilterAnd(fmt.Sprintf("_context==%s", contextId), queryParams) 56 queryParams = queryParameterFilterAnd(fmt.Sprintf("scope==%s", scope), queryParams) 57 58 allProfiles, err := GetAllNetworkContextProfiles(client, queryParams) 59 if err != nil { 60 return nil, fmt.Errorf("error retrieving Network Context Profiles by name '%s', scope '%s' and context ID '%s': %s ", 61 name, scope, contextId, err) 62 } 63 64 return returnSingleNetworkContextProfile(allProfiles) 65 } 66 67 func returnSingleNetworkContextProfile(allProfiles []*types.NsxtNetworkContextProfile) (*types.NsxtNetworkContextProfile, error) { 68 if len(allProfiles) > 1 { 69 return nil, fmt.Errorf("got more than 1 NSX-T Network Context Profile %d", len(allProfiles)) 70 } 71 72 if len(allProfiles) < 1 { 73 return nil, fmt.Errorf("%s: got 0 NSX-T Network Context Profiles", ErrorEntityNotFound) 74 } 75 76 return allProfiles[0], nil 77 }