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