forked from dell/OpenManage-Enterprise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_static_group.py
More file actions
166 lines (141 loc) · 6.7 KB
/
Copy pathnew_static_group.py
File metadata and controls
166 lines (141 loc) · 6.7 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
#
# _author_ = Raajeev Kalyanaraman <Raajeev.Kalyanaraman@Dell.com>
#
# Copyright (c) 2022 Dell EMC Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
#### Synopsis
Script to create a new static group
#### Description
This script uses the OME REST API to create a new static
group. The user is responsible for adding devices to the
group once the group has been successfully created.
For authentication X-Auth is used over Basic Authentication
Note that the credentials entered are not stored to disk.
#### Python Example
`python new_static_group.py --ip <xx> --user <username> --password <pwd> --groupname "Random Test Group"`
"""
import argparse
import json
import sys
from argparse import RawTextHelpFormatter
from getpass import getpass
try:
import urllib3
import requests
except ModuleNotFoundError:
print("This program requires urllib3 and requests. To install them on most systems run `pip install requests"
"urllib3`")
sys.exit(0)
def authenticate(ome_ip_address: str, ome_username: str, ome_password: str) -> dict:
"""
Authenticates with OME and creates a session
Args:
ome_ip_address: IP address of the OME server
ome_username: Username for OME
ome_password: OME password
Returns: A dictionary of HTTP headers
Raises:
Exception: A generic exception in the event of a failure to connect.
"""
authenticated_headers = {'content-type': 'application/json'}
session_url = 'https://%s/api/SessionService/Sessions' % ome_ip_address
user_details = {'UserName': ome_username,
'Password': ome_password,
'SessionType': 'API'}
try:
session_info = requests.post(session_url, verify=False,
data=json.dumps(user_details),
headers=authenticated_headers)
except requests.exceptions.ConnectionError:
print("Failed to connect to OME. This typically indicates a network connectivity problem. Can you ping OME?")
sys.exit(0)
if session_info.status_code == 201:
authenticated_headers['X-Auth-Token'] = session_info.headers['X-Auth-Token']
return authenticated_headers
print("There was a problem authenticating with OME. Are you sure you have the right username, password, "
"and IP?")
raise Exception("There was a problem authenticating with OME. Are you sure you have the right username, "
"password, and IP?")
def create_static_group(authenticated_headers: dict, ome_ip_address: str, group_name: str, parent_group_name: str) -> int:
"""
Authenticate with OME and enumerate groups
Args:
authenticated_headers: A dictionary of HTTP headers generated from an authenticated session with OME
ome_ip_address: IP address of the OME server
group_name: The name of the group which you would like to create
Returns: Returns an integer containing the ID of the group or -1 if the group creation failed
"""
try:
group_url = "https://%s/api/GroupService/Groups?$filter=Name eq '%s'" % (ome_ip_address, parent_group_name)
response = requests.get(group_url, headers=authenticated_headers, verify=False)
if response.status_code == 200:
json_data = response.json()
if json_data['@odata.count'] > 0:
# Technically there should be only one result in the filter
group_id = json_data['value'][0]['Id']
group_payload = {"GroupModel": {
"Name": group_name,
"Description": "",
"MembershipTypeId": 12,
"ParentId": int(group_id)}
}
create_url = 'https://%s/api/GroupService/Actions/GroupService.CreateGroup' % ome_ip_address
create_resp = requests.post(create_url, headers=authenticated_headers,
verify=False,
data=json.dumps(group_payload))
if create_resp.status_code == 200:
print("New group created : ID =", create_resp.text)
return int(create_resp.text)
elif create_resp.status_code == 400:
print("Failed group creation ...See error info below")
print(json.dumps(create_resp.json(), indent=4,
sort_keys=False))
return -1
print("Unable to retrieve group list from %s" % ome_ip_address)
return -1
except Exception as error:
print("Unexpected error:", str(error))
if __name__ == '__main__':
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
parser = argparse.ArgumentParser(description=__doc__, formatter_class=RawTextHelpFormatter)
parser.add_argument("--ip", "-i", required=True, help="OME Appliance IP")
parser.add_argument("--user", "-u", required=False,
help="Username for OME Appliance", default="admin")
parser.add_argument("--password", "-p", required=False,
help="Password for OME Appliance")
parser.add_argument("--groupname", "-g", required=True,
help="A valid name for the group")
parser.add_argument("--parent-group-name", required=False,
help="Parent group name to create the group under", default="Static Groups")
args = parser.parse_args()
if not args.password:
if not sys.stdin.isatty():
# notify user that they have a bad terminal
# perhaps if os.name == 'nt': , prompt them to use winpty?
print("Your terminal is not compatible with Python's getpass module. You will need to provide the"
" --password argument instead. See https://stackoverflow.com/a/58277159/4427375")
sys.exit(0)
else:
password = getpass()
else:
password = args.password
try:
headers = authenticate(args.ip, args.user, password)
if not headers:
sys.exit(0)
create_static_group(headers, args.ip, args.groupname, args.parent_group_name)
except Exception as error:
print("Unexpected error:", str(error))