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

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package uniter
     5  
     6  import (
     7  	"fmt"
     8  	"net/url"
     9  
    10  	"github.com/juju/errors"
    11  	"gopkg.in/juju/charm.v6-unstable"
    12  
    13  	"github.com/juju/juju/apiserver/params"
    14  )
    15  
    16  // This module implements a subset of the interface provided by
    17  // state.Charm, as needed by the uniter API.
    18  
    19  // Charm represents the state of a charm in the model.
    20  type Charm struct {
    21  	st   *State
    22  	curl *charm.URL
    23  }
    24  
    25  // String returns the charm URL as a string.
    26  func (c *Charm) String() string {
    27  	return c.curl.String()
    28  }
    29  
    30  // URL returns the URL that identifies the charm.
    31  func (c *Charm) URL() *charm.URL {
    32  	return c.curl
    33  }
    34  
    35  // ArchiveURLs returns the URLs to the charm archive (bundle) in the
    36  // model storage. Each URL should be tried until one succeeds.
    37  func (c *Charm) ArchiveURLs() ([]*url.URL, error) {
    38  	var results params.StringsResults
    39  	args := params.CharmURLs{
    40  		URLs: []params.CharmURL{{URL: c.curl.String()}},
    41  	}
    42  	err := c.st.facade.FacadeCall("CharmArchiveURLs", args, &results)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	if len(results.Results) != 1 {
    47  		return nil, fmt.Errorf("expected 1 result, got %d", len(results.Results))
    48  	}
    49  	result := results.Results[0]
    50  	if result.Error != nil {
    51  		return nil, result.Error
    52  	}
    53  	archiveURLs := make([]*url.URL, len(result.Result))
    54  	for i, rawurl := range result.Result {
    55  		archiveURL, err := url.Parse(rawurl)
    56  		if err != nil {
    57  			return nil, errors.Annotate(err, "server returned an invalid URL")
    58  		}
    59  		archiveURLs[i] = archiveURL
    60  	}
    61  	return archiveURLs, nil
    62  }
    63  
    64  // ArchiveSha256 returns the SHA256 digest of the charm archive
    65  // (bundle) bytes.
    66  //
    67  // NOTE: This differs from state.Charm.BundleSha256() by returning an
    68  // error as well, because it needs to make an API call. It's also
    69  // renamed to avoid confusion with juju deployment bundles.
    70  //
    71  // TODO(dimitern): 2013-09-06 bug 1221834
    72  // Cache the result after getting it once for the same charm URL,
    73  // because it's immutable.
    74  func (c *Charm) ArchiveSha256() (string, error) {
    75  	var results params.StringResults
    76  	args := params.CharmURLs{
    77  		URLs: []params.CharmURL{{URL: c.curl.String()}},
    78  	}
    79  	err := c.st.facade.FacadeCall("CharmArchiveSha256", args, &results)
    80  	if err != nil {
    81  		return "", err
    82  	}
    83  	if len(results.Results) != 1 {
    84  		return "", fmt.Errorf("expected 1 result, got %d", len(results.Results))
    85  	}
    86  	result := results.Results[0]
    87  	if result.Error != nil {
    88  		return "", result.Error
    89  	}
    90  	return result.Result, nil
    91  }