-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add liquid nove report usage endpoint
- Loading branch information
Showing
4 changed files
with
434 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/******************************************************************************* | ||
* | ||
* Copyright 2024 SAP SE | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You should have received a copy of the License along with this | ||
* program. If not, you may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*******************************************************************************/ | ||
|
||
package nova | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/gophercloud/gophercloud/v2" | ||
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/servers" | ||
"github.com/gophercloud/gophercloud/v2/pagination" | ||
"github.com/sapcc/go-api-declarations/liquid" | ||
) | ||
|
||
type SubresourceAttributes struct { | ||
// base metadata | ||
Status string `json:"status"` | ||
Metadata map[string]string `json:"metadata"` | ||
Tags []string `json:"tags"` | ||
// placement information | ||
AZ liquid.AvailabilityZone `json:"availability_zone"` | ||
// information from flavor | ||
FlavorName string `json:"flavor"` | ||
VCPUs uint64 `json:"vcpu"` | ||
MemoryMiB uint64 `json:"ram"` | ||
DiskGiB uint64 `json:"disk"` | ||
VideoMemoryMiB *uint64 `json:"video_ram,omitempty"` | ||
HWVersion string `json:"-"` // this is only used for sorting the subresource into the right resource | ||
// information from image | ||
OSType string `json:"os_type"` | ||
} | ||
|
||
func (l *Logic) buildInstanceSubresource(ctx context.Context, instance servers.Server) (res liquid.Subresource, err error) { | ||
// copy base attributes | ||
res.ID = instance.ID | ||
res.Name = instance.Name | ||
|
||
attrs := SubresourceAttributes{ | ||
Status: instance.Status, | ||
AZ: liquid.AvailabilityZone(instance.AvailabilityZone), | ||
Metadata: instance.Metadata, | ||
} | ||
if instance.Tags != nil { | ||
attrs.Tags = *instance.Tags | ||
} | ||
|
||
// flavor data is given to us as a map[string]any, but we want something more structured | ||
buf, err := json.Marshal(instance.Flavor) | ||
if err != nil { | ||
return res, fmt.Errorf("could not reserialize flavor data for instance %s: %w", instance.ID, err) | ||
} | ||
var flavorInfo FlavorInfo | ||
err = json.Unmarshal(buf, &flavorInfo) | ||
if err != nil { | ||
return res, fmt.Errorf("could not parse flavor data for instance %s: %w", instance.ID, err) | ||
} | ||
|
||
// copy attributes from flavor data | ||
attrs.FlavorName = flavorInfo.OriginalName | ||
attrs.VCPUs = flavorInfo.VCPUs | ||
attrs.MemoryMiB = flavorInfo.MemoryMiB | ||
attrs.DiskGiB = flavorInfo.DiskGiB | ||
if videoRAMStr, exists := flavorInfo.ExtraSpecs["hw_video:ram_max_mb"]; exists { | ||
videoRAMVal, err := strconv.ParseUint(videoRAMStr, 10, 64) | ||
if err == nil { | ||
attrs.VideoMemoryMiB = &videoRAMVal | ||
} | ||
} | ||
attrs.HWVersion = flavorInfo.ExtraSpecs["quota:hw_version"] | ||
|
||
// calculate classifications based on image data | ||
attrs.OSType = l.OSTypeProber.Get(ctx, instance) | ||
|
||
buf, err = json.Marshal(attrs) | ||
if err != nil { | ||
return res, fmt.Errorf("while serializing Subresource Attributes: %w", err) | ||
} | ||
res.Attributes = json.RawMessage(buf) | ||
return res, nil | ||
} | ||
|
||
func (l *Logic) buildInstanceSubresources(ctx context.Context, projectUUID string) ([]liquid.Subresource, error) { | ||
opts := novaServerListOpts{ | ||
AllTenants: true, | ||
TenantID: projectUUID, | ||
} | ||
|
||
var result []liquid.Subresource | ||
err := servers.List(l.NovaV2, opts).EachPage(ctx, func(ctx context.Context, page pagination.Page) (bool, error) { | ||
var instances []servers.Server | ||
err := servers.ExtractServersInto(page, &instances) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
for _, instance := range instances { | ||
res, err := l.buildInstanceSubresource(ctx, instance) | ||
if err != nil { | ||
return false, err | ||
} | ||
result = append(result, res) | ||
} | ||
return true, nil | ||
}) | ||
return result, err | ||
} | ||
|
||
type novaServerListOpts struct { | ||
AllTenants bool `q:"all_tenants"` | ||
TenantID string `q:"tenant_id"` | ||
} | ||
|
||
func (opts novaServerListOpts) ToServerListQuery() (string, error) { | ||
q, err := gophercloud.BuildQueryString(opts) | ||
return q.String(), err | ||
} |
Oops, something went wrong.