Exemple #1
0
// StatusHistory returns a slice of past statuses for several entities.
func (c *Client) StatusHistory(request params.StatusHistoryRequests) params.StatusHistoryResults {

	results := params.StatusHistoryResults{}
	// TODO(perrito666) the contents of the loop could be split into
	// a oneHistory method for clarity.
	for _, request := range request.Requests {
		filter := status.StatusHistoryFilter{
			Size:  request.Filter.Size,
			Date:  request.Filter.Date,
			Delta: request.Filter.Delta,
		}
		if err := c.checkCanRead(); err != nil {
			history := params.StatusHistoryResult{
				Error: common.ServerError(err),
			}
			results.Results = append(results.Results, history)
			continue

		}

		if err := filter.Validate(); err != nil {
			history := params.StatusHistoryResult{
				Error: common.ServerError(errors.Annotate(err, "cannot validate status history filter")),
			}
			results.Results = append(results.Results, history)
			continue
		}

		var (
			err  error
			hist []params.DetailedStatus
		)
		kind := status.HistoryKind(request.Kind)
		err = errors.NotValidf("%q requires a unit, got %T", kind, request.Tag)
		switch kind {
		case status.KindUnit, status.KindWorkload, status.KindUnitAgent:
			var u names.UnitTag
			if u, err = names.ParseUnitTag(request.Tag); err == nil {
				hist, err = c.unitStatusHistory(u, filter, kind)
			}
		default:
			var m names.MachineTag
			if m, err = names.ParseMachineTag(request.Tag); err == nil {
				hist, err = c.machineStatusHistory(m, filter, kind)
			}
		}

		if err == nil {
			sort.Sort(byTime(hist))
		}

		results.Results = append(results.Results,
			params.StatusHistoryResult{
				History: params.History{Statuses: hist},
				Error:   common.ServerError(errors.Annotatef(err, "fetching status history for %q", request.Tag)),
			})
	}
	return results
}
Exemple #2
0
// StatusHistory returns a slice of past statuses for several entities.
func (c *Client) StatusHistory(args params.StatusHistoryArgs) (params.StatusHistoryResults, error) {
	if args.Size < 1 {
		return params.StatusHistoryResults{}, errors.Errorf("invalid history size: %d", args.Size)
	}
	history := params.StatusHistoryResults{}
	statuses := []params.DetailedStatus{}
	var err error
	switch args.Kind {
	case params.KindUnit, params.KindWorkload, params.KindUnitAgent:
		statuses, err = c.unitStatusHistory(args.Name, args.Size, args.Kind)
		if err != nil {
			return params.StatusHistoryResults{}, errors.Annotatef(err, "fetching unit status history for %q", args.Name)
		}
	case params.KindMachineInstance:
		mIStatuses, err := c.machineInstanceStatusHistory(args.Name, args.Size, params.KindMachineInstance)
		if err != nil {
			return params.StatusHistoryResults{}, errors.Annotate(err, "fetching machine instance status history")
		}
		statuses = mIStatuses
	case params.KindMachine:
		mStatuses, err := c.machineStatusHistory(args.Name, args.Size, params.KindMachine)
		if err != nil {
			return params.StatusHistoryResults{}, errors.Annotate(err, "fetching juju agent status history for machine")
		}
		statuses = mStatuses
	case params.KindContainerInstance:
		cIStatuses, err := c.machineStatusHistory(args.Name, args.Size, params.KindContainerInstance)
		if err != nil {
			return params.StatusHistoryResults{}, errors.Annotate(err, "fetching container status history")
		}
		statuses = cIStatuses
	case params.KindContainer:
		cStatuses, err := c.machineStatusHistory(args.Name, args.Size, params.KindContainer)
		if err != nil {
			return params.StatusHistoryResults{}, errors.Annotate(err, "fetching juju agent status history for container")
		}
		statuses = cStatuses
	}
	history.Statuses = statuses
	sort.Sort(sortableStatuses(history.Statuses))
	return history, nil
}