github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/vsphere_storage_profile.go (about) 1 /* 2 * Copyright 2023 VMware, Inc. All rights reserved. Licensed under the Apache v2 License. 3 */ 4 5 package govcd 6 7 import ( 8 "fmt" 9 "github.com/vmware/go-vcloud-director/v2/types/v56" 10 "net/url" 11 ) 12 13 /* 14 Note: These storage profile methods refer to storage profiles before they get assigned to a provider VDC. 15 This file, with related tests, was created before realizing that these calls do not retrieve the `*(Any)` 16 storage profile. 17 */ 18 19 // StorageProfile contains a storage profile in a given context (usually, a resource pool) 20 type StorageProfile struct { 21 StorageProfile *types.OpenApiStorageProfile 22 vcenter *VCenter 23 client *VCDClient 24 } 25 26 // GetAllStorageProfiles retrieves all storage profiles existing in a given storage profile context 27 // Note: this function finds all *named* resource pools, but not the unnamed one [*(Any)] 28 func (vcenter VCenter) GetAllStorageProfiles(resourcePoolId string, queryParams url.Values) ([]*StorageProfile, error) { 29 client := vcenter.client.Client 30 endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointStorageProfiles 31 minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint) 32 if err != nil { 33 return nil, err 34 } 35 36 urlRef, err := client.OpenApiBuildEndpoint(fmt.Sprintf(endpoint, vcenter.VSphereVCenter.VcId)) 37 if err != nil { 38 return nil, err 39 } 40 41 retrieved := []*types.OpenApiStorageProfile{{}} 42 43 if queryParams == nil { 44 queryParams = url.Values{} 45 } 46 if resourcePoolId != "" { 47 queryParams.Set("filter", fmt.Sprintf("_context==%s", resourcePoolId)) 48 } 49 err = client.OpenApiGetAllItems(minimumApiVersion, urlRef, queryParams, &retrieved, nil) 50 if err != nil { 51 return nil, fmt.Errorf("error getting storage profile list: %s", err) 52 } 53 54 if len(retrieved) == 0 { 55 return nil, nil 56 } 57 var returnList []*StorageProfile 58 59 for _, sp := range retrieved { 60 newSp := sp 61 returnList = append(returnList, &StorageProfile{ 62 StorageProfile: newSp, 63 vcenter: &vcenter, 64 client: vcenter.client, 65 }) 66 } 67 return returnList, nil 68 } 69 70 // GetStorageProfileById retrieves a storage profile in the context of a given resource pool 71 func (vcenter VCenter) GetStorageProfileById(resourcePoolId, id string) (*StorageProfile, error) { 72 storageProfiles, err := vcenter.GetAllStorageProfiles(resourcePoolId, nil) 73 if err != nil { 74 return nil, err 75 } 76 for _, sp := range storageProfiles { 77 if sp.StorageProfile.Moref == id { 78 return sp, nil 79 } 80 } 81 return nil, fmt.Errorf("no storage profile found with ID '%s': %s", id, err) 82 } 83 84 // GetStorageProfileByName retrieves a storage profile in the context of a given resource pool 85 func (vcenter VCenter) GetStorageProfileByName(resourcePoolId, name string) (*StorageProfile, error) { 86 storageProfiles, err := vcenter.GetAllStorageProfiles(resourcePoolId, nil) 87 if err != nil { 88 return nil, err 89 } 90 var found []*StorageProfile 91 for _, sp := range storageProfiles { 92 if sp.StorageProfile.Name == name { 93 found = append(found, sp) 94 } 95 } 96 if len(found) == 0 { 97 return nil, fmt.Errorf("no storage profile found with name '%s': %s", name, ErrorEntityNotFound) 98 } 99 if len(found) > 1 { 100 return nil, fmt.Errorf("more than one storage profile found with name '%s'", name) 101 } 102 return found[0], nil 103 }