54cefc4b3bfffc7675b2d11733c7d4c5ba2fb39f
[osm/common.git] / osm_common / tests / packages / invalid_package_vnf / Scripts / charms / simple / src / charm.py
1 #!/usr/bin/env python3
2 ##
3 # Copyright 2020 Canonical Ltd.
4 # All rights reserved.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
7 # not use this file except in compliance with the License. You may obtain
8 # a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 # License for the specific language governing permissions and limitations
16 # under the License.
17 ##
18
19 import sys
20 import subprocess
21
22 from ops.charm import CharmBase
23 from ops.main import main
24 from ops.model import ActiveStatus
25
26 sys.path.append("lib")
27
28
29 class MyNativeCharm(CharmBase):
30 def __init__(self, framework, key):
31 super().__init__(framework, key)
32
33 # Listen to charm events
34 self.framework.observe(self.on.config_changed, self.on_config_changed)
35 self.framework.observe(self.on.install, self.on_install)
36 self.framework.observe(self.on.start, self.on_start)
37
38 # Listen to the touch action event
39 self.framework.observe(self.on.touch_action, self.on_touch_action)
40
41 def on_config_changed(self, event):
42 """Handle changes in configuration"""
43 self.model.unit.status = ActiveStatus()
44
45 def on_install(self, event):
46 """Called when the charm is being installed"""
47 self.model.unit.status = ActiveStatus()
48
49 def on_start(self, event):
50 """Called when the charm is being started"""
51 self.model.unit.status = ActiveStatus()
52
53 def on_touch_action(self, event):
54 """Touch a file."""
55
56 filename = event.params["filename"]
57 try:
58 subprocess.run(["touch", filename], check=True)
59 event.set_results({"created": True, "filename": filename})
60 except subprocess.CalledProcessError as e:
61 event.fail("Action failed: {}".format(e))
62 self.model.unit.status = ActiveStatus()
63
64
65 if __name__ == "__main__":
66 main(MyNativeCharm)