2 # -------------------------------------------------------------------------
3 # Copyright (c) 2015-2017 AT&T Intellectual Property
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # -------------------------------------------------------------------------
22 from oslo_config import cfg
24 from conductor.common.music import api
25 from conductor import service
30 def current_time_millis():
31 """Current time in milliseconds."""
32 return int(round(time.time() * 1000))
36 """Sample usage of Music."""
37 service.prepare_service()
38 CONF.set_override('debug', True, 'music_api')
39 CONF.set_override('mock', True, 'music_api')
40 CONF.set_override('hostnames', ['music2'], 'music_api')
42 print("Music version %s" % music.version())
44 # Randomize the name so that we don't step on each other.
45 keyspace = 'NewVotingApp' + str(current_time_millis() / 100)
46 music.keyspace_create(keyspace)
55 'PRIMARY KEY': '(name)'
58 music.table_create(**kwargs)
69 # Create an entry in the voting table for each candidate
70 # and with a vote count of 0.
71 kwargs = {'keyspace': keyspace, 'table': 'votecount', 'pk_name': 'name'}
72 for name in data: # We only want the keys
73 kwargs['pk_value'] = name
74 kwargs['values'] = {'name': name, 'count': 0}
75 music.row_create(**kwargs)
77 # Update each candidate's count atomically.
78 kwargs = {'keyspace': keyspace, 'table': 'votecount', 'pk_name': 'name'}
81 kwargs['pk_value'] = name
82 kwargs['values'] = {'count': count}
83 kwargs['atomic'] = True
84 music.row_update(**kwargs)
87 kwargs = {'keyspace': keyspace, 'table': 'votecount'}
88 print(music.row_read(**kwargs)) # Reads all rows
90 # Delete Joe, read Matti
91 kwargs = {'keyspace': keyspace, 'table': 'votecount', 'pk_name': 'name'}
92 kwargs['pk_value'] = 'Joe'
93 music.row_delete(**kwargs)
94 kwargs['pk_value'] = 'Matti'
95 print(music.row_read(**kwargs))
98 kwargs = {'keyspace': keyspace, 'table': 'votecount'}
99 print(music.row_read(**kwargs)) # Reads all rows
102 music.keyspace_delete(keyspace)
105 if __name__ == "__main__":