Skip to content

Commit

Permalink
Merge pull request #110 from MUTHU-RAKESH-27/main
Browse files Browse the repository at this point in the history
Set the logging levels, refactored the log statements and addressed the review comments
  • Loading branch information
madhansansel authored Jan 25, 2024
2 parents e6126d9 + 707b7bc commit e8dfe7d
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 175 deletions.
74 changes: 44 additions & 30 deletions plugins/modules/device_credential_intent.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,13 +890,15 @@ def get_site_id(self, site_name):
)
self.log("Received API response from 'get_site': {0}".format(response), "DEBUG")
if not response:
self.log("Failed to retrieve the site ID for the site name: {0}".format(site_name), "ERROR")
self.log("Failed to retrieve the site ID for the site name: {0}"
.format(site_name), "ERROR")
return None

_id = response.get("response")[0].get("id")
self.log("Site ID for the site name {0}: {1}".format(site_name, _id), "INFO")
except Exception as exec:
self.log("Exception occurred while getting site_id from the site_name: {0}".format(exec), "CRITICAL")
self.log("Exception occurred while getting site_id from the site_name: {0}"
.format(exec), "CRITICAL")
return None

return _id
Expand All @@ -918,9 +920,11 @@ def get_global_credentials_params(self):
function='get_all_global_credentials_v2',
)
global_credentials = global_credentials.get("response")
self.log("All Global Device Credentials Details " + str(global_credentials), "DEBUG")
self.log("All global device credentials details: {0}"
.format(global_credentials), "DEBUG")
except Exception as exec:
self.log("Exception occurred while getting global device credentials: {0}".format(exec), "CRITICAL")
self.log("Exception occurred while getting global device credentials: {0}"
.format(exec), "CRITICAL")
return None

return global_credentials
Expand Down Expand Up @@ -1469,7 +1473,7 @@ def get_have_device_credentials(self, CredentialDetails):
snmpV3 = self.get_snmpV3_params(snmpV3Details)
self.have.get("globalCredential").update({"snmpV3": snmpV3})

