blob: 7c260c79cbbe01bbead1a4b140e892fcbbc12ba7 [file] [log] [blame]
Peter Pentchev469dfea2022-06-27 12:48:18 +03001#! /usr/bin/env python3
2
3# Copyright 2021 Canonical Ltd
4#
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
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
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.
16
Biser Milanov2afeb432022-08-05 10:42:40 +030017import logging
Peter Pentchev469dfea2022-06-27 12:48:18 +030018
19from ops_openstack.plugins.classes import CinderStoragePluginCharm
Peter Pentcheva8266902022-07-29 05:55:35 +000020from ops_openstack.core import charm_class, get_charm_class
Peter Pentchev469dfea2022-06-27 12:48:18 +030021from ops.main import main
Biser Milanov2afeb432022-08-05 10:42:40 +030022from ops.model import BlockedStatus
23
24logger = logging.getLogger(__name__)
Peter Pentchev469dfea2022-06-27 12:48:18 +030025
26
27class CinderCharmBase(CinderStoragePluginCharm):
28
Biser Milanov53644f62022-08-03 16:08:59 +030029 PACKAGES = ["cinder-common"]
30 MANDATORY_CONFIG = ["protocol"]
Peter Pentchev469dfea2022-06-27 12:48:18 +030031 # Overriden from the parent. May be set depending on the charm's properties
32 stateless = True
33 active_active = True
34
35 def __init__(self, *args, **kwargs):
36 super().__init__(*args, **kwargs)
37
Biser Milanov2afeb432022-08-05 10:42:40 +030038 def _check_for_config_errors(self, config):
39 missing = []
40 for mandatory in self.MANDATORY_CONFIG:
41 if mandatory not in config:
42 missing.append(mandatory)
43
44 if missing:
45 return f"Mandatory options are missing: {', '.join(missing)}"
46
47 if config["protocol"] not in ["block", "iscsi"]:
48 return (
49 f"""Invalid 'protocol' option provided: '{config["protocol"]}';"""
50 "valid are 'block' and 'iscsi'"
51 )
52
53 if config["protocol"] == "block":
54 return "'protocol' value 'block' not yet supported"
55
56 def on_config(self, event):
57 config = dict(self.framework.model.config)
58 conf_error = self._check_for_config_errors(config)
59 if conf_error is not None:
60 logger.error(conf_error)
61 self.unit.status = BlockedStatus(conf_error)
62 return
63
64 super().on_config(event)
65
Peter Pentchev469dfea2022-06-27 12:48:18 +030066 def cinder_configuration(self, config):
Biser Milanov2afeb432022-08-05 10:42:40 +030067 conf_error = self._check_for_config_errors(config)
68 if conf_error is not None:
69 logger.error(conf_error)
70 return []
71
Peter Pentchev469dfea2022-06-27 12:48:18 +030072 # Return the configuration to be set by the principal.
Biser Milanov53644f62022-08-03 16:08:59 +030073 backend_name = config.get("volume-backend-name", self.framework.model.app.name)
74 volume_driver = "cinder.volume.drivers.storpool.StorPoolDriver"
Biser Milanov2afeb432022-08-05 10:42:40 +030075
Peter Pentchev469dfea2022-06-27 12:48:18 +030076 options = [
Biser Milanov53644f62022-08-03 16:08:59 +030077 ("volume_driver", volume_driver),
78 ("volume_backend_name", backend_name),
Peter Pentchev469dfea2022-06-27 12:48:18 +030079 ]
80
Biser Milanov53644f62022-08-03 16:08:59 +030081 if config.get("use-multipath"):
82 options.extend(
83 [
84 ("use_multipath_for_image_xfer", True),
85 ("enforce_multipath_for_image_xfer", True),
86 ]
87 )
Peter Pentchev469dfea2022-06-27 12:48:18 +030088
89 return options
90
91
92@charm_class
93class CinderStorPoolCharm(CinderCharmBase):
Biser Milanov53644f62022-08-03 16:08:59 +030094 release = "yoga"
Peter Pentchev469dfea2022-06-27 12:48:18 +030095
96
Biser Milanov53644f62022-08-03 16:08:59 +030097if __name__ == "__main__":
Peter Pentchev3a17d7d2022-07-27 13:11:57 +000098 # main(get_charm_class_for_release())
Peter Pentcheva8266902022-07-29 05:55:35 +000099 # main(CinderStorPoolCharm)
100 main(get_charm_class(release="yoga"))