github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/api/common/charm/charmorigin.go (about)

     1  // Copyright 2020 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package charm
     5  
     6  import (
     7  	"github.com/juju/charm/v12"
     8  	"github.com/juju/errors"
     9  
    10  	corebase "github.com/juju/juju/core/base"
    11  	corecharm "github.com/juju/juju/core/charm"
    12  	"github.com/juju/juju/rpc/params"
    13  )
    14  
    15  // OriginSource represents the source of the charm.
    16  type OriginSource string
    17  
    18  func (c OriginSource) String() string {
    19  	return string(c)
    20  }
    21  
    22  const (
    23  	// OriginLocal represents a local charm.
    24  	OriginLocal OriginSource = "local"
    25  	// OriginCharmHub represents a charm from the new charm-hub.
    26  	OriginCharmHub OriginSource = "charm-hub"
    27  )
    28  
    29  // Origin holds the information about where the charm originates.
    30  type Origin struct {
    31  	// Source is where the charm came from, Local, CharmStore or CharmHub.
    32  	Source OriginSource
    33  	// Type defines the charm type if it's a bundle or a charm
    34  	Type string
    35  	// ID is the CharmHub ID for this charm.
    36  	ID string
    37  	// Hash is the hash of the charm intended to be used.
    38  	Hash string
    39  	// Risk is the CharmHub channel risk, or the CharmStore channel value.
    40  	Risk string
    41  	// Revision is the charm revision number.
    42  	Revision *int
    43  	// Track is a CharmHub channel track.
    44  	Track *string
    45  	// Branch is the CharmHub channel branch
    46  	Branch *string
    47  	// Architecture describes the architecture intended to be used by the charm.
    48  	Architecture string
    49  	// Base describes the OS base intended to be used by the charm.
    50  	Base corebase.Base
    51  
    52  	// InstanceKey is a unique string associated with the application. To
    53  	// assist with keeping KPI data in charmhub, it must be the same for every
    54  	// charmhub Refresh action related to an application. Create with the
    55  	// charmhub.CreateInstanceKey method. LP: 1944582
    56  	InstanceKey string
    57  }
    58  
    59  // WithBase allows to update the base of an origin.
    60  func (o Origin) WithBase(b *corebase.Base) Origin {
    61  	other := o
    62  	other.Base = corebase.Base{}
    63  	if b != nil {
    64  		other.Base = *b
    65  	}
    66  	return other
    67  }
    68  
    69  // CharmChannel returns the channel indicated by this origin.
    70  func (o Origin) CharmChannel() charm.Channel {
    71  	var track string
    72  	if o.Track != nil {
    73  		track = *o.Track
    74  	}
    75  	var branch string
    76  	if o.Branch != nil {
    77  		branch = *o.Branch
    78  	}
    79  	return charm.Channel{
    80  		Track:  track,
    81  		Risk:   charm.Risk(o.Risk),
    82  		Branch: branch,
    83  	}
    84  }
    85  
    86  // ParamsCharmOrigin is a helper method to get a params version
    87  // of this structure.
    88  func (o Origin) ParamsCharmOrigin() params.CharmOrigin {
    89  	return params.CharmOrigin{
    90  		Source:       o.Source.String(),
    91  		Type:         o.Type,
    92  		ID:           o.ID,
    93  		Hash:         o.Hash,
    94  		Revision:     o.Revision,
    95  		Risk:         o.Risk,
    96  		Track:        o.Track,
    97  		Branch:       o.Branch,
    98  		Architecture: o.Architecture,
    99  		Base:         params.Base{Name: o.Base.OS, Channel: o.Base.Channel.String()},
   100  		InstanceKey:  o.InstanceKey,
   101  	}
   102  }
   103  
   104  // CoreCharmOrigin is a help method to get a core version of this structure.
   105  func (o Origin) CoreCharmOrigin() corecharm.Origin {
   106  	var track string
   107  	if o.Track != nil {
   108  		track = *o.Track
   109  	}
   110  	var branch string
   111  	if o.Branch != nil {
   112  		branch = *o.Branch
   113  	}
   114  	var channel *charm.Channel
   115  	if o.Risk != "" {
   116  		channel = &charm.Channel{
   117  			Risk:   charm.Risk(o.Risk),
   118  			Track:  track,
   119  			Branch: branch,
   120  		}
   121  	}
   122  	return corecharm.Origin{
   123  		Source:   corecharm.Source(o.Source),
   124  		Type:     o.Type,
   125  		ID:       o.ID,
   126  		Hash:     o.Hash,
   127  		Revision: o.Revision,
   128  		Channel:  channel,
   129  		Platform: corecharm.Platform{
   130  			Architecture: o.Architecture,
   131  			OS:           o.Base.OS,
   132  			Channel:      o.Base.Channel.Track,
   133  		},
   134  		InstanceKey: o.InstanceKey,
   135  	}
   136  }
   137  
   138  // APICharmOrigin is a helper function to convert params.CharmOrigin
   139  // to an Origin.
   140  func APICharmOrigin(origin params.CharmOrigin) (Origin, error) {
   141  	base, err := corebase.ParseBase(origin.Base.Name, origin.Base.Channel)
   142  	if err != nil {
   143  		return Origin{}, errors.Trace(err)
   144  	}
   145  	return Origin{
   146  		Source:       OriginSource(origin.Source),
   147  		Type:         origin.Type,
   148  		ID:           origin.ID,
   149  		Hash:         origin.Hash,
   150  		Risk:         origin.Risk,
   151  		Revision:     origin.Revision,
   152  		Track:        origin.Track,
   153  		Branch:       origin.Branch,
   154  		Architecture: origin.Architecture,
   155  		Base:         base,
   156  		InstanceKey:  origin.InstanceKey,
   157  	}, nil
   158  }
   159  
   160  // CoreCharmOrigin is a helper function to convert params.CharmOrigin
   161  // to an Origin.
   162  func CoreCharmOrigin(origin corecharm.Origin) (Origin, error) {
   163  	var ch charm.Channel
   164  	if origin.Channel != nil {
   165  		ch = *origin.Channel
   166  	}
   167  	var track *string
   168  	if ch.Track != "" {
   169  		track = &ch.Track
   170  	}
   171  	var branch *string
   172  	if ch.Branch != "" {
   173  		branch = &ch.Branch
   174  	}
   175  	chBase, err := corebase.ParseBase(origin.Platform.OS, origin.Platform.Channel)
   176  	if err != nil {
   177  		return Origin{}, errors.Trace(err)
   178  	}
   179  	return Origin{
   180  		Source:       OriginSource(origin.Source),
   181  		Type:         origin.Type,
   182  		ID:           origin.ID,
   183  		Hash:         origin.Hash,
   184  		Revision:     origin.Revision,
   185  		Risk:         string(ch.Risk),
   186  		Track:        track,
   187  		Branch:       branch,
   188  		Architecture: origin.Platform.Architecture,
   189  		Base:         chBase,
   190  		InstanceKey:  origin.InstanceKey,
   191  	}, nil
   192  }