Example #1
0
def make_volume(volume_name: str, vserver_name: str, aggr_name: str,
                net_path: str, volume_size: int) -> None:
    """Creates a new volume in a SVM"""

    data = {
        'name': volume_name,
        'svm': {
            'name': vserver_name
        },
        'aggregates': [{
            'name': aggr_name
        }],
        'size': volume_size,
        'nas': {
            'security_style': 'ntfs',
            'path': net_path
        }
    }

    volume = Volume(**data)

    try:
        volume.post()
        print("Volume %s created successfully" % volume.name)
    except NetAppRestError as err:
        print("Error: Volume was not created: %s" % err)
    return
Example #2
0
def delete_collection_volume() -> None:
    """Delete a collection of volumes"""
    print("=============================================")
    print()
    show_svm()
    print()
    svm_name = input(
        "Enter the SVM from which the Volumes need to be listed:-")
    print()
    show_volume(svm_name)
    print()

    noofnames = int(
        input(
            "Enter number of Volumes to be Deleted [eg: int value:1,2,3] : "))
    volume_names = list(
        map(
            str,
            input("\nEnter the Volume names to be Deleted [eg: aaa bbb ccc] : "
                  ).strip().split()))[:noofnames]
    volume_names_final = '|'.join([str(v) for v in volume_names])

    try:
        Volume.delete_collection(name=volume_names_final)
        print(list(Volume.get_collection()))
    except NetAppRestError as error:
        print("Exception caught :" + str(error))
Example #3
0
def patch_collection_volume(api, apiuser, apipass):
    """Turn the given volumes off then on again"""
    print("=============================================")
    print()
    show_volume(api, apiuser, apipass)
    print()
    noofnames = int(
        input(
            "Enter number of Volumes to be Updated [eg: int value:1,2,3] : "))
    volume_names = list(
        map(
            str,
            input("\nEnter the Volume names to be updated [eg: aaa bbb ccc] : "
                  ).strip().split()))[:noofnames]
    volume_names_final = '|'.join([str(v) for v in volume_names])
    vol_state = input("Enter the new state of the Volume [offline/online]: ")
    page_size = min(len(volume_names_final) - 1, 1)

    try:
        Volume.patch_collection({"state": vol_state},
                                name=volume_names_final,
                                max_records=page_size)
        print(
            list(Volume.get_collection(fields='state',
                                       name=volume_names_final)))
    except NetAppRestError as e:
        print("HTTP Error Code is " % e.http_err_response.http_response.text)
        print("Exception caught :" + str(e))
    return
Example #4
0
def patch_collection_volume() -> None:
    """Update the volume collection"""
    print("=============================================")
    print()
    show_svm()
    print()
    svm_name = input(
        "Enter the SVM from which the Volumes need to be listed:-")
    print()
    show_volume(svm_name)
    print()

    noofnames = int(
        input("Enter number of Volumes to be Updated [eg: int value:1,2,3] : "))
    volume_names = list(map(str, input(
        "\nEnter the Volume names to be updated [eg: aaa bbb ccc] : ").strip().split()))[:noofnames]
    volume_names_final = '|'.join([str(v) for v in volume_names])
    vol_state = input("Enter the new state of the Volume [offline/online]: ")
    print("Please ensure that the volumes are unmounted in case you are offlining.")
    page_size = min(len(volume_names_final) - 1, 1)

    try:
        Volume.patch_collection({"state": vol_state},
                                name=volume_names_final,
                                max_records=page_size)
        print(
            list(
                Volume.get_collection(
                    fields='state',
                    name=volume_names_final)))
    except NetAppRestError as error:
        print("Exception caught :" + str(error))
