Пример #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
}
Пример #2
0
func (measurement *Measurement) Create(context *ctp.ApiContext) *ctp.HttpError {
	measurement.BuildLinks(context)
	measurement.Metric = ctp.ShortenLink(context.CtpBase, measurement.Metric)
	if !ctp.IsShortLink(measurement.Metric) {
		return ctp.NewBadRequestError("Invalid metric URL")
	}

	if !measurement.State.IsValid() {
		return ctp.NewBadRequestError("Invalid or missing state value")
	}

	if measurement.Result != nil {
		if err := measurementCheckResult(context, measurement); err != nil {
			return err
		}
	} else {
		if err := measurementCheckMetric(context, measurement); err != nil {
			return err
		}
	}

	if measurement.Objective != nil {
		if err := measurementObjectiveEvaluate(context, measurement); err != nil {
			return err
		}
	}

	if !ctp.CreateResource(context, "measurements", measurement) {
		return ctp.NewInternalServerError("Could not save measurement object")
	}
	return nil
}
Пример #3
0
func (metric *Metric) Delete(context *ctp.ApiContext) *ctp.HttpError {
	metricUrl := ctp.NewLink(context.CtpBase, "@/metrics/$", metric.Id) // just to create a clean URL

	query := context.Session.DB("ctp").C("measurements").Find(bson.M{"metric": metricUrl})
	count, err := query.Count()
	if err != nil {
		return ctp.NewInternalServerError(err)
	}
	if count > 0 {
		return ctp.NewHttpError(http.StatusConflict, "Metric cannot be deleted because it is still in use by some measurements.")
	}

	if !ctp.DeleteResource(context, "metrics", metric.Id) {
		return ctp.NewInternalServerError("Metric deletion failed")
	}
	return nil
}
Пример #4
0
func HandleGETCollection(w http.ResponseWriter, r *http.Request, context *ctp.ApiContext) {
	var item ctp.NamedResource
	var parent ctp.Resource
	var query *mgo.Query
	var collectionType string
	var skip, page, items int
	var err error

	collection := new(Collection)
	selector := make(bson.M)

	if name, ok := r.URL.Query()["name"]; ok {
		selector["name"] = name[0]
	}

	page_query := r.URL.Query().Get("page")
	items_query := r.URL.Query().Get("items")
	if page_query != "" || items_query != "" {
		if page_query == "" || items_query == "" {
			ctp.RenderErrorResponse(w, context, ctp.NewHttpError(http.StatusBadRequest, "Must specify both 'page' and 'items' in query string."))
			return
		}
		if page, err = strconv.Atoi(page_query); err != nil || page < 0 {
			ctp.RenderErrorResponse(w, context, ctp.NewHttpError(http.StatusBadRequest, "page must be a positive number."))
			return
		}
		if items, err = strconv.Atoi(items_query); err != nil || items <= 0 {
			ctp.RenderErrorResponse(w, context, ctp.NewHttpError(http.StatusBadRequest, "items must be a non-zero positive number."))
		}
		skip = items * page
	}

	if !context.AuthenticateClient(w, r) {
		return
	}

	var mgoCollection *mgo.Collection

	if len(context.Params) == 1 {
		collectionType = context.Params[0]
		mgoCollection = context.Session.DB("ctp").C(collectionType)

		switch collectionType {
		case "serviceViews":
			if !context.VerifyAccessTags(w, ctp.UserRoleTag) {
				return
			}
			if !ctp.MatchTags(context.AccountTags, ctp.AdminRoleTag) {
				selector["accessTags"] = bson.M{"$in": context.AccountTags.WithPrefix("account:")}
			}
		case "metrics":
			if !context.VerifyAccessTags(w, ctp.UserRoleTag) {
				return
			}
		default:
			if !context.VerifyAccessTags(w, ctp.AdminRoleTag) {
				return
			}
		}
	} else {
		if !context.VerifyAccessTags(w, ctp.UserRoleTag) {
			return
		}

		if !ctp.LoadResource(context, context.Params[0], ctp.Base64Id(context.Params[1]), &parent) {
			ctp.RenderErrorResponse(w, context, ctp.NewNotFoundErrorf("Not found - /%s/%s does not exist", context.Params[0], context.Params[1]))
			return
		}

		if !context.VerifyAccessTags(w, parent.AccessTags) {
			return
		}
		collection.Scope = ctp.NewLink(context.CtpBase, "/$/$", context.Params[0], context.Params[1])

		collectionType = context.Params[2]
		if context.Params[2] == "indicators" {
			mgoCollection = context.Session.DB("ctp").C("measurements")
		} else {
			mgoCollection = context.Session.DB("ctp").C(collectionType)
		}
		selector["parent"] = context.Params[1]
	}

	query = mgoCollection.Find(selector)

	collection_length, err := query.Count()
	if err != nil {
		ctp.RenderErrorResponse(w, context, ctp.NewInternalServerError(err))
		return
	}

	query = query.Sort("$natural").Skip(skip).Limit(items)

	returned_length, err := query.Count()
	if err != nil {
		ctp.RenderErrorResponse(w, context, ctp.NewInternalServerError(err))
		return
	}

	collection.Self = ctp.Link(r.URL.RequestURI())
	collection.CollectionLength = collection_length
	collection.ReturnedLength = returned_length
	collection.CollectionType = collectionType
	collection.Items = make([]CollectionItem, 0, returned_length)

	iter := query.Iter()
	for iter.Next(&item) {
		collection.Items = append(collection.Items, CollectionItem{
			Link: ctp.NewLink(context.CtpBase, "@/$/$", collectionType, item.Id),
			Name: item.Name,
		})
	}

	if err := iter.Close(); err != nil {
		ctp.RenderErrorResponse(w, context, ctp.NewInternalServerError(err))
		return
	}

	ctp.RenderJsonResponse(w, context, 200, collection)
}
Пример #5
0
func (account *Account) Delete(context *ctp.ApiContext) *ctp.HttpError {
	if !ctp.DeleteResource(context, "accounts", account.Id) {
		return ctp.NewInternalServerError("Account deletion failed")
	}
	return nil
}
Пример #6
0
func (measurement *Measurement) Update(context *ctp.ApiContext, update ctp.ResourceUpdater) *ctp.HttpError {
	measurement.BuildLinks(context)
	up, ok := update.(*Measurement)
	if !ok {
		return ctp.NewInternalServerError("Updated object is not a measurement") // should never happen
	}

	switch context.QueryParam {
	case "userActivated":
		switch up.State {
		case "activated":
			if measurement.State == "deactivated" {
				measurement.State = "pending" // FIXME: add backoffice logic for notification of state change?
			}
		case "deactivated":
			measurement.State = "deactivated"
			measurement.Result = nil
		default:
			return ctp.NewBadRequestError("state can only be 'activated' or 'deactivated'")
		}
	case "objective":
		measurement.Objective = up.Objective
		if err := measurementObjectiveEvaluate(context, measurement); err != nil {
			return err
		}
	case "result":
		if measurement.State == "deactivated" {
			return ctp.NewHttpError(http.StatusConflict, "Measurement is not in activated state.")
		}
		if measurement.State == "pending" {
			measurement.State = "activated"
		}

		if up.Result == nil {
			return ctp.NewBadRequestError("No result provided in request")
		}

		measurement.Result = up.Result

		if measurement.Result.UpdateTime.IsZero() {
			measurement.Result.UpdateTime = ctp.Now()
		}

		if err := measurementCheckMetric(context, measurement); err != nil {
			return err
		}

		if measurement.Objective != nil {
			if err := measurementObjectiveEvaluate(context, measurement); err != nil {
				return err
			}
		}

		measurementTriggersEvaluate(context, measurement)

	default:
		return ctp.NewBadRequestError("invalid query string") // should never happen, because already filtered in serve.go
	}

	if !ctp.UpdateResource(context, "measurements", measurement.Id, measurement) {
		return ctp.NewInternalServerError("Could not update measurement object")
	}
	return nil
}