github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/worker/uniter/runner/export_test.go (about)

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