Exemple #1
0
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.")
Exemple #2
0
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 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.")
Exemple #4
0
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(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.")
Exemple #7
0
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"])
Exemple #8
0
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')

    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 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')

    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"])
Exemple #12
0
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.")
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_clouds()

        # Check response code , 200 is success
        if capella_api_response.status_code == 200:
            cloud_table_rows = []
            list_of_clouds = capella_api_response.json()

            for cloud in list_of_clouds['data']:
                cloud_table_rows.append(cloud.values())

            # Table heading / rows for the output
            # The JSON keys are always the same across all clouds so we can use the
            # keys from the first entry that we got
            cloud_table_headings = list_of_clouds['data'][0].keys()

            print('Projects')
            print(pretty_table(cloud_table_headings, cloud_table_rows))

        else:
            print("Failed to get clouds ")
            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.")
Exemple #15
0
def main(cmd_line_args):
    capella_api = CapellaAPI()

    if cmd_line_args.debug:
        capella_logging('debug')
        capella_api.set_logging_level('DEBUG')
    else:
        capella_logging('info')

    cluster_user_struct = {
        "password": cmd_line_args.Password,
        "username": cmd_line_args.UserName,
    }

    # Split up into the individual bits we need
    all_bucket_access = cmd_line_args.Access.strip()

    # Look at the access requested and then map to what the endpoint expects
    if all_bucket_access == 'r':
        cluster_user_struct['allBucketsAccess']="data_reader"
    elif all_bucket_access == 'w':
        cluster_user_struct['allBucketsAccess']="data_writer"
    else:
        raise UserBucketAccessListError('access not found')

    # Check Capella API status
    if capella_api.api_status().status_code == 200:
        capella_api_response = capella_api.create_cluster_user(False, cmd_line_args.ClusterID, cluster_user_struct)

        if capella_api_response.status_code == 201:
            print("Cluster user is being created")
        else:
            print("Failed to create user")
            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')

    bucket_configuration = {"cidrBlock": cmd_line_args.cidrBlock}

    if cappella_api.api_status().status_code == 200:
        capella_api_response = cappella_api.delete_cluster_allowlist(
            cmd_line_args.clusterID, bucket_configuration)

        # Did the delete work?
        if capella_api_response.status_code == 204:
            print("Deleting allowlist ")
        else:
            print("Failed to delete allowlist ")
            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')

    # 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):
    capella_api = CapellaAPI()

    if cmd_line_args.debug:
        capella_logging('debug')
        capella_api.set_logging_level('DEBUG')
    else:
        capella_logging('info')

    # Check Capella API status
    if capella_api.api_status().status_code == 200:
        capella_api_response = capella_api.delete_cluster_user(
            False, cmd_line_args.clusterID, cmd_line_args.UserName)

        if capella_api_response.status_code == 204:
            print("Cluster user is deleted")
        else:
            print("Failed to delete user")
            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')

    capella_api_response = cappella_api.get_cluster_users(
        False, cmd_line_args.ClusterID)

    # Check response code , 200 is success
    if capella_api_response.status_code == 200:
        cluster_user_table_rows = []
        cluster_users = capella_api_response.json()

        # We should have a list of users, but just in case, check
        # Then we will build rows to show in a table
        if cluster_users is not None:
            # Check to see if we got any users back
            if len(cluster_users) > 0:
                ClusterUserTableHeading = ['Name', 'Bucket', 'Access']
                for cluster_user in cluster_users:
                    user_name = cluster_user['username']
                    # We use this to determine if the user has access to more than one bucket
                    # to help with formatting the table
                    first_entry = True
                    for cluster_user_access in cluster_user['access']:
                        bucket_name = cluster_user_access['bucketName']
                        if bucket_name == '*':
                            bucket_name = 'All'
                        # Do they have read & write ?
                        if (len(cluster_user_access['bucketAccess'])) == 2:
                            bucket_access = cluster_user_access['bucketAccess'][0] + ' ' + \
                                            cluster_user_access['bucketAccess'][1]
                        else:
                            bucket_access = cluster_user_access[
                                'bucketAccess'][0]
                        # If it's the first entry, then we include the user name
                        # Otherwise we leave that out so the table looks ok
                        if first_entry:
                            cluster_user_table_rows.append(
                                [user_name, bucket_name, bucket_access])
                            first_entry = False
                        else:
                            cluster_user_table_rows.append(
                                ['', bucket_name, bucket_access])

                # Display the table, which uses pretty table to make things easier
                print('Cluster users')
                print(
                    pretty_table(ClusterUserTableHeading,
                                 cluster_user_table_rows))

            else:
                print("No users found for this cluster")
        else:
            print("No users found for this cluster")
    else:
        print("Failed to get list of users for this 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')

    # Check Capella API status
    if cappella_api.api_status().status_code == 200:
        capella_api_response = cappella_api.get_cluster_health(cmd_line_args.ClusterID)

        print("Cluster status: " + capella_api_response.json()["status"] + "\n")
        if capella_api_response.status_code == 200:
            # Show a table with the node information
            table_rows = []
            table_header = ["Node name", "Services", "Status"]

            if "nodeStats" in capella_api_response.json():
                table_entries = capella_api_response.json()["nodeStats"]
            else:
                table_entries = None
                print("Node information not available\n")

            # We should have a list of nodes, but just in case, check
            # Then we will build the table
            if table_entries is not None:
                # Make sure the list is not empty
                if len(table_entries) > 0:
                    cluster_node_count = str(table_entries["totalCount"])
                    for entry in table_entries["serviceStats"]:
                        service_list_as_str = ' '.join(entry["services"])
                        table_rows.append([entry["nodeName"], service_list_as_str, entry["status"]])

                    print('Cluster nodes: ' + cluster_node_count)
                    print(pretty_table(table_header, table_rows))

            # Reset everything, we're going to do another table with the bucket information
            table_rows = []
            table_header = ["Bucket name", "Status"]

            if "bucketStats" in capella_api_response.json():
                table_entries = capella_api_response.json()["bucketStats"]
            else:
                table_entries = None
                print("Bucket information not available\n")

            if table_entries is not None:
                # Make sure the list is not empty
                if len(table_entries) > 0:
                    cluster_bucket_count = str(table_entries["totalCount"])
                    for entry_key in table_entries["healthStats"].keys():
                        table_rows.append([entry_key, table_entries["healthStats"][entry_key]])

                    print('Cluster buckets: ' + cluster_bucket_count)
                    print(pretty_table(table_header, table_rows))

        else:
            print("Failed to get health 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.")
Exemple #21
0
def main(cmd_line_args):
    capella_api = CapellaAPI()

    if cmd_line_args.debug:
        capella_logging('debug')
        capella_api.set_logging_level('DEBUG')
    else:
        capella_logging('INFO')

    cluster_user_struct = {
        "password": cmd_line_args.password,
        "username": cmd_line_args.user_name,
        "buckets": []
    }

    # Go through each entry
    for bucket_and_scope_entry in cmd_line_args.buckets_and_scope.split(','):

        cluster_user_bucket_and_scope_access = {
            "name": "",
            "scope": "",
            "access": ""
        }

        # Split up into the individual bits we need
        bucket_and_scope_entry = bucket_and_scope_entry.strip()
        bucket_access = bucket_and_scope_entry.split(':')[2]
        bucket_scope = bucket_and_scope_entry.split(':')[1]
        bucket_name = bucket_and_scope_entry.split(':')[0]

        # Set the name of the bucket we're going to grant access to
        cluster_user_bucket_and_scope_access['name'] = bucket_name

        # Set the scope
        cluster_user_bucket_and_scope_access['scope'] = bucket_scope

        # Look at the access requested and then map to what the endpoint expects
        if bucket_access == 'r':
            cluster_user_bucket_and_scope_access["access"] = "data_reader"
        elif bucket_access == 'w':
            cluster_user_bucket_and_scope_access["access"] = "data_writer"
        elif bucket_access == 'rw':
            cluster_user_bucket_and_scope_access["access"] = "data_writer"
        elif bucket_access == 'wr':
            cluster_user_bucket_and_scope_access["access"] = "data_writer"
        else:
            # This should never have passed the regex test done in check_bucket_and_scope_type
            # but just in case....
            print('access not found for ' + bucket_and_scope_entry)
            raise UserBucketAccessListError('access not found for ' +
                                            bucket_and_scope_entry)

        cluster_user_struct["buckets"].append(
            cluster_user_bucket_and_scope_access)

    # Check Capella API status
    if capella_api.api_status().status_code == 200:
        capella_api_response = capella_api.create_cluster_user(
            True, cmd_line_args.cluster_id, cluster_user_struct)

        if capella_api_response.status_code == 201:
            print("Cluster user is being created")
        else:
            print("Failed to create user")
            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.")
Exemple #22
0
def main(CmdLineArgs):
    cappella_api = CapellaAPI()

    if CmdLineArgs.debug:
        capella_logging('debug')
        cappella_api.set_logging_level('DEBUG')
    else:
        capella_logging('info')

    # Caution :  The endpoint treats the allow list as a single resource
    #            This means the new, updated, configuration will replace what is already there
    #            We will get the current allow list, alter the allow list entry that we're changing
    #            and then send everything back.  The change just being the comment key : value
    #            If we didn't do this, we'd end up with a cluster with just the allow list entry we're updating...

    # Step 1 - get the allow list with all of its entries
    if cappella_api.api_status().status_code == 200:
        cluster_allowlist_from_API = cappella_api.get_cluster_allowlist(
            CmdLineArgs.ClusterID)

        # Check response code , 200 is success
        if cluster_allowlist_from_API.status_code == 200:

            # Did we actually get any entries in the allow list?
            if cluster_allowlist_from_API.json() is not None:
                # Yep, we did.
                # Step 2 - Find the allow list entry that we want to update. cidr is unique, so we can search on that
                allow_list_entry_found = False
                allow_list_entries = cluster_allowlist_from_API.json()
                for allow_list in allow_list_entries:
                    # The allow list cidr has /xx and we only need the first bit
                    # so we split it apart on the / and use the first entry in the resulting list
                    # which has an index of zero
                    if allow_list['cidrBlock'].split(
                            '/')[0] == CmdLineArgs.cidrBlock:
                        allow_list_entry_found = True

                        # There should always be a value for comment, but just in case
                        if CmdLineArgs.Comment is not None:
                            allow_list["Comment"] = CmdLineArgs.Comment

                        # We can now update the allow list entry
                        capella_api_response = cappella_api.update_cluster_allowlist(
                            CmdLineArgs.ClusterID, allow_list_entries)

                        # Did the update work?
                        if capella_api_response.status_code == 202:
                            print("Updating allow list ")
                        else:
                            print("Failed to update allow list ")
                            print("Capella API returned " +
                                  str(capella_api_response.status_code))
                            print("Full error message")
                            print(capella_api_response.json()["message"])

                if not allow_list_entry_found:
                    print("Unable to find " + CmdLineArgs.Name +
                          " for this cluster")
        else:
            print("Failed to find cluster for the allow list ")
            print("Capella API returned " +
                  str(cluster_allowlist_from_API.status_code))
            print("Full error message")
            print(cluster_allowlist_from_API.json()["message"])
def main(cmd_line_args):
    capella_api = CapellaAPI()

    if cmd_line_args.debug:
        capella_logging('debug')
        capella_api.set_logging_level('DEBUG')
    else:
        capella_logging('INFO')

    cluster_user_struct = {
        "password": cmd_line_args.Password,
        "username": cmd_line_args.UserName,
        "buckets": []
    }

    # Go through each entry
    for bucket_and_access_entry in cmd_line_args.Buckets.split(','):

        cluster_user_bucket_access_struct = {
            "bucketName": "",
            "bucketAccess": []
        }

        # Split up into the individual bits we need
        bucket_and_access_entry = bucket_and_access_entry.strip()
        bucket_access_requested = bucket_and_access_entry.split(':')[1]
        bucket_access_name = bucket_and_access_entry.split(':')[0]

        # Set the name of the bucket we're going to grant access to
        cluster_user_bucket_access_struct['bucketName'] = bucket_access_name

        # Look at the access requested and then map to what the endpoint expects
        if bucket_access_requested == 'r':
            cluster_user_bucket_access_struct["bucketAccess"].append(
                "data_reader")
        elif bucket_access_requested == 'w':
            cluster_user_bucket_access_struct["bucketAccess"].append(
                "data_writer")
        elif bucket_access_requested == 'rw':
            cluster_user_bucket_access_struct["bucketAccess"].append(
                "data_writer")
            cluster_user_bucket_access_struct["bucketAccess"].append(
                "data_reader")
        elif bucket_access_requested == 'wr':
            cluster_user_bucket_access_struct["bucketAccess"].append(
                "data_writer")
            cluster_user_bucket_access_struct["bucketAccess"].append(
                "data_reader")
        else:
            print('access not found for ' + bucket_and_access_entry)
            raise UserBucketAccessListError('access not found for ' +
                                            bucket_and_access_entry)

        cluster_user_struct["buckets"].append(
            cluster_user_bucket_access_struct)

    # Check Capella API status
    if capella_api.api_status().status_code == 200:
        capella_api_response = capella_api.create_cluster_user(
            False, cmd_line_args.ClusterID, cluster_user_struct)

        if capella_api_response.status_code == 201:
            print("Cluster user is being created")
        else:
            print("Failed to create user")
            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.")
Exemple #24
0
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_allowlist(
            cmd_line_args.ClusterID)
        # Check response code , 200 is success
        if capella_api_response.status_code == 200:
            allowlist_table_header = [
                "cidrBlock", "comment", "createdAt", "duration", "expiresAt",
                "ruleType", "state", "updatedAt"
            ]
            allowlist_table_rows = []
            cluster_allowlist = 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 cluster_allowlist is not None:
                # Check to see if we got any buckets back
                if len(cluster_allowlist) > 0:
                    for allowlist in cluster_allowlist:
                        # Make the two key:values with time stamps look nicer
                        allowlist['createdAt'] = maya.parse(
                            allowlist['createdAt'])
                        allowlist['updatedAt'] = maya.parse(
                            allowlist['updatedAt'])

                        if allowlist['ruleType'] == "permanent":
                            # These need to go into the right place in the dictionary
                            # or the table is messed up
                            new_allowlist = {}
                            for k in allowlist:
                                new_allowlist[k] = allowlist[k]
                                if k == 'createdAt':
                                    new_allowlist['duration'] = " n/a "
                                    new_allowlist['expiresAt'] = " n/a "
                            allowlist = new_allowlist
                        else:
                            allowlist['expiresAt'] = maya.parse(
                                allowlist['expiresAt'])

                        allowlist_table_rows.append(allowlist.values())

                    print('Allowlist')
                    print(
                        pretty_table(allowlist_table_header,
                                     allowlist_table_rows))
                else:
                    print("Allowlist not found")
            else:
                print("Allowlist not found")

        else:
            print("Failed to get 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.")