Beispiel #1
0
func AddEndpoint(c *middleware.Context, cmd m.AddEndpointCommand) Response {
	cmd.OrgId = c.OrgId
	if err := bus.Dispatch(&cmd); err != nil {
		return ApiError(500, "Failed to add endpoint", err)
	}

	return Json(200, cmd.Result)
}
Beispiel #2
0
func AddEndpoint(cmd *m.AddEndpointCommand) error {
	return inTransaction2(func(sess *session) error {
		endpoint := &m.Endpoint{
			OrgId:   cmd.OrgId,
			Name:    cmd.Name,
			Created: time.Now(),
			Updated: time.Now(),
		}
		endpoint.UpdateEndpointSlug()

		if _, err := sess.Insert(endpoint); err != nil {
			return err
		}
		if len(cmd.Tags) > 0 {
			endpointTags := make([]m.EndpointTag, 0, len(cmd.Tags))
			for _, tag := range cmd.Tags {
				endpointTags = append(endpointTags, m.EndpointTag{
					OrgId:      cmd.OrgId,
					EndpointId: endpoint.Id,
					Tag:        tag,
				})
			}
			sess.Table("endpoint_tag")
			if _, err := sess.Insert(&endpointTags); err != nil {
				return err
			}
		}

		cmd.Result = &m.EndpointDTO{
			Id:    endpoint.Id,
			OrgId: endpoint.OrgId,
			Name:  endpoint.Name,
			Slug:  endpoint.Slug,
			Tags:  cmd.Tags,
		}
		sess.publishAfterCommit(&events.EndpointCreated{
			EndpointPayload: events.EndpointPayload{
				Id:    endpoint.Id,
				OrgId: endpoint.OrgId,
				Name:  endpoint.Name,
				Slug:  endpoint.Slug,
				Tags:  cmd.Tags,
			},
			Timestamp: endpoint.Updated,
		})

		// add any included momitors.
		if len(cmd.Monitors) > 0 {
			for _, monitorCmd := range cmd.Monitors {
				monitorCmd.OrgId = cmd.OrgId
				monitorCmd.EndpointId = endpoint.Id
				if err := addMonitorTransaction(monitorCmd, sess); err != nil {
					fmt.Println(err)
					return err
				}
			}
		}

		return nil
	})
}