-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_configuration.py
executable file
·284 lines (222 loc) · 10.5 KB
/
server_configuration.py
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
from examples.authentication import client
from intersight.model.server_profile import ServerProfile
from intersight.api import server_api
from intersight.model.policy_abstract_policy_relationship import PolicyAbstractPolicyRelationship
from intersight.model.ntp_policy import NtpPolicy
from intersight.api import ntp_api
from intersight.model.smtp_policy import SmtpPolicy
from intersight.api import smtp_api
from intersight.model.snmp_policy import SnmpPolicy
from intersight.api import snmp_api
from intersight.model.snmp_user import SnmpUser
from intersight.model.organization_organization_relationship import OrganizationOrganizationRelationship
from intersight.model.compute_physical_relationship import ComputePhysicalRelationship
from intersight.api import compute_api
from intersight.exceptions import NotFoundException
from pprint import pprint
import intersight
import sys
api_key = "api_key"
api_key_file = "~/api_key_file_path"
api_client = client.get_api_client(api_key, api_key_file)
def create_organization():
# Creating and returning an instance of organization
return OrganizationOrganizationRelationship(class_id="mo.MoRef",
object_type="organization.Organization")
def create_policy_reference(policy_moid, obj_type):
return PolicyAbstractPolicyRelationship(moid=policy_moid,
object_type=obj_type,
class_id="mo.MoRef")
def create_server_profile():
api_instance = server_api.ServerApi(api_client)
# Create an instance of organization.
organization = create_organization()
# ServerProfile | The 'server.Profile' resource to create.
server_profile = ServerProfile()
# Setting all the attributes for server_profile instance.
server_profile.name = "sample_server_profile1"
server_profile.description = "sample server profile."
server_profile.organization = organization
# example passing only required values which don't have defaults set
try:
# Create a 'server.Profile' resource.
resp_server_profile = api_instance.create_server_profile(server_profile)
pprint(resp_server_profile)
return resp_server_profile
except intersight.ApiException as e:
print("Exception when calling ServerApi->create_server_profile: %s\n" % e)
sys.exit(1)
def create_ntp_policy():
api_instance = ntp_api.NtpApi(api_client)
# Create an instance of organization.
organization = create_organization()
# NtpPolicy | The 'ntp.Policy' resource to create.
ntp_policy = NtpPolicy()
# Setting all the attributes for server_profile instance.
ntp_policy.name = "sample_ntp_policy1"
ntp_policy.description = "sample ntp policy."
ntp_policy.organization = organization
ntp_servers = [
"10.10.10.250", "10.10.10.10", "10.10.10.20", "10.10.10.30"
]
ntp_policy.ntp_servers = ntp_servers
# example passing only required values which don't have defaults set
try:
# Create a 'ntp.Policy' resource.
resp_ntp_policy = api_instance.create_ntp_policy(ntp_policy)
pprint(resp_ntp_policy)
return resp_ntp_policy
except intersight.ApiException as e:
print("Exception when calling NtpApi->create_ntp_policy: %s\n" % e)
sys.exit(1)
def create_smtp_policy():
# Create an instance of the API class
api_instance = smtp_api.SmtpApi(api_client)
# Create an instance of organization.
organization = create_organization()
# SmtpPolicy | The 'smtp.Policy' resource to create.
smtp_policy = SmtpPolicy()
# Setting all the attributes for server_profile instance.
smtp_policy.name = "sample_smtp_policy1"
smtp_policy.description = "sample smtp policy."
smtp_policy.enabled = True
smtp_policy.min_severity = "critical"
smtp_policy.smtp_recipients = ["test@test"]
smtp_policy.smtp_server = "10.10.10.80"
smtp_policy.organization = organization
# example passing only required values which don't have defaults set
try:
# Create a 'smtp.Policy' resource.
resp_smtp_policy = api_instance.create_smtp_policy(smtp_policy)
pprint(resp_smtp_policy)
return resp_smtp_policy
except intersight.ApiException as e:
print("Exception when calling SmtpApi->create_smtp_policy: %s\n" % e)
sys.exit(1)
def create_snmp_policy():
# Create an instance of the API class
api_instance = snmp_api.SnmpApi(api_client)
# Create an instance of organization.
organization = create_organization()
# SmtpPolicy | The 'smtp.Policy' resource to create.
snmp_policy = SnmpPolicy()
# Setting all the attributes for server_profile instance.
snmp_policy.name = "sample_snmp_policy1"
snmp_policy.description = "sample snmp policy."
snmp_policy.enabled = True
snmp_policy.sys_location = "BLR"
snmp_policy.trap_community = "snmpv3"
snmp_policy.engine_id = "12121"
snmp_policy.snmp_port = 161
snmp_policy.sys_contact = "DA"
snmp_user = SnmpUser(class_id="snmp.User",
object_type="snmp.User",
auth_type="SHA",
privacy_type="AES",
security_level="AuthPriv",
name="user1",
auth_password="Auth_Snmp_user1",
privacy_password="Priv_Snmp_user1",
)
snmp_policy.snmp_users = [snmp_user]
snmp_policy.organization = organization
# example passing only required values which don't have defaults set
try:
# Create a 'Snmp.Policy' resource.
resp_snmp_policy = api_instance.create_snmp_policy(snmp_policy)
pprint(resp_snmp_policy)
return resp_snmp_policy
except intersight.ApiException as e:
print("Exception when calling SnmpApi->create_snmp_policy: %s\n" % e)
sys.exit(1)
def attach_server_to_profile(server_profile_moid):
api_instance = server_api.ServerApi(api_client)
# Creating the compute instance api.
compute_api_instance = compute_api.ComputeApi(api_client)
# Getting the list of compute rack unit.
compute_api_response = compute_api_instance.get_compute_rack_unit_list()
# Handling error scenario if get_compute_rack_unit_list does not return any entry.
if not compute_api_response.results:
raise NotFoundException(reason="The response does not contain any entry for compute rack unit."
"Please connect a compute rack unit and then attach the server profile.")
# Selecting the first compute rack unit to attach the server profile.
# Fetching the moid.
rack_unit_moid = compute_api_response.results[0].moid
# Creation of server profile model instance.
server_profile = ServerProfile()
# Creation of compute physical relationship object.
# This instance contains moid of chosen rack unit.
assigned_server = ComputePhysicalRelationship(class_id="mo.MoRef",
object_type="compute.RackUnit",
moid=rack_unit_moid)
# Setting the attribute for server profile with assigned server and server profile moid.
server_profile.assigned_server = assigned_server
# example passing only required values which don't have defaults set
try:
# Patching a 'Server.Profile' resource.
resp_server_profile = api_instance.patch_server_profile(moid=server_profile_moid,
server_profile=server_profile)
pprint(resp_server_profile)
return resp_server_profile
except intersight.ApiException as e:
print("Exception when calling ServerApi->patch_server_profile: %s\n" % e)
sys.exit(1)
def deploy_server_profile(server_profile_moid):
api_instance = server_api.ServerApi(api_client)
# Creation of server profile model instance.
server_profile = ServerProfile()
# Setting the attribute for server profile with the action and server profile moid.
server_profile.action = "Deploy"
# example passing only required values which don't have defaults set
try:
# Patching a 'Server.Profile' resource.
resp_server_profile = api_instance.patch_server_profile(moid=server_profile_moid,
server_profile=server_profile)
pprint(resp_server_profile)
return resp_server_profile
except intersight.ApiException as e:
print("Exception when calling ServerApi->patch_server_profile: %s\n" % e)
sys.exit(1)
def attach_policies_to_profile(policy_mapping, server_profile_moid):
policy_bucket = []
for obj_type, moid in policy_mapping.items():
policy = create_policy_reference(moid, obj_type)
policy_bucket.append(policy)
api_instance = server_api.ServerApi(api_client)
# ServerProfile | The 'server.Profile' resource to create.
server_profile = ServerProfile()
# Setting all the attributes for server_profile instance.
server_profile.policy_bucket = policy_bucket
# example passing only required values which don't have defaults set
try:
# Patch a 'server.Profile' resource.
resp_server_profile = api_instance.patch_server_profile(moid=server_profile_moid,
server_profile=server_profile)
pprint(resp_server_profile)
return resp_server_profile
except intersight.ApiException as e:
print("Exception when calling ServerApi->patch_server_profile: %s\n" % e)
sys.exit(1)
# create a server profile
server_profile_response = create_server_profile()
# Finding out server profile moid
server_profile_moid = server_profile_response.moid
# create a couple of server policies
# Creating NTP policy
ntp_policy_response = create_ntp_policy()
# Creating SMTP policy
smtp_policy_response = create_smtp_policy()
# Creating SNMP policy
snmp_policy_response = create_snmp_policy()
# Creating a policy mapping with object Type as key and moid will be value.
policies = {
"ntp.Policy": ntp_policy_response.moid,
"smtp.Policy": smtp_policy_response.moid,
"snmp.Policy": snmp_policy_response.moid,
}
# attach the profile to policies
attach_policies_to_profile(policies, server_profile_moid)
# assign a server to the server profile
attach_server_to_profile(server_profile_moid)
# trigger deployment of the server profile
deploy_server_profile(server_profile_moid)