b8e256cabd651a76a9594ca211b73e454769e38a
[platform/core/security/cynara.git] / src / cyad / CommandsDispatcher.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file        src/cyad/CommandsDispatcher.cpp
18  * @author      Aleksander Zdyb <a.zdyb@samsung.com>
19  * @version     1.0
20  * @brief       CommandsDispatcher class (implementation)
21  */
22
23 #include <cynara-error.h>
24 #include <cynara-policy-types.h>
25
26 #include <cyad/AdminLibraryInitializationFailedException.h>
27
28 #include "CommandsDispatcher.h"
29
30 namespace Cynara {
31
32 CommandsDispatcher::CommandsDispatcher(BaseDispatcherIO &io, BaseAdminApiWrapper &adminApiWrapper)
33     : m_io(io), m_adminApiWrapper(adminApiWrapper), m_cynaraAdmin(nullptr)
34 {
35     auto ret = m_adminApiWrapper.cynara_admin_initialize(&m_cynaraAdmin);
36     if (ret != CYNARA_API_SUCCESS)
37         throw AdminLibraryInitializationFailedException(ret);
38 }
39
40 CommandsDispatcher::~CommandsDispatcher() {
41     m_adminApiWrapper.cynara_admin_finish(m_cynaraAdmin);
42 }
43
44 int CommandsDispatcher::execute(CyadCommand &) {
45     m_io.cout() << "Whatever you wanted, it's not implemented" << std::endl;
46     return CYNARA_API_UNKNOWN_ERROR;
47 }
48
49 int CommandsDispatcher::execute(HelpCyadCommand &) {
50     m_io.cout() << helpMessage << std::endl;
51     return CYNARA_API_SUCCESS;
52 }
53
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;
57
58     m_io.cout() << std::endl << helpMessage << std::endl;
59     return CYNARA_API_INVALID_COMMANDLINE_PARAM;
60 }
61
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);
65 }
66
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);
74 }
75
76 } /* namespace Cynara */