github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/runner/context/export_test.go (about)

     1  // Copyright 2012-2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package context
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/proxy"
     9  	"gopkg.in/juju/charm.v6"
    10  	"gopkg.in/juju/names.v2"
    11  
    12  	"github.com/juju/juju/api/uniter"
    13  	"github.com/juju/juju/apiserver/params"
    14  	"github.com/juju/juju/core/leadership"
    15  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    16  )
    17  
    18  var (
    19  	ValidatePortRange = validatePortRange
    20  	TryOpenPorts      = tryOpenPorts
    21  	TryClosePorts     = tryClosePorts
    22  )
    23  
    24  func NewHookContext(
    25  	unit *uniter.Unit,
    26  	state *uniter.State,
    27  	id,
    28  	uuid,
    29  	modelName string,
    30  	relationId int,
    31  	remoteUnitName string,
    32  	relations map[int]*ContextRelation,
    33  	apiAddrs []string,
    34  	legacyProxySettings proxy.Settings,
    35  	jujuProxySettings proxy.Settings,
    36  	canAddMetrics bool,
    37  	charmMetrics *charm.Metrics,
    38  	actionData *ActionData,
    39  	assignedMachineTag names.MachineTag,
    40  	paths Paths,
    41  	clock Clock,
    42  ) (*HookContext, error) {
    43  	ctx := &HookContext{
    44  		unit:                unit,
    45  		state:               state,
    46  		id:                  id,
    47  		uuid:                uuid,
    48  		modelName:           modelName,
    49  		unitName:            unit.Name(),
    50  		relationId:          relationId,
    51  		remoteUnitName:      remoteUnitName,
    52  		relations:           relations,
    53  		apiAddrs:            apiAddrs,
    54  		legacyProxySettings: legacyProxySettings,
    55  		jujuProxySettings:   jujuProxySettings,
    56  		actionData:          actionData,
    57  		pendingPorts:        make(map[PortRange]PortRangeInfo),
    58  		assignedMachineTag:  assignedMachineTag,
    59  		clock:               clock,
    60  	}
    61  	// Get and cache the addresses.
    62  	var err error
    63  	ctx.publicAddress, err = unit.PublicAddress()
    64  	if err != nil && !params.IsCodeNoAddressSet(err) {
    65  		return nil, err
    66  	}
    67  	ctx.privateAddress, err = unit.PrivateAddress()
    68  	if err != nil && !params.IsCodeNoAddressSet(err) {
    69  		return nil, err
    70  	}
    71  	ctx.availabilityzone, err = unit.AvailabilityZone()
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  	ctx.machinePorts, err = state.AllMachinePorts(ctx.assignedMachineTag)
    76  	if err != nil {
    77  		return nil, errors.Trace(err)
    78  	}
    79  
    80  	statusCode, statusInfo, err := unit.MeterStatus()
    81  	if err != nil {
    82  		return nil, errors.Annotate(err, "could not retrieve meter status for unit")
    83  	}
    84  	ctx.meterStatus = &meterStatus{
    85  		code: statusCode,
    86  		info: statusInfo,
    87  	}
    88  	return ctx, nil
    89  }
    90  
    91  // SetEnvironmentHookContextRelation exists purely to set the fields used in hookVars.
    92  // It makes no assumptions about the validity of context.
    93  func SetEnvironmentHookContextRelation(
    94  	context *HookContext,
    95  	relationId int, endpointName, remoteUnitName string,
    96  ) {
    97  	context.relationId = relationId
    98  	context.remoteUnitName = remoteUnitName
    99  	context.relations = map[int]*ContextRelation{
   100  		relationId: {
   101  			endpointName: endpointName,
   102  			relationId:   relationId,
   103  		},
   104  	}
   105  }
   106  
   107  func PatchCachedStatus(ctx jujuc.Context, status, info string, data map[string]interface{}) func() {
   108  	hctx := ctx.(*HookContext)
   109  	oldStatus := hctx.status
   110  	hctx.status = &jujuc.StatusInfo{
   111  		Status: status,
   112  		Info:   info,
   113  		Data:   data,
   114  	}
   115  	return func() {
   116  		hctx.status = oldStatus
   117  	}
   118  }
   119  
   120  func GetStubActionContext(in map[string]interface{}) *HookContext {
   121  	return &HookContext{
   122  		actionData: &ActionData{
   123  			ResultsMap: in,
   124  		},
   125  	}
   126  }
   127  
   128  type LeadershipContextFunc func(LeadershipSettingsAccessor, leadership.Tracker, string) LeadershipContext
   129  
   130  func PatchNewLeadershipContext(f LeadershipContextFunc) func() {
   131  	var old LeadershipContextFunc
   132  	old, newLeadershipContext = newLeadershipContext, f
   133  	return func() { newLeadershipContext = old }
   134  }
   135  
   136  func StorageAddConstraints(ctx *HookContext) map[string][]params.StorageConstraints {
   137  	return ctx.storageAddConstraints
   138  }
   139  
   140  // NewModelHookContext exists purely to set the fields used in rs.
   141  // The returned value is not otherwise valid.
   142  func NewModelHookContext(
   143  	id, modelUUID, modelName, unitName, meterCode, meterInfo, slaLevel, availZone string,
   144  	apiAddresses []string, legacyProxySettings proxy.Settings, jujuProxySettings proxy.Settings,
   145  	machineTag names.MachineTag,
   146  ) *HookContext {
   147  	return &HookContext{
   148  		id:                  id,
   149  		unitName:            unitName,
   150  		uuid:                modelUUID,
   151  		modelName:           modelName,
   152  		apiAddrs:            apiAddresses,
   153  		legacyProxySettings: legacyProxySettings,
   154  		jujuProxySettings:   jujuProxySettings,
   155  		meterStatus: &meterStatus{
   156  			code: meterCode,
   157  			info: meterInfo,
   158  		},
   159  		relationId:         -1,
   160  		assignedMachineTag: machineTag,
   161  		availabilityzone:   availZone,
   162  		slaLevel:           slaLevel,
   163  		principal:          unitName,
   164  	}
   165  }
   166  
   167  func ContextEnvInfo(hctx *HookContext) (name, uuid string) {
   168  	return hctx.modelName, hctx.uuid
   169  }
   170  
   171  func ContextMachineTag(hctx *HookContext) names.MachineTag {
   172  	return hctx.assignedMachineTag
   173  }
   174  
   175  func UpdateCachedSettings(cf0 ContextFactory, relId int, unitName string, settings params.Settings) {
   176  	cf := cf0.(*contextFactory)
   177  	members := cf.relationCaches[relId].members
   178  	if members[unitName] == nil {
   179  		members[unitName] = params.Settings{}
   180  	}
   181  	for key, value := range settings {
   182  		members[unitName][key] = value
   183  	}
   184  }
   185  
   186  func CachedSettings(cf0 ContextFactory, relId int, unitName string) (params.Settings, bool) {
   187  	cf := cf0.(*contextFactory)
   188  	settings, found := cf.relationCaches[relId].members[unitName]
   189  	return settings, found
   190  }
   191  
   192  func (ctx *HookContext) SLALevel() string {
   193  	return ctx.slaLevel
   194  }