func main() {
	apiKeyPtr := flag.String("api-key", "", "Telemetry API Key")
	boardNamePtr := flag.String("n", "", "Name of the board to create")
	boardPrefixPtr := flag.String("p", "", "Flow tag prefix")
	filePathPtr := flag.String("f", "", "Input file path")

	flag.Parse()

	apiKey := strings.TrimSpace(*apiKeyPtr)
	boardName := strings.TrimSpace(*boardNamePtr)
	boardPrefix := strings.TrimSpace(*boardPrefixPtr)
	filePath := strings.TrimSpace(*filePathPtr)

	if apiKey == "" {
		log.Fatal("The API Key is required")
	}

	if boardName == "" {
		log.Fatal("The board name is required")
	}

	if boardPrefix == "" {
		log.Fatal("The board prefix is required")
	}

	if filePath == "" {
		log.Fatal("The file path is required")
	}

	fileContents, err := ioutil.ReadFile(filePath)

	if err != nil {
		log.Fatal(err)
	}

	boardTemplate := &gotelemetry.ExportedBoard{}

	err = json.Unmarshal(fileContents, &boardTemplate)

	if err != nil {
		log.Fatal(err)
	}

	credentials, err := gotelemetry.NewCredentials(apiKey)

	if err != nil {
		log.Fatal(err)
	}

	board, err := gotelemetry.ImportBoard(credentials, boardName, boardPrefix, boardTemplate)

	if err != nil {
		log.Fatal(err)
	}

	log.Printf("Success: %v", board)
}
func ProcessNotificationRequest(configFile *config.ConfigFile, errorChannel chan error, completionChannel chan bool, notificationChannel string, notificationFlow string, notification gotelemetry.Notification) {
	errorChannel <- gotelemetry.NewLogError("Notification mode is on.")

	apiToken, err := configFile.APIToken()

	if err != nil {
		errorChannel <- err
		completionChannel <- true

		return
	}

	credentials, err := gotelemetry.NewCredentials(apiToken)

	if err != nil {
		errorChannel <- err
		completionChannel <- true

		return
	}

	credentials.SetDebugChannel(errorChannel)

	if len(notificationChannel) > 0 {
		channel := gotelemetry.NewChannel(notificationChannel)
		err = channel.SendNotification(credentials, notification)
	} else if len(notificationFlow) > 0 {
		err = gotelemetry.SendFlowChannelNotification(credentials, notificationFlow, notification)
	} else {
		err = gotelemetry.NewError(http.StatusBadRequest, "Either channel or flow is required")
	}

	if err != nil {
		errorChannel <- err
	} else {
		errorChannel <- gotelemetry.NewLogError("Notification sent successfully.")
	}

	completionChannel <- true
}
Exemple #3
0
func ProcessPipeRequest(configFile *config.ConfigFile, errorChannel chan error, completionChannel chan bool, data []byte) {
	errorChannel <- gotelemetry.NewLogError("Piped mode is on.")
	errorChannel <- gotelemetry.NewDebugError("Input data is %s", strings.Replace(string(data), "\n", "\\n", -1))

	submissionType := gotelemetry.BatchTypePATCH

	if config.CLIConfig.UseJSONPatch {
		errorChannel <- gotelemetry.NewDebugError("Will perform a JSON-Patch operation")
		submissionType = gotelemetry.BatchTypeJSONPATCH
	} else if config.CLIConfig.UsePOST {
		errorChannel <- gotelemetry.NewDebugError("Will perform a POST operation")
		submissionType = gotelemetry.BatchTypePOST
	} else {
		errorChannel <- gotelemetry.NewDebugError("Will perform a Rails-style HTTP PATCH operation")
	}

	apiToken, err := configFile.APIToken()

	if err != nil {
		errorChannel <- err
		completionChannel <- true

		return
	}

	credentials, err := gotelemetry.NewCredentials(apiToken, configFile.APIURL())

	if err != nil {
		errorChannel <- err
		completionChannel <- true

		return
	}

	credentials.SetDebugChannel(errorChannel)

	updates := map[string]interface{}{}

	err = json.Unmarshal(data, &updates)

	if err != nil {
		errorChannel <- err
		completionChannel <- true

		return
	}

	b := gotelemetry.Batch{}

	for tag, update := range updates {
		b.SetData(tag, update)
	}

	err = b.Publish(credentials, configFile.ChannelTag(), submissionType)

	if err != nil {
		errorChannel <- err
	}

	errorChannel <- gotelemetry.NewLogError("Processing complete. Exiting.")

	completionChannel <- true
}
Exemple #4
0
func main() {
	keyPtr := flag.String("api-key", "", "Telemetry API Key")
	boardNamePtr := flag.String("n", "", "Name of board to retrieve")
	boardIdPtr := flag.String("b", "", "ID of board to retrieve")

	flag.Parse()

	key := strings.TrimSpace(*keyPtr)
	boardName := strings.TrimSpace(*boardNamePtr)
	boardId := strings.TrimSpace(*boardIdPtr)

	if key == "" {
		log.Fatal("Missing API Key.")
	}

	credentials, err := gotelemetry.NewCredentials(key)

	if err != nil {
		log.Fatalf("Error reported by the Telemetry API while creating a set of credentials: %s", err.Error())
	}

	if boardName == "" && boardId == "" {
		log.Fatal("You must specify either a board ID or a board name")
	}

	if boardName != "" && boardId != "" {
		log.Fatal("You must specify *either* a board ID or a board name")
	}

	var board *gotelemetry.Board

	if boardName != "" {
		board, err = gotelemetry.GetBoardByName(credentials, boardName)

		if err != nil {
			log.Fatalf("Error reported by the Telemetry API while requesting a board by name: %s", err.Error())
		}

		boardId = board.Id
	}

	board, err = gotelemetry.GetBoard(credentials, boardId)

	if err != nil {
		log.Fatalf("Error reported by the Telemetry API while requesting a board: %s", err.Error())
	}

	exportedBoard, err := board.Export()

	if err != nil {
		log.Fatalf("Error reported by the Telemetry API while exporting a board: %s", err.Error())
	}

	result, err := json.Marshal(exportedBoard)

	if err != nil {
		log.Fatalf("Error while converting a board to JSON: %s", err.Error())
	}

	fmt.Print(string(result))
}
func NewJobManager(jobConfig config.ConfigInterface, errorChannel chan error, completionChannel chan bool) (*JobManager, error) {
	result := &JobManager{
		jobs:                 map[string]*Job{},
		completionChannel:    completionChannel,
		jobCompletionChannel: make(chan string),
	}

	apiToken, err := jobConfig.APIToken()

	if err != nil {
		return nil, err
	}

	credentials, err := gotelemetry.NewCredentials(apiToken, jobConfig.APIURL())

	if err != nil {
		return nil, err
	}

	credentials.SetDebugChannel(errorChannel)

	result.credentials = credentials

	submissionInterval := jobConfig.SubmissionInterval()

	if submissionInterval < time.Second {
		errorChannel <- gotelemetry.NewLogError("Submission interval automatically set to 1s. You can change this value by adding a `submission_interval` property to your configuration file.")
		submissionInterval = time.Second
	} else {
		errorChannel <- gotelemetry.NewLogError("Submission interval set to %ds", submissionInterval/time.Second)
	}

	result.accountStreams = map[string]*gotelemetry.BatchStream{}

	for _, jobDescription := range jobConfig.Jobs() {
		jobId := jobDescription.ID()

		if jobId == "" {
			return nil, gotelemetry.NewError(500, "Job ID missing and no `flow_tag` provided.")
		}

		if !config.CLIConfig.Filter.MatchString(jobId) {
			continue
		}

		if config.CLIConfig.ForceRunOnce {
			delete(jobDescription, "refresh")
		}

		channelTag := jobDescription.ChannelTag()

		accountStream, ok := result.accountStreams[channelTag]

		if !ok {
			var err error

			accountStream, err = gotelemetry.NewBatchStream(credentials, channelTag, submissionInterval, errorChannel)

			if err != nil {
				return nil, err
			}

			result.accountStreams[channelTag] = accountStream
		}

		job, err := createJob(result, credentials, accountStream, errorChannel, jobDescription, result.jobCompletionChannel, false)

		if err != nil {
			return nil, err
		}

		if err := result.addJob(job); err != nil {
			return nil, err
		}
	}

	if len(result.jobs) == 0 {
		errorChannel <- gotelemetry.NewLogError("No jobs are being scheduled.")
		return nil, nil
	}

	go result.monitorDoneChannel()

	return result, nil
}