Пример #1
0
// HandleApply builds and expands compute stack template for the given ID and
// sends an apply request to terraformer.
//
// When destroy=false, building and expanding the stack prior to
// terraformer request is done asynchronously, and the result of
// that operation is communicated back with eventer.
//
// When destroy=true, fetching machines from DB is done synchronously, as
// as soon as Apply method returns, allowed user list for each machine
// is zeroed, which could make the destroy oepration to fail - we
// first build machines and rest of the destroy is perfomed asynchronously.
func (bs *BaseStack) HandleApply(ctx context.Context) (interface{}, error) {
	arg, ok := ctx.Value(stack.ApplyRequestKey).(*stack.ApplyRequest)
	if !ok {
		arg = &stack.ApplyRequest{}

		if err := bs.Req.Args.One().Unmarshal(arg); err != nil {
			return nil, err
		}
	}

	if err := arg.Valid(); err != nil {
		return nil, err
	}

	err := bs.Builder.BuildStack(arg.StackID, arg.Credentials)

	if err != nil && !(arg.Destroy && models.IsNotFound(err, "jStackTemplate")) {
		return nil, err
	}

	if state := bs.Builder.Stack.Stack.State(); state.InProgress() {
		return nil, fmt.Errorf("State is currently %s. Please try again later", state)
	}

	if rt, ok := stack.RequestTraceFromContext(ctx); ok {
		rt.Hijack()
	}

	bs.Arg = arg

	if arg.Destroy {
		err = bs.destroy(ctx, arg)
	} else {
		err = bs.apply(ctx, arg)
	}

	if err != nil {
		return nil, err
	}

	return &stack.ControlResult{
		EventId: bs.Eventer.ID(),
	}, nil
}
Пример #2
0
func (db *mongoDatabase) SetCred(c *Cred) error {
	f, ok := c.Perm.(*Filter)
	if !ok {
		return errors.New("invalid credential permission")
	}

	perm, err := db.Validate(f, c)
	if err == nil {
		c.Perm = perm
		return nil
	}

	if !models.IsNotFound(err, "jCredential") {
		return err
	}

	mPerm, ok := perm.(*MongoPerm)
	if !ok {
		return fmt.Errorf("unable to create credential: %s", err)
	}

	if mPerm.AccModel == nil {
		return fmt.Errorf("unable to create credential: missing %q account", f.Username)
	}

	now := time.Now().UTC()

	if c.Title == "" {
		c.Title = mPerm.UserModel.Name + " " + now.String()
	}

	mPerm.CredModel = &models.Credential{
		Id:          bson.NewObjectId(),
		Provider:    c.Provider,
		Identifier:  c.Ident,
		Title:       c.Title,
		OriginId:    mPerm.AccModel.Id,
		Verified:    false,
		AccessLevel: "private",
		Meta: &models.CredentialMeta{
			CreatedAt:  now,
			ModifiedAt: now,
		},
	}

	db.Log.Debug("creating credential: %#v", mPerm.CredModel)

	if err := modelhelper.CreateCredential(mPerm.CredModel); err != nil {
		return err
	}

	rel := &models.Relationship{
		Id:         bson.NewObjectId(),
		TargetId:   mPerm.CredModel.Id,
		TargetName: "JCredential",
		SourceId:   mPerm.AccModel.Id,
		SourceName: "JAccount",
		As:         "owner",
		TimeStamp:  now,
	}

	if err := modelhelper.AddRelationship(rel); err != nil {
		return err
	}

	c.Perm = mPerm

	return nil
}