github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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"
    14  	"github.com/juju/juju/charmstore"
    15  	"github.com/juju/juju/resource/api/client"
    16  	"github.com/juju/juju/resource/cmd"
    17  )
    18  
    19  // DeployResources uploads the bytes for the given files to the server and
    20  // creates pending resource metadata for the all resource mentioned in the
    21  // metadata. It returns a map of resource name to pending resource IDs.
    22  func DeployResources(serviceID string, chID charmstore.CharmID, csMac *macaroon.Macaroon, filesAndRevisions map[string]string, resources map[string]charmresource.Meta, conn api.Connection) (ids map[string]string, err error) {
    23  	client, err := newAPIClient(conn)
    24  	if err != nil {
    25  		return nil, errors.Trace(err)
    26  	}
    27  
    28  	filenames := make(map[string]string)
    29  	revisions := make(map[string]int)
    30  	for name, val := range filesAndRevisions {
    31  		rev, err := strconv.Atoi(val)
    32  		if err != nil {
    33  			filenames[name] = val
    34  		} else {
    35  			revisions[name] = rev
    36  		}
    37  	}
    38  
    39  	ids, err = cmd.DeployResources(cmd.DeployResourcesArgs{
    40  		ServiceID:          serviceID,
    41  		CharmID:            chID,
    42  		CharmStoreMacaroon: csMac,
    43  		Filenames:          filenames,
    44  		Revisions:          revisions,
    45  		ResourcesMeta:      resources,
    46  		Client:             &deployClient{client},
    47  	})
    48  	if err != nil {
    49  		return nil, errors.Trace(err)
    50  	}
    51  	return ids, nil
    52  }
    53  
    54  type deployClient struct {
    55  	*client.Client
    56  }
    57  
    58  // AddPendingResources adds pending metadata for store-based resources.
    59  func (cl *deployClient) AddPendingResources(serviceID string, chID charmstore.CharmID, csMac *macaroon.Macaroon, resources []charmresource.Resource) ([]string, error) {
    60  	return cl.Client.AddPendingResources(client.AddPendingResourcesArgs{
    61  		ServiceID:          serviceID,
    62  		CharmID:            chID,
    63  		CharmStoreMacaroon: csMac,
    64  		Resources:          resources,
    65  	})
    66  }