JIRA: BOTTLENECKS-29
[bottlenecks.git] / vstf / vstf / controller / settings / mail_settings.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import logging
11 import pprint
12
13 import vstf.controller.settings.settings as sets
14 import vstf.common.decorator as deco
15 from vstf.common.input import raw_choice
16
17 LOG = logging.getLogger(__name__)
18
19
20 class MailSettings(sets.Settings):
21     def __init__(self, path="/etc/vstf", filename="reporters.mail.mail-settings", mode=sets.SETS_DEFAULT):
22         super(MailSettings, self).__init__(path, filename, mode)
23
24     def _register_func(self):
25         super(MailSettings, self)._register_func()
26         body = set(
27             self._fset['body'].keys()
28         )
29         LOG.debug(body)
30         for item in body:
31             item = item.encode()
32             func_name = "set_%s" % item
33             setattr(self, func_name, self._setting_file(func_name, self._mset['body'], self._fset['body'], item))
34         other = {"attach", "content", "subtype"}
35         for item in other:
36             func_name = "mset_%s" % item
37             setattr(self, func_name, self._setting_memory(func_name, self._mset['body'], item))
38
39         LOG.debug(self.__dict__)
40
41     def sinput(self):
42         if raw_choice("if set mail server"):
43             server = self.raw_server()
44             self.set_server(server)
45
46         if raw_choice("if set mail body"):
47             body = self.raw_body()
48             self.set_body(body)
49         print "%s set finish: " % (self._filename)
50         print "+++++++++++++++++++++++++++++++++++++++++"
51         pprint.pprint(self.settings, indent=4)
52         print "+++++++++++++++++++++++++++++++++++++++++"
53
54     @deco.vstf_input("password", types=str)
55     @deco.vstf_input("username", types=str)
56     @deco.vstf_input('host', types=str)
57     def raw_server(self):
58         print "---------------------------------------"
59         print "Please vstf set mail server info like:"
60         print "    'host': 'localhost',"
61         print "    'username': 'user',['\\n' = None]"
62         print "    'password': '******',['\\n' = None]"
63         print "---------------------------------------"
64
65     @deco.vstf_input("subject", types=str, default='vstf mail')
66     @deco.vstf_input("bcc", types=list, default=[])
67     @deco.vstf_input("cc", types=list, default=[])
68     @deco.vstf_input("to", types=list, default=[])
69     @deco.vstf_input('from', types=list, default=['vstf_from@vstf.com'])
70     def raw_body(self):
71         print "----------------------------------------------------"
72         print "Please vstf set mail server info like:"
73         print "    'from': ['vstf_from@vstf.com'],"
74         print "    'to': ['vstf_to@vstf.com'],"
75         print "    'cc': ['vstf_cc@vstf.com']"
76         print "    'bcc': ['vstf_bcc@vstf.com']"
77         print "    'subject': Vstf Performance Test Report"
78         print "----------------------------------------------------"
79
80
81 def unit_test():
82     from vstf.common.log import setup_logging
83     setup_logging(level=logging.DEBUG, log_file="/var/log/vstf/vstf-mail-settings.log", clevel=logging.INFO)
84
85     mail_settings = MailSettings()
86     mail_settings.sinput()
87
88     return
89
90     mail_server = {
91         "host": "localhost",
92         "username": None,
93         "password": None
94     }
95     mail_settings.set_server(mail_server)
96
97     from_list = ['vstf_from@vstf.com']
98     mail_settings.set_from(from_list)
99     to_list = ['wangli11@huawei.com']
100     mail_settings.set_to(to_list)
101     cc_list = ['wangli11@huawei.com']
102     mail_settings.set_cc(cc_list)
103     bcc_list = ['wangli11@huawei.com']
104     mail_settings.set_bcc(bcc_list)
105     bcc_list = ['wangli11@huawei.com']
106     mail_settings.set_bcc(bcc_list)
107
108     subject = "Virtual Switching Performance Test Report"
109     mail_settings.set_subject(subject)
110
111     subtype = "plain"
112     mail_settings.mset_subtype(subtype)
113
114     attach_list = []
115     mail_settings.mset_attach(attach_list)
116
117     content = "this is a test"
118     mail_settings.mset_content(content)
119
120     LOG.info(mail_settings.settings)
121
122
123 if __name__ == '__main__':
124     unit_test()