Example #5
0
def test_cert_auth(parsed_args: argparse.Namespace, temp_path: str) -> None:
    """Verify our account can connect with a cert and no user/pass. After uploading
    and creating a user account, it can take some time before authentication works.
    In my testing, I've seen it take about 10 seconds. The while loop here tries
    to paper over that.
    """

    substep("List the volumes using a connection with cert-based auth")
    cert_conn = HostConnection(
        parsed_args.cluster,
        cert="%s/cert_user.cert" % temp_path,
        key="%s/cert_user.key" % temp_path,
        verify=False,
    )
    with cert_conn:
        tries = 0
        while True:
            try:
                logging.info([vol.name for vol in Volume.get_collection()])
                logging.debug("Succeeded on try %s", tries)
                break
            except NetAppRestError as err:
                resp = err.http_err_response
                if resp is None or resp.http_response.status_code == 401:
                    tries += 1
                    if tries > 60:
                        logging.error(
                            "Not able to authenticate within 60 tries")
                        raise
                    time.sleep(1)
                else:
                    raise
def netappSnapshot(
    ontapClusterMgmtHostname: str, 
    pvName: str, 
    verifySSLCert: bool = True
) -> str :
    # Install netapp_ontap package
    import sys, subprocess;
    subprocess.run([sys.executable, '-m', 'pip', 'install', 'netapp_ontap'])
    
    # Import needed functions/classes
    from netapp_ontap import config as netappConfig
    from netapp_ontap.host_connection import HostConnection as NetAppHostConnection
    from netapp_ontap.resources import Volume, Snapshot
    from datetime import datetime
    import json

    # Retrieve ONTAP cluster admin account details from mounted K8s secrets
    usernameSecret = open('/mnt/secret/username', 'r')
    ontapClusterAdminUsername = usernameSecret.read().strip()
    passwordSecret = open('/mnt/secret/password', 'r')
    ontapClusterAdminPassword = passwordSecret.read().strip()
    
    # Configure connection to ONTAP cluster/instance
    netappConfig.CONNECTION = NetAppHostConnection(
        host = ontapClusterMgmtHostname,
        username = ontapClusterAdminUsername,
        password = ontapClusterAdminPassword,
        verify = verifySSLCert
    )
    
    # Convert pv name to ONTAP volume name
    # The following will not work if you specified a custom storagePrefix when creating your
    #   Trident backend. If you specified a custom storagePrefix, you will need to update this
    #   code to match your prefix.
    volumeName = 'trident_%s' % pvName.replace("-", "_")
    print('\npv name: ', pvName)
    print('ONTAP volume name: ', volumeName)

    # Create snapshot; print API response
    volume = Volume.find(name = volumeName)
    timestamp = datetime.today().strftime("%Y%m%d_%H%M%S")
    snapshot = Snapshot.from_dict({
        'name': 'kfp_%s' % timestamp,
        'comment': 'Snapshot created by a Kubeflow pipeline',
        'volume': volume.to_dict()
    })
    response = snapshot.post()
    print("\nAPI Response:")
    print(response.http_response.text)

    # Retrieve snapshot details
    snapshot.get()

    # Convert snapshot details to JSON string and print
    snapshotDetails = snapshot.to_dict()
    print("\nSnapshot Details:")
    print(json.dumps(snapshotDetails, indent=2))

    # Return name of newly created snapshot
    return snapshotDetails['name']
Example #7
0
    async def create_volume(self, message):
        """
        A skills function to create a volume. The parser looks for the message argument.

        Arguments:
            message {str} -- create a {size} MB volume called {name} on svm {svm} and aggregate {aggr}
        """
        name = message.regex.group('name')
        size = message.regex.group('size')
        aggr = message.regex.group('aggr')
        svm = message.regex.group('svm')
        volume = Volume.from_dict({
            'name': name,
            'svm': {
                'name': svm
            },
            'nas': {
                'path': '/' + name
            },
            'size': int(size) * 1024 * 1024,
            'aggregates': [{
                'name': aggr
            }]
        })
        volume.post()
        await message.respond('All done! Response: {}'.format(volume))