self.log("Global Device Credential Details: {0}"
self.log("Global device credential details: {0}"
.format(self.have.get("globalCredential")), "DEBUG")
self.msg = "Collected the Global Device Credential Details from the Cisco DNA Center"
self.status = "success"
Expand All @@ -1494,7 +1498,7 @@ def get_have(self, config):
CredentialDetails = config.get("global_credential_details")
self.get_have_device_credentials(CredentialDetails).check_return_status()

self.log("Current State (have): " + str(self.have), "INFO")
self.log("Current State (have): {0}".format(self.have), "INFO")
self.msg = "Successfully retrieved the details from the Cisco DNA Center"
self.status = "success"
return self
Expand Down Expand Up @@ -1680,7 +1684,7 @@ def get_want_device_credentials(self, CredentialDetails):
values = ["password", "description", "username", "id", "port"]
have_httpsRead = self.have.get("globalCredential").get("httpsRead")
for item in httpsRead:
self.log("Global Credentials Details: {0}"
self.log("Global credentials details: {0}"
.format(self.have.get("globalCredential")), "DEBUG")
if not have_httpsRead or have_httpsRead[have_httpsread_ptr] is None:
if want.get("want_create").get("httpsRead") is None:
Expand Down Expand Up @@ -2193,16 +2197,18 @@ def create_device_credentials(self):
return self

credential_params = want_create
self.log("Creating Global Credential API input parameters: {0}".format(credential_params), "DEBUG")
self.log("Creating global credential API input parameters: {0}"
.format(credential_params), "DEBUG")
response = self.dnac._exec(
family="discovery",
function='create_global_credentials_v2',
params=credential_params,
)
self.log("Received API response from 'create_global_credentials_v2': {0}".format(response), "DEBUG")
self.log("Received API response from 'create_global_credentials_v2': {0}"
.format(response), "DEBUG")
validation_string = "global credential addition performed"
self.check_task_response_status(response, validation_string).check_return_status()
self.log("Global Credential Created Successfully", "INFO")
self.log("Global credential created successfully", "INFO")
result_global_credential.update({
"Creation": {
"response": credential_params,
Expand Down Expand Up @@ -2245,7 +2251,8 @@ def update_device_credentials(self):
values = ["cliCredential", "snmpV2cRead", "snmpV2cWrite",
"httpsRead", "httpsWrite", "snmpV3"]
final_response = []
self.log("Desired State for Updation: {0}".format(want_update), "DEBUG")
self.log("Desired State for global device credentials updation: {0}"
.format(want_update), "DEBUG")
while flag:
flag = False
credential_params = {}
Expand All @@ -2261,11 +2268,13 @@ def update_device_credentials(self):
function='update_global_credentials_v2',
params=credential_params,
)
self.log("Received API response for 'update_global_credentials_v2': {0}".format(response), "DEBUG")
self.log("Received API response for 'update_global_credentials_v2': {0}"
.format(response), "DEBUG")
validation_string = "global credential update performed"
self.check_task_response_status(response, validation_string).check_return_status()
self.log("Updating Device Credential API input parameters: {0}".format(final_response), "DEBUG")
self.log("Global Device Credential Updated Successfully", "INFO")
self.log("Updating device credential API input parameters: {0}"
.format(final_response), "DEBUG")
self.log("Global device credential updated successfully", "INFO")
result_global_credential.update({
"Updation": {
"response": final_response,
Expand All @@ -2292,7 +2301,8 @@ def assign_credentials_to_site(self):
result_assign_credential = self.result.get("response")[0].get("assignCredential")
credential_params = self.want.get("assign_credentials")
final_response = []
self.log("Assigning Device Credential to site API input parameters: {0}".format(credential_params), "DEBUG")
self.log("Assigning device credential to site API input parameters: {0}"
.format(credential_params), "DEBUG")
if not credential_params:
result_assign_credential.update({
"No Assign Credentials": {
Expand All @@ -2313,12 +2323,14 @@ def assign_credentials_to_site(self):
function='assign_device_credential_to_site_v2',
params=credential_params,
)
self.log("Response for API assign_device_credential_to_site_v2: " +
str(response), "DEBUG")
self.log("Received API response for 'assign_device_credential_to_site_v2': {0}"
.format(response), "DEBUG")
validation_string = "desired common settings operation successful"
self.check_task_response_status(response, validation_string).check_return_status()
self.log("Device Credential Assigned to site {0} is Successfully.".format(site_ids), "INFO")
self.log("Desired State for Assign Credentials to a Site: {0}".format(final_response), "DEBUG")
self.log("Device credential assigned to site {0} is successfully."
.format(site_ids), "INFO")
self.log("Desired State for assign credentials to a site: {0}"
.format(final_response), "DEBUG")
result_assign_credential.update({
"Assign Credentials": {
"response": final_response,
Expand Down Expand Up @@ -2369,7 +2381,7 @@ def delete_device_credential(self, config):
result_global_credential = self.result.get("response")[0].get("globalCredential")
have_values = self.have.get("globalCredential")
final_response = {}
self.log("Global Device Credentials to be Deleted: {0}".format(have_values), "DEBUG")
self.log("Global device credentials to be deleted: {0}".format(have_values), "DEBUG")
credential_mapping = {
"cliCredential": "cli_credential",
"snmpV2cRead": "snmp_v2c_read",
Expand Down Expand Up @@ -2397,14 +2409,16 @@ def delete_device_credential(self, config):
function="delete_global_credential_v2",
params={"id": _id},
)
self.log("Received API response for 'delete_global_credential_v2': {0}".format(response), "DEBUG")
self.log("Received API response for 'delete_global_credential_v2': {0}"
.format(response), "DEBUG")
validation_string = "global credential deleted successfully"
self.check_task_response_status(response, validation_string).check_return_status()
final_response.get(item).append(_id)
config_itr = config_itr + 1

self.log("Deleting Device Credential API input parameters: {0}".format(final_response), "DEBUG")
self.log("Successfully Deleted Global Device Credential.", "INFO")
self.log("Deleting device credential API input parameters: {0}"
.format(final_response), "DEBUG")
self.log("Successfully deleted global device credential.", "INFO")
result_global_credential.update({
"Deletion": {
"response": final_response,
Expand Down Expand Up @@ -2448,8 +2462,8 @@ def verify_diff_merged(self, config):
self.log(str("Entered the verify function."), "DEBUG")
self.get_have(config)
self.get_want(config)
self.log("Current State (have): " + str(self.have), "INFO")
self.log("Desired State (want): " + str(self.want), "INFO")
self.log("Current State (have): {0}".format(self.have), "INFO")
self.log("Desired State (want): {0}".format(self.want), "INFO")

if config.get("global_credential_details") is not None:
if self.want.get("want_create"):
Expand Down Expand Up @@ -2481,11 +2495,11 @@ def verify_diff_merged(self, config):
self.status = "failed"
return self

self.log("Successfully validated Global Device Credential", "INFO")
self.log("Successfully validated global device credential", "INFO")
self.result.get("response")[0].get("globalCredential").update({"Validation": "Success"})

if config.get("assign_credentials_to_site") is not None:
self.log("Successfully validated the Assign Device Credential to Site", "INFO")
self.log("Successfully validated the assign device credential to site", "INFO")
self.result.get("response")[0].get("assignCredential").update({"Validation": "Success"})

self.msg = "Successfully validated the Global Device Credential and \
Expand All @@ -2507,8 +2521,8 @@ def verify_diff_deleted(self, config):
"""

self.get_have(config)
self.log("Current State (have): " + str(self.have), "INFO")
self.log("Desired State (want): " + str(self.want), "INFO")
self.log("Current State (have): {0}".format(self.have), "INFO")
self.log("Desired State (want): {0}".format(self.want), "INFO")

if config.get("global_credential_details") is not None:
have_global_credential = self.have.get("globalCredential")
Expand All @@ -2522,7 +2536,7 @@ def verify_diff_deleted(self, config):
self.status = "failed"
return self

self.log("Successfully validated absence of Global Device Credential.", "INFO")
self.log("Successfully validated absence of global device credential.", "INFO")
self.result.get("response")[0].get("globalCredential").update({"Validation": "Success"})

self.msg = "Successfully validated the absence of Global Device Credential."
Expand Down
Loading

0 comments on commit e8dfe7d

Please sign in to comment.