Skip to content

Commit

Permalink
tests wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kqdeng committed Jun 22, 2020
1 parent 06e7933 commit 813f767
Show file tree
Hide file tree
Showing 3 changed files with 440 additions and 37 deletions.
177 changes: 176 additions & 1 deletion data_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
"allow_workflow": true,
"plan_slug": "t1.small.x86",
"facility_code": "onprem",
"efi_boot": false,
"instance": {
"storage": {
"disks": [
Expand Down Expand Up @@ -95,6 +96,126 @@ const (
}
]
}
`
cacherPartitionSizeInt = `
{
"id": "8978e7d4-1a55-4845-8a66-a5259236b104",
"instance": {
"storage": {
"disks": [
{
"partitions": [
{
"size": 4096,
"label": "BIOS",
"number": 1
}
]
}
]
}
}
}
`
cacherPartitionSizeString = `
{
"id": "8978e7d4-1a55-4845-8a66-a5259236b104",
"instance": {
"storage": {
"disks": [
{
"partitions": [
{
"size": "3333",
"label": "BIOS",
"number": 1
}
]
}
]
}
}
}
`
cacherPartitionSizeWhitespace = `
{
"id": "8978e7d4-1a55-4845-8a66-a5259236b104",
"instance": {
"storage": {
"disks": [
{
"partitions": [
{
"size": " 1234 ",
"label": "BIOS",
"number": 1
}
]
}
]
}
}
}
`
cacherPartitionSizeK = `
{
"id": "8978e7d4-1a55-4845-8a66-a5259236b104",
"instance": {
"storage": {
"disks": [
{
"partitions": [
{
"size": "24K",
"label": "BIOS",
"number": 1
}
]
}
]
}
}
}
`
cacherPartitionSizeKB = `
{
"id": "8978e7d4-1a55-4845-8a66-a5259236b104",
"instance": {
"storage": {
"disks": [
{
"partitions": [
{
"size": "24Kb",
"label": "BIOS",
"number": 1
}
]
}
]
}
}
}
`
cacherPartitionSizeM = `
{
"id": "8978e7d4-1a55-4845-8a66-a5259236b104",
"instance": {
"storage": {
"disks": [
{
"partitions": [
{
"size": "3m",
"label": "BIOS",
"number": 1
}
]
}
]
}
}
}
`
tinkerbellDataModel = `
{
Expand Down Expand Up @@ -139,7 +260,61 @@ const (
"id":"",
"slug":""
},
"instance":{},
"instance":{
"storage": {
"disks": [
{
"device": "/dev/sda",
"wipeTable": true,
"partitions": [
{
"size": 4096,
"label": "BIOS",
"number": 1
},
{
"size": 3993600,
"label": "SWAP",
"number": 2
},
{
"size": 0,
"label": "ROOT",
"number": 3
}
]
}
],
"filesystems": [
{
"mount": {
"point": "/",
"create": {
"options": ["-L", "ROOT"]
},
"device": "/dev/sda3",
"format": "ext4"
}
},
{
"mount": {
"point": "none",
"create": {
"options": ["-L", "SWAP"]
},
"device": "/dev/sda2",
"format": "swap"
}
}
]
},
"crypted_root_password": "$6$qViImWbWFfH/a4pq$s1bpFFXMpQj1eQbHWsruLy6/",
"operating_system_version": {
"distro": "ubuntu",
"version": "16.04",
"os_slug": "ubuntu_16_04"
}
},
"custom":{
"preinstalled_operating_system_version":{},
"private_subnets":[]
Expand Down
128 changes: 122 additions & 6 deletions grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import (
"encoding/json"
"errors"
"io"
"math"
"net"
"os"
"strconv"
"strings"
"unicode"

"github.com/packethost/cacher/protos/cacher"
"github.com/packethost/hegel/grpc/hegel"
Expand All @@ -25,8 +29,10 @@ type exportedHardware interface{}

type exportedHardwareCacher struct {
ID string `json:"id"`
Arch string `json:"arch"`
State string `json:"state"`
Instance interface{} `json:"instance"`
EFIBoot bool `json:"efi_boot"`
Instance instance `json:"instance"`
PreinstalledOperatingSystemVersion interface{} `json:"preinstalled_operating_system_version"`
NetworkPorts []map[string]interface{} `json:"network_ports"`
PlanSlug string `json:"plan_slug"`
Expand All @@ -37,18 +43,129 @@ type exportedHardwareCacher struct {

type exportedHardwareTinkerbell struct {
ID string `json:"id"`
Metadata Metadata `json:"metadata"`
Metadata metadata `json:"metadata"`
}

type Metadata struct {
type metadata struct {
State string `json:"state"`
BondingMode int `json:"bonding_mode"`
Manufacturer interface{} `json:"manufacturer"`
Instance interface{} `json:"instance"`
Instance instance `json:"instance"`
Custom interface{} `json:"custom"`
Facility interface{} `json:"facility"`
}

type instance struct {
ID string `json:"id"`
State string `json:"state"`
Hostname string `json:"hostname"`
AllowPXE bool `json:"allow_pxe"`
Rescue bool `json:"rescue"`

OS operatingSystem `json:"operating_system_version"`
UserData string `json:"userdata,omitempty"`

CryptedRootPassword string `json:"crypted_root_password,omitempty"`

Storage storage `json:"storage,omitempty"`
SSHKeys []string `json:"ssh_keys,omitempty"`
NetworkReady bool `json:"network_ready,omitempty"`
}

type operatingSystem struct {
Slug string `json:"slug"`
Distro string `json:"distro"`
Version string `json:"version"`
ImageTag string `json:"image_tag"`
OsSlug string `json:"os_slug"`
}

type disk struct {
Device string `json:"device"`
WipeTable bool `json:"wipeTable,omitempty"`
Paritions []*partition `json:"partitions,omitempty"`
}

type file struct {
Path string `json:"path"`
Contents string `json:"contents,omitempty"`
Mode int `json:"mode,omitempty"`
UID int `json:"uid,omitempty"`
GID int `json:"gid,omitempty"`
}

type filesystem struct {
Mount struct {
Device string `json:"device"`
Format string `json:"format"`
Files []*file `json:"files,omitempty"`
Create *filesystemOptions `json:"create,omitempty"`
} `json:"mount"`
}

type filesystemOptions struct {
Force bool `json:"force,omitempty"`
Options []string `json:"options,omitempty"`
}

type partition struct {
Label string `json:"label,omitempty"`
Number int `json:"number,omitempty"`
Size intOrString `json:"size,omitempty"`
Start int `json:"start,omitempty"`
TypeGUID string `json:"typeGuid,omitempty"`
}

type RAID struct {
Name string `json:"name"`
Level string `json:"level"`
Devices []string `json:"devices"`
Spares int `json:"spares,omitempty"`
}

type storage struct {
Disks []*disk `json:"disks,omitempty"`
RAID []*RAID `json:"raid,omitempty"`
Filesystems []*filesystem `json:"filesystems,omitempty"`
}

type intOrString int

func (ios *intOrString) UnmarshalJSON(b []byte) error {
if b[0] != '"' {
return json.Unmarshal(b, (*int)(ios))
}
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
i, err := convertSuffix(s)
if err != nil {
return err
}
*ios = intOrString(i)
return nil
}

func convertSuffix(s string) (int, error) {
suffixes := map[string]int{"k": 1, "m": 2, "g": 3, "t": 4}
s = strings.ToLower(s)
i := strings.TrimFunc(s, func(r rune) bool {
return !unicode.IsNumber(r)
})
size, err := strconv.Atoi(i)
if err != nil {
return 0, err
}

suf := strings.TrimFunc(s, func(r rune) bool {
return !unicode.IsLetter(r)
})
suf = strings.ReplaceAll(suf, "b", "")
res := size * int(math.Pow(1024, float64(suffixes[suf])))
return res, nil
}

// exportedHardware transforms hardware that is returned from cacher/tink into what we want to expose to clients
func exportHardware(hw []byte) ([]byte, error) {
var exported exportedHardware
Expand Down Expand Up @@ -87,7 +204,6 @@ func (eh *exportedHardwareCacher) UnmarshalJSON(b []byte) error {
}

func (s *server) Get(ctx context.Context, in *hegel.GetRequest) (*hegel.GetResponse, error) {
// todo add tink
p, ok := peer.FromContext(ctx)
if !ok {
return nil, errors.New("could not get peer info from client")
Expand Down Expand Up @@ -300,7 +416,7 @@ func getByIP(ctx context.Context, s *server, userIP string) ([]byte, error) {
return nil, errors.New("could not find hardware")
}

hw = []byte(resp.(*cacher.Hardware).JSON)
hw = []byte(resp.(cacher.Hardware).JSON)
}

ehw, err := exportHardware(hw)
Expand Down
Loading

0 comments on commit 813f767

Please sign in to comment.