Skip to content

Commit

Permalink
Hook-up cost tracking via vmtemplate service
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-sva committed Jan 10, 2025
1 parent 97308e2 commit 8db7f04
Show file tree
Hide file tree
Showing 13 changed files with 478 additions and 194 deletions.
28 changes: 28 additions & 0 deletions v3/pkg/util/timeunit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package util

import (
"fmt"
"strings"
)

type TimeUnit = string

const (
TimeUnitSeconds TimeUnit = "seconds"
TimeUnitMinutes TimeUnit = "minutes"
TimeUnitHours TimeUnit = "hours"
)

func ParseTimeUnit(s string) (TimeUnit, error) {
lower := strings.ToLower(s)
switch lower {
case "seconds", "second", "sec", "s":
return TimeUnitSeconds, nil
case "minutes", "minute", "min", "m":
return TimeUnitMinutes, nil
case "hours", "hour", "h":
return TimeUnitHours, nil
default:
return TimeUnitSeconds, fmt.Errorf("%s is not a valid time unit", s)
}
}
54 changes: 54 additions & 0 deletions v3/pkg/util/timeunit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package util

import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)

func TestParseTimeUnit(t *testing.T) {
tests := []struct {
name string
input []string
want TimeUnit
wantErr bool
}{
{
name: "ok second",
input: []string{"seconds", "second", "sec", "s", "SECONDS", "SECOND", "SEC", "S"},
want: TimeUnitSeconds,
wantErr: false,
},
{
name: "ok minute",
input: []string{"minutes", "minute", "min", "m", "MINUTES", "MINUTE", "MIN", "M"},
want: TimeUnitMinutes,
wantErr: false,
},
{
name: "ok hour",
input: []string{"hours", "hour", "h", "HOURS", "HOUR", "H"},
want: TimeUnitHours,
wantErr: false,
},
{
name: "nok",
input: []string{"", " ", "idk"},
wantErr: true,
},
}
for _, tt := range tests {
for _, input := range tt.input {
t.Run(fmt.Sprintf("%s %s", tt.name, input), func(t *testing.T) {
actual, err := ParseTimeUnit(input)
if tt.wantErr {
require.Errorf(t, err, "error expected ParseTimeUnit(%v)", input)
} else {
require.NoErrorf(t, err, "no error expected ParseTimeUnit(%v)", input)
assert.Equalf(t, tt.want, actual, "ParseTimeUnit(%v)", input)
}
})
}
}
}
167 changes: 121 additions & 46 deletions v3/protos/vmtemplate/vmtemplate.pb.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions v3/protos/vmtemplate/vmtemplate.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,25 @@ message VMTemplate {
string name = 3;
string image = 4;
map<string, string> config_map = 5;
optional string cost_base_price = 6;
optional string cost_time_unit = 7;
}

message CreateVMTemplateRequest {
string name = 1;
string image = 2;
string config_map_raw = 3;
optional string cost_base_price = 4;
optional string cost_time_unit = 5;
}

message UpdateVMTemplateRequest {
string id = 1;
string name = 2;
string image = 3;
string config_map_raw = 4;
optional string cost_base_price = 5;
optional string cost_time_unit = 6;
}

message ListVMTemplatesResponse {
Expand Down
4 changes: 2 additions & 2 deletions v3/services/costsvc/internal/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type costGroup struct {
Kind string
CostGroup string
BasePrice uint64
TimeUnit TimeUnit
TimeUnit util.TimeUnit
CreationTimestamp int64
DeletionTimestamp *int64
}
Expand Down Expand Up @@ -50,7 +50,7 @@ func newCostGroup(obj interface{}) (*costGroup, error) {
if !found {
return nil, fmt.Errorf("%s label not found", labels.CostTimeUnit)
}
timeUnit, err := ParseTimeUnit(timeUnitLabel)
timeUnit, err := util.ParseTimeUnit(timeUnitLabel)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 8db7f04

Please sign in to comment.