"Fossies" - the Fresh Open Source Software Archive

Member "solum-12.0.0/solum/api/handlers/app_handler.py" (30 Mar 2022, 6261 Bytes) of package /linux/misc/openstack/solum-12.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "app_handler.py" see the Fossies "Dox" file reference documentation.

    1 # Copyright 2015 Rackspace Hosting
    2 #
    3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    4 # not use this file except in compliance with the License. You may obtain
    5 # 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, WITHOUT
   11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   12 # License for the specific language governing permissions and limitations
   13 # under the License.
   14 
   15 from oslo_log import log as logging
   16 from oslo_utils import uuidutils
   17 
   18 import json
   19 
   20 from solum.api.handlers import common
   21 from solum.api.handlers import handler
   22 from solum.api.handlers import workflow_handler
   23 from solum.common import exception
   24 from solum.common import keystone_utils
   25 from solum.common import utils
   26 from solum.deployer import api as deploy_api
   27 from solum import objects
   28 
   29 
   30 LOG = logging.getLogger(__name__)
   31 
   32 
   33 class AppHandler(handler.Handler):
   34     """Fulfills a request on the app resource."""
   35 
   36     def get(self, id):
   37         """Return an app."""
   38         return objects.registry.App.get_by_uuid(self.context, id)
   39 
   40     def patch(self, id, data):
   41         """Update an app."""
   42         db_obj = objects.registry.App.get_by_uuid(self.context, id)
   43 
   44         data_dict = data.as_dict(objects.registry.App)
   45         obj_dict = db_obj.as_dict()
   46 
   47         # Source and workflow are a little tricky to update.
   48         new_source = obj_dict['source']
   49         new_source.update(data_dict.get('source', {}))
   50         data_dict['source'] = new_source
   51 
   52         new_wf = obj_dict['workflow_config']
   53         new_wf.update(data_dict.get('workflow_config', {}))
   54         data_dict['workflow_config'] = new_wf
   55 
   56         if data_dict.get('repo_token'):
   57             new_raw_content = json.loads(obj_dict['raw_content'])
   58             new_raw_content['repo_token'] = data_dict['repo_token']
   59             data_dict['raw_content'] = json.dumps(new_raw_content)
   60             del data_dict['repo_token']
   61 
   62         updated = objects.registry.App.update_and_save(self.context,
   63                                                        id, data_dict)
   64         return updated
   65 
   66     def delete(self, id):
   67         """Delete an existing app."""
   68         try:
   69             objects.registry.App.get_by_uuid(self.context, id)
   70         except exception.ResourceNotFound:
   71             raise
   72 
   73         deploy_api.API(context=self.context).destroy_app(id)
   74 
   75     def create(self, data):
   76         """Create a new app."""
   77         db_obj = objects.registry.App()
   78         db_obj.id = uuidutils.generate_uuid()
   79         db_obj.user_id = self.context.user
   80         db_obj.project_id = self.context.tenant
   81         db_obj.deleted = False
   82 
   83         # create a delegation trust_id\token, if required
   84         db_obj.trust_id = keystone_utils.create_delegation_token(self.context)
   85 
   86         db_obj.trust_user = self.context.user_name
   87 
   88         db_obj.name = data.get('name')
   89         db_obj.description = data.get('description')
   90         db_obj.languagepack = data.get('languagepack')
   91         db_obj.stack_id = data.get('stack_id')
   92         db_obj.ports = data.get('ports')
   93         db_obj.source = data.get('source')
   94         db_obj.workflow_config = data.get('workflow_config')
   95         db_obj.trigger_uuid = uuidutils.generate_uuid()
   96         db_obj.trigger_actions = data.get('trigger_actions')
   97 
   98         raw_content = data.get('raw_content')
   99         raw_content_json = json.loads(raw_content)
  100 
  101         raw_content_json['username'] = self.context.user_name
  102         raw_content_json['user_id'] = self.context.user
  103         encrypted_password = utils.encrypt(self.context.password)
  104         # encrypted_password contains encoded characters that cannot
  105         # be json dumped which is required in order to save it to db.
  106         # So we need to decode it first.
  107         decoded_password = encrypted_password.decode('ISO-8859-1')
  108         raw_content_json['password'] = decoded_password
  109 
  110         raw_content_json['auth_url'] = self.context.auth_url
  111         raw_content_json['tenant'] = self.context.tenant
  112         raw_content_json['tenant_name'] = self.context.tenant_name
  113 
  114         db_obj.raw_content = json.dumps(raw_content_json)
  115 
  116         common.check_url(db_obj.source['repository'])
  117 
  118         db_obj.create(self.context)
  119         return db_obj
  120 
  121     def trigger_workflow(self, trigger_id, commit_sha='',
  122                          status_url=None, collab_url=None, workflow=None):
  123         """Get trigger by trigger id and start git workflow associated."""
  124         # Note: self.context will be None at this point as this is a
  125         # non-authenticated request.
  126         app_obj = objects.registry.App.get_by_trigger_id(None, trigger_id)
  127         # get the trust context and authenticate it.
  128         try:
  129             self.context = keystone_utils.create_delegation_context(
  130                 app_obj, self.context)
  131             self.context.tenant = app_obj.project_id
  132             self.context.user = app_obj.user_id
  133             self.context.user_name = app_obj.trust_user
  134         except exception.AuthorizationFailure as auth_ex:
  135             LOG.warning(auth_ex)
  136             return
  137 
  138         # TODO(devkulkarni): Call repo_utils.verify_artifact
  139         # as we are calling it in the plan_handler to verify
  140         # the collaborator
  141         self._build_artifact(app_obj, commit_sha=commit_sha,
  142                              status_url=status_url, wf=workflow)
  143 
  144     def _build_artifact(self, app, commit_sha='',
  145                         status_url=None, wf=None):
  146 
  147         # TODO(devkulkarni): Check why wf is being passed
  148         # if wf is None:
  149         #    wf = ['unittest', 'build', 'deploy']
  150         wfhand = workflow_handler.WorkflowHandler(self.context)
  151 
  152         wfdata = {
  153             'app_id': app.id,
  154             'name': "%s" % app.name,
  155             'description': '',
  156             'source': app.source,
  157             'config': app.workflow_config,
  158             'actions': app.trigger_actions
  159         }
  160         wfhand.create(wfdata, commit_sha=commit_sha, status_url=status_url,
  161                       du_id=None)
  162 
  163     def get_all(self):
  164         """Return all apps."""
  165         all_apps = objects.registry.AppList.get_all(self.context)
  166         return all_apps