def get_db_users(pod, tenant, cluster_id, page=1, limit=100): capella_api = CapellaAPI(pod.url_public, tenant.api_secret_key, tenant.api_access_key, tenant.user, tenant.pwd) resp = capella_api.get_db_users(tenant.id, tenant.project_id, cluster_id, page, limit) return json.loads(resp.content)
def destroy_cluster(cluster): capella_api = CapellaAPI(cluster.pod.url_public, cluster.tenant.api_secret_key, cluster.tenant.api_access_key, cluster.tenant.user, cluster.tenant.pwd) resp = capella_api.delete_cluster(cluster.id) if resp.status_code != 202: raise Exception("Deleting Capella Cluster Failed.") time.sleep(10) while True: resp = capella_api.get_cluster_internal(cluster.tenant.id, cluster.tenant.project_id, cluster.id) content = json.loads(resp.content) if content.get("data"): CapellaUtils.log.info( "Cluster status %s: %s" % (cluster.cluster_config.get("name"), content.get("data").get("status").get("state"))) if content.get("data").get("status").get( "state") == "destroying": time.sleep(5) continue elif content.get("message") == 'Not Found.': CapellaUtils.log.info("Cluster is destroyed.") cluster.tenant.clusters.pop(cluster.id) break
def create_cluster(pod, tenant, cluster_details, timeout=1800): end_time = time.time() + timeout while time.time() < end_time: subnet = CapellaUtils.get_next_cidr() + "/20" CapellaUtils.log.info("Trying with cidr: {}".format(subnet)) cluster_details["place"]["hosted"].update({"CIDR": subnet}) cluster_details.update({"projectId": tenant.project_id}) capella_api = CapellaAPI(pod.url_public, tenant.api_secret_key, tenant.api_access_key, tenant.user, tenant.pwd) capella_api_resp = capella_api.create_cluster(cluster_details) # Check resp code , 202 is success if capella_api_resp.status_code == 202: break else: CapellaUtils.log.critical("Create capella cluster failed.") CapellaUtils.log.critical("Capella API returned " + str(capella_api_resp.status_code)) CapellaUtils.log.critical(capella_api_resp.json()["message"]) cluster_id = capella_api_resp.headers['Location'].split("/")[-1] CapellaUtils.log.info("Cluster created with cluster ID: {}"\ .format(cluster_id)) CapellaUtils.wait_until_done(pod, tenant, cluster_id, "Creating Cluster {}".format( cluster_details.get("clusterName")), timeout=timeout) cluster_srv = CapellaUtils.get_cluster_srv(pod, tenant, cluster_id) CapellaUtils.allow_my_ip(pod, tenant, cluster_id) servers = CapellaUtils.get_nodes(pod, tenant, cluster_id) return cluster_id, cluster_srv, servers
def main(cmd_line_args): cappella_api = CapellaAPI() if cmd_line_args.debug: capella_logging('debug') cappella_api.set_logging_level('DEBUG') else: capella_logging('info') bucket_configuration = { "replicas": cmd_line_args.replicas, "name": cmd_line_args.name, "memoryQuota": cmd_line_args.memory_quota } # Create the bucket capella_api_response = cappella_api.create_cluster_bucket( cmd_line_args.cluster_id, bucket_configuration) # Check response code , 201 is success if capella_api_response.status_code == 201: print("Creating bucket ") else: print("Failed to create bucket ") print("Capella API returned " + str(capella_api_response.status_code)) print("Full error message") print(capella_api_response.json()["message"])
def get_all_buckets(cluster): capella_api = CapellaAPI(cluster.pod.url_public, cluster.tenant.api_secret_key, cluster.tenant.api_access_key, cluster.tenant.user, cluster.tenant.pwd) resp = capella_api.get_buckets(cluster.tenant.id, cluster.tenant.project_id, cluster.id) return resp
def main(cmd_line_args): cappella_api = CapellaAPI() if cmd_line_args.debug: capella_logging('debug') cappella_api.set_logging_level('DEBUG') else: capella_logging('info') cluster_configuration = { "environment": "hosted", "clusterName": cmd_line_args.clusterName, "description": "Example hosted cluster create from Public API", "projectId": cmd_line_args.projectID, "place": { "singleAZ": False, "hosted": { "provider": "aws", "CIDR": "10.199.0.0/20", "region": "us-east-2" } }, "servers": [ { "compute": "m5.xlarge", "size": 3, "services": ["data", "index", "search", "query"], "storage": { "size": 50, "IOPS": 3000, "type": "GP3" }, } ], "supportPackage": { "timezone": "GMT", "type": "DeveloperPro" }, "version": "latest", } # Create the cluster, indicate that this is a hosted cluster by calling with True # and pass the configuration capella_api_response = cappella_api.create_cluster(True, cluster_configuration) # Check response code , 202 is success if capella_api_response.status_code == 202: print("Creating cluster ") print("Check deployment status here: " + cappella_api.API_BASE_URL + capella_api_response.headers['Location'] + '/status') else: print("Failed to create cluster ") print("Capella API returned " + str(capella_api_response.status_code)) print("Full error message") print(capella_api_response.json()["message"])
def create_project(pod, tenant, name): capella_api = CapellaAPI(pod.url_public, tenant.api_secret_key, tenant.api_access_key, tenant.user, tenant.pwd) resp = capella_api.create_project(tenant.id, name) if resp.status_code != 201: raise Exception("Creating capella project failed: {}".format( resp.content)) project_id = json.loads(resp.content).get("id") tenant.project_id = project_id CapellaUtils.log.info("Project ID: {}".format(project_id))
def main(cmd_line_args): cappella_api = CapellaAPI() if cmd_line_args.debug: capella_logging('debug') cappella_api.set_logging_level('DEBUG') else: capella_logging('info') capella_api_response = cappella_api.get_clusters() # Check response code , 200 is success if capella_api_response.status_code == 200: clusters_table_rows = [] # There could not be any clusters, lets check if that's the case if capella_api_response.json()['data'] == {}: cluster_entries = None print("No clusters found") else: cluster_entries = capella_api_response.json()['data']['items'] # Got a list of clusters, just double check that we do have entries cluster_table_heading = [ 'Environment', 'Name', 'Cluster ID', 'Cloud ID', 'Project ID' ] for cluster_entry in cluster_entries: cluster_environment = cluster_entry['environment'] cluster_id = cluster_entry['id'] cluster_name = cluster_entry['name'] project_id = cluster_entry['projectId'] # if the cluster is inVPC, then we will have a cloud ID if cluster_environment == 'inVpc': clusterCloudID = cluster_entry['cloudId'] else: clusterCloudID = 'N/A' # Put the completed row in the table clusters_table_rows.append([ cluster_environment, cluster_name, cluster_id, clusterCloudID, project_id ]) # Display the table, which uses pretty table to make things easier print('Capella Clusters ') print(pretty_table(cluster_table_heading, clusters_table_rows)) else: print("Failed to get list of clusters ") print("Capella API returned " + str(capella_api_response.status_code)) print("Full error message") print(capella_api_response.json()["message"])
def get_cluster_info(pod, tenant, cluster_id): capella_api = CapellaAPI(pod.url_public, tenant.api_secret_key, tenant.api_access_key, tenant.user, tenant.pwd) resp = capella_api.get_cluster_info(cluster_id) if resp.status_code != 200: CapellaUtils.log.critical("LOG A BUG: Fetch Cluster API returns :\ {}".format(resp.status_code)) print(resp.content) time.sleep(5) return CapellaUtils.get_cluster_info(pod, tenant, cluster_id) return json.loads(resp.content)
def get_bucket_id(cluster, name): capella_api = CapellaAPI(cluster.pod.url_public, cluster.tenant.api_secret_key, cluster.tenant.api_access_key, cluster.tenant.user, cluster.tenant.pwd) resp = capella_api.get_buckets(cluster.tenant.id, cluster.tenant.project_id, cluster.id) content = json.loads(resp.content) bucket_id = None for bucket in content.get("buckets").get("data"): if bucket.get("data").get("name") == name: bucket_id = bucket.get("data").get("id") return bucket_id
def allow_my_ip(pod, tenant, cluster_id): capella_api = CapellaAPI(pod.url_public, tenant.api_secret_key, tenant.api_access_key, tenant.user, tenant.pwd) resp = capella_api.allow_my_ip(tenant.id, tenant.project_id, cluster_id) if resp.status_code != 202: result = json.loads(resp.content) if result["errorType"] == "ErrAllowListsCreateDuplicateCIDR": CapellaUtils.log.warn("IP is already added: %s" % result["message"]) return CapellaUtils.log.critical(resp.content) raise Exception("Adding allowed IP failed.")
def update_bucket_settings(cluster, bucket_id, bucket_params): capella_api = CapellaAPI(cluster.pod.url_public, cluster.tenant.api_secret_key, cluster.tenant.api_access_key, cluster.tenant.user, cluster.tenant.pwd) resp = capella_api.update_bucket_settings(cluster.tenant.id, cluster.tenant.project_id, cluster.id, bucket_id, bucket_params) code = resp.status if 200 > code or code >= 300: CapellaUtils.log.critical("Bucket update failed: %s" % resp.content) return resp.status
def scale(cluster, new_config): capella_api = CapellaAPI(cluster.pod.url_public, cluster.tenant.api_secret_key, cluster.tenant.api_access_key, cluster.tenant.user, cluster.tenant.pwd) while True: resp = capella_api.update_cluster_servers(cluster.id, new_config) if resp.status_code != 202: result = json.loads(resp.content) if result["errorType"] == "ClusterModifySpecsInvalidState": CapellaUtils.wait_until_done( cluster.pod, cluster.tenant, cluster.id, "Wait for healthy cluster state") else: break
def flush_bucket(cluster, name): bucket_id = CapellaUtils.get_bucket_id(cluster, name) if bucket_id: capella_api = CapellaAPI(cluster.pod.url_public, cluster.tenant.api_secret_key, cluster.tenant.api_access_key, cluster.tenant.user, cluster.tenant.pwd) resp = capella_api.flush_bucket(cluster.tenant.id, cluster.tenant.project_id, cluster.id, bucket_id) if resp.status >= 200 and resp.status < 300: CapellaUtils.log.info("Bucket deleted successfully!") else: CapellaUtils.log.info(resp.content) else: CapellaUtils.log.info("Bucket not found.")
def delete_bucket(cluster, name): bucket_id = CapellaUtils.get_bucket_id(cluster, name) if bucket_id: capella_api = CapellaAPI(cluster.pod.url_public, cluster.tenant.api_secret_key, cluster.tenant.api_access_key, cluster.tenant.user, cluster.tenant.pwd) resp = capella_api.delete_bucket(cluster.tenant.id, cluster.tenant.project_id, cluster.id, bucket_id) if resp.status_code == 204: CapellaUtils.log.info("Bucket deleted successfully!") else: CapellaUtils.log.critical(resp.content) raise Exception("Bucket {} cannot be deleted".format(name)) else: CapellaUtils.log.info("Bucket not found.")
def get_nodes(pod, tenant, cluster_id): capella_api = CapellaAPI(pod.url_public, tenant.api_secret_key, tenant.api_access_key, tenant.user, tenant.pwd) resp = capella_api.get_nodes(tenant.id, tenant.project_id, cluster_id) if resp.status_code != 200: CapellaUtils.log.critical( "LOG A BUG: Fetch Cluster Node API returns :\ {}".format(resp.status_code)) print(resp.content) time.sleep(5) return CapellaUtils.get_nodes(pod, tenant, cluster_id) CapellaUtils.log.info(json.loads(resp.content)) return [ server.get("data") for server in json.loads(resp.content).get("data") ]
def main(cmd_line_args): cappella_api = CapellaAPI() if cmd_line_args.debug: capella_logging('debug') cappella_api.set_logging_level('DEBUG') else: capella_logging('info') cluster_configuration = { "cloudId": cmd_line_args.cloudID, "name": cmd_line_args.clusterName, "projectId": cmd_line_args.projectID, "servers": [ { "services": [ "data", "index", "query", "search" ], "size": 3, "aws": { "ebsSizeGib": 50, "instanceSize": "m5.xlarge" } } ], "version": "latest" } # Create the cluster, pass the configuration # and indicate that this cluster will run in the customers own cloud by calling with False capella_api_response = cappella_api.create_cluster(False, cluster_configuration) # Check response code , 201 is success if capella_api_response.status_code == 202: print("Creating cluster ") print("Check deployment status here: " + cappella_api.API_BASE_URL + capella_api_response.headers['Location'] + '/status') else: print("Failed to create cluster ") print("Capella API returned " + str(capella_api_response.status_code)) print("Full error message") print(capella_api_response.json()["message"])
def main(CmdLineArgs): # This represents the new server service configuration new_server_service_configuration = { "servers": [{ "compute": "m5.xlarge", "size": 3, "services": ["data"], "storage": { "size": 300, "IOPS": 3000, "type": "GP3" } }] } cappella_api = CapellaAPI() if CmdLineArgs.debug: capella_logging('debug') cappella_api.set_logging_level('DEBUG') else: capella_logging('info') # Check API is up if cappella_api.api_status().status_code == 200: # Show the current cluster configuration # first we'll need to get the configuration capella_api_response = cappella_api.get_cluster_info( True, CmdLineArgs.ClusterID) #Then pass the bit we're interested in to the function that will display it if capella_api_response.status_code == 200: print('Current cluster configuration') show_cluster_server_configuration( capella_api_response.json()['servers']) # Now we will tell the API to change the configuration capella_api_response = cappella_api.update_cluster_servers( CmdLineArgs.ClusterID, new_server_service_configuration) # Check response code , 201 is success if capella_api_response.status_code == 202: # Got a response back print("Changing cluster to ") show_cluster_server_configuration( new_server_service_configuration['servers']) else: print("Failed to change the cluster configuration ") print("Capella API returned " + str(capella_api_response.status_code)) print("Full error message") print(capella_api_response.json()["message"]) else: print("Check Capella API is up.")
def create_db_user(pod, tenant, cluster_id, user, pwd): capella_api = CapellaAPI(pod.url_public, tenant.api_secret_key, tenant.api_access_key, tenant.user, tenant.pwd) resp = capella_api.create_db_user(tenant.id, tenant.project_id, cluster_id, user, pwd) if resp.status_code != 200: result = json.loads(resp.content) CapellaUtils.log.critical( "Add capella cluster user failed: (}".format(resp.status_code)) CapellaUtils.log.critical(result) if result["errorType"] == "ErrDataplaneUserNameExists": CapellaUtils.log.warn("User is already added: %s" % result["message"]) return CapellaUtils.create_db_user(pod, tenant, cluster_id, user, pwd) CapellaUtils.log.critical(json.loads(resp.content)) CapellaUtils.log.info(json.loads(resp.content)) return json.loads(resp.content)
def create_bucket(cluster, bucket_params): while True: state = CapellaUtils.get_cluster_state(cluster.pod, cluster.tenant, cluster.id) if state == "healthy": break time.sleep(1) capella_api = CapellaAPI(cluster.pod.url_public, cluster.tenant.api_secret_key, cluster.tenant.api_access_key, cluster.tenant.user, cluster.tenant.pwd) resp = capella_api.create_bucket(cluster.tenant.id, cluster.tenant.project_id, cluster.id, bucket_params) if resp.status_code in [200, 201, 202]: CapellaUtils.log.info("Bucket create successfully!") else: CapellaUtils.log.critical("Bucket creation failed: {}, {}".format( resp.status_code, resp.content)) raise Exception("Bucket creation failed")
def jobs(pod, tenant, cluster_id): capella_api = CapellaAPI(pod.url_public, tenant.api_secret_key, tenant.api_access_key, tenant.user, tenant.pwd) resp = capella_api.jobs(tenant.project_id, tenant.id, cluster_id) if resp.status_code != 200: CapellaUtils.log.critical("LOG A BUG: Internal API returns :\ {}".format(resp.status_code)) print(resp.content) time.sleep(5) return CapellaUtils.jobs(pod, tenant, cluster_id) try: content = json.loads(resp.content) except Exception as e: CapellaUtils.log.critical("LOG A BUG: Internal API returns :\ {}".format(resp.status_code)) print(resp.content) time.sleep(5) return CapellaUtils.jobs(pod, tenant, cluster_id) return content
def main(cmd_line_args): cappella_api = CapellaAPI() if cmd_line_args.debug: capella_logging('debug') cappella_api.set_logging_level('DEBUG') else: capella_logging('info') # Check Capella API status if cappella_api.api_status().status_code == 200: capella_api_response = cappella_api.get_cluster_status(True, cmd_line_args.ClusterID) if capella_api_response.status_code == 200: # Cluster information was found print( "Status for cluster with ID " + cmd_line_args.ClusterID + " is " + capella_api_response.json()['status']) else: print("Failed to get status for cluster ID " + cmd_line_args.ClusterID) print("Capella API returned " + str(capella_api_response.status_code)) print("Full error message") print(capella_api_response.json()["message"]) else: print("Check Capella API is up.")
def main(cmd_line_args): cappella_api = CapellaAPI() if cmd_line_args.debug: capella_logging('debug') cappella_api.set_logging_level('DEBUG') else: capella_logging('info') # Check Capella API status if cappella_api.api_status().status_code == 200: capella_api_response = cappella_api.get_cluster_info( False, cmd_line_args.ClusterID) if capella_api_response.status_code == 200: # Cluster information was found print("Got information for cluster ID " + cmd_line_args.ClusterID) print(json.dumps(capella_api_response.json(), indent=3)) else: print("Failed to get information for cluster ID " + cmd_line_args.ClusterID) print("Capella API returned " + str(capella_api_response.status_code)) else: print("Check Capella API is up.")
def main(cmd_line_args): cappella_api = CapellaAPI() if cmd_line_args.debug: capella_logging('debug') cappella_api.set_logging_level('DEBUG') else: capella_logging('info') # Check Capella API status if cappella_api.api_status().status_code == 200: capella_api_response = cappella_api.get_cluster_certificate( False, cmd_line_args.ClusterID) if capella_api_response.status_code == 200: # Cluster certificate was found print("Cluster certificate:\n" + capella_api_response.json()['certificate']) else: print("Failed to get certificate for cluster ID " + cmd_line_args.ClusterID) print("Capella API returned " + str(capella_api_response.status_code)) print("Full error message") print(capella_api_response.json()["message"]) else: print("Check Capella API is up.")
def main(cmd_line_args): cappella_api = CapellaAPI() if cmd_line_args.debug: capella_logging('debug') cappella_api.set_logging_level('DEBUG') else: capella_logging('info') # Check Capella API status if cappella_api.api_status().status_code == 200: capella_api_response = cappella_api.delete_project( cmd_line_args.projectID) if capella_api_response.status_code == 204: # Our project was created print("Deleted project with ID " + cmd_line_args.projectID) else: print("Failed to delete project with ID " + cmd_line_args.projectID) print("Capella API returned " + str(capella_api_response.status_code)) print("Full error message") print(capella_api_response.json()["message"]) else: print("Check Capella API is up.")
def main(CmdLineArgs): cappella_api = CapellaAPI() if CmdLineArgs.debug: capella_logging('debug') cappella_api.set_logging_level('DEBUG') else: capella_logging('info') # Check the status of the API, response code = 200 is success if cappella_api.api_status().status_code == 200: # Work out the bucket ID # which is the base64 value of the bucket name bucketName_bytes = CmdLineArgs.bucketName.encode('ascii') base64_bytes = base64.b64encode(bucketName_bytes) bucketName_base64 = base64_bytes.decode('ascii') # Get the current cluster buckets capella_api_response = cappella_api.get_cluster_buckets( CmdLineArgs.ClusterID) # We got the buckets, now check that we have the one we want to update if capella_api_response.status_code == 200: cluster_buckets = capella_api_response.json() found_bucket = False for cluster_bucket in cluster_buckets: if cluster_bucket['name'] == CmdLineArgs.bucketName: cluster_bucket['memoryQuota'] = CmdLineArgs.MemoryQuota found_bucket = True # Now update the bucket capella_api_response = cappella_api.update_cluster_bucket( CmdLineArgs.ClusterID, bucketName_base64, cluster_bucket) # Did the update work? if capella_api_response.status_code == 202: print("Bucket memory quota is being updated ") else: print("Failed to update bucket ") print("Capella API returned " + str(capella_api_response.status_code)) print("Full error message") print(capella_api_response.json()["message"]) if not found_bucket: # We didn't find the bucket to update print("Unable to find " + CmdLineArgs.bucketName + " to update on this cluster") else: print("This cluster cannot be found") print("Full error message") print(capella_api_response.json()["message"]) else: print("Check Capella API is up.")
def main(cmd_line_args): cappella_api = CapellaAPI() if cmd_line_args.debug: capella_logging('debug') cappella_api.set_logging_level('DEBUG') else: capella_logging('info') # Delete the cluster and indicate that this cluster will run in the Couchbase cloud # by calling with True capella_api_response = cappella_api.delete_cluster(True, cmd_line_args.clusterID) # Check response code , 201 is success if capella_api_response.status_code == 202: print("Deleting cluster ") print("Check status here: " + cappella_api.API_BASE_URL + capella_api_response.headers['Location'] + '/status') else: print("Failed to delete cluster ") print("Capella API returned " + str(capella_api_response.status_code)) print("Full error message") print(capella_api_response.json()["message"])
def main(cmd_line_args): cappella_api = CapellaAPI() if cmd_line_args.debug: capella_logging('debug') cappella_api.set_logging_level('DEBUG') else: capella_logging('info') cluster_allowlist_configuration = { "cidrBlock": cmd_line_args.CidrBlock, "ruleType": cmd_line_args.RuleType.lower(), } if cmd_line_args.RuleType.lower() == 'temporary': cluster_allowlist_configuration["duration"] = str( cmd_line_args.Duration) + "h0m0s" if cmd_line_args.RuleType.lower() == 'permanent': # If rule is permanent , Duration should not be given # Will look into seeing if arg parser can deal with this # For now, the check is here if cmd_line_args.Duration is not None: raise AllowlistRuleError( "Duration is not a permitted option for allow list when type is permanent" ) if cmd_line_args.Comment is not None: cluster_allowlist_configuration["comment"] = cmd_line_args.Comment # Check Capella API status if cappella_api.api_status().status_code == 200: capella_api_response = cappella_api.create_cluster_allowlist( cmd_line_args.ClusterID, cluster_allowlist_configuration) if capella_api_response.status_code == 202: print("Allow list is being created") else: print("Failed to create allowlist ") print("Capella API returned " + str(capella_api_response.status_code)) print("Full error message") print(capella_api_response.json()["message"]) else: print("Check Capella API is up.")
def main(cmd_line_args): cappella_api = CapellaAPI() if cmd_line_args.debug: capella_logging('debug') cappella_api.set_logging_level('DEBUG') else: capella_logging('info') # Check Capella API status if cappella_api.api_status().status_code == 200: capella_api_response = cappella_api.get_cluster_buckets(cmd_line_args.ClusterID) # Check response code , 200 is success if capella_api_response.status_code == 200: bucket_table_rows = [] list_of_buckets = capella_api_response.json() # We should have a list of buckets, but just in case, check # Then we will build rows to show in a table if list_of_buckets is not None: # Check to see if we got any buckets back if len(list_of_buckets) > 0: for bucket in list_of_buckets: bucket_table_rows.append(bucket.values()) print('Buckets') print(pretty_table(list_of_buckets[0].keys(), bucket_table_rows)) else: print("No buckets found") else: print("No buckets found") else: print("Failed to get buckets ") print("Capella API returned " + str(capella_api_response.status_code)) print("Full error message") print(capella_api_response.json()["message"]) else: print("Check Capella API is up.")
def main(cmd_line_args): cappella_api = CapellaAPI() if cmd_line_args.debug: capella_logging('debug') cappella_api.set_logging_level('DEBUG') else: capella_logging('info') # Check Capella API status if cappella_api.api_status().status_code == 200: capella_api_response = cappella_api.get_projects() # Check response code , 200 is success if capella_api_response.status_code == 200: project_table_rows = [] list_of_projects = capella_api_response.json() for project in list_of_projects['data']: project_table_rows.append([ project['name'], maya.parse(project['createdAt']), project['id'] ]) # Table heading / rows for the output project_table_headings = ['Name', 'Created at', 'ID'] print('Projects') print(pretty_table(project_table_headings, project_table_rows)) else: print("Failed to get projects ") print("Capella API returned " + str(capella_api_response.status_code)) print("Full error message") print(capella_api_response.json()["message"]) else: print("Check Capella API is up.")