Skip to content
This repository has been archived by the owner on Jul 24, 2021. It is now read-only.

import rack slot assignments from a json blob #11

Merged
merged 2 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 47 additions & 3 deletions racks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

package main

// TODO: rack assignments: CRUD

//lint:file-ignore U1000 WIP

import (
Expand Down Expand Up @@ -546,7 +544,7 @@ func (r RackAssignments) Less(i, j int) bool {
}

type RackAssignment struct {
DeviceID uuid.UUID `json:"device_id,omitempty"`
DeviceID uuid.UUID `json:"device_id"`
DeviceAssetTag string `json:"device_asset_tag,omitempty"`
HardwareProductName string `json:"hardware_product_name,omitempty"`
RackUnitStart int `json:"rack_unit_start"`
Expand Down Expand Up @@ -605,6 +603,32 @@ func (r *Racks) Assignments(id uuid.UUID) RackAssignments {
return assignments
}

func (r *Racks) ImportAssignments(id uuid.UUID, b []byte) RackAssignments {
type Assignment struct {
DeviceID uuid.UUID `json:"device_id"`
RackUnitStart int `json:"rack_unit_start"`
DeviceAssetTag string `json:"device_asset_tag,omitempty"`
}

imported := make([]Assignment, 0)
if err := json.Unmarshal(b, &imported); err != nil {
panic(err)
}

uri := fmt.Sprintf(
"/rack/%s/assignment",
url.PathEscape(id.String()),
)

r.Do(
r.Sling().New().Post(uri).
Set("Content-Type", "application/json").
BodyJSON(imported),
)

return r.Assignments(id)
}

/****/

func init() {
Expand Down Expand Up @@ -820,6 +844,26 @@ func init() {
})
})

cmd.Command("assign", "Assign devices to rack slots, using the `--json` output from 'assignments'", func(cmd *cli.Cmd) {
filePathArg := cmd.StringArg("FILE", "-", "Path to a JSON file to use as the data source. '-' indicates STDIN")
cmd.Action = func() {

var b []byte
var err error
if *filePathArg == "-" {
b, err = ioutil.ReadAll(os.Stdin)
} else {
b, err = ioutil.ReadFile(*filePathArg)
}
if err != nil {
panic(err)
}

fmt.Println(API.Racks().ImportAssignments(rackID, b))

}
})

cmd.Command("assignments", "The devices assigned to the rack", func(cmd *cli.Cmd) {
cmd.Action = func() {
fmt.Println(API.Racks().Assignments(rackID))
Expand Down