Example #1
0
func (serviceview *ServiceView) BuildLinks(context *ctp.ApiContext) {
	serviceview.Self = ctp.NewLink(context, "@/serviceViews/$", serviceview.Id)
	serviceview.Scope = ctp.NewLink(context, "@/")
	serviceview.Dependencies = ctp.NewLink(context, "@/serviceViews/$/dependencies", serviceview.Id)
	serviceview.Assets = ctp.NewLink(context, "@/serviceViews/$/assets", serviceview.Id)
	serviceview.Logs = ctp.NewLink(context, "@/serviceViews/$/logs", serviceview.Id)
	serviceview.Triggers = ctp.NewLink(context, "@/serviceViews/$/triggers", serviceview.Id)
}
Example #2
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
}
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)
}
Example #4
0
func (metric *Metric) BuildLinks(context *ctp.ApiContext) {
	metric.Self = ctp.NewLink(context.CtpBase, "@/metrics/$", metric.Id)
	metric.Scope = ctp.NewLink(context.CtpBase, "@/")
}
Example #5
0
func (attribute *Attribute) BuildLinks(context *ctp.ApiContext) {
	attribute.Self = ctp.NewLink(context.CtpBase, "@/attributes/$", attribute.Id)
	attribute.Scope = ctp.NewLink(context.CtpBase, "@/assets/$", attribute.Parent[0])
	attribute.Measurements = ctp.NewLink(context.CtpBase, "@/attributes/$/measurements", attribute.Id)
}
Example #6
0
func (base *BaseURI) BuildLinks(context *ctp.ApiContext) {
	base.Self = ctp.NewLink(context.CtpBase, "@/")
	base.ServiceViews = ctp.NewLink(context.CtpBase, "@/serviceViews")
	base.Metrics = ctp.NewLink(context.CtpBase, "@/metrics")
}
Example #7
0
func (res *TaggedResource) BuildLinks(context *ctp.ApiContext) {
	res.Self = ctp.NewLink(context.CtpBase, "@/$/$?x=tags", context.Params[0], res.Id)
}
Example #8
0
func (asset *Asset) BuildLinks(context *ctp.ApiContext) {
	asset.Self = ctp.NewLink(context, "@/assets/$", asset.Id)
	asset.Scope = ctp.NewLink(context, "@/serviceViews/$", asset.Parent)
	asset.Attributes = ctp.NewLink(context, "@/assets/$/attributes", asset.Id)
}
Example #9
0
func (trigger *Trigger) BuildLinks(context *ctp.ApiContext) {
	trigger.Self = ctp.NewLink(context.CtpBase, "@/triggers/$", trigger.Id)
	trigger.Scope = ctp.NewLink(context.CtpBase, "@/serviceView/$", trigger.Parent[0])
}
Example #10
0
func (log *LogEntry) BuildLinks(context *ctp.ApiContext) {
	log.Self = ctp.NewLink(context.CtpBase, "@/logs/$", log.Id)
}
Example #11
0
func (account *Account) BuildLinks(context *ctp.ApiContext) {
	account.Self = ctp.NewLink(context.CtpBase, "@/accounts/$", account.Id)
}
func (measurement *Measurement) BuildLinks(context *ctp.ApiContext) {
	measurement.Self = ctp.NewLink(context.CtpBase, "@/measurements/$", measurement.Id)
	measurement.Scope = ctp.NewLink(context.CtpBase, "@/attributes/$", measurement.Parent[0])
}
Example #13
0
func (access *Access) BuildLinks(context *ctp.ApiContext) {
	access.Self = ctp.NewLink(context, "@/access/$", access.Id)
}