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 <google/protobuf/map.h>
24 
25 #include <google/protobuf/util/json_util.h>
26 
27 #include <mesos/mesos.hpp>
28 
29 #include <stout/foreach.hpp>
30 #include <stout/try.hpp>
31 #include <stout/unreachable.hpp>
32 
33 #include "csi/spec.hpp"
34 #include "csi/state.hpp"
35 
36 namespace csi {
37 
38 bool operator==(
39  const ControllerServiceCapability& left,
40  const ControllerServiceCapability& right);
41 
42 
43 bool operator==(const Version& left, const Version& right);
44 
45 
46 bool operator==(const VolumeCapability& left, const VolumeCapability& right);
47 
48 
49 bool operator!=(const Version& left, const Version& right);
50 
51 
52 std::ostream& operator<<(
53  std::ostream& stream,
55 
56 
57 std::ostream& operator<<(std::ostream& stream, const Version& version);
58 
59 
60 // Default imprementation for output protobuf messages in namespace
61 // `csi`. Note that any non-template overloading of the output operator
62 // would take precedence over this function template.
63 template <
64  typename Message,
65  typename std::enable_if<std::is_convertible<
66  Message*, google::protobuf::Message*>::value, int>::type = 0>
67 std::ostream& operator<<(std::ostream& stream, const Message& message)
68 {
69  // NOTE: We use Google's JSON utility functions for proto3.
70  std::string output;
71  google::protobuf::util::MessageToJsonString(message, &output);
72  return stream << output;
73 }
74 
75 } // namespace csi {
76 
77 
78 namespace mesos {
79 namespace csi {
80 
82 {
83  ControllerCapabilities() = default;
84 
85  template <typename Iterable>
86  ControllerCapabilities(const Iterable& capabilities)
87  {
88  foreach (const auto& capability, capabilities) {
89  if (capability.has_rpc() &&
90  ControllerServiceCapability::RPC::Type_IsValid(
91  capability.rpc().type())) {
92  switch(capability.rpc().type()) {
94  break;
95  case ControllerServiceCapability::RPC::CREATE_DELETE_VOLUME:
96  createDeleteVolume = true;
97  break;
98  case ControllerServiceCapability::RPC::PUBLISH_UNPUBLISH_VOLUME:
100  break;
101  case ControllerServiceCapability::RPC::LIST_VOLUMES:
102  listVolumes = true;
103  break;
104  case ControllerServiceCapability::RPC::GET_CAPACITY:
105  getCapacity = true;
106  break;
107  case google::protobuf::kint32min:
108  case google::protobuf::kint32max:
109  UNREACHABLE();
110  }
111  }
112  }
113  }
114 
115  bool createDeleteVolume = false;
117  bool listVolumes = false;
118  bool getCapacity = false;
119 };
120 
121 
122 namespace state {
123 
124 std::ostream& operator<<(std::ostream& stream, const VolumeState::State& state);
125 
126 } // namespace state {
127 } // namespace csi {
128 } // namespace mesos {
129 
130 #endif // __CSI_UTILS_HPP__
bool operator==(const ControllerServiceCapability &left, const ControllerServiceCapability &right)
ControllerCapabilities(const Iterable &capabilities)
Definition: utils.hpp:86
bool listVolumes
Definition: utils.hpp:117
Definition: utils.hpp:81
std::ostream & operator<<(std::ostream &stream, const VolumeState::State &state)
bool createDeleteVolume
Definition: utils.hpp:115
bool operator!=(const Version &left, const Version &right)
process::Future< Version > version()
Definition: version.hpp:32
const int UNKNOWN
Definition: diagnosis.hpp:39
#define UNREACHABLE()
Definition: unreachable.hpp:22
std::ostream & operator<<(std::ostream &stream, const ControllerServiceCapability::RPC::Type &type)
Type
Definition: capabilities.hpp:79
bool publishUnpublishVolume
Definition: utils.hpp:116
Definition: version.hpp:41
Try< uint32_t > type(const std::string &path)
bool getCapacity
Definition: utils.hpp:118