2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file src/cyad/CommandsDispatcher.cpp
18 * @author Aleksander Zdyb <a.zdyb@samsung.com>
20 * @brief CommandsDispatcher class (implementation)
23 #include <cynara-error.h>
24 #include <cynara-policy-types.h>
26 #include <cyad/AdminLibraryInitializationFailedException.h>
28 #include "CommandsDispatcher.h"
32 CommandsDispatcher::CommandsDispatcher(BaseDispatcherIO &io, BaseAdminApiWrapper &adminApiWrapper)
33 : m_io(io), m_adminApiWrapper(adminApiWrapper), m_cynaraAdmin(nullptr)
35 auto ret = m_adminApiWrapper.cynara_admin_initialize(&m_cynaraAdmin);
36 if (ret != CYNARA_API_SUCCESS)
37 throw AdminLibraryInitializationFailedException(ret);
40 CommandsDispatcher::~CommandsDispatcher() {
41 m_adminApiWrapper.cynara_admin_finish(m_cynaraAdmin);
44 int CommandsDispatcher::execute(CyadCommand &) {
45 m_io.cout() << "Whatever you wanted, it's not implemented" << std::endl;
46 return CYNARA_API_UNKNOWN_ERROR;
49 int CommandsDispatcher::execute(HelpCyadCommand &) {
50 m_io.cout() << helpMessage << std::endl;
51 return CYNARA_API_SUCCESS;
54 int CommandsDispatcher::execute(ErrorCyadCommand &result) {
55 m_io.cout() << "There was an error in command-line options:" << std::endl;
56 m_io.cout() << result.message() << std::endl;
58 m_io.cout() << std::endl << helpMessage << std::endl;
59 return CYNARA_API_INVALID_COMMANDLINE_PARAM;
62 int CommandsDispatcher::execute(DeleteBucketCyadCommand &result) {
63 return m_adminApiWrapper.cynara_admin_set_bucket(m_cynaraAdmin, result.bucketId().c_str(),
64 CYNARA_ADMIN_DELETE, nullptr);
67 int CommandsDispatcher::execute(SetBucketCyadCommand &result) {
68 const auto &policyResult = result.policyResult();
69 const char *metadata = policyResult.metadata().empty() ? nullptr
70 : policyResult.metadata().c_str();
71 return m_adminApiWrapper.cynara_admin_set_bucket(m_cynaraAdmin,
72 result.bucketId().c_str(),
73 policyResult.policyType(), metadata);
76 } /* namespace Cynara */