|
| 1 | +package govcd |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright 2023 VMware, Inc. All rights reserved. Licensed under the Apache v2 License. |
| 5 | + */ |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "net/url" |
| 10 | + |
| 11 | + "github.com/vmware/go-vcloud-director/v2/types/v56" |
| 12 | +) |
| 13 | + |
| 14 | +// VgpuProfile defines a vGPU profile which is fetched from vCenter |
| 15 | +type VgpuProfile struct { |
| 16 | + VgpuProfile *types.VgpuProfile |
| 17 | + client *Client |
| 18 | +} |
| 19 | + |
| 20 | +// GetAllVgpuProfiles gets all vGPU profiles that are available to VCD |
| 21 | +func (client *VCDClient) GetAllVgpuProfiles(queryParameters url.Values) ([]*VgpuProfile, error) { |
| 22 | + return getAllVgpuProfiles(queryParameters, &client.Client) |
| 23 | +} |
| 24 | + |
| 25 | +// GetVgpuProfilesByProviderVdc gets all vGPU profiles that are available to a specific provider VDC |
| 26 | +func (client *VCDClient) GetVgpuProfilesByProviderVdc(providerVdcUrn string) ([]*VgpuProfile, error) { |
| 27 | + queryParameters := url.Values{} |
| 28 | + queryParameters = queryParameterFilterAnd(fmt.Sprintf("pvdcId==%s", providerVdcUrn), queryParameters) |
| 29 | + return client.GetAllVgpuProfiles(queryParameters) |
| 30 | +} |
| 31 | + |
| 32 | +// GetVgpuProfileById gets a vGPU profile by ID |
| 33 | +func (client *VCDClient) GetVgpuProfileById(vgpuProfileId string) (*VgpuProfile, error) { |
| 34 | + return getVgpuProfileById(vgpuProfileId, &client.Client) |
| 35 | +} |
| 36 | + |
| 37 | +// GetVgpuProfileByName gets a vGPU profile by name |
| 38 | +func (client *VCDClient) GetVgpuProfileByName(vgpuProfileName string) (*VgpuProfile, error) { |
| 39 | + return getVgpuProfileByFilter("name", vgpuProfileName, &client.Client) |
| 40 | +} |
| 41 | + |
| 42 | +// GetVgpuProfileByTenantFacingName gets a vGPU profile by its tenant facing name |
| 43 | +func (client *VCDClient) GetVgpuProfileByTenantFacingName(tenantFacingName string) (*VgpuProfile, error) { |
| 44 | + return getVgpuProfileByFilter("tenantFacingName", tenantFacingName, &client.Client) |
| 45 | +} |
| 46 | + |
| 47 | +// Update updates a vGPU profile with new parameters |
| 48 | +func (profile *VgpuProfile) Update(newProfile *types.VgpuProfile) error { |
| 49 | + client := profile.client |
| 50 | + endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointVgpuProfile |
| 51 | + minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + urlRef, err := client.OpenApiBuildEndpoint(endpoint, "/", profile.VgpuProfile.Id) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + err = client.OpenApiPutItemSync(minimumApiVersion, urlRef, nil, newProfile, nil, nil) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + // We need to refresh here, as PUT returns the original struct instead of the updated one |
| 67 | + err = profile.Refresh() |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +// Refresh updates the current state of the vGPU profile |
| 76 | +func (profile *VgpuProfile) Refresh() error { |
| 77 | + var err error |
| 78 | + newProfile, err := getVgpuProfileById(profile.VgpuProfile.Id, profile.client) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + profile.VgpuProfile = newProfile.VgpuProfile |
| 83 | + |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +func getVgpuProfileByFilter(filter, filterValue string, client *Client) (*VgpuProfile, error) { |
| 88 | + queryParameters := url.Values{} |
| 89 | + queryParameters = queryParameterFilterAnd(fmt.Sprintf("%s==%s", filter, filterValue), queryParameters) |
| 90 | + vgpuProfiles, err := getAllVgpuProfiles(queryParameters, client) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + |
| 95 | + vgpuProfile, err := oneOrError(filter, filterValue, vgpuProfiles) |
| 96 | + if err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + |
| 100 | + return vgpuProfile, nil |
| 101 | +} |
| 102 | + |
| 103 | +func getVgpuProfileById(vgpuProfileId string, client *Client) (*VgpuProfile, error) { |
| 104 | + endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointVgpuProfile |
| 105 | + minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint) |
| 106 | + if err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + |
| 110 | + urlRef, err := client.OpenApiBuildEndpoint(endpoint, "/", vgpuProfileId) |
| 111 | + if err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + |
| 115 | + profile := &VgpuProfile{ |
| 116 | + client: client, |
| 117 | + } |
| 118 | + err = client.OpenApiGetItem(minimumApiVersion, urlRef, nil, &profile.VgpuProfile, nil) |
| 119 | + if err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + |
| 123 | + return profile, nil |
| 124 | +} |
| 125 | + |
| 126 | +func getAllVgpuProfiles(queryParameters url.Values, client *Client) ([]*VgpuProfile, error) { |
| 127 | + endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointVgpuProfile |
| 128 | + minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint) |
| 129 | + if err != nil { |
| 130 | + return nil, err |
| 131 | + } |
| 132 | + |
| 133 | + urlRef, err := client.OpenApiBuildEndpoint(endpoint) |
| 134 | + if err != nil { |
| 135 | + return nil, err |
| 136 | + } |
| 137 | + |
| 138 | + responses := []*types.VgpuProfile{{}} |
| 139 | + |
| 140 | + err = client.OpenApiGetAllItems(minimumApiVersion, urlRef, queryParameters, &responses, nil) |
| 141 | + if err != nil { |
| 142 | + return nil, err |
| 143 | + } |
| 144 | + |
| 145 | + wrappedVgpuProfiles := make([]*VgpuProfile, len(responses)) |
| 146 | + for index, response := range responses { |
| 147 | + wrappedVgpuProfile := &VgpuProfile{ |
| 148 | + client: client, |
| 149 | + VgpuProfile: response, |
| 150 | + } |
| 151 | + wrappedVgpuProfiles[index] = wrappedVgpuProfile |
| 152 | + } |
| 153 | + |
| 154 | + return wrappedVgpuProfiles, nil |
| 155 | +} |
0 commit comments