func getUnitStatePage(cAPI client.API, machineID, unitName string, tok PageToken) (*schema.UnitStatePage, error) { states, err := cAPI.UnitStates() if err != nil { return nil, err } var filtered []*schema.UnitState for _, us := range states { if machineID != "" && machineID != us.MachineID { continue } if unitName != "" && unitName != us.Name { continue } filtered = append(filtered, us) } items, next := extractUnitStatePageData(filtered, tok) page := schema.UnitStatePage{ States: items, } if next != nil { page.NextPageToken = next.Encode() } return &page, nil }
func getMachinePage(cAPI client.API, tok PageToken) (*schema.MachinePage, error) { all, err := cAPI.Machines() if err != nil { return nil, err } page := extractMachinePage(all, tok) return page, nil }
func getMachines(client fleetClient.API, outMachines map[string]string) error { machines, err := client.Machines() if err != nil { return err } for _, machine := range machines { outMachines[machine.ID] = machine.PublicIP } return nil }
func fleetUnitHealthHandler(fleetAPIClient client.API, checker fleetUnitHealthChecker) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { checks := []fthealth.Check{} unitStates, err := fleetAPIClient.UnitStates() if err != nil { panic(err) } for _, unitState := range unitStates { checks = append(checks, newFleetUnitHealthCheck(*unitState, checker)) } fthealth.HandlerParallel("Coco Fleet Unit Healthcheck", "Checks the health of all fleet units", checks...)(w, r) } }
func waitForSystemdState(client client.API, unit string, allowedStates []string) error { seenDesiredState := 0 for { states, err := client.UnitStates() if err != nil { return maskAny(err) } found := false seenDesired := false for _, state := range states { if state.Name == unit { found = true for _, allowedState := range allowedStates { if allowedState == state.SystemdActiveState { seenDesired = true } } } } if found && !seenDesired { seenDesiredState = 0 } else { seenDesiredState++ } if seenDesiredState > 5 { return nil } time.Sleep(500 * time.Millisecond) } return nil }
func getUnitPage(cAPI client.API, tok PageToken) (*schema.UnitPage, error) { units, err := cAPI.Units() if err != nil { return nil, err } items, next := extractUnitPageData(units, tok) page := schema.UnitPage{ Units: items, } if next != nil { page.NextPageToken = next.Encode() } return &page, nil }
func TestNullAPI(test *testing.T) { var api client.API = new(nullAPI) // all the methods return errors, all the time. if _, err := api.Machines(); err == nil { test.Error("Machines() did not return an error") } if _, err := api.Unit("foo"); err == nil { test.Error("Unit() did not return an error") } if _, err := api.Units(); err == nil { test.Error("Units() did not return an error") } if _, err := api.UnitStates(); err == nil { test.Error("UnitStates() did not return an error") } if err := api.SetUnitTargetState("foo", "bar"); err == nil { test.Error("SetUnitTargetState() did not return an error") } if err := api.CreateUnit(nil); err == nil { test.Error("CreateUnit() did not return an error") } if err := api.DestroyUnit("foo"); err == nil { test.Error("DestroyUnit() did not return an error") } }