|
| 1 | +# |
| 2 | +# Copyright (c) 2017-2021, The Storage Networking Industry Association. |
| 3 | +# |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted provided that the following conditions are met: |
| 6 | +# |
| 7 | +# Redistributions of source code must retain the above copyright notice, |
| 8 | +# this list of conditions and the following disclaimer. |
| 9 | +# |
| 10 | +# Redistributions in binary form must reproduce the above copyright notice, |
| 11 | +# this list of conditions and the following disclaimer in the documentation |
| 12 | +# and/or other materials provided with the distribution. |
| 13 | +# |
| 14 | +# Neither the name of The Storage Networking Industry Association (SNIA) nor |
| 15 | +# the names of its contributors may be used to endorse or promote products |
| 16 | +# derived from this software without specific prior written permission. |
| 17 | +# |
| 18 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 22 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 28 | +# THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + |
| 30 | +# Resource implementation for - /redfish/v1/CompositionService/ResourceBlocks/{ResourceBlockId}/Processors/{ProcessorId}/AccelerationFunctions/{AccelerationFunctionId} |
| 31 | +# Program name - AccelerationFunction1_api.py |
| 32 | + |
| 33 | +import g |
| 34 | +import json, os, random, string |
| 35 | +import traceback |
| 36 | +import logging |
| 37 | + |
| 38 | +from flask import Flask, request |
| 39 | +from flask_restful import Resource |
| 40 | +from .constants import * |
| 41 | +from api_emulator.utils import check_authentication, create_path, get_json_data, create_and_patch_object, delete_object, patch_object, put_object, create_collection |
| 42 | +from .templates.AccelerationFunction1 import get_AccelerationFunction1_instance |
| 43 | + |
| 44 | +members = [] |
| 45 | +member_ids = [] |
| 46 | +INTERNAL_ERROR = 500 |
| 47 | + |
| 48 | +# AccelerationFunction1 Collection API |
| 49 | +class AccelerationFunction1CollectionAPI(Resource): |
| 50 | + def __init__(self, **kwargs): |
| 51 | + logging.info('AccelerationFunction1 Collection init called') |
| 52 | + self.root = PATHS['Root'] |
| 53 | + self.auth = kwargs['auth'] |
| 54 | + |
| 55 | + # HTTP GET |
| 56 | + def get(self, ResourceBlockId, ProcessorId): |
| 57 | + logging.info('AccelerationFunction1 Collection get called') |
| 58 | + msg, code = check_authentication(self.auth) |
| 59 | + |
| 60 | + if code == 200: |
| 61 | + path = os.path.join(self.root, 'CompositionService/ResourceBlocks/{0}/Processors/{1}/AccelerationFunctions', 'index.json').format(ResourceBlockId, ProcessorId) |
| 62 | + return get_json_data(path) |
| 63 | + else: |
| 64 | + return msg, code |
| 65 | + |
| 66 | + # HTTP POST Collection |
| 67 | + def post(self, ResourceBlockId, ProcessorId): |
| 68 | + logging.info('AccelerationFunction1 Collection post called') |
| 69 | + msg, code = check_authentication(self.auth) |
| 70 | + |
| 71 | + if code == 200: |
| 72 | + if request.data: |
| 73 | + config = json.loads(request.data) |
| 74 | + if "@odata.type" in config: |
| 75 | + if "Collection" in config["@odata.type"]: |
| 76 | + return "Invalid data in POST body", 400 |
| 77 | + |
| 78 | + if ProcessorId in members: |
| 79 | + resp = 404 |
| 80 | + return resp |
| 81 | + path = create_path(self.root, 'CompositionService/ResourceBlocks/{0}/Processors/{1}/AccelerationFunctions').format(ResourceBlockId, ProcessorId) |
| 82 | + parent_path = os.path.dirname(path) |
| 83 | + if not os.path.exists(path): |
| 84 | + os.mkdir(path) |
| 85 | + create_collection (path, 'AccelerationFunction', parent_path) |
| 86 | + |
| 87 | + res = ''.join(random.choices(string.ascii_uppercase + string.digits, k=5)) |
| 88 | + if request.data: |
| 89 | + config = json.loads(request.data) |
| 90 | + if "@odata.id" in config: |
| 91 | + return AccelerationFunction1API.post(self, ResourceBlockId, ProcessorId, os.path.basename(config['@odata.id'])) |
| 92 | + else: |
| 93 | + return AccelerationFunction1API.post(self, ResourceBlockId, ProcessorId, str(res)) |
| 94 | + else: |
| 95 | + return AccelerationFunction1API.post(self, ResourceBlockId, ProcessorId, str(res)) |
| 96 | + else: |
| 97 | + return msg, code |
| 98 | + |
| 99 | +# AccelerationFunction1 API |
| 100 | +class AccelerationFunction1API(Resource): |
| 101 | + def __init__(self, **kwargs): |
| 102 | + logging.info('AccelerationFunction1 init called') |
| 103 | + self.root = PATHS['Root'] |
| 104 | + self.auth = kwargs['auth'] |
| 105 | + |
| 106 | + # HTTP GET |
| 107 | + def get(self, ResourceBlockId, ProcessorId, AccelerationFunctionId): |
| 108 | + logging.info('AccelerationFunction1 get called') |
| 109 | + msg, code = check_authentication(self.auth) |
| 110 | + |
| 111 | + if code == 200: |
| 112 | + path = create_path(self.root, 'CompositionService/ResourceBlocks/{0}/Processors/{1}/AccelerationFunctions/{2}', 'index.json').format(ResourceBlockId, ProcessorId, AccelerationFunctionId) |
| 113 | + return get_json_data (path) |
| 114 | + else: |
| 115 | + return msg, code |
| 116 | + |
| 117 | + # HTTP POST |
| 118 | + # - Create the resource (since URI variables are available) |
| 119 | + # - Update the members and members.id lists |
| 120 | + # - Attach the APIs of subordinate resources (do this only once) |
| 121 | + # - Finally, create an instance of the subordiante resources |
| 122 | + def post(self, ResourceBlockId, ProcessorId, AccelerationFunctionId): |
| 123 | + logging.info('AccelerationFunction1 post called') |
| 124 | + msg, code = check_authentication(self.auth) |
| 125 | + |
| 126 | + if code == 200: |
| 127 | + path = create_path(self.root, 'CompositionService/ResourceBlocks/{0}/Processors/{1}/AccelerationFunctions/{2}').format(ResourceBlockId, ProcessorId, AccelerationFunctionId) |
| 128 | + collection_path = os.path.join(self.root, 'CompositionService/ResourceBlocks/{0}/Processors/{1}/AccelerationFunctions', 'index.json').format(ResourceBlockId, ProcessorId) |
| 129 | + |
| 130 | + # Check if collection exists: |
| 131 | + if not os.path.exists(collection_path): |
| 132 | + AccelerationFunction1CollectionAPI.post(self, ResourceBlockId, ProcessorId) |
| 133 | + |
| 134 | + if AccelerationFunctionId in members: |
| 135 | + resp = 404 |
| 136 | + return resp |
| 137 | + try: |
| 138 | + global config |
| 139 | + wildcards = {'ResourceBlockId':ResourceBlockId, 'ProcessorId':ProcessorId, 'AccelerationFunctionId':AccelerationFunctionId, 'rb':g.rest_base} |
| 140 | + config=get_AccelerationFunction1_instance(wildcards) |
| 141 | + config = create_and_patch_object (config, members, member_ids, path, collection_path) |
| 142 | + resp = config, 200 |
| 143 | + |
| 144 | + except Exception: |
| 145 | + traceback.print_exc() |
| 146 | + resp = INTERNAL_ERROR |
| 147 | + logging.info('AccelerationFunction1API POST exit') |
| 148 | + return resp |
| 149 | + else: |
| 150 | + return msg, code |
| 151 | + |
| 152 | + # HTTP PUT |
| 153 | + def put(self, ResourceBlockId, ProcessorId, AccelerationFunctionId): |
| 154 | + logging.info('AccelerationFunction1 put called') |
| 155 | + msg, code = check_authentication(self.auth) |
| 156 | + |
| 157 | + if code == 200: |
| 158 | + path = os.path.join(self.root, 'CompositionService/ResourceBlocks/{0}/Processors/{1}/AccelerationFunctions/{2}', 'index.json').format(ResourceBlockId, ProcessorId, AccelerationFunctionId) |
| 159 | + put_object(path) |
| 160 | + return self.get(ResourceBlockId, ProcessorId, AccelerationFunctionId) |
| 161 | + else: |
| 162 | + return msg, code |
| 163 | + |
| 164 | + # HTTP PATCH |
| 165 | + def patch(self, ResourceBlockId, ProcessorId, AccelerationFunctionId): |
| 166 | + logging.info('AccelerationFunction1 patch called') |
| 167 | + msg, code = check_authentication(self.auth) |
| 168 | + |
| 169 | + if code == 200: |
| 170 | + path = os.path.join(self.root, 'CompositionService/ResourceBlocks/{0}/Processors/{1}/AccelerationFunctions/{2}', 'index.json').format(ResourceBlockId, ProcessorId, AccelerationFunctionId) |
| 171 | + patch_object(path) |
| 172 | + return self.get(ResourceBlockId, ProcessorId, AccelerationFunctionId) |
| 173 | + else: |
| 174 | + return msg, code |
| 175 | + |
| 176 | + # HTTP DELETE |
| 177 | + def delete(self, ResourceBlockId, ProcessorId, AccelerationFunctionId): |
| 178 | + logging.info('AccelerationFunction1 delete called') |
| 179 | + msg, code = check_authentication(self.auth) |
| 180 | + |
| 181 | + if code == 200: |
| 182 | + path = create_path(self.root, 'CompositionService/ResourceBlocks/{0}/Processors/{1}/AccelerationFunctions/{2}').format(ResourceBlockId, ProcessorId, AccelerationFunctionId) |
| 183 | + base_path = create_path(self.root, 'CompositionService/ResourceBlocks/{0}/Processors/{1}/AccelerationFunctions').format(ResourceBlockId, ProcessorId) |
| 184 | + return delete_object(path, base_path) |
| 185 | + else: |
| 186 | + return msg, code |
| 187 | + |
0 commit comments