-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathidentity_tools.py
More file actions
222 lines (171 loc) · 5.98 KB
/
identity_tools.py
File metadata and controls
222 lines (171 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from fastmcp import FastMCP
from .base import get_openstack_conn
from .response.identity import Domain, Region
class IdentityTools:
"""
A class to encapsulate Identity-related tools and utilities.
"""
def register_tools(self, mcp: FastMCP):
"""
Register Identity-related tools with the FastMCP instance.
"""
mcp.tool()(self.get_regions)
mcp.tool()(self.get_region)
mcp.tool()(self.create_region)
mcp.tool()(self.delete_region)
mcp.tool()(self.update_region)
mcp.tool()(self.get_domains)
mcp.tool()(self.get_domain)
mcp.tool()(self.create_domain)
mcp.tool()(self.delete_domain)
mcp.tool()(self.update_domain)
def get_regions(self) -> list[Region]:
"""
Get the list of Identity regions.
:return: A list of Region objects representing the regions.
"""
conn = get_openstack_conn()
region_list = []
for region in conn.identity.regions():
region_list.append(
Region(id=region.id, description=region.description),
)
return region_list
def get_region(self, id: str) -> Region:
"""
Get a region.
:param id: The ID of the region. (required)
:return: The Region object.
"""
conn = get_openstack_conn()
region = conn.identity.get_region(region=id)
return Region(id=region.id, description=region.description)
def create_region(self, id: str, description: str = "") -> Region:
"""
Create a new region.
:param id: The ID of the region. (required)
:param description: The description of the region. (optional)
:return: The created Region object.
"""
conn = get_openstack_conn()
region = conn.identity.create_region(id=id, description=description)
return Region(id=region.id, description=region.description)
def delete_region(self, id: str) -> None:
"""
Delete a region.
:param id: The ID of the region. (required)
:return: None
"""
conn = get_openstack_conn()
# ignore_missing is set to False to raise an exception if the region does not exist.
conn.identity.delete_region(region=id, ignore_missing=False)
return None
def update_region(self, id: str, description: str = "") -> Region:
"""
Update a region.
:param id: The ID of the region. (required)
:param description: The string description of the region. (optional)
:return: The updated Region object.
"""
conn = get_openstack_conn()
updated_region = conn.identity.update_region(
region=id,
description=description,
)
return Region(
id=updated_region.id,
description=updated_region.description,
)
def get_domains(self) -> list[Domain]:
"""
Get the list of Identity domains.
:return: A list of Domain objects representing the domains.
"""
conn = get_openstack_conn()
domain_list = []
for domain in conn.identity.domains():
domain_list.append(
Domain(
id=domain.id,
name=domain.name,
description=domain.description,
is_enabled=domain.is_enabled,
),
)
return domain_list
def get_domain(self, name: str) -> Domain:
"""
Get a domain.
:param name: The name of the domain. (required)
:return: The Domain object.
"""
conn = get_openstack_conn()
domain = conn.identity.find_domain(name_or_id=name)
return Domain(
id=domain.id,
name=domain.name,
description=domain.description,
is_enabled=domain.is_enabled,
)
def create_domain(
self,
name: str,
description: str = "",
is_enabled: bool = False,
) -> Domain:
"""
Create a new domain.
:param name: The name of the domain. (required)
:param description: The description of the domain. (optional)
:param is_enabled: Whether the domain is enabled. (optional)
"""
conn = get_openstack_conn()
domain = conn.identity.create_domain(
name=name,
description=description,
enabled=is_enabled,
)
return Domain(
id=domain.id,
name=domain.name,
description=domain.description,
is_enabled=domain.is_enabled,
)
def delete_domain(self, name: str) -> None:
"""
Delete a domain.
:param name: The name of the domain. (required)
"""
conn = get_openstack_conn()
domain = conn.identity.find_domain(name_or_id=name)
conn.identity.delete_domain(domain=domain, ignore_missing=False)
return None
def update_domain(
self,
id: str,
name: str | None = None,
description: str | None = None,
is_enabled: bool | None = None,
) -> Domain:
"""
Update a domain.
:param id: The ID of the domain. (required)
:param name: The name of the domain. (optional)
:param description: The description of the domain. (optional)
:param is_enabled: Whether the domain is enabled. (optional)
"""
conn = get_openstack_conn()
args = {}
if name is not None:
args["name"] = name
if description is not None:
args["description"] = description
if is_enabled is not None:
args["is_enabled"] = is_enabled
updated_domain = conn.identity.update_domain(domain=id, **args)
return Domain(
id=updated_domain.id,
name=updated_domain.name,
description=updated_domain.description,
is_enabled=updated_domain.is_enabled,
)