|
| 1 | + |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import json |
| 5 | +import logging |
| 6 | +from typing import List |
| 7 | + |
| 8 | +from smb.enums import Intent |
| 9 | +from smb.proto import Simplified |
| 10 | +from smb.resources import Cluster, Share |
| 11 | + |
| 12 | +from dashboard.controllers._docs import EndpointDoc |
| 13 | +from dashboard.controllers._permissions import CreatePermission, DeletePermission |
| 14 | +from dashboard.exceptions import DashboardException |
| 15 | + |
| 16 | +from .. import mgr |
| 17 | +from ..security import Scope |
| 18 | +from . import APIDoc, APIRouter, ReadPermission, RESTController |
| 19 | + |
| 20 | +logger = logging.getLogger('controllers.smb') |
| 21 | + |
| 22 | +CLUSTER_SCHEMA = { |
| 23 | + "resource_type": (str, "ceph.smb.cluster"), |
| 24 | + "cluster_id": (str, "Unique identifier for the cluster"), |
| 25 | + "auth_mode": (str, "Either 'active-directory' or 'user'"), |
| 26 | + "intent": (str, "Desired state of the resource, e.g., 'present' or 'removed'"), |
| 27 | + "domain_settings": ({ |
| 28 | + "realm": (str, "Domain realm, e.g., 'DOMAIN1.SINK.TEST'"), |
| 29 | + "join_sources": ([{ |
| 30 | + "source_type": (str, "resource"), |
| 31 | + "ref": (str, "Reference identifier for the join auth resource") |
| 32 | + }], "List of join auth sources for domain settings") |
| 33 | + }, "Domain-specific settings for active-directory auth mode"), |
| 34 | + "user_group_settings": ([{ |
| 35 | + "source_type": (str, "resource"), |
| 36 | + "ref": (str, "Reference identifier for the user group resource") |
| 37 | + }], "User group settings for user auth mode"), |
| 38 | + "custom_dns": ([str], "List of custom DNS server addresses"), |
| 39 | + "placement": ({ |
| 40 | + "count": (int, "Number of instances to place") |
| 41 | + }, "Placement configuration for the resource") |
| 42 | +} |
| 43 | + |
| 44 | +CLUSTER_SCHEMA_RESULTS = { |
| 45 | + "results": ([{ |
| 46 | + "resource": ({ |
| 47 | + "resource_type": (str, "ceph.smb.cluster"), |
| 48 | + "cluster_id": (str, "Unique identifier for the cluster"), |
| 49 | + "auth_mode": (str, "Either 'active-directory' or 'user'"), |
| 50 | + "intent": (str, "Desired state of the resource, e.g., 'present' or 'removed'"), |
| 51 | + "domain_settings": ({ |
| 52 | + "realm": (str, "Domain realm, e.g., 'DOMAIN1.SINK.TEST'"), |
| 53 | + "join_sources": ([{ |
| 54 | + "source_type": (str, "resource"), |
| 55 | + "ref": (str, "Reference identifier for the join auth resource") |
| 56 | + }], "List of join auth sources for domain settings") |
| 57 | + }, "Domain-specific settings for active-directory auth mode"), |
| 58 | + "user_group_settings": ([{ |
| 59 | + "source_type": (str, "resource"), |
| 60 | + "ref": (str, "Reference identifier for the user group resource") |
| 61 | + }], "User group settings for user auth mode (optional)"), |
| 62 | + "custom_dns": ([str], "List of custom DNS server addresses (optional)"), |
| 63 | + "placement": ({ |
| 64 | + "count": (int, "Number of instances to place") |
| 65 | + }, "Placement configuration for the resource (optional)"), |
| 66 | + }, "Resource details"), |
| 67 | + "state": (str, "State of the resource"), |
| 68 | + "success": (bool, "Indicates whether the operation was successful") |
| 69 | + }], "List of results with resource details"), |
| 70 | + "success": (bool, "Overall success status of the operation") |
| 71 | +} |
| 72 | + |
| 73 | +LIST_CLUSTER_SCHEMA = [CLUSTER_SCHEMA] |
| 74 | + |
| 75 | +SHARE_SCHEMA = { |
| 76 | + "resource_type": (str, "ceph.smb.share"), |
| 77 | + "cluster_id": (str, "Unique identifier for the cluster"), |
| 78 | + "share_id": (str, "Unique identifier for the share"), |
| 79 | + "intent": (str, "Desired state of the resource, e.g., 'present' or 'removed'"), |
| 80 | + "name": (str, "Name of the share"), |
| 81 | + "readonly": (bool, "Indicates if the share is read-only"), |
| 82 | + "browseable": (bool, "Indicates if the share is browseable"), |
| 83 | + "cephfs": ({ |
| 84 | + "volume": (str, "Name of the CephFS file system"), |
| 85 | + "path": (str, "Path within the CephFS file system"), |
| 86 | + "provider": (str, "Provider of the CephFS share, e.g., 'samba-vfs'") |
| 87 | + }, "Configuration for the CephFS share") |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +@APIRouter('/smb/cluster', Scope.SMB) |
| 92 | +@APIDoc("SMB Cluster Management API", "SMB") |
| 93 | +class SMBCluster(RESTController): |
| 94 | + _resource: str = 'ceph.smb.cluster' |
| 95 | + |
| 96 | + @ReadPermission |
| 97 | + @EndpointDoc("List smb clusters", |
| 98 | + responses={200: LIST_CLUSTER_SCHEMA}) |
| 99 | + def list(self) -> List[Cluster]: |
| 100 | + """ |
| 101 | + List smb clusters |
| 102 | + """ |
| 103 | + res = mgr.remote('smb', 'show', [self._resource]) |
| 104 | + return res['resources'] if 'resources' in res else [res] |
| 105 | + |
| 106 | + @ReadPermission |
| 107 | + @EndpointDoc("Get an smb cluster", |
| 108 | + parameters={ |
| 109 | + 'cluster_id': (str, 'Unique identifier for the cluster') |
| 110 | + }, |
| 111 | + responses={200: CLUSTER_SCHEMA}) |
| 112 | + def get(self, cluster_id: str) -> Cluster: |
| 113 | + """ |
| 114 | + Get an smb cluster by cluster id |
| 115 | + """ |
| 116 | + return mgr.remote('smb', 'show', [f'{self._resource}.{cluster_id}']) |
| 117 | + |
| 118 | + @CreatePermission |
| 119 | + @EndpointDoc("Create smb cluster", |
| 120 | + parameters={ |
| 121 | + 'cluster_resource': (str, 'cluster_resource') |
| 122 | + }, |
| 123 | + responses={201: CLUSTER_SCHEMA_RESULTS}) |
| 124 | + def create(self, cluster_resource: Cluster) -> Simplified: |
| 125 | + """ |
| 126 | + Create an smb cluster |
| 127 | +
|
| 128 | + :param cluster_resource: Dict cluster data |
| 129 | + :return: Returns cluster resource. |
| 130 | + :rtype: Dict[str, Any] |
| 131 | + """ |
| 132 | + try: |
| 133 | + return mgr.remote( |
| 134 | + 'smb', |
| 135 | + 'apply_resources', |
| 136 | + json.dumps(cluster_resource)).to_simplified() |
| 137 | + except RuntimeError as e: |
| 138 | + raise DashboardException(e, component='smb') |
| 139 | + |
| 140 | + |
| 141 | +@APIRouter('/smb/share', Scope.SMB) |
| 142 | +@APIDoc("SMB Share Management API", "SMB") |
| 143 | +class SMBShare(RESTController): |
| 144 | + _resource: str = 'ceph.smb.share' |
| 145 | + |
| 146 | + @ReadPermission |
| 147 | + @EndpointDoc("List smb shares", |
| 148 | + parameters={ |
| 149 | + 'cluster_id': (str, 'Unique identifier for the cluster') |
| 150 | + }, |
| 151 | + responses={200: SHARE_SCHEMA}) |
| 152 | + def list(self, cluster_id: str = '') -> List[Share]: |
| 153 | + """ |
| 154 | + List all smb shares or all shares for a given cluster |
| 155 | +
|
| 156 | + :param cluster_id: Dict containing cluster information |
| 157 | + :return: Returns list of shares. |
| 158 | + :rtype: List[Dict] |
| 159 | + """ |
| 160 | + res = mgr.remote( |
| 161 | + 'smb', |
| 162 | + 'show', |
| 163 | + [f'{self._resource}.{cluster_id}' if cluster_id else self._resource]) |
| 164 | + return res['resources'] if 'resources' in res else res |
| 165 | + |
| 166 | + @DeletePermission |
| 167 | + @EndpointDoc("Remove smb shares", |
| 168 | + parameters={ |
| 169 | + 'cluster_id': (str, 'Unique identifier for the cluster'), |
| 170 | + 'share_id': (str, 'Unique identifier for the share') |
| 171 | + }, |
| 172 | + responses={204: None}) |
| 173 | + def delete(self, cluster_id: str, share_id: str): |
| 174 | + """ |
| 175 | + Remove an smb share from a given cluster |
| 176 | +
|
| 177 | + :param cluster_id: Cluster identifier |
| 178 | + :param share_id: Share identifier |
| 179 | + :return: None. |
| 180 | + """ |
| 181 | + resource = {} |
| 182 | + resource['resource_type'] = self._resource |
| 183 | + resource['cluster_id'] = cluster_id |
| 184 | + resource['share_id'] = share_id |
| 185 | + resource['intent'] = Intent.REMOVED |
| 186 | + return mgr.remote('smb', 'apply_resources', json.dumps(resource)).one().to_simplified() |
0 commit comments