Skip to content

Commit

Permalink
implement without,simpleoutput,some racks
Browse files Browse the repository at this point in the history
  • Loading branch information
yamatcha committed Feb 10, 2022
1 parent 1393008 commit ee21c7c
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 16 deletions.
44 changes: 33 additions & 11 deletions pkg/sabactl/cmd/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"strings"
"text/tabwriter"
"time"

"github.com/cybozu-go/sabakan/v2"
Expand All @@ -32,6 +33,10 @@ var machinesGetCmd = &cobra.Command{

RunE: func(cmd *cobra.Command, args []string) error {
params := make(map[string]string)
outputFormat, ok := machinesGetParams["output"]
if ok {
delete(machinesGetParams, "output")
}
for k, v := range machinesGetParams {
params[k] = *v
}
Expand All @@ -40,9 +45,17 @@ var machinesGetCmd = &cobra.Command{
if err != nil {
return err
}
e := json.NewEncoder(cmd.OutOrStdout())
e.SetIndent("", " ")
return e.Encode(ms)
if *outputFormat == "simple" {
w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 1, 1, ' ', 0)
for _, m := range ms {
w.Write([]byte(fmt.Sprintf("%v\t%v\t%v\t%v\t%v\t%v\t\n", m.Spec.Serial, m.Spec.Rack, m.Spec.Role, m.Status, m.Spec.IPv4[0], m.Spec.BMC.IPv4)))
}
return w.Flush()
} else {
e := json.NewEncoder(cmd.OutOrStdout())
e.SetIndent("", " ")
return e.Encode(ms)
}
})
well.Stop()
return well.Wait()
Expand Down Expand Up @@ -191,14 +204,23 @@ var machinesSetRetireDateCmd = &cobra.Command{

func init() {
getOpts := map[string]string{
"serial": "Serial name",
"rack": "Rack name",
"role": "Role name",
"labels": "Label name and value (--labels key=val,...)",
"ipv4": "IPv4 address",
"ipv6": "IPv6 address",
"bmc-type": "BMC type",
"state": "State",
"serial": "Serial name",
"rack": "Rack name",
"role": "Role name",
"labels": "Label name and value (--labels key=val,...)",
"ipv4": "IPv4 address",
"ipv6": "IPv6 address",
"bmc-type": "BMC type",
"state": "State",
"without-serial": "without Serial name",
"without-rack": "without Rack name",
"without-role": "without Role name",
"without-labels": "without Label name and value (--labels key=val,...)",
"without-ipv4": "without IPv4 address",
"without-ipv6": "without IPv6 address",
"without-bmc-type": "without BMC type",
"without-state": "without State",
"o": "Output format",
}
for k, v := range getOpts {
val := new(string)
Expand Down
85 changes: 80 additions & 5 deletions query.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package sabakan

import "fmt"
import "net/url"
import "strings"
import (
"fmt"
"net/url"
"strings"
)

// Query is an URL query
type Query map[string]string
Expand Down Expand Up @@ -56,8 +58,18 @@ func (q Query) Match(m *Machine) bool {
}
}
}
if rack := q["rack"]; len(rack) > 0 && rack != fmt.Sprint(m.Spec.Rack) {
return false
if rack := q["rack"]; len(rack) > 0 {
racks := strings.Split(q["rack"], ",")
match := false
for _, rackname := range racks {
if rackname == fmt.Sprint(m.Spec.Rack) {
match = true
break
}
if !match {
return false
}
}
}
if role := q["role"]; len(role) > 0 && role != m.Spec.Role {
return false
Expand All @@ -68,6 +80,69 @@ func (q Query) Match(m *Machine) bool {
if state := q["state"]; len(state) > 0 && state != m.Status.State.String() {
return false
}
if withoutRole := q["without-role"]; len(withoutRole) > 0 && withoutRole == m.Spec.Role {
return false
}
if withoutBmc := q["without-bmc-type"]; len(withoutBmc) > 0 && withoutBmc == m.Spec.BMC.Type {
return false
}
if withoutState := q["without-state"]; len(withoutState) > 0 && withoutState == m.Status.State.String() {
return false
}
if withoutSerial := q["without-serial"]; len(withoutSerial) > 0 && withoutSerial == m.Spec.Serial {
return false
}
if withoutIpv4 := q["without-ipv4"]; len(withoutIpv4) > 0 {
for _, ip := range m.Spec.IPv4 {
if ip == withoutIpv4 {
return false
}
}
}
if withoutIpv6 := q["without-ipv6"]; len(withoutIpv6) > 0 {
for _, ip := range m.Spec.IPv6 {
if ip == withoutIpv6 {
return false
}
}
}
if withoutLabels := q["without-labels"]; len(withoutLabels) > 0 {
// Split into each query
rawQueries := strings.Split(withoutLabels, ",")
for _, rawQuery := range rawQueries {
rawQuery = strings.TrimSpace(rawQuery)
query, err := url.ParseQuery(rawQuery)
if err != nil {
return false
}
for k, v := range query {
if label, exists := m.Spec.Labels[k]; exists {
if v[0] == label {
return false
}
} else {
return false
}
}
}
}
if withoutRack := q["without-rack"]; len(withoutRack) > 0 {
withoutRacks := strings.Split(q["rack"], ",")
for _, rackname := range withoutRacks {
if rackname == fmt.Sprint(m.Spec.Rack) {
return false
}
}
}
if withoutRole := q["without-role"]; len(withoutRole) > 0 && withoutRole == m.Spec.Role {
return false
}
if withoutBmc := q["without-bmc-type"]; len(withoutBmc) > 0 && withoutBmc == m.Spec.BMC.Type {
return false
}
if withoutState := q["without-state"]; len(withoutState) > 0 && withoutState == m.Status.State.String() {
return false
}

return true
}
Expand Down

0 comments on commit ee21c7c

Please sign in to comment.