github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/resource/resourceadapters/deploy.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package resourceadapters
     5  
     6  import (
     7  	"strconv"
     8  
     9  	"github.com/juju/errors"
    10  	charmresource "gopkg.in/juju/charm.v6-unstable/resource"
    11  	"gopkg.in/macaroon.v1"
    12  
    13  	"github.com/juju/juju/api/base"
    14  	"github.com/juju/juju/charmstore"
    15  	"github.com/juju/juju/resource/api/client"
    16  	"github.com/juju/juju/resource/cmd"
    17  )
    18  
    19  // DeployResourcesFunc is the function type of DeployResources.
    20  type DeployResourcesFunc func(
    21  	applicationID string,
    22  	chID charmstore.CharmID,
    23  	csMac *macaroon.Macaroon,
    24  	filesAndRevisions map[string]string,
    25  	resources map[string]charmresource.Meta,
    26  	conn base.APICallCloser,
    27  ) (ids map[string]string, err error)
    28  
    29  // DeployResources uploads the bytes for the given files to the server and
    30  // creates pending resource metadata for the all resource mentioned in the
    31  // metadata. It returns a map of resource name to pending resource IDs.
    32  func DeployResources(
    33  	applicationID string,
    34  	chID charmstore.CharmID,
    35  	csMac *macaroon.Macaroon,
    36  	filesAndRevisions map[string]string,
    37  	resources map[string]charmresource.Meta,
    38  	conn base.APICallCloser,
    39  ) (ids map[string]string, err error) {
    40  
    41  	if len(filesAndRevisions)+len(resources) == 0 {
    42  		// Nothing to upload.
    43  		return nil, nil
    44  	}
    45  
    46  	client, err := NewAPIClient(conn)
    47  	if err != nil {
    48  		return nil, errors.Trace(err)
    49  	}
    50  
    51  	filenames := make(map[string]string)
    52  	revisions := make(map[string]int)
    53  	for name, val := range filesAndRevisions {
    54  		rev, err := strconv.Atoi(val)
    55  		if err != nil {
    56  			filenames[name] = val
    57  		} else {
    58  			revisions[name] = rev
    59  		}
    60  	}
    61  
    62  	ids, err = cmd.DeployResources(cmd.DeployResourcesArgs{
    63  		ApplicationID:      applicationID,
    64  		CharmID:            chID,
    65  		CharmStoreMacaroon: csMac,
    66  		Filenames:          filenames,
    67  		Revisions:          revisions,
    68  		ResourcesMeta:      resources,
    69  		Client:             &deployClient{client},
    70  	})
    71  	if err != nil {
    72  		return nil, errors.Trace(err)
    73  	}
    74  	return ids, nil
    75  }
    76  
    77  type deployClient struct {
    78  	*client.Client
    79  }
    80  
    81  // AddPendingResources adds pending metadata for store-based resources.
    82  func (cl *deployClient) AddPendingResources(applicationID string, chID charmstore.CharmID, csMac *macaroon.Macaroon, resources []charmresource.Resource) ([]string, error) {
    83  	return cl.Client.AddPendingResources(client.AddPendingResourcesArgs{
    84  		ApplicationID:      applicationID,
    85  		CharmID:            chID,
    86  		CharmStoreMacaroon: csMac,
    87  		Resources:          resources,
    88  	})
    89  }