def groups(self, payload): Obj = get_resource_api(ahv.GROUPS, self.connection) if not payload: raise Exception("no payload") return Obj.create(payload)
def watch_task(task_uuid, poll_interval=2): client = get_api_client() Obj = get_resource_api("tasks", client.connection) cnt = 0 while True: LOG.info("Fetching status of task") res, err = Obj.read(task_uuid) if err: raise Exception("[{}] - {}".format(err["code"], err["error"])) res = res.json() status = res["status"] LOG.info(status) if status in ERGON_TASK.TERMINAL_STATES: error_detail = res.get("error_detail", "") if error_detail: LOG.error(error_detail) return status time.sleep(poll_interval) cnt += 1 if cnt == 10: break LOG.info( "Task couldn't reached to terminal state in {} seconds. Exiting...".format( poll_interval * 10 ) )
def get_server_status(): """Get calm server connection status""" LOG.info("Checking if Calm is enabled on Server") client = get_api_client() Obj = get_resource_api("services/nucalm/status", client.connection) res, err = Obj.read() if err: click.echo("[Fail]") raise Exception("[{}] - {}".format(err["code"], err["error"])) result = json.loads(res.content) service_enablement_status = result["service_enablement_status"] res, err = client.version.get_calm_version() calm_version = res.content.decode("utf-8") LOG.info(service_enablement_status) LOG.info("Server URL: {}".format(client.connection.base_url)) LOG.info("Calm Version: {}".format(calm_version)) res, err = client.version.get_pc_version() if not err: res = res.json() pc_version = res["version"] LOG.info("PC Version: {}".format(pc_version))
def sync(cls): """sync the table from server""" # clear old data cls.clear() client = get_api_client() Obj = get_resource_api("users", client.connection) res, err = Obj.list({"length": 1000}) if err: raise Exception("[{}] - {}".format(err["code"], err["error"])) res = res.json() for entity in res["entities"]: name = entity["status"]["name"] uuid = entity["metadata"]["uuid"] display_name = entity["status"]["resources"].get( "display_name") or "" directory_service_user = ( entity["status"]["resources"].get("directory_service_user") or dict()) directory_service_ref = ( directory_service_user.get("directory_service_reference") or dict()) directory_service_name = directory_service_ref.get("name", "LOCAL") if directory_service_name: cls.create_entry( name=name, uuid=uuid, display_name=display_name, directory=directory_service_name, )
def security_groups(self, account_id, region_name, vpc_id, inc_classic_sg=False): inc_classic_sg = "true" if inc_classic_sg else "false" payload = { "filter": "account_uuid=={};region=={};vpc_id=={};include_classic_sg=={}". format(account_id, region_name, vpc_id, inc_classic_sg) } Obj = get_resource_api(aws.SECURITY_GROUPS, self.connection) res, err = Obj.list(payload) if err: raise Exception("[{}] - {}".format(err["code"], err["error"])) res = res.json() sg_name_id_map = {} for entity in res["entities"]: sg_id = entity["status"]["resources"]["id"] name = entity["status"]["name"] sg_name_id_map[name] = sg_id return sg_name_id_map
def mixed_images(self, account_id, region_name): """Returns a map m[key] = (tupVal1, tupVal2) tupVal1 = id of the image tupVal2 = root_device_name of the image """ payload = { "filter": "account_uuid=={};region=={}".format(account_id, region_name) } Obj = get_resource_api(aws.MIXED_IMAGES, self.connection) res, err = Obj.list(payload) if err: raise Exception("[{}] - {}".format(err["code"], err["error"])) res = res.json() result = {} for entity in res["entities"]: name = entity["status"]["resources"]["name"] image_id = entity["status"]["resources"]["id"] root_device_name = entity["status"]["resources"]["root_device_name"] result[name] = (image_id, root_device_name) return result
def subnets(self, payload): Obj = get_resource_api(ahv.SUBNETS, self.connection) res, err = Obj.list(payload) if err: raise Exception("[{}] - {}".format(err["code"], err["error"])) return res.json()
def add_account_details(config): """add account details to config""" # Get api client client = get_api_client() # Add accounts details payload = {"length": 250, "filter": "state==VERIFIED;type!=nutanix"} res, err = client.account.list(payload) if err: raise Exception("[{}] - {}".format(err["code"], err["error"])) response = res.json() a_entities = response.get("entities", None) accounts = {} for a_entity in a_entities: account_type = a_entity["status"]["resources"]["type"].upper() if account_type not in accounts: accounts[account_type] = [] account_data = { "NAME": a_entity["status"]["name"], "UUID": a_entity["metadata"]["uuid"], } if account_type == "NUTANIX_PC": account_data["SUBNETS"] = [] Obj = get_resource_api("nutanix/v1/subnets", client.connection) payload = { "filter": "account_uuid=={}".format(account_data["UUID"]) } result, er = Obj.list(payload) if er: pass else: result = result.json() for entity in result["entities"]: cluster_ref = entity["status"]["cluster_reference"] cluster_name = cluster_ref.get("name", "") account_data["SUBNETS"].append({ "NAME": entity["status"]["name"], "CLUSTER": cluster_name, "UUID": entity["metadata"]["uuid"], }) # If it is local nutanix account, assign it to local nutanix ACCOUNT if a_entity["status"]["resources"]["data"].get("host_pc", False): accounts["NTNX_LOCAL_AZ"] = account_data accounts[account_type].append(account_data) # fill accounts data config["ACCOUNTS"] = accounts
def get_brownfield_vms(limit, offset, quiet, out, project_name, provider_type, account_name): """displays brownfield vms for a provider""" client = get_api_client() account_detail = get_account_details( project_name=project_name, account_name=account_name, provider_type=provider_type, pe_account_needed=True, ) project_uuid = account_detail["project"]["uuid"] account_name = account_detail["account"]["name"] account_uuid = account_detail["account"]["uuid"] LOG.info( "Using account '{}' for listing brownfield vms".format(account_name)) LOG.info("Fetching brownfield vms") Obj = get_resource_api("blueprints/brownfield_import/vms", client.connection) filter_query = "project_uuid=={};account_uuid=={}".format( project_uuid, account_uuid) params = {"length": limit, "offset": offset, "filter": filter_query} res, err = Obj.list(params=params) if err: LOG.error(err) sys.exit(-1) if out == "json": click.echo(json.dumps(res.json(), indent=4, separators=(",", ": "))) return json_rows = res.json()["entities"] if not json_rows: click.echo( highlight_text( "No brownfield {} found on account '{}' !!!\n".format( provider_type, account_name))) return if quiet: for _row in json_rows: row = _row["status"] click.echo(highlight_text(row["name"])) return if provider_type == "AHV_VM": get_brownfield_ahv_vm_list(json_rows) elif provider_type == "AWS_VM": get_brownfield_aws_vm_list(json_rows) elif provider_type == "AZURE_VM": get_brownfield_azure_vm_list(json_rows) elif provider_type == "GCP_VM": get_brownfield_gcp_vm_list(json_rows) elif provider_type == "VMWARE_VM": get_brownfield_vmware_vm_list(json_rows)
def test_describe_marketplace_item_with_default_version(self): payload = { "grouping_attribute": "app_group_uuid", "group_member_sort_attribute": "version", "group_member_sort_order": "DESCENDING", "group_count": 48, "group_member_count": 1, "group_offset": 0, "filter_criteria": "marketplace_item_type_list==APP;(app_state==PUBLISHED)", "entity_type": "marketplace_item", "group_member_attributes": [ { "attribute": "name" }, { "attribute": "version" }, ], } client = get_api_client() Obj = get_resource_api("groups", client.connection) res, err = Obj.create(payload=payload) if err: LOG.error("[{}] - {}".format(err["code"], err["error"])) sys.exit(-1) res = res.json() group_results = res["group_results"] if not group_results: pytest.skip("No marketplace item found.") group = group_results[0] entity_data = group["entity_results"][0]["data"] mpi_name = get_group_data_value(entity_data, "name") runner = CliRunner() LOG.info("Testing 'calm describe marketplace_item {}' command".format( mpi_name)) result = runner.invoke(cli, ["describe", "marketplace", "item", mpi_name]) if result.exit_code: LOG.error(result.output) pytest.fail("MPI list call failed") LOG.info("Success")
def describe_gcp_account(client, spec, account_id): click.echo("Project Id: {}".format(highlight_text(spec["project_id"]))) click.echo("Client Email: {}".format(highlight_text(spec["client_email"]))) click.echo("Token URI: {}".format(highlight_text(spec["token_uri"]))) click.echo("\nRegions:\n--------------\n") regions = spec["regions"] for index, region in enumerate(regions): click.echo("\t{}. {}".format(str(index + 1), highlight_text(region["name"]))) if not regions: click.echo("\t{}".format(highlight_text("No regions provided"))) click.echo("\nPublic Images:\n--------------\n") images = spec["public_images"] Obj = get_resource_api("gcp/v1/images", client.connection) payload = { "filter": "account_uuid=={};public_only==true".format(account_id) } res, err = Obj.list(payload) # TODO move this to GCP specific method if err: raise Exception("[{}] - {}".format(err["code"], err["error"])) public_images = res.json()["entities"] image_selfLink_name_map = {} for image in public_images: name = image["status"]["name"] selfLink = image["status"]["resources"]["selfLink"] image_selfLink_name_map[selfLink] = name for index, image in enumerate(images): name = image_selfLink_name_map.get(image["selfLink"], None) if name: click.echo("\t{}. {}".format(str(index + 1), highlight_text(name))) if not regions: click.echo(highlight_text("No regions provided")) click.echo("\nGKE Details:\n--------------\n") gke_config = spec["gke_config"] if not gke_config: click.echo("\t{}".format(highlight_text("GKE not enabled"))) else: click.echo("{}: {}".format("Port", highlight_text(str(gke_config["port"])))) click.echo("{}: {}".format("Server", highlight_text(gke_config["server"])))
def volume_types(self): Obj = get_resource_api(aws.VOLUME_TYPES, self.connection) res, err = Obj.list() if err: raise Exception("[{}] - {}".format(err["code"], err["error"])) entity_list = [] res = res.json() for entity in res["entities"]: entity_list.append(entity["metadata"]["name"]) return entity_list
def delete_acp(acp_name, project_name): client = get_api_client() params = {"length": 1000} project_name_uuid_map = client.project.get_name_uuid_map(params) project_uuid = project_name_uuid_map.get(project_name, "") if not project_uuid: LOG.error("Project '{}' not found.".format(project_name)) sys.exit(-1) LOG.info("Fetching project '{}' details".format(project_name)) ProjectInternalObj = get_resource_api("projects_internal", client.connection) res, err = ProjectInternalObj.read(project_uuid) if err: LOG.error(err) sys.exit(-1) project_payload = res.json() project_payload.pop("status", None) is_acp_present = False for _row in project_payload["spec"].get("access_control_policy_list", []): if _row["acp"]["name"] == acp_name: _row["operation"] = "DELETE" is_acp_present = True else: _row["operation"] = "UPDATE" if not is_acp_present: LOG.error( "ACP({}) is not associated with project({})".format(acp_name, project_name) ) sys.exit(-1) LOG.info( "Deleting acp '{}' associated with project '{}'".format(acp_name, project_name) ) res, err = ProjectInternalObj.update(project_uuid, project_payload) if err: LOG.error(err) sys.exit(-1) res = res.json() stdout_dict = { "name": acp_name, "execution_context": res["status"]["execution_context"], } click.echo(json.dumps(stdout_dict, indent=4, separators=(",", ": "))) LOG.info("Polling on acp deletion task") watch_task(res["status"]["execution_context"]["task_uuid"])
def regions(self, account_id): Obj = get_resource_api("accounts", self.connection) res, err = Obj.read(account_id) # TODO remove it from here if err: raise Exception("[{}] - {}".format(err["code"], err["error"])) region_list = [] res = res.json() entities = res["spec"]["resources"]["data"]["regions"] for entity in entities: region_list.append(entity["name"]) return region_list
def resource_groups(self, account_id): Obj = get_resource_api(azure.RESOURCE_GROUPS, self.connection) payload = {"filter": "account_uuid=={};".format(account_id)} res, err = Obj.list(payload) if err: raise Exception("[{}] - {}".format(err["code"], err["error"])) res_groups = [] res = res.json() for entity in res["entities"]: res_groups.append(entity["status"]["name"]) return res_groups
def zones(self, account_id, region="undefined"): Obj = get_resource_api(gcp.ZONES, self.connection) payload = {"filter": "account_uuid=={};region=={}".format(account_id, region)} res, err = Obj.list(payload) if err: raise Exception("[{}] - {}".format(err["code"], err["error"])) entity_list = [] res = res.json() for entity in res["entities"]: entity_list.append(entity["status"]["name"]) return entity_list
def configured_public_images(self, account_id): Obj = get_resource_api("accounts", self.connection) res, err = Obj.read(account_id) if err: raise Exception("[{}] - {}".format(err["code"], err["error"])) res = res.json() public_images = res["status"]["resources"]["data"]["public_images"] public_image_map = {} for entity in public_images: selfLink = entity["selfLink"] name = selfLink[selfLink.rindex("/") + 1 :] # noqa public_image_map[name] = selfLink return public_image_map
def availability_zones(self, account_id, region_name): payload = { "filter": "account_uuid=={};region=={}".format(account_id, region_name) } Obj = get_resource_api(aws.AVAILABILTY_ZONES, self.connection) res, err = Obj.list(payload) if err: raise Exception("[{}] - {}".format(err["code"], err["error"])) entity_list = [] res = res.json() for entity in res["entities"]: entity_list.append(entity["metadata"]["name"]) return entity_list
def sync(cls, *args, **kwargs): # clear old data cls.clear() # update by latest data client = get_api_client() Obj = get_resource_api("network_function_chains", client.connection) res, err = Obj.list({"length": 1000}) if err: raise Exception("[{}] - {}".format(err["code"], err["error"])) res = res.json() for entity in res["entities"]: name = entity["status"]["name"] uuid = entity["metadata"]["uuid"] cls.create_entry(name=name, uuid=uuid)
def locations(self, account_id): Obj = get_resource_api(azure.LOCATIONS, self.connection) payload = {"filter": "account_uuid=={};".format(account_id)} res, err = Obj.list(payload) if err: raise Exception("[{}] - {}".format(err["code"], err["error"])) res = res.json() name_value_map = {} for entity in res["entities"]: name = entity["status"]["resources"]["displayName"] value = entity["status"]["resources"]["name"] name_value_map[name] = value return name_value_map
def network_tags(self, account_id): Obj = get_resource_api(gcp.FIREWALLS, self.connection) payload = {"filter": "account_uuid=={};".format(account_id)} res, err = Obj.list(payload) if err: raise Exception("[{}] - {}".format(err["code"], err["error"])) entity_list = [] res = res.json() for entity in res["entities"]: targetTags = entity["status"]["resources"].get("targetTags") if targetTags: entity_list.extend(targetTags) return entity_list
def machine_types(self, account_id, zone): Obj = get_resource_api(gcp.MACHINE_TYPES, self.connection) payload = {"filter": "account_uuid=={};zone=={}".format(account_id, zone)} res, err = Obj.list(payload) if err: raise Exception("[{}] - {}".format(err["code"], err["error"])) entity_map = {} res = res.json() for entity in res["entities"]: name = entity["status"]["resources"]["name"] selfLink = entity["status"]["resources"]["selfLink"] entity_map[name] = selfLink return entity_map
def get_server_status(obj): """Get calm server connection status""" LOG.info("Checking if Calm is enabled on Server") client = get_api_client() Obj = get_resource_api("services/nucalm/status", client.connection) res, err = Obj.read() if err: click.echo("[Fail]") raise Exception("[{}] - {}".format(err["code"], err["error"])) result = json.loads(res.content) service_enablement_status = result["service_enablement_status"] LOG.info(service_enablement_status) LOG.info("Server URL: {}".format(client.connection.base_url))
def image_publishers(self, account_id, location): Obj = get_resource_api(azure.IMAGE_PUBLISHERS, self.connection) payload = { "filter": "account_uuid=={};location=={}".format(account_id, location) } res, err = Obj.list(payload) if err: raise Exception("[{}] - {}".format(err["code"], err["error"])) res = res.json() entity_list = [] for entity in res["entities"]: name = entity["status"]["name"] entity_list.append(name) return entity_list
def sync(cls): """sync the table from server""" # clear old data cls.clear() client = get_api_client() Obj = get_resource_api("directory_services", client.connection) res, err = Obj.list({"length": 1000}) if err: raise Exception("[{}] - {}".format(err["code"], err["error"])) res = res.json() for entity in res["entities"]: name = entity["status"]["name"] uuid = entity["metadata"]["uuid"] cls.create_entry(name=name, uuid=uuid)
def get_acps_from_project(client, project_uuid, **kwargs): """This routine gets acps from project using project uuid""" # get project details projects_intermal_obj = get_resource_api("projects_internal", client.connection) proj_info, err = projects_intermal_obj.read(project_uuid) if err: return None, err proj_info = proj_info.json() # construct acp info dict acps = {} acps["entities"] = [] role_uuid = kwargs.get("role_uuid", None) acp_name = kwargs.get("acp_name", None) limit = kwargs.get("limit", 20) offset = kwargs.get("offset", 0) terminate = False for acp in proj_info["status"]["access_control_policy_list_status"]: # role uuid filter if (role_uuid and role_uuid != acp["access_control_policy_status"] ["resources"]["role_reference"]["uuid"]): continue # acp name filter if acp_name and acp_name != acp["access_control_policy_status"]["name"]: continue elif acp_name: terminate = True (acps["entities"]).append({ "status": acp["access_control_policy_status"], "metadata": acp["metadata"] }) if terminate: break acps["metadata"] = {"total_matches": len(acps["entities"])} acps["entities"] = acps["entities"][offset:offset + limit] return acps, None
def get_app_family_list(): """returns the app family list categories""" client = get_api_client() Obj = get_resource_api("categories/AppFamily", client.connection) res, err = Obj.list(params={}) if err: LOG.error("[{}] - {}".format(err["code"], err["error"])) sys.exit(-1) res = res.json() categories = [] for entity in res["entities"]: categories.append(entity["value"]) return categories
def VPCs(self, account_id, region_name): payload = { "filter": "account_uuid=={};region=={}".format(account_id, region_name) } Obj = get_resource_api(aws.VPCS, self.connection) res, err = Obj.list(payload) if err: raise Exception("[{}] - {}".format(err["code"], err["error"])) res = res.json() vpc_cidr_id_map = {} for entity in res["entities"]: ip_blk = entity["status"]["resources"]["cidr_block"] vpc_id = entity["status"]["resources"]["id"] vpc_cidr_id_map[ip_blk] = vpc_id return vpc_cidr_id_map
def custom_images(self, account_id, location): Obj = get_resource_api(azure.SUBSCRIPTION_IMAGES, self.connection) payload = { "filter": "account_uuid=={};location=={}".format(account_id, location) } res, err = Obj.list(payload) if err: raise Exception("[{}] - {}".format(err["code"], err["error"])) res = res.json() name_id_map = {} for entity in res["entities"]: name = entity["status"]["resources"]["name"] id = entity["status"]["resources"]["id"] name_id_map[name] = id return name_id_map
def subnets(self, account_id, region_name, vpc_id, availability_zone): payload = { "filter": "account_uuid=={};region=={};vpc_id=={};availability_zone=={}".format( account_id, region_name, vpc_id, availability_zone ) } subnet_list = [] Obj = get_resource_api(aws.SUBNETS, self.connection) res, err = Obj.list(payload) res = res.json() for entity in res["entities"]: subnet_id = entity["status"]["resources"]["id"] subnet_list.append(subnet_id) return subnet_list