func (i *importer) makeUnitDoc(s description.Application, u description.Unit) (*unitDoc, error) { // NOTE: if we want to support units having different charms deployed // than the application recomments and migrate that, then we should serialize // the charm url for each unit rather than grabbing the applications charm url. // Currently the units charm url matching the application is a precondiation // to migration. charmUrl, err := charm.ParseURL(s.CharmURL()) if err != nil { return nil, errors.Trace(err) } var subordinates []string if subs := u.Subordinates(); len(subs) > 0 { for _, s := range subs { subordinates = append(subordinates, s.Id()) } } return &unitDoc{ Name: u.Name(), Application: s.Name(), Series: s.Series(), CharmURL: charmUrl, Principal: u.Principal().Id(), Subordinates: subordinates, StorageAttachmentCount: i.unitStorageAttachmentCount(u.Tag()), MachineId: u.Machine().Id(), Tools: i.makeTools(u.Tools()), Life: Alive, PasswordHash: u.PasswordHash(), }, nil }
func (i *importer) makeApplicationDoc(s description.Application) (*applicationDoc, error) { charmUrl, err := charm.ParseURL(s.CharmURL()) if err != nil { return nil, errors.Trace(err) } return &applicationDoc{ Name: s.Name(), Series: s.Series(), Subordinate: s.Subordinate(), CharmURL: charmUrl, Channel: s.Channel(), CharmModifiedVersion: s.CharmModifiedVersion(), ForceCharm: s.ForceCharm(), Life: Alive, UnitCount: len(s.Units()), RelationCount: i.relationCount(s.Name()), Exposed: s.Exposed(), MinUnits: s.MinUnits(), MetricCredentials: s.MetricsCredentials(), }, nil }
func (i *importer) application(s description.Application) error { // Import this application, then its units. i.logger.Debugf("importing application %s", s.Name()) // 1. construct an applicationDoc sdoc, err := i.makeApplicationDoc(s) if err != nil { return errors.Trace(err) } // 2. construct a statusDoc status := s.Status() if status == nil { return errors.NotValidf("missing status") } statusDoc := i.makeStatusDoc(status) // TODO: update never set malarky... maybe... ops, err := addApplicationOps(i.st, addApplicationOpsArgs{ applicationDoc: sdoc, statusDoc: statusDoc, constraints: i.constraints(s.Constraints()), storage: i.storageConstraints(s.StorageConstraints()), settings: s.Settings(), leadershipSettings: s.LeadershipSettings(), }) if err != nil { return errors.Trace(err) } if err := i.st.runTransaction(ops); err != nil { return errors.Trace(err) } svc := newApplication(i.st, sdoc) if annotations := s.Annotations(); len(annotations) > 0 { if err := i.st.SetAnnotations(svc, annotations); err != nil { return errors.Trace(err) } } if err := i.importStatusHistory(svc.globalKey(), s.StatusHistory()); err != nil { return errors.Trace(err) } for _, unit := range s.Units() { if err := i.unit(s, unit); err != nil { return errors.Trace(err) } } if s.Leader() != "" { if err := i.st.LeadershipClaimer().ClaimLeadership( s.Name(), s.Leader(), initialLeaderClaimTime); err != nil { return errors.Trace(err) } } return nil }