Esempio n. 1
0
func (account *Account) Create(context *ctp.ApiContext) *ctp.HttpError {
	var key [24]byte

	account.BuildLinks(context)

	if account.Token == "" {
		_, err := rand.Read(key[:])
		if err != nil {
			return ctp.NewInternalServerError("Error generating key")
		}
		account.Token = base64.StdEncoding.EncodeToString(key[:])
	}

	if len(account.AccountTags.WithPrefix("account:")) == 0 {
		account.AccountTags.Append(ctp.NewTags("account:" + string(account.Id)))
	}

	if len(account.AccountTags.WithPrefix("role:")) == 0 {
		account.AccountTags.Append(ctp.UserRoleTag)
	}

	if !ctp.CreateResource(context, "accounts", account) {
		return ctp.NewHttpError(http.StatusInternalServerError, "Could not save account")
	}
	return nil
}
Esempio n. 2
0
func (metric *Metric) Create(context *ctp.ApiContext) *ctp.HttpError {
	metric.BuildLinks(context)

	if metric.AccessTags == nil {
		metric.AccessTags = ctp.NewTags("access:user")
	}

	if !ctp.CreateResource(context, "metrics", metric) {
		return ctp.NewHttpError(http.StatusInternalServerError, "Could not save object")
	}
	return nil
}