func buildServiceMatcherShims(s *state.Application, patterns ...string) (shims []closurePredicate, _ error) { // Match on name. shims = append(shims, func() (bool, bool, error) { for _, p := range patterns { if strings.ToLower(s.Name()) == strings.ToLower(p) { return true, true, nil } } return false, false, nil }) // Match on exposure. shims = append(shims, func() (bool, bool, error) { return matchExposure(patterns, s) }) // If the service has an unit instance that matches any of the // given criteria, consider the service a match as well. unitShims, err := buildShimsForUnit(s.AllUnits, patterns...) if err != nil { return nil, err } shims = append(shims, unitShims...) // Units may be able to match the pattern. Ultimately defer to // that logic, and guard against breaking the predicate-chain. if len(unitShims) <= 0 { shims = append(shims, func() (bool, bool, error) { return false, true, nil }) } return shims, nil }
func (s *statusUnitTestSuite) checkAppVersion(c *gc.C, application *state.Application, expectedVersion string) params.ApplicationStatus { client := s.APIState.Client() status, err := client.Status(nil) c.Assert(err, jc.ErrorIsNil) appStatus, found := status.Applications[application.Name()] c.Assert(found, jc.IsTrue) c.Check(appStatus.WorkloadVersion, gc.Equals, expectedVersion) return appStatus }
func (context *statusContext) processServiceRelations(service *state.Application) (related map[string][]string, subord []string, err error) { subordSet := make(set.Strings) related = make(map[string][]string) relations := context.relations[service.Name()] for _, relation := range relations { ep, err := relation.Endpoint(service.Name()) if err != nil { return nil, nil, err } relationName := ep.Relation.Name eps, err := relation.RelatedEndpoints(service.Name()) if err != nil { return nil, nil, err } for _, ep := range eps { if isSubordinate(&ep, service) { subordSet.Add(ep.ApplicationName) } related[relationName] = append(related[relationName], ep.ApplicationName) } } for relationName, serviceNames := range related { sn := set.NewStrings(serviceNames...) related[relationName] = sn.SortedValues() } return related, subordSet.SortedValues(), nil }
func assertOneRelation(c *gc.C, srv *state.Application, relId int, endpoints ...state.Endpoint) *state.Relation { rels, err := srv.Relations() c.Assert(err, jc.ErrorIsNil) c.Assert(rels, gc.HasLen, 1) rel := rels[0] c.Assert(rel.Id(), gc.Equals, relId) c.Assert(rel.Endpoints(), jc.SameContents, endpoints) name := srv.Name() expectEp := endpoints[0] ep, err := rel.Endpoint(name) c.Assert(err, jc.ErrorIsNil) c.Assert(ep, gc.DeepEquals, expectEp) if len(endpoints) == 2 { expectEp = endpoints[1] } eps, err := rel.RelatedEndpoints(name) c.Assert(err, jc.ErrorIsNil) c.Assert(eps, gc.DeepEquals, []state.Endpoint{expectEp}) return rel }
func NewProReqRelation(c *gc.C, s *ConnSuite, scope charm.RelationScope) *ProReqRelation { psvc := s.AddTestingService(c, "mysql", s.AddTestingCharm(c, "mysql")) var rsvc *state.Application if scope == charm.ScopeGlobal { rsvc = s.AddTestingService(c, "wordpress", s.AddTestingCharm(c, "wordpress")) } else { rsvc = s.AddTestingService(c, "logging", s.AddTestingCharm(c, "logging")) } eps, err := s.State.InferEndpoints("mysql", rsvc.Name()) c.Assert(err, jc.ErrorIsNil) rel, err := s.State.AddRelation(eps...) c.Assert(err, jc.ErrorIsNil) prr := &ProReqRelation{rel: rel, psvc: psvc, rsvc: rsvc} prr.pu0, prr.pru0 = addRU(c, psvc, rel, nil) prr.pu1, prr.pru1 = addRU(c, psvc, rel, nil) if scope == charm.ScopeGlobal { prr.ru0, prr.rru0 = addRU(c, rsvc, rel, nil) prr.ru1, prr.rru1 = addRU(c, rsvc, rel, nil) } else { prr.ru0, prr.rru0 = addRU(c, rsvc, rel, prr.pu0) prr.ru1, prr.rru1 = addRU(c, rsvc, rel, prr.pu1) } return prr }
func (s *serviceSuite) claimLeadership(c *gc.C, unit *state.Unit, service *state.Application) { claimer := s.State.LeadershipClaimer() err := claimer.ClaimLeadership(service.Name(), unit.Name(), time.Minute) c.Assert(err, jc.ErrorIsNil) }
func (context *statusContext) processApplication(service *state.Application) params.ApplicationStatus { serviceCharm, _, err := service.Charm() if err != nil { return params.ApplicationStatus{Err: common.ServerError(err)} } var processedStatus = params.ApplicationStatus{ Charm: serviceCharm.URL().String(), Series: service.Series(), Exposed: service.IsExposed(), Life: processLife(service), } if latestCharm, ok := context.latestCharms[*serviceCharm.URL().WithRevision(-1)]; ok && latestCharm != nil { if latestCharm.Revision() > serviceCharm.URL().Revision { processedStatus.CanUpgradeTo = latestCharm.String() } } processedStatus.Relations, processedStatus.SubordinateTo, err = context.processServiceRelations(service) if err != nil { processedStatus.Err = common.ServerError(err) return processedStatus } units := context.units[service.Name()] if service.IsPrincipal() { processedStatus.Units = context.processUnits(units, serviceCharm.URL().String()) } applicationStatus, err := service.Status() if err != nil { processedStatus.Err = common.ServerError(err) return processedStatus } processedStatus.Status.Status = applicationStatus.Status.String() processedStatus.Status.Info = applicationStatus.Message processedStatus.Status.Data = applicationStatus.Data processedStatus.Status.Since = applicationStatus.Since metrics := serviceCharm.Metrics() planRequired := metrics != nil && metrics.Plan != nil && metrics.Plan.Required if planRequired || len(service.MetricCredentials()) > 0 { processedStatus.MeterStatuses = context.processUnitMeterStatuses(units) } versions := make([]status.StatusInfo, 0, len(units)) for _, unit := range units { statuses, err := unit.WorkloadVersionHistory().StatusHistory( status.StatusHistoryFilter{Size: 1}, ) if err != nil { processedStatus.Err = common.ServerError(err) return processedStatus } // Even though we fully expect there to be historical values there, // even the first should be the empty string, the status history // collection is not added to in a transactional manner, so it may be // not there even though we'd really like it to be. Such is mongo. if len(statuses) > 0 { versions = append(versions, statuses[0]) } } if len(versions) > 0 { sort.Sort(bySinceDescending(versions)) processedStatus.WorkloadVersion = versions[0].Message } return processedStatus }