def get_vols_list():
    ### Step 1 - Read in global variables
    #with open(os.path.dirname(sys.argv[0])+'/global.vars') as json_file:
    with open(os.getcwd() + '/global.vars') as json_file:
        global_vars = json.load(json_file)

    ### Step 2 - Configure connection
    config.CONNECTION = HostConnection(global_vars["PRI_CLU"],
                                       username=global_vars["PRI_CLU_USER"],
                                       password=global_vars["PRI_CLU_PASS"],
                                       verify=False)

    # Volume
    myvols = []
    try:
        for volume in Volume.get_collection(**{
                "svm.name": global_vars["PRI_SVM"],
                "name": "!*_root"
        }):
            myvols.append(volume.name)
        print(json.dumps(myvols))
    except NetAppRestError as err:
        print("--> Error: Volume was not deleted:\n{}".format(err))

    return (myvols)
def clona_volumen(svm_name, vol_name, vol_uuid, num_clones) -> None:
    for i in range(1, num_clones + 1):
        dataobj = {}
        clone_name = vol_name + "__clon_" + str(i)

        tmp = {'name': svm_name}
        dataobj['svm'] = tmp
        dataobj['name'] = clone_name

        clone_volume_json = {
            "is_flexclone": bool("true"),
            "parent_svm": {
                "name": svm_name,
            },
            "parent_volume": {
                "name": vol_name,
                "uuid": vol_uuid
            }
        }

        dataobj['clone'] = clone_volume_json

        try:
            volume = Volume.from_dict(dataobj)
            if volume.post(poll=True):
                print("El CLON  %s se ha creado satisfactoriamente" %
                      volume.name)
        except NetAppRestError as error:
            print('ERROR !\n {0}'.format(
                error.http_err_response.http_response.text))
        print("\n")
Example #10
0
def print_vol_perf(message):
    try:
        config.CONNECTION = HostConnection('*.*.*.*',
                                           username='******',
                                           password='******',
                                           verify=False)
    except NetAppRestError as err:
        bot.send_message(
            message.chat.id,
            "Couldn't connect to the appliance. Error was: %s" % err)

    try:
        for volume in Volume.get_collection(
                **{"svm.name": message.text.split()[-1]}):
            volume.get()
            t = "Name: " + volume.name + '\n' + "Latency: " + '\n' + "Read: " + str(
                volume.metric.latency.read) + '\n' + "Write: " + str(
                    volume.metric.latency.write)
            #print(volume.metric.latency.read)

            bot.send_message(message.chat.id, t)

    except NetAppRestError as err:
        bot.send_message(
            message.chat.id, "Error: Volume list  was not created for SVM %s" %
            message.text.split()[-1])
def list_volume_pycl(svm_name: str) -> None:
    """List Volumes in a SVM """
    print("\n List of Volumes:- \n")
    try:
        print(*(vol.name
                for vol in Volume.get_collection(**{"svm.name": svm_name})),
              sep="\n")
    except NetAppRestError as err:
        print("Error: Volume list  was not created: %s" % err)
def devuelve_uid_vol(svm_name, vol_name):
    try:
        for volumen in Volume.get_collection(**{"svm.name": svm_name},
                                             **{"name": vol_name},
                                             fields="uuid"):
            return volumen.uuid
    except NetAppRestError as error:
        print("Por favor, INTRODUZCA EL NOMBRE DEL VOLUMEN!\n" +
              error.http_err_response.http_response.text)
Example #13
0
def delete_collection_volume() -> None:
    """Delete a collection of volumes"""
    print("=============================================")
    print()
    show_volume()
    print()
    noofnames = int(
        input("Enter number of Volumes to be Deleted [eg: int value:1,2,3] : "))
    volume_names = list(map(str, input(
        "\nEnter the Volume names to be Deleted [eg: aaa bbb ccc] : ").strip().split()))[:noofnames]
    volume_names_final = '|'.join([str(v) for v in volume_names])
    page_size = min(len(volume_names_final) - 1, 1)

    try:
        Volume.delete_collection(name=volume_names_final)
        print(list(Volume.get_collection()))
    except NetAppRestError as error:
        print("Error:- " % error.http_err_response.http_response.text)
        print("Exception caught :" + str(error))
