File: action.py

package info (click to toggle)
python-muranoclient 2.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,324 kB
  • sloc: python: 10,663; sh: 231; makefile: 27
file content (90 lines) | stat: -rw-r--r-- 3,044 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""Application-catalog v1 action implementation"""

import json

from osc_lib.command import command
from oslo_log import log as logging

from muranoclient.apiclient import exceptions

LOG = logging.getLogger(__name__)


class StaticActionCall(command.ShowOne):
    """Call static method of the MuranoPL class."""

    def get_parser(self, prog_name):
        parser = super(StaticActionCall, self).get_parser(prog_name)
        parser.add_argument(
            "class_name",
            metavar='<CLASS>',
            help="FQN of the class with static method",
        )
        parser.add_argument(
            "method_name",
            metavar='<METHOD>',
            help="Static method to run",
        )
        parser.add_argument(
            "--arguments",
            metavar='<KEY=VALUE>',
            nargs='*',
            help="Method arguments. No arguments by default",
        )
        parser.add_argument(
            "--package-name",
            metavar='<PACKAGE>',
            default='',
            help='Optional FQN of the package to look for the class in',
        )
        parser.add_argument(
            "--class-version",
            default='',
            help='Optional version of the class, otherwise version =0 is '
                 'used ',
        )

        return parser

    def take_action(self, parsed_args):
        LOG.debug("take_action({0})".format(parsed_args))
        client = self.app.client_manager.application_catalog

        arguments = {}
        for argument in parsed_args.arguments or []:
            if '=' not in argument:
                raise exceptions.CommandError(
                    "Argument should be in form of KEY=VALUE. Found: "
                    "{0}".format(argument))
            key, value = argument.split('=', 1)
            try:
                value = json.loads(value)
            except ValueError:
                # treat value as a string if it doesn't load as json
                pass

            arguments[key] = value

        request_body = {
            "className": parsed_args.class_name,
            "methodName": parsed_args.method_name,
            "packageName": parsed_args.package_name or None,
            "classVersion": parsed_args.class_version or '=0',
            "parameters": arguments
        }

        print("Waiting for result...")
        result = client.static_actions.call(request_body).get_result()
        return ["Static action result"], [result]