Google Cloud C++ Client  1.32.1
C++ Client Library for Google Cloud Platform
kms_key_name.cc
Go to the documentation of this file.
1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "google/cloud/kms_key_name.h"
16 #include <ostream>
17 #include <regex>
18 
19 namespace google {
20 namespace cloud {
21 inline namespace GOOGLE_CLOUD_CPP_NS {
22 
23 KmsKeyName::KmsKeyName(std::string const& project_id,
24  std::string const& location, std::string const& key_ring,
25  std::string const& kms_key_name)
26  : full_name_("projects/" + project_id + "/locations/" + location +
27  "/keyRings/" + key_ring + "/cryptoKeys/" + kms_key_name) {}
28 
29 bool operator==(KmsKeyName const& a, KmsKeyName const& b) {
30  return a.full_name_ == b.full_name_;
31 }
32 
33 std::ostream& operator<<(std::ostream& os, KmsKeyName const& key) {
34  return os << key.FullName();
35 }
36 
37 StatusOr<KmsKeyName> MakeKmsKeyName(std::string const& full_name) {
38  std::regex re(
39  "projects/([^/]+)/locations/([^/]+)/keyRings/([^/]+)/cryptoKeys/([^/]+)");
40  std::smatch matches;
41  if (!std::regex_match(full_name, matches, re)) {
43  "Improperly formatted KmsKeyName: " + full_name);
44  }
45  return KmsKeyName(std::move(matches[1]), std::move(matches[2]),
46  std::move(matches[3]), std::move(matches[4]));
47 }
48 
49 } // namespace GOOGLE_CLOUD_CPP_NS
50 } // namespace cloud
51 } // namespace google