summaryrefslogtreecommitdiffstats
path: root/src/rgw/driver/json_config/store.cc
blob: cf5adda2512c20fd5b87ac2855b58fa89113b255 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// vim: ts=8 sw=2 smarttab ft=cpp

/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2022 Red Hat, Inc.
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation. See file COPYING.
 *
 */

#include <system_error>
#include "include/buffer.h"
#include "common/errno.h"
#include "common/ceph_json.h"
#include "rgw_zone.h"
#include "driver/immutable_config/store.h"
#include "store.h"

namespace rgw::sal {

namespace {

struct DecodedConfig {
  RGWZoneGroup zonegroup;
  RGWZoneParams zone;
  RGWPeriodConfig period_config;

  void decode_json(JSONObj *obj)
  {
    JSONDecoder::decode_json("zonegroup", zonegroup, obj);
    JSONDecoder::decode_json("zone", zone, obj);
    JSONDecoder::decode_json("period_config", period_config, obj);
  }
};

static void parse_config(const DoutPrefixProvider* dpp, const char* filename)
{
  bufferlist bl;
  std::string errmsg;
  int r = bl.read_file(filename, &errmsg);
  if (r < 0) {
    ldpp_dout(dpp, 0) << "failed to read json config file '" << filename
        << "': " << errmsg << dendl;
    throw std::system_error(-r, std::system_category());
  }

  JSONParser p;
  if (!p.parse(bl.c_str(), bl.length())) {
    ldpp_dout(dpp, 0) << "failed to parse json config file" << dendl;
    throw std::system_error(make_error_code(std::errc::invalid_argument));
  }

  DecodedConfig config;
  try {
    decode_json_obj(config, &p);
  } catch (const JSONDecoder::err& e) {
    ldpp_dout(dpp, 0) << "failed to decode JSON input: " << e.what() << dendl;
    throw std::system_error(make_error_code(std::errc::invalid_argument));
  }
}

void sanity_check_config(const DoutPrefixProvider* dpp, DecodedConfig& config)
{
  if (config.zonegroup.id.empty()) {
    config.zonegroup.id = "default";
  }
  if (config.zonegroup.name.empty()) {
    config.zonegroup.name = "default";
  }
  if (config.zonegroup.api_name.empty()) {
    config.zonegroup.api_name = config.zonegroup.name;
  }

  if (config.zone.id.empty()) {
    config.zone.id = "default";
  }
  if (config.zone.name.empty()) {
    config.zone.name = "default";
  }

  // add default placement if it doesn't exist
  rgw_pool pool;
  RGWZonePlacementInfo placement;
  placement.storage_classes.set_storage_class(
      RGW_STORAGE_CLASS_STANDARD, &pool, nullptr);
  config.zone.placement_pools.emplace("default-placement",
                                      std::move(placement));

  std::set<rgw_pool> pools;
  int r = rgw::init_zone_pool_names(dpp, null_yield, pools, config.zone);
  if (r < 0) {
    ldpp_dout(dpp, 0) << "failed to set default zone pool names" << dendl;
    throw std::system_error(-r, std::system_category());
  }

  // verify that config.zonegroup only contains config.zone
  if (config.zonegroup.zones.size() > 1) {
    ldpp_dout(dpp, 0) << "zonegroup cannot contain multiple zones" << dendl;
    throw std::system_error(make_error_code(std::errc::invalid_argument));
  }

  if (config.zonegroup.zones.size() == 1) {
    auto z = config.zonegroup.zones.begin();
    if (z->first != config.zone.id) {
      ldpp_dout(dpp, 0) << "zonegroup contains unknown zone id="
          << z->first << dendl;
      throw std::system_error(make_error_code(std::errc::invalid_argument));
    }
    if (z->second.id != config.zone.id) {
      ldpp_dout(dpp, 0) << "zonegroup contains unknown zone id="
          << z->second.id << dendl;
      throw std::system_error(make_error_code(std::errc::invalid_argument));
    }
    if (z->second.name != config.zone.name) {
      ldpp_dout(dpp, 0) << "zonegroup contains unknown zone name="
          << z->second.name << dendl;
      throw std::system_error(make_error_code(std::errc::invalid_argument));
    }
    if (config.zonegroup.master_zone != config.zone.id) {
      ldpp_dout(dpp, 0) << "zonegroup contains unknown master_zone="
          << config.zonegroup.master_zone << dendl;
      throw std::system_error(make_error_code(std::errc::invalid_argument));
    }
  } else {
    // add the zone to the group
    const bool is_master = true;
    const bool read_only = false;
    std::list<std::string> endpoints;
    std::list<std::string> sync_from;
    std::list<std::string> sync_from_rm;
    rgw::zone_features::set enable_features;
    rgw::zone_features::set disable_features;

    enable_features.insert(rgw::zone_features::supported.begin(),
                           rgw::zone_features::supported.end());

    int r = rgw::add_zone_to_group(dpp, config.zonegroup, config.zone,
                                   &is_master, &read_only, endpoints,
                                   nullptr, nullptr, sync_from, sync_from_rm,
                                   nullptr, std::nullopt,
                                   enable_features, disable_features);
    if (r < 0) {
      ldpp_dout(dpp, 0) << "failed to add zone to zonegroup: "
          << cpp_strerror(r) << dendl;
      throw std::system_error(-r, std::system_category());
    }

    config.zonegroup.enabled_features.insert(rgw::zone_features::enabled.begin(),
                                             rgw::zone_features::enabled.end());
  }

  // insert the default placement target if it doesn't exist
  auto target = RGWZoneGroupPlacementTarget{.name = "default-placement"};
  config.zonegroup.placement_targets.emplace(target.name, target);
  if (config.zonegroup.default_placement.name.empty()) {
    config.zonegroup.default_placement.name = target.name;
  }
}

} // anonymous namespace

auto create_json_config_store(const DoutPrefixProvider* dpp,
                              const std::string& filename)
    -> std::unique_ptr<ConfigStore>
{
  DecodedConfig config;
  parse_config(dpp, filename.c_str());
  sanity_check_config(dpp, config);
  return create_immutable_config_store(dpp, config.zonegroup, config.zone,
                                       config.period_config);
}

} // namespace rgw::sal