github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/resource/api/data.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package api
     5  
     6  // TODO(ericsnow) Eliminate the dependence on apiserver if possible.
     7  
     8  import (
     9  	"strings"
    10  	"time"
    11  
    12  	"github.com/juju/errors"
    13  	"github.com/juju/names"
    14  	charmresource "gopkg.in/juju/charm.v6-unstable/resource"
    15  	"gopkg.in/macaroon.v1"
    16  
    17  	"github.com/juju/juju/apiserver/params"
    18  	"github.com/juju/juju/charmstore"
    19  )
    20  
    21  // ListResourcesArgs are the arguments for the ListResources endpoint.
    22  type ListResourcesArgs params.Entities
    23  
    24  // NewListResourcesArgs returns the arguments for the ListResources endpoint.
    25  func NewListResourcesArgs(services []string) (ListResourcesArgs, error) {
    26  	var args ListResourcesArgs
    27  	var errs []error
    28  	for _, service := range services {
    29  		if !names.IsValidService(service) {
    30  			err := errors.Errorf("invalid service %q", service)
    31  			errs = append(errs, err)
    32  			continue
    33  		}
    34  		args.Entities = append(args.Entities, params.Entity{
    35  			Tag: names.NewServiceTag(service).String(),
    36  		})
    37  	}
    38  	if err := resolveErrors(errs); err != nil {
    39  		return args, errors.Trace(err)
    40  	}
    41  	return args, nil
    42  }
    43  
    44  // AddPendingResourcesArgs holds the arguments to the AddPendingResources
    45  // API endpoint.
    46  type AddPendingResourcesArgs struct {
    47  	params.Entity
    48  	params.AddCharmWithAuthorization
    49  
    50  	// Resources is the list of resources to add as pending.
    51  	Resources []CharmResource
    52  }
    53  
    54  // NewAddPendingResourcesArgs returns the arguments for the
    55  // AddPendingResources API endpoint.
    56  func NewAddPendingResourcesArgs(serviceID string, chID charmstore.CharmID, csMac *macaroon.Macaroon, resources []charmresource.Resource) (AddPendingResourcesArgs, error) {
    57  	var args AddPendingResourcesArgs
    58  
    59  	if !names.IsValidService(serviceID) {
    60  		return args, errors.Errorf("invalid service %q", serviceID)
    61  	}
    62  	tag := names.NewServiceTag(serviceID).String()
    63  
    64  	var apiResources []CharmResource
    65  	for _, res := range resources {
    66  		if err := res.Validate(); err != nil {
    67  			return args, errors.Trace(err)
    68  		}
    69  		apiRes := CharmResource2API(res)
    70  		apiResources = append(apiResources, apiRes)
    71  	}
    72  	args.Tag = tag
    73  	args.Resources = apiResources
    74  	if chID.URL != nil {
    75  		args.URL = chID.URL.String()
    76  		args.Channel = string(chID.Channel)
    77  		args.CharmStoreMacaroon = csMac
    78  	}
    79  	return args, nil
    80  }
    81  
    82  // AddPendingResourcesResult holds the result of the AddPendingResources
    83  // API endpoint.
    84  type AddPendingResourcesResult struct {
    85  	params.ErrorResult
    86  
    87  	// PendingIDs holds the "pending ID" for each of the requested
    88  	// resources.
    89  	PendingIDs []string
    90  }
    91  
    92  // ResourcesResults holds the resources that result
    93  // from a bulk API call.
    94  type ResourcesResults struct {
    95  	// Results is the list of resource results.
    96  	Results []ResourcesResult
    97  }
    98  
    99  // ResourcesResult holds the resources that result from an API call
   100  // for a single service.
   101  type ResourcesResult struct {
   102  	params.ErrorResult
   103  
   104  	// Resources is the list of resources for the service.
   105  	Resources []Resource
   106  
   107  	// CharmStoreResources is the list of resources associated with the charm in
   108  	// the charmstore.
   109  	CharmStoreResources []CharmResource
   110  
   111  	// UnitResources contains a list of the resources for each unit in the
   112  	// service.
   113  	UnitResources []UnitResources
   114  }
   115  
   116  // A UnitResources contains a list of the resources the unit defined by Entity.
   117  type UnitResources struct {
   118  	params.Entity
   119  
   120  	// Resources is a list of resources for the unit.
   121  	Resources []Resource
   122  
   123  	// DownloadProgress indicates the number of bytes of a resource file
   124  	// have been downloaded so far the uniter. Only currently downloading
   125  	// resources are included.
   126  	DownloadProgress map[string]int64
   127  }
   128  
   129  // UploadResult is the response from an upload request.
   130  type UploadResult struct {
   131  	params.ErrorResult
   132  
   133  	// Resource describes the resource that was stored in the model.
   134  	Resource Resource
   135  }
   136  
   137  // Resource contains info about a Resource.
   138  type Resource struct {
   139  	CharmResource
   140  
   141  	// ID uniquely identifies a resource-service pair within the model.
   142  	// Note that the model ignores pending resources (those with a
   143  	// pending ID) except for in a few clearly pending-related places.
   144  	ID string
   145  
   146  	// PendingID identifies that this resource is pending and
   147  	// distinguishes it from other pending resources with the same model
   148  	// ID (and from the active resource).
   149  	PendingID string
   150  
   151  	// ServiceID identifies the service for the resource.
   152  	ServiceID string
   153  
   154  	// Username is the ID of the user that added the revision
   155  	// to the model (whether implicitly or explicitly).
   156  	Username string `json:"username"`
   157  
   158  	// Timestamp indicates when the resource was added to the model.
   159  	Timestamp time.Time `json:"timestamp"`
   160  }
   161  
   162  // CharmResource contains the definition for a resource.
   163  type CharmResource struct {
   164  	// Name identifies the resource.
   165  	Name string `json:"name"`
   166  
   167  	// Type is the name of the resource type.
   168  	Type string `json:"type"`
   169  
   170  	// Path is where the resource will be stored.
   171  	Path string `json:"path"`
   172  
   173  	// Description contains user-facing info about the resource.
   174  	Description string `json:"description,omitempty"`
   175  
   176  	// Origin is where the resource will come from.
   177  	Origin string `json:"origin"`
   178  
   179  	// Revision is the revision, if applicable.
   180  	Revision int `json:"revision"`
   181  
   182  	// Fingerprint is the SHA-384 checksum for the resource blob.
   183  	Fingerprint []byte `json:"fingerprint"`
   184  
   185  	// Size is the size of the resource, in bytes.
   186  	Size int64
   187  }
   188  
   189  func resolveErrors(errs []error) error {
   190  	switch len(errs) {
   191  	case 0:
   192  		return nil
   193  	case 1:
   194  		return errs[0]
   195  	default:
   196  		msgs := make([]string, len(errs))
   197  		for i, err := range errs {
   198  			msgs[i] = err.Error()
   199  		}
   200  		return errors.New(strings.Join(msgs, "\n"))
   201  	}
   202  }