func (helpersSuite) TestAPI2CharmResource(c *gc.C) { res, err := api.API2CharmResource(api.CharmResource{ Name: "spam", Type: "file", Path: "spam.tgz", Description: "you need it", Origin: "upload", Revision: 1, Fingerprint: []byte(fingerprint), Size: 10, }) c.Assert(err, jc.ErrorIsNil) fp, err := charmresource.NewFingerprint([]byte(fingerprint)) c.Assert(err, jc.ErrorIsNil) expected := charmresource.Resource{ Meta: charmresource.Meta{ Name: "spam", Type: charmresource.TypeFile, Path: "spam.tgz", Description: "you need it", }, Origin: charmresource.OriginUpload, Revision: 1, Fingerprint: fp, Size: 10, } err = expected.Validate() c.Assert(err, jc.ErrorIsNil) c.Check(res, jc.DeepEquals, expected) }
func (f Facade) addPendingResource(serviceID string, apiRes api.CharmResource) (pendingID string, err error) { chRes, err := api.API2CharmResource(apiRes) if err != nil { return "", errors.Annotatef(err, "bad resource info for %q", chRes.Name) } userID := "" var reader io.Reader pendingID, err = f.store.AddPendingResource(serviceID, userID, chRes, reader) if err != nil { return "", errors.Annotatef(err, "while adding pending resource info for %q", chRes.Name) } return pendingID, nil }
func (f Facade) addPendingResources(applicationID, chRef string, channel csparams.Channel, csMac *macaroon.Macaroon, apiResources []api.CharmResource) ([]string, error) { var resources []charmresource.Resource for _, apiRes := range apiResources { res, err := api.API2CharmResource(apiRes) if err != nil { return nil, errors.Annotatef(err, "bad resource info for %q", apiRes.Name) } resources = append(resources, res) } if chRef != "" { cURL, err := charm.ParseURL(chRef) if err != nil { return nil, err } switch cURL.Schema { case "cs": id := charmstore.CharmID{ URL: cURL, Channel: channel, } resources, err = f.resolveCharmstoreResources(id, csMac, resources) if err != nil { return nil, errors.Trace(err) } case "local": resources, err = f.resolveLocalResources(resources) if err != nil { return nil, errors.Trace(err) } default: return nil, errors.Errorf("unrecognized charm schema %q", cURL.Schema) } } var ids []string for _, res := range resources { pendingID, err := f.addPendingResource(applicationID, res) if err != nil { // We don't bother aggregating errors since a partial // completion is disruptive and a retry of this endpoint // is not expensive. return nil, err } ids = append(ids, pendingID) } return ids, nil }