github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/state/utils/charm.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package utils
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"gopkg.in/juju/charm.v6-unstable"
     9  
    10  	"github.com/juju/juju/state"
    11  )
    12  
    13  func charmMetadata(st CharmState, applicationID string) (*charm.Meta, error) {
    14  	application, err := st.Service(applicationID)
    15  	if err != nil {
    16  		return nil, errors.Annotatef(err, "while looking up application %q", applicationID)
    17  	}
    18  
    19  	ch, err := application.Charm()
    20  	if err != nil {
    21  		return nil, errors.Annotatef(err, "while looking up charm info for application %q", applicationID)
    22  	}
    23  
    24  	meta := ch.Meta()
    25  
    26  	return meta, nil
    27  }
    28  
    29  // CharmState exposes the methods of state.State used here.
    30  type CharmState interface {
    31  	Service(id string) (CharmService, error)
    32  }
    33  
    34  // CharmService exposes the methods of state.Service used here.
    35  type CharmService interface {
    36  	Charm() (Charm, error)
    37  }
    38  
    39  // Charm exposes the methods of state.Charm used here.
    40  type Charm interface {
    41  	Meta() *charm.Meta
    42  }
    43  
    44  // NewCharmState returns a new CharmState for the given state.State.
    45  func NewCharmState(st *state.State) CharmState {
    46  	return &charmState{raw: st}
    47  }
    48  
    49  type charmState struct {
    50  	raw *state.State
    51  }
    52  
    53  // Service implements CharmState.
    54  func (st charmState) Service(id string) (CharmService, error) {
    55  	raw, err := st.raw.Application(id)
    56  	if err != nil {
    57  		return nil, errors.Trace(err)
    58  	}
    59  	return &charmService{raw: raw}, nil
    60  }
    61  
    62  type charmService struct {
    63  	raw *state.Application
    64  }
    65  
    66  // Charm implements CharmService.
    67  func (svc charmService) Charm() (Charm, error) {
    68  	raw, _, err := svc.raw.Charm()
    69  	if err != nil {
    70  		return nil, errors.Trace(err)
    71  	}
    72  	return &charmInfo{raw}, nil
    73  }
    74  
    75  type charmInfo struct {
    76  	*state.Charm
    77  }