github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  
     9  	"gopkg.in/juju/charm.v6"
    10  
    11  	"github.com/juju/juju/apiserver/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 model.
    18  type Charm struct {
    19  	st   *State
    20  	curl *charm.URL
    21  }
    22  
    23  // String returns the charm URL as a string.
    24  func (c *Charm) String() string {
    25  	return c.curl.String()
    26  }
    27  
    28  // URL returns the URL that identifies the charm.
    29  func (c *Charm) URL() *charm.URL {
    30  	return c.curl
    31  }
    32  
    33  // ArchiveSha256 returns the SHA256 digest of the charm archive
    34  // (bundle) bytes.
    35  //
    36  // NOTE: This differs from state.Charm.BundleSha256() by returning an
    37  // error as well, because it needs to make an API call. It's also
    38  // renamed to avoid confusion with juju deployment bundles.
    39  //
    40  // TODO(dimitern): 2013-09-06 bug 1221834
    41  // Cache the result after getting it once for the same charm URL,
    42  // because it's immutable.
    43  func (c *Charm) ArchiveSha256() (string, error) {
    44  	var results params.StringResults
    45  	args := params.CharmURLs{
    46  		URLs: []params.CharmURL{{URL: c.curl.String()}},
    47  	}
    48  	err := c.st.facade.FacadeCall("CharmArchiveSha256", args, &results)
    49  	if err != nil {
    50  		return "", err
    51  	}
    52  	if len(results.Results) != 1 {
    53  		return "", fmt.Errorf("expected 1 result, got %d", len(results.Results))
    54  	}
    55  	result := results.Results[0]
    56  	if result.Error != nil {
    57  		return "", result.Error
    58  	}
    59  	return result.Result, nil
    60  }