// UploadResource uploads a resource (an archive) into the store,
// tags it to a charm/bundle represented by id for other clients
// to download when they download the charm/bundle.
//
// In this implementation, the progress parameter is ignored.
339327
func (c ChannelAwareFakeClient) UploadResource(id *charm.URL, name, path string, file io.ReaderAt, size int64, progress csclient.Progress) (revision int, err error) {
	return c.charmstore.UploadResource(id, name, path, file, size, progress)
}
1051502
func (p Protos) CreateResource(rsc resource.Resource) (*resource.Resource, error) {
	rscJSON, err := json.Marshal(rsc)
	if err != nil {
		return nil, err
	}

	url := p.createURL("resource")
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(rscJSON))
	if err != nil {
		return nil, err
	}
	req.Header.Set("Content-Type", "application/json")

	res, err := p.makeRequest(req)
	if err != nil {
		return nil, err
	}

	err = json.Unmarshal(res, &rsc)
	if err != nil {
		return nil, err
	}
	return &rsc, nil
}
214423
func (s *RelationalDatabaseEvent) SetResource(v string) *RelationalDatabaseEvent {
	s.Resource = &v
	return s
}
1196733
func NewResource(rl v1.ResourceList) *Resource {
	r := &Resource{}
	r.Add(rl)
	return r
}
658871
func (c *Client) Upload(archive io.ReadSeeker, meta params.BackupsMetadataResult) (string, error) {
	// Empty out some of the metadata.
	meta.ID = ""
	meta.Stored = time.Time{}

	req, err := http.NewRequest("PUT", "/backups", nil)
	if err != nil {
		return "", errors.Trace(err)
	}
	body, contentType, err := httpattachment.NewBody(archive, meta, "juju-backup.tar.gz")
	if err != nil {
		return "", errors.Annotatef(err, "cannot create multipart body")
	}
	req.Header.Set("Content-Type", contentType)
	var result params.BackupsUploadResult
	if err := c.client.Do(req, body, &result); err != nil {
		return "", errors.Trace(err)
	}
	return result.ID, nil
}
312803
func (rc *ResourceController) LoadResource(c *gin.Context) (interface{}, error) {
	result, err := rc.DAL.Get(c.Param("id"), rc.Name)
	if err != nil {
		return nil, err
	}

	c.Set(rc.Name, result)
	c.Set("Resource", rc.Name)
	return result, nil
}
379639
func (c *Client) Upload(ctx context.Context, f io.Reader, u *url.URL, param *Upload) error {
	var err error

	if param.Progress != nil {
		pr := progress.NewReader(ctx, param.Progress, f, param.ContentLength)
		f = pr

		// Mark progress reader as done when returning from this function.
		defer func() {
			pr.Done(err)
		}()
	}

	req, err := http.NewRequest(param.Method, u.String(), f)
	if err != nil {
		return err
	}

	req = req.WithContext(ctx)

	req.ContentLength = param.ContentLength
	req.Header.Set("Content-Type", param.Type)

	for k, v := range param.Headers {
		req.Header.Add(k, v)
	}

	if param.Ticket != nil {
		req.AddCookie(param.Ticket)
	}

	res, err := c.Client.Do(req)
	if err != nil {
		return err
	}

	defer res.Body.Close()

	switch res.StatusCode {
	case http.StatusOK:
	case http.StatusCreated:
	default:
		err = errors.New(res.Status)
	}

	return err
}
1057866
func (p ResourcePersistence) SetResource(res resource.Resource) error {
	stored, err := p.getStored(res)
	if errors.IsNotFound(err) {
		stored = storedResource{Resource: res}
	} else if err != nil {
		return errors.Trace(err)
	}
	// TODO(ericsnow) Ensure that stored.Resource matches res? If we do
	// so then the following line is unnecessary.
	stored.Resource = res

	if err := res.Validate(); err != nil {
		return errors.Annotate(err, "bad resource")
	}

	buildTxn := func(attempt int) ([]txn.Op, error) {
		// This is an "upsert".
		var ops []txn.Op
		switch attempt {
		case 0:
			ops = newInsertResourceOps(stored)
		case 1:
			ops = newUpdateResourceOps(stored)
		default:
			// Either insert or update will work so we should not get here.
			return nil, errors.New("setting the resource failed")
		}
		if stored.PendingID == "" {
			// Only non-pending resources must have an existing application.
			ops = append(ops, p.base.ApplicationExistsOps(res.ApplicationID)...)
		}
		return ops, nil
	}
	if err := p.base.Run(buildTxn); err != nil {
		return errors.Trace(err)
	}
	return nil
}
844543
func UploadResources(resources []interface{}, baseURL string) (map[string]string, error) {
	refMap := make(map[string]string)
	resources = sortResourcesByDependency(resources)
	for _, t := range resources {
		err := updateReferences(t, refMap)
		if err != nil {
			return refMap, err
		}

		// Remember the old ID so we can create a map from old ID to new location
		oldId := getId(t)

		// Upload the resource and get the new location
		_, err = UploadResource(t, baseURL)
		if err != nil {
			return refMap, err
		}

		// Add entry to map from the old ID to the new relative path
		if oldId != "" {
			refMap[oldId] = reflect.TypeOf(t).Elem().Name() + "/" + getId(t)
		}
	}

	return refMap, nil
}
120323
func NewResource(init ...*Resource) *Resource {
	var o *Resource
	if len(init) == 1 {
		o = init[0]
	} else {
		o = new(Resource)
	}
	return o.Init()
}
1247787
func (w *uploadWrapper) UploadResource(res resource.Resource, content io.ReadSeeker) error {
	return w.client.UploadResource(w.modelUUID, res, content)
}
526674
func (h *ResourcesHandler) readResource(backend ResourcesBackend, req *http.Request) (*uploadedResource, error) {
	uReq, err := extractUploadRequest(req)
	if err != nil {
		return nil, errors.Trace(err)
	}
	var res resource.Resource
	if uReq.PendingID != "" {
		res, err = backend.GetPendingResource(uReq.Application, uReq.Name, uReq.PendingID)
	} else {
		res, err = backend.GetResource(uReq.Application, uReq.Name)
	}
	if err != nil {
		return nil, errors.Trace(err)
	}

	switch res.Type {
	case charmresource.TypeFile:
		ext := path.Ext(res.Path)
		if path.Ext(uReq.Filename) != ext {
			return nil, errors.Errorf("incorrect extension on resource upload %q, expected %q", uReq.Filename, ext)
		}
	case charmresource.TypeContainerImage:
		// At this stage the docker 'filename' could be a reference to
		// what the user has on their local filesystem that got parsed and
		// is in the process of being uploaded so it makes no sense to validate uReq.Filename
		// The uploaded finger print from the CLI will not match what comes in from the API request, do not store it.
		uReq.Fingerprint = charmresource.Fingerprint{}
	}

	chRes, err := updateResource(res.Resource, uReq.Fingerprint, uReq.Size)
	if err != nil {
		return nil, errors.Trace(err)
	}
	return &uploadedResource{
		Application: uReq.Application,
		PendingID:   uReq.PendingID,
		Resource:    chRes,
		Data:        req.Body,
	}, nil
}
797469
func (u *ConcurrentUploader) UploadFile(item *Item, ps *isolatedclient.PushState, done func()) {
	u.wg.Add(1)
	go u.upload(item.RelPath, fileSource(item.Path), ps, done)
}
874294
func (s *TaskSucceededEventDetails) SetResource(v string) *TaskSucceededEventDetails {
	s.Resource = &v
	return s
}
1291119
func (t *Template) AddResource(name string, resource ResourceProperties) *Resource {
	templateResource := &Resource{Properties: resource}
	t.Resources[name] = templateResource
	return templateResource
}
331976
func (c *Compiler) AddResource(url string, r io.Reader) error {
	res, err := newResource(url, r)
	if err != nil {
		return err
	}
	c.resources[res.url] = res
	return nil
}
441602
func (v *StorageVol) Upload(stream *Stream, offset, length uint64, flags StorageVolUploadFlags) error {
	var err C.virError
	if C.virStorageVolUploadWrapper(v.ptr, stream.ptr, C.ulonglong(offset),
		C.ulonglong(length), C.uint(flags), &err) == -1 {
		return makeError(&err)
	}
	return nil
}