func()

in step_attach_disks.go [93:132]


func (a *AttachDisks) run(ctx context.Context, s *Step) DError {
	var wg sync.WaitGroup
	w := s.w
	e := make(chan DError)
	for _, ad := range *a {
		wg.Add(1)
		go func(ad *AttachDisk) {
			defer wg.Done()

			if diskRes, ok := w.disks.get(ad.Source); ok {
				ad.Source = diskRes.link
			}

			inst := ad.Instance
			if instRes, ok := w.instances.get(ad.Instance); ok {
				inst = instRes.link
				ad.Instance = instRes.RealName
			}

			w.LogStepInfo(s.name, "AttachDisks", "Attaching disk %q to instance %q.", ad.AttachedDisk.Source, inst)
			if err := w.ComputeClient.AttachDisk(ad.project, ad.zone, ad.Instance, &ad.AttachedDisk); err != nil {
				e <- newErr("failed to attach disk", err)
				return
			}
		}(ad)
	}

	go func() {
		wg.Wait()
		e <- nil
	}()

	select {
	case err := <-e:
		return err
	case <-w.Cancel:
		wg.Wait()
		return nil
	}
}