code: func createVM(c context.Context, payload proto.Message) error {
  	task, ok := payload.(*tasks.CreateVM)
  	switch {
  	case !ok:
  		return errors.Reason("unexpected payload type %T", payload).Err()
  	case task.GetId() == "":
  		return errors.Reason("ID is required").Err()
  	case task.GetConfig() == "":
  		return errors.Reason("config is required").Err()
  	}
  	vm := &model.VM{
  		ID:       task.Id,
  		Config:   task.Config,
  		Hostname: fmt.Sprintf("%s-%d-%s", task.Prefix, task.Index, getSuffix(c)),
  		Index:    task.Index,
  		Lifetime: task.Lifetime,
  		Prefix:   task.Prefix,
  		Revision: task.Revision,
  		Swarming: task.Swarming,
  		Timeout:  task.Timeout,
  	}
  	if task.Attributes != nil {
  		vm.Attributes = *task.Attributes
  		// TODO(crbug/942301): Auto-select zone if zone is unspecified.
  		vm.Attributes.SetZone(vm.Attributes.GetZone())
  	}
  	// createVM is called repeatedly, so do a fast check outside the transaction.
  	// In most cases, this will skip the more expensive transactional check.
  	switch err := datastore.Get(c, vm); {
  	case err == datastore.ErrNoSuchEntity:
  	case err != nil:
  		return errors.Annotate(err, "failed to fetch VM").Err()
  	default:
  		return nil
  	}
  	return datastore.RunInTransaction(c, func(c context.Context) error {
  		switch err := datastore.Get(c, vm); {
  		case err == datastore.ErrNoSuchEntity:
  		case err != nil:
  			return errors.Annotate(err, "failed to fetch VM").Err()
  		default:
  			return nil
  		}
  		if err := datastore.Put(c, vm); err != nil {
  			return errors.Annotate(err, "failed to store VM").Err()
  		}
  		return nil
  	}, nil)
  }
docs: // createVM creates a VM if it doesn't already exist.