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