def make_snap_pycl(vol_name: str, snapshot_name: str, svm_name: str) -> None:
    """Create a new snapshot with default settings for a given volume"""

    volume = Volume.find(**{'svm.name': svm_name, 'name': vol_name})
    snapshot = Snapshot(volume.uuid, name=snapshot_name)

    try:
        snapshot.post()
        print("Snapshot %s created successfully" % snapshot.name)
    except NetAppRestError as err:
        print("Error: Snapshot was not created: %s" % err)
Example #15
0
def delete_volume(volume_name: str) -> None:
    """Delete a volume in a SVM"""

    volume = Volume.find(name=volume_name)

    try:
        volume.delete()
        print("Volume %s deleted successfully" % volume.name)
    except NetAppRestError as err:
        print("Error: Volume was not deleted: %s" % err)
    return
def get_volume(volume_name: str) -> None:
    """Get the details of a volume"""
    volume = Volume.find(name=volume_name)
    #volumemetrics = VolumeMetrics(volume.uuid)
    volumemetrics = VolumeMetrics.get_collection(volume.uuid,
                                                 fields="iops.total",
                                                 interval="1h")
    print(
        f"IOPs over the last hour for volume {volume.name} was {volumemetrics.iops.total}"
    )
    return
Example #17
0
def list_volume_pycl(vserver_name: str) -> None:
    """List Volumes in a SVM """

    print("\n List of Volumes:- \n")
    try:
        for volume in Volume.get_collection(**{"svm.name": vserver_name}):
            volume.get()
            print(volume.name)
    except NetAppRestError as err:
        print("Error: Volume list  was not created: %s" % err)
    return
Example #18
0
    async def delete_volume(self, message):
        """
        A skills function to delete a volume. The parser looks for the message argument.

        Arguments:
            message {str} -- delete volume {name} on svm {svm}
        """
        name = message.regex.group('name')
        svm = message.regex.group('svm')
        volume = Volume.find(name=name, svm=svm)
        volume.delete()
        await message.respond('All done! Response: {}'.format(volume))
def show_volume(svm_name) -> None:
    """Show volumes in a SVM"""
    print()
    print("Getting Volume Details")
    print("======================")
    try:
        for volume in Volume.get_collection(**{"svm.name": svm_name},
                                            fields="uuid"):
            print("Name = %s; UUID = %s" % (volume.name, volume.uuid))
    except NetAppRestError as error:
        print("Error:- " % error.http_err_response.http_response.text)
        print("Exception caught :" + str(error))
Example #20
0
def get_volume(volume_name: str) -> None:
    """Get the details of a volume"""

    volume = Volume.find(name=volume_name)

    try:
        volume.get()
        print(volume.name)
        print("Volume details for %s obtained successfully" % volume.name)
    except NetAppRestError as err:
        print("Error: Volume details not obtained: %s" % err)
    return
Example #21
0
def resize_volume(volume_name: str, volume_resize: int) -> None:
    """Resize a volume in a SVM"""

    volume = Volume.find(name=volume_name)
    volume.size = volume_resize

    try:
        volume.patch()
        print("Volume %s resized successfully" % volume.name)
    except NetAppRestError as err:
        print("Error: Volume was not resized: %s" % err)
    return
def list_volumes(vserver_name: str) -> None:
    """List Volumes"""
    for volume in Volume.get_collection():
        one_hour_iops_total = []
        for metrics in VolumeMetrics.get_collection(volume.uuid,
                                                    fields="iops.total",
                                                    interval="1h"):
            one_hour_iops_total.append(metrics.iops.total)
        print(
            f"IOPs over the last hour for volume {volume.name} was {sum(one_hour_iops_total)}"
        )
    return
Example #23
0
def get_key_volume(svm_name, volume_name) -> None:
    """Lists Volumes"""
    print()
    print("Getting Volume Details")
    print("======================")
    try:
        for volume in Volume.get_collection(
                **{"svm.name": svm_name, 'volume.name': volume_name}, fields="uuid"):
            print(volume.uuid)
            return volume.uuid
    except NetAppRestError as error:
        print("Error:- " % error.http_err_response.http_response.text)
        print("Exception caught :" + str(error))
