github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/state/charm.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package state
     5  
     6  import (
     7  	"net/url"
     8  
     9  	"github.com/juju/names"
    10  	"gopkg.in/juju/charm.v4"
    11  )
    12  
    13  // charmDoc represents the internal state of a charm in MongoDB.
    14  type charmDoc struct {
    15  	DocID   string     `bson:"_id"`
    16  	URL     *charm.URL `bson:"url"`
    17  	EnvUUID string     `bson:"env-uuid"`
    18  	Meta    *charm.Meta
    19  	Config  *charm.Config
    20  	Actions *charm.Actions
    21  	Metrics *charm.Metrics
    22  
    23  	// DEPRECATED: BundleURL is deprecated, and exists here
    24  	// only for migration purposes. We should remove this
    25  	// when migrations are no longer necessary.
    26  	BundleURL *url.URL `bson:"bundleurl,omitempty"`
    27  
    28  	BundleSha256  string
    29  	StoragePath   string
    30  	PendingUpload bool
    31  	Placeholder   bool
    32  }
    33  
    34  // Charm represents the state of a charm in the environment.
    35  type Charm struct {
    36  	st  *State
    37  	doc charmDoc
    38  }
    39  
    40  func newCharm(st *State, cdoc *charmDoc) *Charm {
    41  	// Because we probably just read the doc from state, make sure we
    42  	// unescape any config option names for "$" and ".". See
    43  	// http://pad.lv/1308146
    44  	if cdoc != nil && cdoc.Config != nil {
    45  		unescapedConfig := charm.NewConfig()
    46  		for optionName, option := range cdoc.Config.Options {
    47  			unescapedName := unescapeReplacer.Replace(optionName)
    48  			unescapedConfig.Options[unescapedName] = option
    49  		}
    50  		cdoc.Config = unescapedConfig
    51  	}
    52  	return &Charm{st: st, doc: *cdoc}
    53  }
    54  
    55  // Tag returns a tag identifying the charm.
    56  // Implementing state.GlobalEntity interface.
    57  func (c *Charm) Tag() names.Tag {
    58  	return names.NewCharmTag(c.URL().String())
    59  }
    60  
    61  // charmGlobalKey returns the global database key for the charm
    62  // with the given url.
    63  func charmGlobalKey(charmURL *charm.URL) string {
    64  	return "c#" + charmURL.String()
    65  }
    66  
    67  // GlobalKey returns the global database key for the charm.
    68  // Implementing state.GlobalEntity interface.
    69  func (c *Charm) globalKey() string {
    70  	return charmGlobalKey(c.doc.URL)
    71  }
    72  
    73  func (c *Charm) String() string {
    74  	return c.doc.URL.String()
    75  }
    76  
    77  // URL returns the URL that identifies the charm.
    78  func (c *Charm) URL() *charm.URL {
    79  	clone := *c.doc.URL
    80  	return &clone
    81  }
    82  
    83  // Revision returns the monotonically increasing charm
    84  // revision number.
    85  func (c *Charm) Revision() int {
    86  	return c.doc.URL.Revision
    87  }
    88  
    89  // Meta returns the metadata of the charm.
    90  func (c *Charm) Meta() *charm.Meta {
    91  	return c.doc.Meta
    92  }
    93  
    94  // Config returns the configuration of the charm.
    95  func (c *Charm) Config() *charm.Config {
    96  	return c.doc.Config
    97  }
    98  
    99  // Metrics returns the metrics declared for the charm.
   100  func (c *Charm) Metrics() *charm.Metrics {
   101  	return c.doc.Metrics
   102  }
   103  
   104  // Actions returns the actions definition of the charm.
   105  func (c *Charm) Actions() *charm.Actions {
   106  	return c.doc.Actions
   107  }
   108  
   109  // StoragePath returns the storage path of the charm bundle.
   110  func (c *Charm) StoragePath() string {
   111  	return c.doc.StoragePath
   112  }
   113  
   114  // BundleURL returns the url to the charm bundle in
   115  // the provider storage.
   116  //
   117  // DEPRECATED: this is only to be used for migrating
   118  // charm archives to environment storage.
   119  func (c *Charm) BundleURL() *url.URL {
   120  	return c.doc.BundleURL
   121  }
   122  
   123  // BundleSha256 returns the SHA256 digest of the charm bundle bytes.
   124  func (c *Charm) BundleSha256() string {
   125  	return c.doc.BundleSha256
   126  }
   127  
   128  // IsUploaded returns whether the charm has been uploaded to the
   129  // environment storage.
   130  func (c *Charm) IsUploaded() bool {
   131  	return !c.doc.PendingUpload
   132  }
   133  
   134  // IsPlaceholder returns whether the charm record is just a placeholder
   135  // rather than representing a deployed charm.
   136  func (c *Charm) IsPlaceholder() bool {
   137  	return c.doc.Placeholder
   138  }