github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/resource/context/internal/resource.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package internal
     5  
     6  // TODO(ericsnow) Move this file elsewhere?
     7  //  (e.g. top-level resource pkg, charm/resource)
     8  
     9  import (
    10  	"io"
    11  
    12  	"github.com/juju/errors"
    13  
    14  	"github.com/juju/juju/resource"
    15  )
    16  
    17  // OpenedResourceClient exposes the API functionality needed by OpenResource.
    18  type OpenedResourceClient interface {
    19  	// GetResource returns the resource info and content for the given
    20  	// name (and unit-implied application).
    21  	GetResource(resourceName string) (resource.Resource, io.ReadCloser, error)
    22  }
    23  
    24  // OpenedResource wraps the resource info and reader returned
    25  // from the API.
    26  type OpenedResource struct {
    27  	resource.Resource
    28  	io.ReadCloser
    29  }
    30  
    31  // OpenResource opens the identified resource using the provided client.
    32  func OpenResource(name string, client OpenedResourceClient) (*OpenedResource, error) {
    33  	info, reader, err := client.GetResource(name)
    34  	if err != nil {
    35  		return nil, errors.Trace(err)
    36  	}
    37  	or := &OpenedResource{
    38  		Resource:   info,
    39  		ReadCloser: reader,
    40  	}
    41  	return or, nil
    42  }
    43  
    44  // Content returns the "content" for the opened resource.
    45  func (or OpenedResource) Content() Content {
    46  	return Content{
    47  		Data:        or.ReadCloser,
    48  		Size:        or.Size,
    49  		Fingerprint: or.Fingerprint,
    50  	}
    51  }
    52  
    53  // Info returns the info for the opened resource.
    54  func (or OpenedResource) Info() resource.Resource {
    55  	return or.Resource
    56  }