Apache Mesos
utils.hpp
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __CSI_UTILS_HPP__
18 #define __CSI_UTILS_HPP__
19 
20 #include <ostream>
21 #include <type_traits>
22 
23 #include <csi/spec.hpp>
24 
25 #include <google/protobuf/map.h>
26 
27 #include <google/protobuf/util/json_util.h>
28 
29 #include <mesos/mesos.hpp>
30 
31 #include <stout/foreach.hpp>
32 #include <stout/try.hpp>
33 #include <stout/unreachable.hpp>
34 
35 #include "csi/state.hpp"
36 
37 namespace csi {
38 namespace v0 {
39 
40 bool operator==(
41  const ControllerServiceCapability& left,
42  const ControllerServiceCapability& right);
43 
44 
45 bool operator==(const VolumeCapability& left, const VolumeCapability& right);
46 
47 
48 std::ostream& operator<<(
49  std::ostream& stream,
51 
52 
53 // Default imprementation for output protobuf messages in namespace
54 // `csi`. Note that any non-template overloading of the output operator
55 // would take precedence over this function template.
56 template <
57  typename Message,
58  typename std::enable_if<std::is_convertible<
59  Message*, google::protobuf::Message*>::value, int>::type = 0>
60 std::ostream& operator<<(std::ostream& stream, const Message& message)
61 {
62  // NOTE: We use Google's JSON utility functions for proto3.
63  std::string output;
64  google::protobuf::util::MessageToJsonString(message, &output);
65  return stream << output;
66 }
67 
68 } // namespace v0 {
69 } // namespace csi {
70 
71 
72 namespace mesos {
73 namespace csi {
74 namespace v0 {
75 
77 {
78  PluginCapabilities() = default;
79 
80  template <typename Iterable> PluginCapabilities(const Iterable& capabilities)
81  {
82  foreach (const auto& capability, capabilities) {
83  if (capability.has_service() &&
84  PluginCapability::Service::Type_IsValid(
85  capability.service().type())) {
86  switch (capability.service().type()) {
88  break;
89  case PluginCapability::Service::CONTROLLER_SERVICE:
90  controllerService = true;
91  break;
92  case google::protobuf::kint32min:
93  case google::protobuf::kint32max:
94  UNREACHABLE();
95  }
96  }
97  }
98  }
99 
100  bool controllerService = false;
101 };
102 
103 
105 {
106  ControllerCapabilities() = default;
107 
108  template <typename Iterable>
109  ControllerCapabilities(const Iterable& capabilities)
110  {
111  foreach (const auto& capability, capabilities) {
112  if (capability.has_rpc() &&
113  ControllerServiceCapability::RPC::Type_IsValid(
114  capability.rpc().type())) {
115  switch (capability.rpc().type()) {
117  break;
118  case ControllerServiceCapability::RPC::CREATE_DELETE_VOLUME:
119  createDeleteVolume = true;
120  break;
121  case ControllerServiceCapability::RPC::PUBLISH_UNPUBLISH_VOLUME:
122  publishUnpublishVolume = true;
123  break;
124  case ControllerServiceCapability::RPC::LIST_VOLUMES:
125  listVolumes = true;
126  break;
127  case ControllerServiceCapability::RPC::GET_CAPACITY:
128  getCapacity = true;
129  break;
130  case google::protobuf::kint32min:
131  case google::protobuf::kint32max:
132  UNREACHABLE();
133  }
134  }
135  }
136  }
137 
138  bool createDeleteVolume = false;
139  bool publishUnpublishVolume = false;
140  bool listVolumes = false;
141  bool getCapacity = false;
142 };
143 
144 
146 {
147  NodeCapabilities() = default;
148 
149  template <typename Iterable> NodeCapabilities(const Iterable& capabilities)
150  {
151  foreach (const auto& capability, capabilities) {
152  if (capability.has_rpc() &&
153  NodeServiceCapability::RPC::Type_IsValid(capability.rpc().type())) {
154  switch (capability.rpc().type()) {
156  break;
157  case NodeServiceCapability::RPC::STAGE_UNSTAGE_VOLUME:
158  stageUnstageVolume = true;
159  break;
160  case google::protobuf::kint32min:
161  case google::protobuf::kint32max:
162  UNREACHABLE();
163  }
164  }
165  }
166  }
167 
168  bool stageUnstageVolume = false;
169 };
170 
171 } // namespace v0 {
172 } // namespace csi {
173 } // namespace mesos {
174 
175 #endif // __CSI_UTILS_HPP__
Definition: utils.hpp:37
std::ostream & operator<<(std::ostream &stream, const ControllerServiceCapability::RPC::Type &type)
Definition: utils.hpp:76
Definition: spec.hpp:30
const int UNKNOWN
Definition: diagnosis.hpp:39
#define UNREACHABLE()
Definition: unreachable.hpp:22
Type
Definition: capabilities.hpp:79
ControllerCapabilities(const Iterable &capabilities)
Definition: utils.hpp:109
bool operator==(const ControllerServiceCapability &left, const ControllerServiceCapability &right)
Try< uint32_t > type(const std::string &path)
Definition: utils.hpp:145
PluginCapabilities(const Iterable &capabilities)
Definition: utils.hpp:80
NodeCapabilities(const Iterable &capabilities)
Definition: utils.hpp:149