// API2Result converts the API result to a payload.Result. func API2Result(r PayloadResult) (payload.Result, error) { result := payload.Result{ NotFound: r.NotFound, } id, err := API2ID(r.Tag) if err != nil { return result, errors.Trace(err) } result.ID = id if r.Payload != nil { pl, err := api.API2Payload(*r.Payload) if err != nil { return result, errors.Trace(err) } result.Payload = &pl } if r.Error != nil { result.Error = common.RestoreError(r.Error) } return result, nil }
// List builds the list of payload information for the provided payload // IDs. If none are provided then the list contains the info for all // payloads associated with the unit. Missing payloads // are ignored. func (uw UnitPayloads) List(ids ...string) ([]payload.Result, error) { logger.Tracef("listing %v", ids) var err error var payloads []payload.Payload missingIDs := make(map[string]bool) if len(ids) == 0 { payloads, err = uw.Persist.ListAll() if err != nil { return nil, errors.Trace(err) } for _ = range payloads { ids = append(ids, "") } } else { var missing []string payloads, missing, err = uw.Persist.List(ids...) if err != nil { return nil, errors.Trace(err) } for _, id := range missing { missingIDs[id] = true } } var results []payload.Result i := 0 for _, id := range ids { if missingIDs[id] { results = append(results, payload.Result{ ID: id, NotFound: true, Error: errors.NotFoundf(id), }) continue } pl := payloads[i] i += 1 // TODO(ericsnow) Ensure that pl.Unit == uw.Unit? result := payload.Result{ ID: id, Payload: &payload.FullPayloadInfo{ Payload: pl, Machine: uw.Machine, }, } if id == "" { // TODO(ericsnow) Do this more efficiently. id, err := uw.LookUp(pl.Name, pl.ID) if err != nil { id = "" result.Error = errors.Trace(err) } result.ID = id } results = append(results, result) } return results, nil }