1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 /*  Fluent Bit
4  *  ==========
5  *  Copyright (C) 2019-2021 The Fluent Bit Authors
6  *  Copyright (C) 2015-2018 Treasure Data Inc.
7  *
8  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  you may not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  */
20 
21 #include <fluent-bit/flb_output_plugin.h>
22 #include "td_config.h"
23 #include <stdlib.h>
24 
25 struct flb_td *td_config_init(struct flb_output_instance *ins)
26 {
27     const char *tmp;
28     const char *api;
29     const char *db_name;
30     const char *db_table;
31     struct flb_td *ctx;
32 
33     /* Validate TD section keys */
34     api = flb_output_get_property("API", ins);
35     db_name = flb_output_get_property("Database", ins);
36     db_table = flb_output_get_property("Table", ins);
37 
38     if (!api) {
39         flb_plg_error(ins, "error reading API key value");
40         return NULL;
41     }
42 
43     if (!db_name) {
44         flb_plg_error(ins, "error reading Database name");
45         return NULL;
46     }
47 
48     if (!db_table) {
49         flb_plg_error(ins, "error reading Table name");
50         return NULL;
51     }
52 
53     /* Allocate context */
54     ctx = flb_calloc(1, sizeof(struct flb_td));
55     if (!ctx) {
56         flb_errno();
57         return NULL;
58     }
59     ctx->ins      = ins;
60     ctx->fd       = -1;
61     ctx->api      = api;
62     ctx->db_name  = db_name;
63     ctx->db_table = db_table;
64 
65     /* Lookup desired region */
66     tmp = flb_output_get_property("region", ins);
67     if (tmp) {
68         if (strcasecmp(tmp, "us") == 0) {
69             ctx->region = FLB_TD_REGION_US;
70         }
71         else if (strcasecmp(tmp, "jp") == 0) {
72             ctx->region = FLB_TD_REGION_JP;
73         }
74         else {
75             flb_plg_error(ctx->ins, "invalid region in configuration");
76             flb_free(ctx);
77             return NULL;
78         }
79     }
80     else {
81         ctx->region = FLB_TD_REGION_US;
82     }
83 
84     flb_plg_info(ctx->ins, "Treasure Data / database='%s' table='%s'",
85                  ctx->db_name, ctx->db_table);
86 
87     return ctx;
88 }
89