本文整理汇总了Golang中github.com/juju/juju/state.State.Service方法的典型用法代码示例。如果您正苦于以下问题:Golang State.Service方法的具体用法?Golang State.Service怎么用?Golang State.Service使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/juju/juju/state.State
的用法示例。
在下文中一共展示了State.Service方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: addServiceUnits
// addServiceUnits adds a given number of units to a service.
func addServiceUnits(st *state.State, args params.AddServiceUnits) ([]*state.Unit, error) {
service, err := st.Service(args.ServiceName)
if err != nil {
return nil, err
}
if args.NumUnits < 1 {
return nil, fmt.Errorf("must add at least one unit")
}
// New API uses placement directives.
if len(args.Placement) > 0 {
return jjj.AddUnitsWithPlacement(st, service, args.NumUnits, args.Placement)
}
// Otherwise we use the older machine spec.
if args.NumUnits > 1 && args.ToMachineSpec != "" {
return nil, fmt.Errorf("cannot use NumUnits with ToMachineSpec")
}
if args.ToMachineSpec != "" && names.IsValidMachine(args.ToMachineSpec) {
_, err = st.Machine(args.ToMachineSpec)
if err != nil {
return nil, errors.Annotatef(err, `cannot add units for service "%v" to machine %v`, args.ServiceName, args.ToMachineSpec)
}
}
return jjj.AddUnits(st, service, args.NumUnits, args.ToMachineSpec)
}
示例2: getAllUnitNames
// getAllUnitNames returns a sequence of valid Unit objects from state. If any
// of the service names or unit names are not found, an error is returned.
func getAllUnitNames(st *state.State, units, services []string) (result []*state.Unit, err error) {
unitsSet := set.NewStrings(units...)
for _, name := range services {
service, err := st.Service(name)
if err != nil {
return nil, err
}
units, err := service.AllUnits()
if err != nil {
return nil, err
}
for _, unit := range units {
unitsSet.Add(unit.Name())
}
}
for _, unitName := range unitsSet.Values() {
unit, err := st.Unit(unitName)
if err != nil {
return nil, err
}
// We only operate on units that have an assigned machine.
if _, err := unit.AssignedMachineId(); err != nil {
return nil, err
}
result = append(result, unit)
}
return result, nil
}
示例3: getAllUnitNames
// getAllUnitNames returns a sequence of valid Unit objects from state. If any
// of the service names or unit names are not found, an error is returned.
func getAllUnitNames(st *state.State, units, services []string) (result []*state.Unit, err error) {
unitsSet := set.NewStrings(units...)
for _, name := range services {
service, err := st.Service(name)
if err != nil {
return nil, err
}
units, err := service.AllUnits()
if err != nil {
return nil, err
}
for _, unit := range units {
unitsSet.Add(unit.Name())
}
}
for _, unitName := range unitsSet.Values() {
unit, err := st.Unit(unitName)
if err != nil {
return nil, err
}
// We only operate on principal units, and only thise that have an
// assigned machines.
if unit.IsPrincipal() {
if _, err := unit.AssignedMachineId(); err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf("%s is not a principal unit", unit)
}
result = append(result, unit)
}
return result, nil
}
示例4: addServiceUnits
// addServiceUnits adds a given number of units to a service.
func addServiceUnits(st *state.State, args params.AddServiceUnits) ([]*state.Unit, error) {
service, err := st.Service(args.ServiceName)
if err != nil {
return nil, err
}
if args.NumUnits < 1 {
return nil, errors.New("must add at least one unit")
}
return jjj.AddUnits(st, service, args.NumUnits, args.Placement)
}
示例5: opClientServiceExpose
func opClientServiceExpose(c *gc.C, st api.Connection, mst *state.State) (func(), error) {
err := st.Client().ServiceExpose("wordpress")
if err != nil {
return func() {}, err
}
return func() {
svc, err := mst.Service("wordpress")
c.Assert(err, jc.ErrorIsNil)
svc.ClearExposed()
}, nil
}
示例6: AssertPrincipalServiceDeployed
func AssertPrincipalServiceDeployed(c *gc.C, st *state.State, serviceName string, curl *charm.URL, forced bool, bundle charm.Charm, cons constraints.Value) *state.Service {
service, err := st.Service(serviceName)
c.Assert(err, jc.ErrorIsNil)
charm, force, err := service.Charm()
c.Assert(err, jc.ErrorIsNil)
c.Assert(force, gc.Equals, forced)
c.Assert(charm.URL(), gc.DeepEquals, curl)
// When charms are read from state, storage properties are
// always deserialised as empty slices if empty or nil, so
// update bundle to match (bundle comes from parsing charm
// metadata yaml where nil means nil).
for name, bundleMeta := range bundle.Meta().Storage {
if bundleMeta.Properties == nil {
bundleMeta.Properties = []string{}
bundle.Meta().Storage[name] = bundleMeta
}
}
c.Assert(charm.Meta(), jc.DeepEquals, bundle.Meta())
c.Assert(charm.Config(), jc.DeepEquals, bundle.Config())
serviceCons, err := service.Constraints()
c.Assert(err, jc.ErrorIsNil)
c.Assert(serviceCons, gc.DeepEquals, cons)
for a := coretesting.LongAttempt.Start(); a.Next(); {
units, err := service.AllUnits()
c.Assert(err, jc.ErrorIsNil)
for _, unit := range units {
mid, err := unit.AssignedMachineId()
if !a.HasNext() {
c.Assert(err, jc.ErrorIsNil)
} else if err != nil {
continue
}
machine, err := st.Machine(mid)
c.Assert(err, jc.ErrorIsNil)
machineCons, err := machine.Constraints()
c.Assert(err, jc.ErrorIsNil)
c.Assert(machineCons, gc.DeepEquals, cons)
}
break
}
return service
}
示例7: getAllUnitNames
// getAllUnitNames returns a sequence of valid Unit objects from state. If any
// of the service names or unit names are not found, an error is returned.
func getAllUnitNames(st *state.State, units, services []string) (result []names.Tag, err error) {
unitsSet := set.NewStrings(units...)
for _, name := range services {
service, err := st.Service(name)
if err != nil {
return nil, err
}
units, err := service.AllUnits()
if err != nil {
return nil, err
}
for _, unit := range units {
unitsSet.Add(unit.Name())
}
}
for _, unitName := range unitsSet.SortedValues() {
result = append(result, names.NewUnitTag(unitName))
}
return result, nil
}
示例8: addServiceUnits
// addServiceUnits adds a given number of units to a service.
func addServiceUnits(state *state.State, args params.AddServiceUnits) ([]*state.Unit, error) {
service, err := state.Service(args.ServiceName)
if err != nil {
return nil, err
}
if args.NumUnits < 1 {
return nil, fmt.Errorf("must add at least one unit")
}
if args.NumUnits > 1 && args.ToMachineSpec != "" {
return nil, fmt.Errorf("cannot use NumUnits with ToMachineSpec")
}
if args.ToMachineSpec != "" && names.IsValidMachine(args.ToMachineSpec) {
_, err = state.Machine(args.ToMachineSpec)
if err != nil {
return nil, errors.Annotatef(err, `cannot add units for service "%v" to machine %v`, args.ServiceName, args.ToMachineSpec)
}
}
return jjj.AddUnits(state, service, args.NumUnits, args.ToMachineSpec)
}
示例9: leadershipSettingsAccessorFactory
func leadershipSettingsAccessorFactory(
st *state.State,
resources *common.Resources,
auth common.Authorizer,
) *leadershipapiserver.LeadershipSettingsAccessor {
registerWatcher := func(serviceId string) (string, error) {
service, err := st.Service(serviceId)
if err != nil {
return "", err
}
w := service.WatchLeaderSettings()
if _, ok := <-w.Changes(); ok {
return resources.Register(w), nil
}
return "", watcher.EnsureErr(w)
}
getSettings := func(serviceId string) (map[string]string, error) {
service, err := st.Service(serviceId)
if err != nil {
return nil, err
}
return service.LeaderSettings()
}
writeSettings := func(token leadership.Token, serviceId string, settings map[string]string) error {
service, err := st.Service(serviceId)
if err != nil {
return err
}
return service.UpdateLeaderSettings(token, settings)
}
return leadershipapiserver.NewLeadershipSettingsAccessor(
auth,
registerWatcher,
getSettings,
st.LeadershipChecker().LeadershipCheck,
writeSettings,
)
}