2
# Copyright (c) 2010 OpenStack, LLC.
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
8
# http://www.apache.org/licenses/LICENSE-2.0
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
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
18
import simplejson as json
22
from optparse import OptionParser
23
from os.path import basename
24
from sys import argv, exit
26
from swift.common.bufferedhttp import http_connect_raw as http_connect
27
from swift.common.utils import urlparse
30
if __name__ == '__main__':
31
gettext.install('swift', unicode=1)
32
parser = OptionParser(usage='''
33
Usage: %prog [options] <account> <service> <name> <value>
35
Sets a service URL for an account. Can only be set by a reseller admin.
37
Example: %prog -K swauthkey test storage local http://127.0.0.1:8080/v1/AUTH_018c3946-23f8-4efb-a8fb-b67aae8e4162
39
parser.add_option('-A', '--admin-url', dest='admin_url',
40
default='http://127.0.0.1:8080/auth/', help='The URL to the auth '
41
'subsystem (default: http://127.0.0.1:8080/auth/)')
42
parser.add_option('-U', '--admin-user', dest='admin_user',
43
default='.super_admin', help='The user with admin rights to add users '
44
'(default: .super_admin).')
45
parser.add_option('-K', '--admin-key', dest='admin_key',
46
help='The key for the user with admin rights to add users.')
50
(options, args) = parser.parse_args(args)
52
parser.parse_args(['-h'])
53
account, service, name, url = args
54
parsed = urlparse(options.admin_url)
55
if parsed.scheme not in ('http', 'https'):
56
raise Exception('Cannot handle protocol scheme %s for url %s' %
57
(parsed.scheme, repr(options.admin_url)))
58
parsed_path = parsed.path
61
elif parsed_path[-1] != '/':
63
path = '%sv2/%s/.services' % (parsed_path, account)
64
body = json.dumps({service: {name: url}})
65
headers = {'Content-Length': str(len(body)),
66
'X-Auth-Admin-User': options.admin_user,
67
'X-Auth-Admin-Key': options.admin_key}
68
conn = http_connect(parsed.hostname, parsed.port, 'POST', path, headers,
69
ssl=(parsed.scheme == 'https'))
71
resp = conn.getresponse()
72
if resp.status // 100 != 2:
73
exit('Service set failed: %s %s' % (resp.status, resp.reason))