409f286495c3cc00739021dc49c9c7520cc9c33c
[osm/common.git] / osm_common / tests / packages / native_charm_with_metadata_dir_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
31 def __init__(self, framework, key):
32 super().__init__(framework, key)
33
34 # Listen to charm events
35 self.framework.observe(self.on.config_changed, self.on_config_changed)
36 self.framework.observe(self.on.install, self.on_install)
37 self.framework.observe(self.on.start, self.on_start)
38
39 # Listen to the touch action event
40 self.framework.observe(self.on.touch_action, self.on_touch_action)
41
42 def on_config_changed(self, event):
43 """Handle changes in configuration"""
44 self.model.unit.status = ActiveStatus()
45
46 def on_install(self, event):
47 """Called when the charm is being installed"""
48 self.model.unit.status = ActiveStatus()
49
50 def on_start(self, event):
51 """Called when the charm is being started"""
52 self.model.unit.status = ActiveStatus()
53
54 def on_touch_action(self, event):
55 """Touch a file."""
56
57 filename = event.params["filename"]
58 try:
59 subprocess.run(["touch", filename], check=True)
60 event.set_results({"created": True, "filename": filename})
61 except subprocess.CalledProcessError as e:
62 event.fail("Action failed: {}".format(e))
63 self.model.unit.status = ActiveStatus()
64
65
66 if __name__ == "__main__":
67 main(MyNativeCharm)