# -*- coding: utf-8 -*-
###
# (C) Copyright (2012-2017) Hewlett Packard Enterprise Development LP
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
###

from pprint import pprint
from hpOneView.oneview_client import OneViewClient
from config_loader import try_load_from_file

# This resource is only available on HPE Synergy

config = {
    "ip": "<oneview_ip>",
    "credentials": {
        "userName": "<username>",
        "password": "<password>"
    }
}

# Try load config from a file (if there is a config file)
config = try_load_from_file(config)

oneview_client = OneViewClient(config)

# The Interconnect Type URI which is permitted to form SAS interconnect map must be defined to run this example
permittedInterconnectTypeUri = '/rest/sas-interconnect-types/Synergy12GbSASConnectionModule'

# Create a SAS Logical Interconnect Group
data = {
    "name": "Test SAS Logical Interconnect Group",
    "state": "Active",
    "interconnectMapTemplate": {
        "interconnectMapEntryTemplates": [{
            "logicalLocation": {
                "locationEntries": [
                    {
                        "type": "Bay",
                        "relativeValue": 1
                    }, {
                        "type": "Enclosure",
                        "relativeValue": 1
                    }
                ]
            },
            "enclosureIndex": 1,
            "permittedInterconnectTypeUri": permittedInterconnectTypeUri
        }, {
            "logicalLocation": {
                "locationEntries": [
                    {
                        "type": "Bay",
                        "relativeValue": 4
                    }, {
                        "type": "Enclosure",
                        "relativeValue": 1
                    }
                ]
            },
            "enclosureIndex": 1,
            "permittedInterconnectTypeUri": permittedInterconnectTypeUri
        }]
    },
    "enclosureType": "SY12000",
    "enclosureIndexes": [1],
    "interconnectBaySet": 1
}
sas_lig_created = oneview_client.sas_logical_interconnect_groups.create(data)
print("\nSAS Logical Interconnect Group '{name}' created successfully.\n  uri = '{uri}'".format(**sas_lig_created))

# Get all SAS Logical Interconnect Groups
print("\nGet all SAS Logical Interconnect Groups")
sas_ligs = oneview_client.sas_logical_interconnect_groups.get_all()
for sas_lig in sas_ligs:
    print("\n   '{name}' at uri: {uri}".format(**sas_lig))

# Get a SAS Logical Interconnect Group by URI
print("\nGet a SAS Logical Interconnect Group by URI")
sas_lig_found_by_uri = oneview_client.sas_logical_interconnect_groups.get(sas_lig_created['uri'])
pprint(sas_lig_found_by_uri)

# Get a SAS Logical Interconnect Group by name
print("\nGet a SAS Logical Interconnect Group by name")
sas_lig_found_by_name = oneview_client.sas_logical_interconnect_groups.get_by('name', data['name'])
print("\n   '{name}' at uri: {uri}".format(**sas_lig_found_by_name[0]))

# Get SAS Interconnect Group by scope_uris
if oneview_client.api_version >= 600:
    sas_lig_by_scope_uris = oneview_client.sas_logical_interconnect_groups.get_all(
        scope_uris="\"'/rest/scopes/3bb0c754-fd38-45af-be8a-4d4419de06e9'\"")
    if len(sas_lig_by_scope_uris) > 0:
        print("found %d SAS Interconnect Groups" % (len(sas_lig_by_scope_uris)))
        i = 0
        while i < len(sas_lig_by_scope_uris):
            print("Found SAS Interconnect Group by scope_uris: '%s'.\n  uri = '%s'" % (sas_lig_by_scope_uris[i]['name'], sas_lig_by_scope_uris[i]['uri']))
            i += 1
        pprint(sas_lig_by_scope_uris)
    else:
        print("No SAS Interconnect Group found.")

# Update the SAS Logical Interconnect Group
print("\nUpdate the SAS Logical Interconnect Group")
resource_to_update = sas_lig_created.copy()
resource_to_update['name'] = 'Test SAS Logical Interconnect Group - Renamed'

sas_lig_updated = oneview_client.sas_logical_interconnect_groups.update(resource_to_update)
pprint(sas_lig_updated)

# Delete the SAS Logical Interconnect Group
oneview_client.sas_logical_interconnect_groups.delete(sas_lig_updated)
print("\nSAS Logical Interconnect Group deleted successfully")

# -*- coding: utf-8 -*-
###
# (C) Copyright (2012-2017) Hewlett Packard Enterprise Development LP
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
###

from pprint import pprint
from hpOneView.oneview_client import OneViewClient
from config_loader import try_load_from_file

# This resource is only available on HPE Synergy

