softwarecontainer  0.18.0-739e8d7 2017-05-04
envgatewayparser.cpp
1 /*
2  * Copyright (C) 2016-2017 Pelagicore AB
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
9  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
11  * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
12  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
13  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
14  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  *
17  * For further information see LICENSE
18  */
19 
20 #include "envgatewayparser.h"
21 
22 namespace softwarecontainer {
23 
24 bool EnvironmentGatewayParser::parseEnvironmentGatewayConfigElement(
25  const json_t *element,
26  EnvironmentVariable &result,
27  const EnvironmentVariables &store)
28 {
29 
30  if (!requireNonEmptyKeyValue(element, "name", result.first)
31  || !requireNonEmptyKeyValue(element, "value", result.second)) {
32  return false;
33  }
34 
35  std::string mode = "set";
36  std::string separator = "";
37 
38  if (!JSONParser::readOptional(element, "mode", mode)) {
39  log_error() << "Could not parse \"mode\" key";
40  return false;
41  }
42 
43  // Convert to lowercase
44  std::transform(mode.begin(), mode.end(), mode.begin(), ::tolower);
45 
46  std::vector<std::string> validModes = { "set", "prepend", "append" };
47  if (validModes.end() == std::find(validModes.begin(), validModes.end(), mode)) {
48  log_error() << "Invalid mode, only " << validModes << " are valid";
49  return false;
50  }
51 
52  if (!JSONParser::readOptional(element, "separator", separator)) {
53  log_error() << "Could not parse \"separator\" key";
54  return false;
55  }
56 
57  if (store.count(result.first) == 0) {
58  if (mode != "set") {
59  log_info() << "Env variable \"" << result.first << "\" was configured to "
60  << "be appended/prepended but the variable has not previously"
61  << " been set, so it will be created. Value is set to: \""
62  << result.second << "\"";
63  }
64  return true;
65  } else {
66  if (mode == "append") {
67  result.second = store.at(result.first) + separator + result.second;
68  return true;
69  } else if (mode == "prepend") {
70  result.second = result.second + separator + store.at(result.first);
71  return true;
72  } else { // mode == set
73  log_error() << "Env variable " << result.first
74  << " already defined with value : " << store.at(result.first);
75  return false;
76  }
77  }
78 }
79 
80 bool EnvironmentGatewayParser::requireNonEmptyKeyValue(const json_t *element,
81  const std::string key,
82  std::string &result)
83 {
84  if (!JSONParser::read(element, key.c_str(), result)) {
85  log_error() << "Key " << key << " missing or not a string in json configuration";
86  return false;
87  }
88 
89  if (result.length() == 0) {
90  log_error() << "Value for " << key << " key is an empty string";
91  return false;
92  }
93 
94  return true;
95 }
96 
97 } // namespace softwarecontainer
static bool readOptional(const json_t *element, const char *key, std::string &result)
Reads an optional value from a JSON object.
Definition: jsonparser.cpp:24
Developers guide to adding a config item:
static bool read(const json_t *element, const char *key, std::string &result)
Reads a string from a JSON Object.
Definition: jsonparser.cpp:51