blob: 1d774d4c90464227dd8f33f5aac1a6fea2712c62 [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 Milanov8bc000b2022-08-08 14:39:29 +030029 PACKAGES = ["charm-cinder-storpool-deps", "cinder-common"]
Biser Milanov4ea59212022-08-05 11:03:05 +030030 MANDATORY_CONFIG = [
31 "protocol",
32 "storpool-template",
33 "sp-api-http-host",
34 "sp-api-http-port",
35 "sp-auth-token",
36 ]
Peter Pentchev469dfea2022-06-27 12:48:18 +030037 # Overriden from the parent. May be set depending on the charm's properties
38 stateless = True
39 active_active = True
40
41 def __init__(self, *args, **kwargs):
42 super().__init__(*args, **kwargs)
43
Biser Milanov2afeb432022-08-05 10:42:40 +030044 def _check_for_config_errors(self, config):
45 missing = []
46 for mandatory in self.MANDATORY_CONFIG:
47 if mandatory not in config:
48 missing.append(mandatory)
49
50 if missing:
51 return f"Mandatory options are missing: {', '.join(missing)}"
52
53 if config["protocol"] not in ["block", "iscsi"]:
54 return (
55 f"""Invalid 'protocol' option provided: '{config["protocol"]}';"""
56 "valid are 'block' and 'iscsi'"
57 )
58
59 if config["protocol"] == "block":
60 return "'protocol' value 'block' not yet supported"
61
Biser Milanov4ea59212022-08-05 11:03:05 +030062 if not (0 < config["sp-api-http-port"] < 65536):
63 return (
64 f"""'sp-api-http-port' ('{config["sp-api-http-port"]}')"""
65 "is not a valid port (0-65535)"
66 )
67
Biser Milanov2afeb432022-08-05 10:42:40 +030068 def on_config(self, event):
69 config = dict(self.framework.model.config)
70 conf_error = self._check_for_config_errors(config)
71 if conf_error is not None:
72 logger.error(conf_error)
73 self.unit.status = BlockedStatus(conf_error)
74 return
75
76 super().on_config(event)
77
Peter Pentchev469dfea2022-06-27 12:48:18 +030078 def cinder_configuration(self, config):
Biser Milanov2afeb432022-08-05 10:42:40 +030079 conf_error = self._check_for_config_errors(config)
80 if conf_error is not None:
81 logger.error(conf_error)
82 return []
83
Peter Pentchev469dfea2022-06-27 12:48:18 +030084 # Return the configuration to be set by the principal.
Biser Milanov53644f62022-08-03 16:08:59 +030085 backend_name = config.get("volume-backend-name", self.framework.model.app.name)
86 volume_driver = "cinder.volume.drivers.storpool.StorPoolDriver"
Biser Milanov2afeb432022-08-05 10:42:40 +030087
Peter Pentchev469dfea2022-06-27 12:48:18 +030088 options = [
Biser Milanov53644f62022-08-03 16:08:59 +030089 ("volume_driver", volume_driver),
90 ("volume_backend_name", backend_name),
Biser Milanov7712cce2022-08-05 10:57:29 +030091 ("storpool_template", config["storpool-template"]),
Biser Milanov4ea59212022-08-05 11:03:05 +030092 ("sp_api_http_host", config["sp-api-http-host"]),
93 ("sp_api_http_port", config["sp-api-http-port"]),
94 ("sp_auth_token", config["sp-auth-token"]),
Peter Pentchev469dfea2022-06-27 12:48:18 +030095 ]
96
Biser Milanov53644f62022-08-03 16:08:59 +030097 if config.get("use-multipath"):
98 options.extend(
99 [
100 ("use_multipath_for_image_xfer", True),
101 ("enforce_multipath_for_image_xfer", True),
102 ]
103 )
Peter Pentchev469dfea2022-06-27 12:48:18 +0300104
105 return options
106
107
108@charm_class
109class CinderStorPoolCharm(CinderCharmBase):
Biser Milanov53644f62022-08-03 16:08:59 +0300110 release = "yoga"
Peter Pentchev469dfea2022-06-27 12:48:18 +0300111
112
Biser Milanov53644f62022-08-03 16:08:59 +0300113if __name__ == "__main__":
Peter Pentchev3a17d7d2022-07-27 13:11:57 +0000114 # main(get_charm_class_for_release())
Peter Pentcheva8266902022-07-29 05:55:35 +0000115 # main(CinderStorPoolCharm)
116 main(get_charm_class(release="yoga"))