-
Notifications
You must be signed in to change notification settings - Fork 5
/
instance.go
65 lines (55 loc) · 1.95 KB
/
instance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package jupiterbrain
import (
"context"
"time"
"github.com/honeycombio/beeline-go"
"github.com/lib/pq"
"github.com/vmware/govmomi/vim25/types"
)
// Instance is our representation of an instance woop woop
type Instance struct {
ID string `db:"id"`
IPAddresses []string
State string
CreatedAt time.Time `db:"created_at"`
DestroyedAt pq.NullTime `db:"destroyed_at"`
}
// InstanceConfig specifies how a new instance should be configured
type InstanceConfig struct {
// Type is the type of data in the payload and is required to be "instances".
// The endpoint for creating new instances will check this value for correctness.
Type string `json:"type"`
// BaseImage is the VM name of the image the new instance will be cloned from.
BaseImage string `json:"base-image"`
// CPUCount is the number of CPUs the new instance should have.
// If CPUCount is 0, the number of CPUs will not be changed from what is configured
// in the base image.
CPUCount int `json:"cpus"`
// RAM is the amount of RAM in MB that the new instance should have.
// If RAM is 0, the amount of RAM will not be changed from what is configured in the
// base image.
RAM int `json:"ram"`
}
// ConfigSpec constructs a spec for reconfiguring the VM after it is cloned.
// ConfigSpec returns nil if none of the config parameters for the VM are set.
// In that case, no reconfigure task for the VM should be run.
func (c InstanceConfig) ConfigSpec() *types.VirtualMachineConfigSpec {
if c.CPUCount == 0 && c.RAM == 0 {
return nil
}
return &types.VirtualMachineConfigSpec{
NumCPUs: int32(c.CPUCount),
MemoryMB: int64(c.RAM),
}
}
// Track adds fields to the current Honeycomb event with information about the VM
// config that was requested.
func (c InstanceConfig) Track(ctx context.Context) {
beeline.AddField(ctx, "image_name", c.BaseImage)
if c.CPUCount != 0 {
beeline.AddField(ctx, "vm_cpus", c.CPUCount)
}
if c.RAM != 0 {
beeline.AddField(ctx, "vm_ram", c.RAM)
}
}