1 // Copyright 2019 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 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_HMAC_KEY_REQUESTS_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_HMAC_KEY_REQUESTS_H
17 
18 #include "google/cloud/storage/hmac_key_metadata.h"
19 #include "google/cloud/storage/internal/generic_request.h"
20 #include "google/cloud/storage/internal/http_response.h"
21 #include "google/cloud/storage/override_default_project.h"
22 #include "google/cloud/storage/version.h"
23 #include <nlohmann/json.hpp>
24 #include <iosfwd>
25 
26 namespace google {
27 namespace cloud {
28 namespace storage {
29 inline namespace STORAGE_CLIENT_NS {
30 namespace internal {
31 
32 struct HmacKeyMetadataParser {
33   static StatusOr<HmacKeyMetadata> FromJson(nlohmann::json const& json);
34   static StatusOr<HmacKeyMetadata> FromString(std::string const& payload);
35 };
36 
37 template <typename Derived, typename... Options>
38 class GenericHmacKeyRequest : public GenericRequest<Derived, Options...> {
39  public:
40   GenericHmacKeyRequest() = default;
GenericHmacKeyRequest(std::string project_id)41   explicit GenericHmacKeyRequest(std::string project_id)
42       : project_id_(std::move(project_id)) {}
43 
project_id()44   std::string const& project_id() const { return project_id_; }
45 
46   //@{
47   /**
48    * @name Handle request options.
49    *
50    * Modify the default implementation from GenericRequest. We want to set the
51    * `project_id_` member variable when the option is `OverrideDefaultProject`.
52    */
53   template <typename H, typename... T>
set_multiple_options(H && h,T &&...tail)54   Derived& set_multiple_options(H&& h, T&&... tail) {
55     set_option(std::forward<H>(h));
56     return set_multiple_options(std::forward<T>(tail)...);
57   }
58 
set_multiple_options()59   Derived& set_multiple_options() { return *static_cast<Derived*>(this); }
60 
61   using GenericRequest<Derived, Options...>::set_option;
set_option(OverrideDefaultProject const & o)62   void set_option(OverrideDefaultProject const& o) {
63     if (o.has_value()) {
64       project_id_ = o.value();
65     }
66   }
67 
68  private:
69   std::string project_id_;
70 };
71 
72 /// Represents a request to create a call the `HmacKeys: insert` API.
73 class CreateHmacKeyRequest
74     : public GenericHmacKeyRequest<CreateHmacKeyRequest> {
75  public:
76   CreateHmacKeyRequest() = default;
CreateHmacKeyRequest(std::string project_id,std::string service_account)77   CreateHmacKeyRequest(std::string project_id, std::string service_account)
78       : GenericHmacKeyRequest(std::move(project_id)),
79         service_account_(std::move(service_account)) {}
80 
service_account()81   std::string const& service_account() const { return service_account_; }
82 
83  private:
84   std::string service_account_;
85 };
86 
87 std::ostream& operator<<(std::ostream& os, CreateHmacKeyRequest const& r);
88 
89 /// The response from a `HmacKeys: insert` API.
90 struct CreateHmacKeyResponse {
91   static StatusOr<CreateHmacKeyResponse> FromHttpResponse(
92       std::string const& payload);
93 
94   std::string kind;
95   HmacKeyMetadata metadata;
96   std::string secret;
97 };
98 
99 std::ostream& operator<<(std::ostream& os, CreateHmacKeyResponse const& r);
100 
101 /// Represents a request to call the `HmacKeys: list` API.
102 class ListHmacKeysRequest
103     : public GenericHmacKeyRequest<ListHmacKeysRequest, Deleted, MaxResults,
104                                    ServiceAccountFilter, UserProject> {
105  public:
ListHmacKeysRequest(std::string project_id)106   explicit ListHmacKeysRequest(std::string project_id)
107       : GenericHmacKeyRequest(std::move(project_id)) {}
108 
page_token()109   std::string const& page_token() const { return page_token_; }
set_page_token(std::string page_token)110   ListHmacKeysRequest& set_page_token(std::string page_token) {
111     page_token_ = std::move(page_token);
112     return *this;
113   }
114 
115  private:
116   std::string page_token_;
117 };
118 
119 std::ostream& operator<<(std::ostream& os, ListHmacKeysRequest const& r);
120 
121 /// Represents a response to the `HmacKeys: list` API.
122 struct ListHmacKeysResponse {
123   static StatusOr<ListHmacKeysResponse> FromHttpResponse(
124       std::string const& payload);
125 
126   std::string next_page_token;
127   std::vector<HmacKeyMetadata> items;
128 };
129 
130 std::ostream& operator<<(std::ostream& os, ListHmacKeysResponse const& r);
131 
132 /// Represents a request to call the `HmacKeys: delete` API.
133 class DeleteHmacKeyRequest
134     : public GenericHmacKeyRequest<DeleteHmacKeyRequest> {
135  public:
DeleteHmacKeyRequest(std::string project_id,std::string access_id)136   explicit DeleteHmacKeyRequest(std::string project_id, std::string access_id)
137       : GenericHmacKeyRequest(std::move(project_id)),
138         access_id_(std::move(access_id)) {}
139 
access_id()140   std::string const& access_id() const { return access_id_; }
141 
142  private:
143   std::string access_id_;
144 };
145 
146 std::ostream& operator<<(std::ostream& os, DeleteHmacKeyRequest const& r);
147 
148 /// Represents a request to call the `HmacKeys: get` API.
149 class GetHmacKeyRequest : public GenericHmacKeyRequest<GetHmacKeyRequest> {
150  public:
GetHmacKeyRequest(std::string project_id,std::string access_id)151   explicit GetHmacKeyRequest(std::string project_id, std::string access_id)
152       : GenericHmacKeyRequest(std::move(project_id)),
153         access_id_(std::move(access_id)) {}
154 
access_id()155   std::string const& access_id() const { return access_id_; }
156 
157  private:
158   std::string access_id_;
159 };
160 
161 std::ostream& operator<<(std::ostream& os, GetHmacKeyRequest const& r);
162 
163 /// Represents a request to call the `HmacKeys: update` API.
164 class UpdateHmacKeyRequest
165     : public GenericHmacKeyRequest<UpdateHmacKeyRequest> {
166  public:
UpdateHmacKeyRequest(std::string project_id,std::string access_id,HmacKeyMetadata resource)167   explicit UpdateHmacKeyRequest(std::string project_id, std::string access_id,
168                                 HmacKeyMetadata resource)
169       : GenericHmacKeyRequest(std::move(project_id)),
170         access_id_(std::move(access_id)),
171         resource_(std::move(resource)) {}
172 
access_id()173   std::string const& access_id() const { return access_id_; }
resource()174   HmacKeyMetadata const& resource() const { return resource_; }
175 
176  private:
177   std::string access_id_;
178   HmacKeyMetadata resource_;
179 };
180 
181 std::ostream& operator<<(std::ostream& os, UpdateHmacKeyRequest const& r);
182 
183 }  // namespace internal
184 }  // namespace STORAGE_CLIENT_NS
185 }  // namespace storage
186 }  // namespace cloud
187 }  // namespace google
188 
189 #endif  // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_HMAC_KEY_REQUESTS_H
190