summaryrefslogtreecommitdiff
path: root/saharaclient/osc/v2/job_binaries.py
blob: e1aa30ba7f7210a875bc1e44db31234228cc70ab (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Copyright (c) 2015 Mirantis Inc.
#
# 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.

from osc_lib.command import command
from osc_lib import exceptions
from osc_lib import utils as osc_utils
from oslo_log import log as logging
from oslo_serialization import jsonutils

from saharaclient.osc import utils
from saharaclient.osc.v1 import job_binaries as jb_v1


class CreateJobBinary(command.ShowOne):
    """Creates job binary"""

    log = logging.getLogger(__name__ + ".CreateJobBinary")

    def get_parser(self, prog_name):
        parser = super(CreateJobBinary, self).get_parser(prog_name)

        parser.add_argument(
            '--name',
            metavar="<name>",
            help="Name of the job binary [REQUIRED if JSON is not provided]",
        )
        creation_type = parser.add_mutually_exclusive_group()
        creation_type.add_argument(
            '--url',
            metavar='<url>',
            help='URL for the job binary [REQUIRED if JSON and file are '
                 'not provided]'
        )
        parser.add_argument(
            '--description',
            metavar="<description>",
            help="Description of the job binary"
        )
        username = parser.add_mutually_exclusive_group()
        username.add_argument(
            '--username',
            metavar='<username>',
            help='Username for accessing the job binary URL',
        )
        username.add_argument(
            '--access-key',
            metavar='<accesskey>',
            help='S3 access key for accessing the job binary URL',
        )
        password = parser.add_mutually_exclusive_group()
        password.add_argument(
            '--password',
            metavar='<password>',
            help='Password for accessing the job binary URL',
        )
        password.add_argument(
            '--secret-key',
            metavar='<secretkey>',
            help='S3 secret key for accessing the job binary URL',
        )
        password.add_argument(
            '--password-prompt',
            dest="password_prompt",
            action="store_true",
            help='Prompt interactively for password',
        )
        password.add_argument(
            '--secret-key-prompt',
            dest="secret_key_prompt",
            action="store_true",
            help='Prompt interactively for S3 secret key',
        )
        parser.add_argument(
            '--s3-endpoint',
            metavar='<endpoint>',
            help='S3 endpoint for accessing the job binary URL (ignored if '
                 'binary not in S3',
        )
        parser.add_argument(
            '--public',
            action='store_true',
            default=False,
            help='Make the job binary public',
        )
        parser.add_argument(
            '--protected',
            action='store_true',
            default=False,
            help='Make the job binary protected',
        )
        parser.add_argument(
            '--json',
            metavar='<filename>',
            help='JSON representation of the job binary. Other '
                 'arguments will not be taken into account if this one is '
                 'provided'
        )
        return parser

    def take_action(self, parsed_args):
        self.log.debug("take_action(%s)", parsed_args)
        client = self.app.client_manager.data_processing

        if parsed_args.json:
            blob = osc_utils.read_blob_file_contents(parsed_args.json)
            try:
                template = jsonutils.loads(blob)
            except ValueError as e:
                raise exceptions.CommandError(
                    'An error occurred when reading '
                    'template from file %s: %s' % (parsed_args.json, e))
            data = client.job_binaries.create(**template).to_dict()
        else:
            if parsed_args.password_prompt:
                parsed_args.password = osc_utils.get_password(
                    self.app.stdin, confirm=False)

            if parsed_args.secret_key_prompt:
                parsed_args.secret_key = osc_utils.get_password(
                    self.app.stdin, confirm=False)

            if not parsed_args.password:
                parsed_args.password = parsed_args.secret_key

            if not parsed_args.username:
                parsed_args.username = parsed_args.access_key

            if parsed_args.password and not parsed_args.username:
                raise exceptions.CommandError(
                    'Username via --username, or S3 access key via '
                    '--access-key should be provided with password')

            if parsed_args.username and not parsed_args.password:
                raise exceptions.CommandError(
                    'Password should be provided via --password or '
                    '--secret-key, or entered interactively with '
                    '--password-prompt or --secret-key-prompt')

            if parsed_args.password and parsed_args.username:
                if not parsed_args.url:
                    raise exceptions.CommandError(
                        'URL must be provided via --url')
                if parsed_args.url.startswith('s3'):
                    if not parsed_args.s3_endpoint:
                        raise exceptions.CommandError(
                            'S3 job binaries need an endpoint provided via '
                            '--s3-endpoint')
                    extra = {
                        'accesskey': parsed_args.username,
                        'secretkey': parsed_args.password,
                        'endpoint': parsed_args.s3_endpoint,
                    }

                else:
                    extra = {
                        'user': parsed_args.username,
                        'password': parsed_args.password
                    }
            else:
                extra = None

            data = client.job_binaries.create(
                name=parsed_args.name, url=parsed_args.url,
                description=parsed_args.description, extra=extra,
                is_public=parsed_args.public,
                is_protected=parsed_args.protected).to_dict()

        data = utils.prepare_data(data, jb_v1.JOB_BINARY_FIELDS)

        return self.dict2columns(data)


class ListJobBinaries(jb_v1.ListJobBinaries):
    """Lists job binaries"""

    log = logging.getLogger(__name__ + ".ListJobBinaries")


class ShowJobBinary(jb_v1.ShowJobBinary):
    """Display job binary details"""

    log = logging.getLogger(__name__ + ".ShowJobBinary")


class DeleteJobBinary(jb_v1.DeleteJobBinary):
    """Deletes job binary"""

    log = logging.getLogger(__name__ + ".DeleteJobBinary")


class UpdateJobBinary(jb_v1.UpdateJobBinary):
    """Updates job binary"""

    log = logging.getLogger(__name__ + ".UpdateJobBinary")


class DownloadJobBinary(jb_v1.DownloadJobBinary):
    """Downloads job binary"""

    log = logging.getLogger(__name__ + ".DownloadJobBinary")