github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/state/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  	"launchpad.net/juju-core/charm"
    11  	"launchpad.net/juju-core/state/api/params"
    12  )
    13  
    14  // This module implements a subset of the interface provided by
    15  // state.Charm, as needed by the uniter API.
    16  
    17  // Charm represents the state of a charm in the environment.
    18  type Charm struct {
    19  	st  *State
    20  	url string
    21  }
    22  
    23  // Strings returns the charm URL as a string.
    24  func (c *Charm) String() string {
    25  	return c.url
    26  }
    27  
    28  // URL returns the URL that identifies the charm.
    29  func (c *Charm) URL() *charm.URL {
    30  	return charm.MustParseURL(c.url)
    31  }
    32  
    33  func (c *Charm) getArchiveInfo(apiCall string) (string, error) {
    34  	var results params.StringResults
    35  	args := params.CharmURLs{
    36  		URLs: []params.CharmURL{{URL: c.url}},
    37  	}
    38  	err := c.st.caller.Call("Uniter", "", apiCall, args, &results)
    39  	if err != nil {
    40  		return "", err
    41  	}
    42  	if len(results.Results) != 1 {
    43  		return "", fmt.Errorf("expected one result, got %d", len(results.Results))
    44  	}
    45  	result := results.Results[0]
    46  	if result.Error != nil {
    47  		return "", result.Error
    48  	}
    49  	return result.Result, nil
    50  }
    51  
    52  // ArchiveURL returns the url to the charm archive (bundle) in the
    53  // provider storage, and DisableSSLHostnameVerification flag.
    54  //
    55  // NOTE: This differs from state.Charm.BundleURL() by returning an
    56  // error as well, because it needs to make an API call. It's also
    57  // renamed to avoid confusion with juju deployment bundles.
    58  //
    59  // TODO(dimitern): 2013-09-06 bug 1221834
    60  // Cache the result after getting it once for the same charm URL,
    61  // because it's immutable.
    62  func (c *Charm) ArchiveURL() (*url.URL, bool, error) {
    63  	var results params.CharmArchiveURLResults
    64  	args := params.CharmURLs{
    65  		URLs: []params.CharmURL{{URL: c.url}},
    66  	}
    67  	err := c.st.caller.Call("Uniter", "", "CharmArchiveURL", args, &results)
    68  	if err != nil {
    69  		return nil, false, err
    70  	}
    71  	if len(results.Results) != 1 {
    72  		return nil, false, fmt.Errorf("expected one result, got %d", len(results.Results))
    73  	}
    74  	result := results.Results[0]
    75  	if result.Error != nil {
    76  		return nil, false, result.Error
    77  	}
    78  	archiveURL, err := url.Parse(result.Result)
    79  	if err != nil {
    80  		return nil, false, err
    81  	}
    82  	return archiveURL, result.DisableSSLHostnameVerification, nil
    83  }
    84  
    85  // ArchiveSha256 returns the SHA256 digest of the charm archive
    86  // (bundle) bytes.
    87  //
    88  // NOTE: This differs from state.Charm.BundleSha256() by returning an
    89  // error as well, because it needs to make an API call. It's also
    90  // renamed to avoid confusion with juju deployment bundles.
    91  //
    92  // TODO(dimitern): 2013-09-06 bug 1221834
    93  // Cache the result after getting it once for the same charm URL,
    94  // because it's immutable.
    95  func (c *Charm) ArchiveSha256() (string, error) {
    96  	return c.getArchiveInfo("CharmArchiveSha256")
    97  }