config = {
    "ip": "<oneview_ip>",
    "credentials": {
        "userName": "<username>",
        "password": "<password>"
    }
}

# Try load config from a file (if there is a config file)
config = try_load_from_file(config)

oneview_client = OneViewClient(config)

# The Interconnect Type URI which is permitted to form SAS interconnect map must be defined to run this example
permittedInterconnectTypeUri = '/rest/sas-interconnect-types/Synergy12GbSASConnectionModule'

# Create a SAS Logical Interconnect Group
data = {
    "name": "Test SAS Logical Interconnect Group",
    "state": "Active",
    "interconnectMapTemplate": {
        "interconnectMapEntryTemplates": [{
            "logicalLocation": {
                "locationEntries": [
                    {
                        "type": "Bay",
                        "relativeValue": 1
                    }, {
                        "type": "Enclosure",
                        "relativeValue": 1
                    }
                ]
            },
            "enclosureIndex": 1,
            "permittedInterconnectTypeUri": permittedInterconnectTypeUri
        }, {
            "logicalLocation": {
                "locationEntries": [
                    {
                        "type": "Bay",
                        "relativeValue": 4
                    }, {
                        "type": "Enclosure",
                        "relativeValue": 1
                    }
                ]
            },
            "enclosureIndex": 1,
            "permittedInterconnectTypeUri": permittedInterconnectTypeUri
        }]
    },
    "enclosureType": "SY12000",
    "enclosureIndexes": [1],
    "interconnectBaySet": 1
}
sas_lig_created = oneview_client.sas_logical_interconnect_groups.create(data)
print("\nSAS Logical Interconnect Group '{name}' created successfully.\n  uri = '{uri}'".format(**sas_lig_created))

# Get all SAS Logical Interconnect Groups
print("\nGet all SAS Logical Interconnect Groups")
sas_ligs = oneview_client.sas_logical_interconnect_groups.get_all()
for sas_lig in sas_ligs:
    print("\n   '{name}' at uri: {uri}".format(**sas_lig))

# Get a SAS Logical Interconnect Group by URI
print("\nGet a SAS Logical Interconnect Group by URI")
sas_lig_found_by_uri = oneview_client.sas_logical_interconnect_groups.get(sas_lig_created['uri'])
pprint(sas_lig_found_by_uri)

# Get a SAS Logical Interconnect Group by name
print("\nGet a SAS Logical Interconnect Group by name")
sas_lig_found_by_name = oneview_client.sas_logical_interconnect_groups.get_by('name', data['name'])
print("\n   '{name}' at uri: {uri}".format(**sas_lig_found_by_name[0]))

# Get SAS Interconnect Group by scope_uris
if oneview_client.api_version >= 600:
    sas_lig_by_scope_uris = oneview_client.sas_logical_interconnect_groups.get_all(
        scope_uris="\"'/rest/scopes/3bb0c754-fd38-45af-be8a-4d4419de06e9'\"")
    if len(sas_lig_by_scope_uris) > 0:
        print("found %d SAS Interconnect Groups" % (len(sas_lig_by_scope_uris)))
        i = 0
        while i < len(sas_lig_by_scope_uris):
            print("Found SAS Interconnect Group by scope_uris: '%s'.\n  uri = '%s'" % (sas_lig_by_scope_uris[i]['name'], sas_lig_by_scope_uris[i]['uri']))
            i += 1
        pprint(sas_lig_by_scope_uris)
    else:
        print("No SAS Interconnect Group found.")

# Update the SAS Logical Interconnect Group
print("\nUpdate the SAS Logical Interconnect Group")
resource_to_update = sas_lig_created.copy()
resource_to_update['name'] = 'Test SAS Logical Interconnect Group - Renamed'

sas_lig_updated = oneview_client.sas_logical_interconnect_groups.update(resource_to_update)
pprint(sas_lig_updated)

# Delete the SAS Logical Interconnect Group
oneview_client.sas_logical_interconnect_groups.delete(sas_lig_updated)
print("\nSAS Logical Interconnect Group deleted successfully")

# -*- coding: utf-8 -*- ### # (C) Copyright (2012-2017) Hewlett Packard Enterprise Development LP # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. ### # This resource is only available on HPE Synergy # Try load config from a file (if there is a config file) # The Interconnect Type URI which is permitted to form SAS interconnect map must be defined to run this example # Create a SAS Logical Interconnect Group # Get all SAS Logical Interconnect Groups # Get a SAS Logical Interconnect Group by URI # Get a SAS Logical Interconnect Group by name # Get SAS Interconnect Group by scope_uris # Update the SAS Logical Interconnect Group # Delete the SAS Logical Interconnect Group