Skip to content

Commit

Permalink
add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
morozovalekseywot authored and ftersin committed May 13, 2024
1 parent 19fd917 commit 15d2ea9
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 18 deletions.
6 changes: 2 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ description: |-
# VKCS Provider's changelog

#### v0.7.3 (unreleased)
- Add a warning when creating a block_device with incorrect use of the delete_on_termination parameter
- Add `cpu_generation` param to terraform schema of `vkcs_compute_flavor`
- improve search flavor using min_ram and min_disk
- Improve error messages for `vkcs_compute_flavor`
- Add cpu_generation param to terraform schema of vkcs_compute_flavor
- Fix searching the closest appropriate flavor by min_ram and min_disk argument


#### v0.7.2
Expand Down
16 changes: 16 additions & 0 deletions examples/compute/flavor/cpu_generation/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# if we set only vcpus = 2 and ram = 2048 we find: STD2-2-2 and STD3-2-2.
# Which differs by process generations.

# If we want a STD2-2-2
data "vkcs_compute_flavor" "basic_cascadelake" {
vcpus = 2
ram = 2048
cpu_generation = "cascadelake-v1"
}

# If we want a STD3-2-2
data "vkcs_compute_flavor" "basic_icelake" {
vcpus = 2
ram = 2048
cpu_generation = "icelake-v1"
}
15 changes: 15 additions & 0 deletions examples/compute/flavor/min_disk/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# If the exact amount of disk is not so important to you, then you can specify the minimum value that will satisfy you
# and flavor with minimum of disk will be automatically selected for you.
data "vkcs_compute_flavor" "basic1" {
vcpus = 4
ram = 8192
min_disk = 30
}

# But if you also do not specify the exact amount of RAM, then flavor will first be selected based on the minimum RAM
# and if RAM is equal in terms of disk space.
data "vkcs_compute_flavor" "basic2" {
vcpus = 4
min_ram = 4096
min_disk = 20
}
6 changes: 6 additions & 0 deletions examples/compute/flavor/min_ram/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# If the exact amount of RAM is not so important to you, then you can specify the minimum value that will satisfy you
# and flavor with minimum of ram will be automatically selected for you.
data "vkcs_compute_flavor" "basic" {
vcpus = 4
min_ram = 4096
}
10 changes: 5 additions & 5 deletions vkcs/compute/data_source_flavor.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func DataSourceComputeFlavor() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The `cpu_generation` of the flavor.",
Description: "The `cpu_generation` of the flavor. __note__ ask nova owners https://cloud.vk.com/home/node/app/docs/base/iaas/concepts/vm-concept#cpu_generations_a045e625",
},

// Computed values
Expand Down Expand Up @@ -263,7 +263,7 @@ func dataSourceComputeFlavorRead(ctx context.Context, d *schema.ResourceData, me

// Loop through all flavors to find a more specific one.
if len(allFlavors) > 0 {
var filteredFlavors []iflavors.FlavorWithExtraSpecs
var filteredFlavors []iflavors.FlavorWithExtraFields
for _, flavor := range allFlavors {
switch {
case requiredFlavor.HasName && flavor.Name != requiredFlavor.Name:
Expand All @@ -278,15 +278,15 @@ func dataSourceComputeFlavorRead(ctx context.Context, d *schema.ResourceData, me
continue
case requiredFlavor.HasRxTxFactor && flavor.RxTxFactor != requiredFlavor.RxTxFactor:
continue
case requiredFlavor.HasCPUGeneration && flavor.FlavorMissingFields.ExtraSpecs == nil:
case requiredFlavor.HasCPUGeneration && flavor.FlavorExtraFields.ExtraSpecs == nil:
continue
}
if !requiredFlavor.HasCPUGeneration {
filteredFlavors = append(filteredFlavors, flavor)
continue
}

if flavorCPU, ok := flavor.FlavorMissingFields.ExtraSpecs["mcs:cpu_generation"]; ok {
if flavorCPU, ok := flavor.FlavorExtraFields.ExtraSpecs["mcs:cpu_generation"]; ok {
if requiredFlavor.CPUGeneration == flavorCPU {
filteredFlavors = append(filteredFlavors, flavor)
}
Expand All @@ -304,7 +304,7 @@ func dataSourceComputeFlavorRead(ctx context.Context, d *schema.ResourceData, me
// if we find many flavors and the user sets the min_ram or min_disk values
// we give him the flavor with the minimum amount of RAM from the found flavors
if len(allFlavors) > 1 && (requiredFlavor.HasMinRAM || requiredFlavor.HasMinDisk) {
minFlavor := slices.MinFunc(allFlavors, func(a, b iflavors.FlavorWithExtraSpecs) int {
minFlavor := slices.MinFunc(allFlavors, func(a, b iflavors.FlavorWithExtraFields) int {
if a.RAM != b.RAM {
return cmp.Compare(a.RAM, b.RAM)
}
Expand Down
19 changes: 10 additions & 9 deletions vkcs/internal/services/compute/v2/flavors/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,43 @@ package flavors

import (
"encoding/json"

"github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
"github.com/gophercloud/gophercloud/pagination"
)

type FlavorMissingFields struct {
type FlavorExtraFields struct {
ExtraSpecs map[string]interface{} `json:"extra_specs"`
}

// FlavorWithExtraSpecs needs for extract ExtraSpecs from flavors.FlavorPage
type FlavorWithExtraSpecs struct {
// FlavorWithExtraFields needs for extract FlavorExtraFields from flavors.FlavorPage
type FlavorWithExtraFields struct {
flavors.Flavor
FlavorMissingFields
FlavorExtraFields
}

func (f *FlavorWithExtraSpecs) UnmarshalJSON(data []byte) error {
func (f *FlavorWithExtraFields) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &f.Flavor); err != nil {
return err
}

if err := json.Unmarshal(data, &f.FlavorMissingFields); err != nil {
if err := json.Unmarshal(data, &f.FlavorExtraFields); err != nil {
return err
}

return nil
}

func ExtractFlavorWithExtraSpecs(r pagination.Page) ([]FlavorWithExtraSpecs, error) {
func ExtractFlavorWithExtraSpecs(r pagination.Page) ([]FlavorWithExtraFields, error) {
var s struct {
Flavors []FlavorWithExtraSpecs `json:"flavors"`
Flavors []FlavorWithExtraFields `json:"flavors"`
}
err := (r.(flavors.FlavorPage)).ExtractInto(&s)

return s.Flavors, err
}

func (f *FlavorWithExtraSpecs) ToFlavor() *flavors.Flavor {
func (f *FlavorWithExtraFields) ToFlavor() *flavors.Flavor {
return &flavors.Flavor{
ID: f.ID,
Disk: f.Disk,
Expand Down

0 comments on commit 15d2ea9

Please sign in to comment.