Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ugo-emekauwa authored Dec 7, 2022
1 parent b4ec05f commit edbaff8
Showing 1 changed file with 175 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@
intersight_base_url = "https://www.intersight.com/api/v1"
url_certificate_verification = True

# UCS Server Profile Attachment Settings
ucs_server_profile_name = ""
# UCS Server Profile Attachment Settings (If providing more than one UCS Server Profile and/or UCS Server Profile Template, additional entries should be comma-separated)
ucs_server_profile_names = ""
ucs_server_profile_template_names = ""

####### Finish Configuration Settings - The required value entries are complete. #######

Expand Down Expand Up @@ -351,7 +352,7 @@ def intersight_object_moid_retriever(intersight_api_key_id,
provided_organization_moid = intersight_object_moid_retriever(intersight_api_key_id=None,
intersight_api_key=None,
object_name=organization,
intersight_api_path="organization/Organizations",
intersight_api_path="organization/Organizations?$top=1000",
object_type="Organization",
preconfigured_api_client=api_client
)
Expand Down Expand Up @@ -550,6 +551,102 @@ def get_single_intersight_object(intersight_api_key_id,
sys.exit(0)


# Establish function to convert a list of strings in string type format to list type format.
def string_to_list_maker(string_list,
remove_duplicate_elements_in_list=True
):
"""This function converts a list of strings in string type format to list
type format. The provided string should contain commas, semicolons, or
spaces as the separator between strings. For each string in the list,
leading and rear spaces will be removed. Duplicate strings in the list are
removed by default.
Args:
string_list (str):
A string containing an element or range of elements.
remove_duplicate_elements_in_list (bool):
Optional; A setting to determine whether duplicate elements are
removed from the provided string list. The default value is True.
Returns:
A list of elements.
"""
def string_to_list_separator(string_list,
separator
):
"""This function converts a list of elements in string type format to
list type format using the provided separator. For each element in the
list, leading and rear spaces are removed.
Args:
string_list (str):
A string containing an element or range of elements.
separator (str):
The character to identify where elements in the
list should be separated (e.g., a comma, semicolon,
hyphen, etc.).
Returns:
A list of separated elements that have been stripped of any spaces.
"""
fully_stripped_list = []
# Split string by provided separator and create list of separated elements.
split_list = string_list.split(separator)
for element in split_list:
if element:
# Remove leading spaces from elements in list.
lstripped_element = element.lstrip()
# Remove rear spaces from elements in list.
rstripped_element = lstripped_element.rstrip()
# Populate new list with fully stripped elements.
fully_stripped_list.append(rstripped_element)
return fully_stripped_list

def list_to_list_separator(provided_list,
separator
):
"""This function converts a list of elements in list type format to
list type format using the provided separator. For each element in the
list, leading and rear spaces are removed.
Args:
provided_list (list): A list of elements to be separated.
separator (str): The character to identify where elements in the
list should be separated (e.g., a comma, semicolon,
hyphen, etc.).
Returns:
A list of separated elements that have been stripped of any spaces.
"""
new_list = []
# Split list by provided separator and create new list of separated elements.
for element in provided_list:
if separator in element:
split_provided_list = string_to_list_separator(element, separator)
new_list.extend(split_provided_list)
else:
new_list.append(element)
return new_list

staged_list = []
# Split provided list by spaces.
space_split_list = string_to_list_separator(string_list, " ")
# Split provided list by commas.
post_comma_split_list = list_to_list_separator(space_split_list, ",")
# Split provided list by semicolons.
post_semicolon_split_list = list_to_list_separator(post_comma_split_list, ";")
# Split provided list by hyphens.
for post_semicolon_split_string_set in post_semicolon_split_list:
staged_list.append(post_semicolon_split_string_set)
# Remove duplicates from list if enabled.
if remove_duplicate_elements_in_list:
final_list = list(set(staged_list))
return final_list


# Establish Maker specific classes and functions
class UcsPolicy:
"""This class is used to configure a UCS Policy in Intersight.
Expand Down Expand Up @@ -642,7 +739,7 @@ def _post_intersight_object(self):
existing_intersight_object_moid = intersight_object_moid_retriever(intersight_api_key_id=None,
intersight_api_key=None,
object_name=existing_intersight_object_name,
intersight_api_path=self.intersight_api_path,
intersight_api_path=f"{self.intersight_api_path}?$top=1000",
object_type=self.object_type,
preconfigured_api_client=self.api_client
)
Expand Down Expand Up @@ -693,7 +790,7 @@ def _update_api_body_general_attributes(self):
policy_organization_moid = intersight_object_moid_retriever(intersight_api_key_id=None,
intersight_api_key=None,
object_name=self.organization,
intersight_api_path="organization/Organizations",
intersight_api_path="organization/Organizations?$top=1000",
object_type="Organization",
preconfigured_api_client=self.api_client
)
Expand Down Expand Up @@ -945,8 +1042,8 @@ def object_maker(self):

class DirectlyAttachedUcsServerPolicy(UcsPolicy):
"""This class is used to configure a UCS Server Policy in Intersight that
is logically directly attached to UCS Servers through UCS
Server Profiles.
is logically directly attached to UCS Servers through UCS Server Profiles
and/or UCS Server Profile Templates.
"""
object_type = "Directly Attached UCS Server Policy"
intersight_api_path = None
Expand All @@ -960,7 +1057,8 @@ def __init__(self,
intersight_base_url="https://www.intersight.com/api/v1",
tags=None,
preconfigured_api_client=None,
ucs_server_profile_name=""
ucs_server_profile_names="",
ucs_server_profile_template_names=""
):
super().__init__(intersight_api_key_id,
intersight_api_key,
Expand All @@ -971,7 +1069,8 @@ def __init__(self,
tags,
preconfigured_api_client
)
self.ucs_server_profile_name = ucs_server_profile_name
self.ucs_server_profile_names = ucs_server_profile_names
self.ucs_server_profile_template_names = ucs_server_profile_template_names

def __repr__(self):
return (
Expand All @@ -984,35 +1083,60 @@ def __repr__(self):
f"'{self.intersight_base_url}', "
f"{self.tags}, "
f"{self.api_client}, "
f"'{self.ucs_server_profile_name}')"
f"'{self.ucs_server_profile_names}', "
f"'{self.ucs_server_profile_template_names}')"
)

def _attach_ucs_server_profile(self):
"""This is a function to attach an Intersight UCS Server Profile to an
Intersight Policy.
"""This is a function to attach Intersight UCS Server Profiles and/or
UCS Server Profile Templates to an Intersight Policy.
Returns:
A dictionary for the API body of the policy object to be posted on
Intersight.
"""
# Update the API body with the Profiles key
self.intersight_api_body["Profiles"] = []
# Attach UCS Server Profile
if self.ucs_server_profile_name:
print("Attaching the UCS Server Profile named "
f"{self.ucs_server_profile_name}...")
# Get UCS Server Profile MOID
ucs_server_profile_moid = intersight_object_moid_retriever(intersight_api_key_id=None,
intersight_api_key=None,
object_name=self.ucs_server_profile_name,
intersight_api_path="server/Profiles",
object_type="UCS Server Profile",
organization=self.organization,
preconfigured_api_client=self.api_client
)
# Update the API body with the appropriate Server Profile MOID
self.intersight_api_body["Profiles"] = [
{"Moid": ucs_server_profile_moid,
"ObjectType": "server.Profile"}
]
if self.ucs_server_profile_names:
provided_ucs_server_profile_list = string_to_list_maker(self.ucs_server_profile_names)
for provided_ucs_server_profile in provided_ucs_server_profile_list:
print("Attaching the UCS Server Profile named "
f"{provided_ucs_server_profile}...")
# Get UCS Server Profile MOID
ucs_server_profile_moid = intersight_object_moid_retriever(intersight_api_key_id=None,
intersight_api_key=None,
object_name=provided_ucs_server_profile,
intersight_api_path="server/Profiles?$top=1000",
object_type="UCS Server Profile",
organization=self.organization,
preconfigured_api_client=self.api_client
)
# Update the API body with the appropriate Server Profile MOID
self.intersight_api_body["Profiles"].append(
{"Moid": ucs_server_profile_moid,
"ObjectType": "server.Profile"}
)
# Attach UCS Server Profile Templates
if self.ucs_server_profile_template_names:
provided_ucs_server_profile_template_list = string_to_list_maker(self.ucs_server_profile_template_names)
for provided_ucs_server_profile_template in provided_ucs_server_profile_template_list:
print("Attaching the UCS Server Profile Template named "
f"{provided_ucs_server_profile_template}...")
# Get UCS Server Profile MOID
ucs_server_profile_template_moid = intersight_object_moid_retriever(intersight_api_key_id=None,
intersight_api_key=None,
object_name=provided_ucs_server_profile_template,
intersight_api_path="server/ProfileTemplates?$top=1000",
object_type="UCS Server Profile Template",
organization=self.organization,
preconfigured_api_client=self.api_client
)
# Update the API body with the appropriate Server Profile MOID
self.intersight_api_body["Profiles"].append(
{"Moid": ucs_server_profile_template_moid,
"ObjectType": "server.ProfileTemplate"}
)

def object_maker(self):
"""This function makes the targeted policy object.
Expand Down Expand Up @@ -1184,7 +1308,8 @@ def __init__(self,
intersight_base_url="https://www.intersight.com/api/v1",
tags=None,
preconfigured_api_client=None,
ucs_server_profile_name="",
ucs_server_profile_names="",
ucs_server_profile_template_names="",
enable_virtual_media=True,
enable_virtual_media_encryption=True,
enable_low_power_usb=True,
Expand All @@ -1198,7 +1323,8 @@ def __init__(self,
intersight_base_url,
tags,
preconfigured_api_client,
ucs_server_profile_name
ucs_server_profile_names,
ucs_server_profile_template_names
)
self.enable_virtual_media = enable_virtual_media
self.enable_virtual_media_encryption = enable_virtual_media_encryption
Expand Down Expand Up @@ -1226,7 +1352,8 @@ def __repr__(self):
f"'{self.intersight_base_url}', "
f"{self.tags}, "
f"{self.api_client}, "
f"'{self.ucs_server_profile_name}', "
f"'{self.ucs_server_profile_names}', "
f"'{self.ucs_server_profile_template_names}', "
f"{self.enable_virtual_media}, "
f"{self.enable_virtual_media_encryption}, "
f"{self.enable_low_power_usb}, "
Expand All @@ -1247,7 +1374,8 @@ def virtual_media_policy_maker(
intersight_base_url="https://www.intersight.com/api/v1",
tags=None,
preconfigured_api_client=None,
ucs_server_profile_name=""
ucs_server_profile_names="",
ucs_server_profile_template_names=""
):
"""This is a function used to make a Virtual Media Policy on Cisco Intersight.
Expand Down Expand Up @@ -1350,9 +1478,16 @@ def virtual_media_policy_maker(
is provided, empty strings ("") or None can be provided for the
intersight_api_key_id, intersight_api_key, and intersight_base_url
arguments.
ucs_server_profile_name (str):
Optional; The UCS Server Profile the policy should be attached to.
The default value is an empty string ("").
ucs_server_profile_names (str):
Optional; The UCS Server Profiles the policy should be attached
to. If providing more than one UCS Server Profile, additional
entries should be comma-separated. The default value is an empty
string ("").
ucs_server_profile_template_names (str):
Optional; The UCS Server Profile Templates the policy should be
attached to. If providing more than one UCS Server Profile Template,
additional entries should be comma-separated. The default value is
an empty string ("").
"""
def builder(target_object):
"""This is a function used to build the objects that are components of
Expand Down Expand Up @@ -1389,7 +1524,8 @@ def builder(target_object):
intersight_base_url=intersight_base_url,
tags=tags,
preconfigured_api_client=preconfigured_api_client,
ucs_server_profile_name=ucs_server_profile_name,
ucs_server_profile_names=ucs_server_profile_names,
ucs_server_profile_template_names=ucs_server_profile_template_names,
enable_virtual_media=enable_virtual_media,
enable_virtual_media_encryption=enable_virtual_media_encryption,
enable_low_power_usb=enable_low_power_usb,
Expand Down Expand Up @@ -1433,7 +1569,8 @@ def main():
intersight_base_url=intersight_base_url,
tags=virtual_media_policy_tags,
preconfigured_api_client=main_intersight_api_client,
ucs_server_profile_name=ucs_server_profile_name
ucs_server_profile_names=ucs_server_profile_names,
ucs_server_profile_template_names=ucs_server_profile_template_names
)

# Policy Maker completion
Expand Down

0 comments on commit edbaff8

Please sign in to comment.