def main(): # Get and log the config from the Env variable config = json.loads(os.environ["CUSTOM_SCRIPT_CONFIG"]) INFO(config) # Get PC info from the config dict pc_info = config.get("tdaas_pc") pc_external_ip = pc_info.get("ips")[0][0] pc_internal_ip = pc_info.get("ips")[0][1] pc_password = pc_info.get("prism_password") try: # Read in the spec files and conver to dicts subnet_spec = file_to_dict("specs/calm_subnet.spec") INFO(f"subnet_spec: {subnet_spec}") bp_spec = file_to_dict("specs/calm_bp_upload.spec") INFO(f"bp_spec: {bp_spec}") # Get our subnet info from the infra subnet_info = get_subnet_info(pc_external_ip, pc_password, subnet_spec["vlan"]) # Loop through the blueprints to upload for bp in bp_spec["entities"]: # Get our project uuid and create our payload project_uuid = uuid_via_v3_post(pc_external_ip, "projects", pc_password, bp["bp_project"]) payload = {"name": bp["bp_name"], "project_uuid": project_uuid} # Upload our blueprint resp = upload_bp_via_v3_post(pc_external_ip, pc_password, payload, bp["bp_file"]) # Log appropriately based on response if (resp.code == 200 or resp.code == 202): INFO(f"{bp['bp_name']} blueprint created successfully.") else: raise Exception(f"{bp['bp_name']} blueprint create" + f" failed with:\n" + f"Error Code: {resp.code}\n" + f"Error Message: {resp.message}") except Exception as ex: INFO(ex)
def main(): # Get and log the config from the Env variable config = json.loads(os.environ["CUSTOM_SCRIPT_CONFIG"]) print(config) # Get PC info from the config dict pc_info = config.get("tdaas_pc") pc_external_ip = pc_info.get("ips")[0][0] pc_internal_ip = pc_info.get("ips")[0][1] pc_password = pc_info.get("prism_password") try: # Read in the delete spec file delete_spec = file_to_dict("specs/calm_app_delete.json") # Loop through the apps to delete for app in delete_spec["entities"]: # Get the app uuid app_uuid = uuid_via_v3_post(pc_external_ip, "apps", pc_password, app["app_name"]) # Handle soft_delete if app["soft_delete"]: app_uuid = app_uuid + "?type=soft" # Delete the app resp = del_via_v3_delete(pc_external_ip, "apps", pc_password, app_uuid) # Log appropriately based on response if resp.code == 200 or resp.code == 202: print(f'{app["app_name"]} app deleted successfully.') else: raise Exception( 'f{app["app_name"]} app delete failed with:\n' + f"Resp: {resp}\n" + f"Error Code: {resp.code}\n" + f"Error Message: {resp.message}") except Exception as ex: print(traceback.format_exc()) sys.exit(1)
def main(): # Get and log the config from the Env variable config = json.loads(os.environ["CUSTOM_SCRIPT_CONFIG"]) INFO(config) # Get PC info from the config dict pc_info = config.get("tdaas_pc") pc_external_ip = pc_info.get("ips")[0][0] pc_internal_ip = pc_info.get("ips")[0][1] pc_password = pc_info.get("prism_password") try: # Read in the delete spec file delete_spec = file_to_dict("specs/calm_bp_delete.json") # Loop through the bps to delete for bp in delete_spec["entities"]: # Get the bp uuid bp_uuid = uuid_via_v3_post(pc_external_ip, "blueprints", pc_password, bp["bp_name"]) # Delete the bp resp = del_via_v3_delete(pc_external_ip, "blueprints", pc_password, bp_uuid) # Log appropriately based on response if resp.code == 200 or resp.code == 202: INFO(f'{bp["bp_name"]} blueprint deleted successfully.') else: raise Exception( 'f{bp["bp_name"]} blueprint delete failed with:\n' + f"Resp: {resp}\n" + f"Error Code: {resp.code}\n" + f"Error Message: {resp.message}") except Exception as ex: ERROR(traceback.format_exc())
def main(): # Get and log the config from the Env variable config = json.loads(os.environ["CUSTOM_SCRIPT_CONFIG"]) INFO(config) # Get PC info from the config dict pc_info = config.get("tdaas_pc") pc_external_ip = pc_info.get("ips")[0][0] pc_internal_ip = pc_info.get("ips")[0][1] pc_password = pc_info.get("prism_password") try: # Read in the spec files and convert to dicts env_spec = file_to_dict("specs/calm_environment.json") image_spec = file_to_dict("specs/pc_image.json") subnet_spec = file_to_dict("specs/calm_subnet.json") secret_spec = file_to_dict("specs/calm_secrets.json") # Get our subnet info from the infra subnet_info = get_subnet_info(pc_external_ip, pc_password, subnet_spec["entities"][0]["vlan"]) INFO(f"subnet_uuid: {subnet_info['uuid']}") # Get our image info from the infra cent_image_name = image_spec["entities"][0]["metadata"]["name"] cent_image_uuid = uuid_via_v3_post(pc_external_ip, "images", pc_password, cent_image_name) INFO(f"cent_image_uuid: {cent_image_uuid}") # win_image_name = image_spec["entities"][1]["metadata"]["name"] # win_image_uuid = uuid_via_v3_post(pc_external_ip, "images", # pc_password, win_image_name) # INFO(f"win_image_uuid: {win_image_uuid}") # Generate UUIDs for new components env_name = str(uuid.uuid4()) env_uuid = str(uuid.uuid4()) cent_substrate_uuid = str(uuid.uuid4()) # win_substrate_uuid = str(uuid.uuid4()) cent_key_uuid = str(uuid.uuid4()) cent_pass_uuid = str(uuid.uuid4()) # win_pass_uuid = str(uuid.uuid4()) # Sub in our UUIDs and names: # env env_spec["spec"]["name"] = env_name env_spec["metadata"]["name"] = env_name env_spec["metadata"]["uuid"] = env_uuid # substrate env_spec["spec"]["resources"]["substrate_definition_list"][0][ "uuid"] = cent_substrate_uuid # env_spec["spec"]["resources"]["substrate_definition_list"][1]\ # ["uuid"] = win_substrate_uuid # account env_spec["spec"]["resources"]["substrate_definition_list"][0][ "readiness_probe"]["login_credential_local_reference"][ "uuid"] = cent_key_uuid # env_spec["spec"]["resources"]["substrate_definition_list"][1]\ # ["readiness_probe"]["login_credential_local_reference"]\ # ["uuid"] = win_pass_uuid # subnet env_spec["spec"]["resources"]["substrate_definition_list"][0][ "create_spec"]["resources"]["nic_list"][0]["subnet_reference"][ "uuid"] = subnet_info["uuid"] # env_spec["spec"]["resources"]["substrate_definition_list"][1]\ # ["create_spec"]["resources"]["nic_list"][0]\ # ["subnet_reference"]["uuid"] = subnet_info["uuid"] # image env_spec["spec"]["resources"]["substrate_definition_list"][0][ "create_spec"]["resources"]["disk_list"][0][ "data_source_reference"]["name"] = cent_image_name env_spec["spec"]["resources"]["substrate_definition_list"][0][ "create_spec"]["resources"]["disk_list"][0][ "data_source_reference"]["uuid"] = cent_image_uuid # env_spec["spec"]["resources"]["substrate_definition_list"][1]\ # ["create_spec"]["resources"]["disk_list"][0]\ # ["data_source_reference"]["name"] = win_image_name # env_spec["spec"]["resources"]["substrate_definition_list"][1]\ # ["create_spec"]["resources"]["disk_list"][0]\ # ["data_source_reference"]["uuid"] = win_image_uuid # secrets for secret in secret_spec["entities"]: if secret["name"] == "CENTOS_KEY": suuid = cent_key_uuid elif secret["name"] == "CENTOS_PASS": suuid = cent_pass_uuid else: continue env_spec["spec"]["resources"]["credential_definition_list"].append( { "name": secret["name"], "type": secret["type"], "username": secret["username"], "secret": { "attrs": { "is_secret_modified": True }, "value": secret["secret"], }, "uuid": suuid, }) # Make the API call to create the environment INFO(f"env_spec: {env_spec}") resp = create_via_v3_post(pc_external_ip, "environments", pc_password, env_spec) # Log appropriately based on response if resp.code == 200 or resp.code == 202: INFO(f"{env_spec['spec']['name']} Env created successfully.") else: raise Exception( f"{env_spec['spec']['name']} Env create failed with:\n" + f"Resp: {resp}\n" + f"Error Code: {resp.code}\n" + f"Error Message: {resp.message}") except Exception as ex: ERROR(traceback.format_exc())
def main(): # Get and log the config from the Env variable config = json.loads(os.environ["CUSTOM_SCRIPT_CONFIG"]) INFO(config) # Get PC info from the config dict pc_info = config.get("tdaas_pc") pc_external_ip = pc_info.get("ips")[0][0] pc_internal_ip = pc_info.get("ips")[0][1] pc_password = pc_info.get("prism_password") try: # Convert our spec to dict subnet_spec = file_to_dict("specs/calm_subnet.json") # Get our info from the infra subnet_info = get_subnet_info( pc_external_ip, pc_password, subnet_spec["entities"][0]["vlan"] ) if "proxy_vm" in config: public_subnet_info = get_subnet_info( pc_external_ip, pc_password, subnet_spec["entities"][1]["vlan"] ) account_info = body_via_v3_post(pc_external_ip, "accounts", pc_password, None) env_uuid = uuid_via_v3_post(pc_external_ip, "environments", pc_password, "") # Get the pojects body project_resp = body_via_v3_post(pc_external_ip, "projects", pc_password, None) # Loop through each project to add the env for project in project_resp.json["entities"]: # Delete the unneeded "status" del project["status"] # If default project, add subnet_ref_list if project["spec"]["name"] == "default": # add subnet if not present if len(project["spec"]["resources"]["subnet_reference_list"]) == 0: project["spec"]["resources"]["subnet_reference_list"].append( { "kind": "subnet", "name": subnet_info["name"], "uuid": subnet_info["uuid"], } ) if "proxy_vm" in config: project["spec"]["resources"]["subnet_reference_list"].append( { "kind": "subnet", "name": public_subnet_info["name"], "uuid": public_subnet_info["uuid"], } ) # Add account if not present if len(project["spec"]["resources"]["account_reference_list"]) == 0: for account in account_info.json["entities"]: if account["status"]["resources"]["type"] == "nutanix_pc": project["spec"]["resources"][ "account_reference_list" ].append( { "kind": "account", "name": account["metadata"]["name"], "uuid": account["metadata"]["uuid"], } ) # Add env if not present if len(project["spec"]["resources"]["environment_reference_list"]) == 0: project["spec"]["resources"]["environment_reference_list"].append( {"kind": "environment", "uuid": env_uuid} ) # Make the API call to update the Project INFO(f"project: {project}") resp = update_via_v3_put( pc_external_ip, "projects", pc_password, project["metadata"]["uuid"], project, ) # Log appropriately based on response if resp.code == 202 or resp.code == 200: INFO(f"{project['spec']['name']} Project updated successfully.") else: raise Exception( f"{project['spec']['name']} Project update failed with:\n" + f"Resp: {resp}\n" + f"Error Code: {resp.code}\n" + f"Error Message: {resp.message}" ) except Exception as ex: ERROR(traceback.format_exc())