Example #24
0
def move_volume(volume_name: str, move_aggr_name: str) -> None:
    """Move the volume to a new aggregate"""

    volume = Volume.find(name=volume_name)
    volume.movement = VolumeMovement(
        destination_aggregate={'name': move_aggr_name})

    try:
        volume.patch()
        print("Volume %s moved successfully" % volume.name)
    except NetAppRestError as err:
        print("Error: Volume was not moved: %s" % err)
    return
Example #25
0
    async def get_volumes(self, message):
        """
        A skills function to get volumes on an SVM. The parser looks for the message argument.

        Arguments:
            message {str} -- get volumes on svm {svm}
        """
        svm = message.regex.group('svm')
        volumes = []
        for vol in Volume.get_collection(svm=svm):
            vol.get(fields='name')
            volumes.append(vol.name)
        await message.respond('All done! Response: {}'.format(volumes))
def make_snap(vol_name: str, snapshot_name: str) -> Optional[Snapshot]:
    """Create a new snapshot with default settings for a given volume"""

    volume = Volume.find(name=vol_name)
    snapshot = Snapshot(volume.uuid, name=snapshot_name)

    try:
        snapshot.post()
        print("Snapshot %s created successfullys" % snapshot.name)
        return snapshot
    except NetAppRestError as err:
        print("Error: Snapshot was not created: %s" % err)
    return None
Example #27
0
def show_volume(api, apiuser, apipass):
    config.CONNECTION = HostConnection(api, apiuser, apipass, verify=False)
    print("The List of SVMs")
    show_svm(api, apiuser, apipass)
    print()
    svm_name = input(
        "Enter the SVM from which the Volumes need to be listed:-")
    print()
    print("Getting Volume Details")
    print("===================")
    for volume in Volume.get_collection(**{"svm.name": svm_name},
                                        fields="uuid"):
        print("Volume Name = %s;  Volume UUID = %s" %
              (volume.name, volume.uuid))
    return
Example #28
0
def delete_collection_volume(api, apiuser, apipass):
    print("=============================================")
    print()
    show_volume(api, apiuser, apipass)
    print()
    noofnames = int(
        input(
            "Enter number of Volumes to be Deleted [eg: int value:1,2,3] : "))
    volume_names = list(
        map(
            str,
            input("\nEnter the Volume names to be Deleted [eg: aaa bbb ccc] : "
                  ).strip().split()))[:noofnames]
    volume_names_final = '|'.join([str(v) for v in volume_names])
    vol_state = input("Enter the new state of the Volume [offline/online]: ")
    page_size = min(len(volume_names_final) - 1, 1)

    try:
        Volume.delete_collection(name=volume_names_final)
        print(list(Volume.get_collection()))
    except NetAppRestError as e:
        print("HTTP Error Code is " % e.http_err_response.http_response.text)
        print("Exception caught :" + str(e))
    return
Example #29
0
def show_volume() -> None:
    """Lists Volumes"""
    show_svm()
    print()
    svm_name = input(
        "Enter the SVM from which the Volumes need to be listed:-")
    print()
    print("Getting Volume Details")
    print("======================")
    try:
        for volume in Volume.get_collection(**{"svm.name": svm_name},
                                            fields="uuid"):
            print("Volume Name = %s;  Volume UUID = %s" %
                  (volume.name, volume.uuid))
    except NetAppRestError as error:
        print("Error:- " % error.http_err_response.http_response.text)
        print("Exception caught :" + str(error))
Example #30
0
def delete_volume(api, apiuser, apipass):
    config.CONNECTION = HostConnection(api, apiuser, apipass, verify=False)

    print("=============================================")
    print()
    show_volume(api, apiuser, apipass)
    print()
    volname = input("Enter the name of the volume that needs to be Deleted:- ")
    vol = Volume.find(name=volname)

    try:
        if (vol.delete(poll=True)):
            print("Volume  has been deleted Successfully.")
    except NetAppRestError as e:
        print("HTTP Error Code is " % e.http_err_response.http_response.text)
        print("Exception caught :" + str(e))
    return