|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | ### |
3 | | -# (C) Copyright (2012-2017) Hewlett Packard Enterprise Development LP |
| 3 | +# (C) Copyright (2012-2019) Hewlett Packard Enterprise Development LP |
4 | 4 | # |
5 | 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
6 | 6 | # of this software and associated documentation files (the "Software"), to deal |
|
25 | 25 | from pprint import pprint |
26 | 26 |
|
27 | 27 | from config_loader import try_load_from_file |
28 | | -from hpOneView.exceptions import HPOneViewException |
29 | 28 | from hpOneView.oneview_client import OneViewClient |
30 | 29 |
|
31 | 30 | # This resource is only available on C7000 enclosures |
|
38 | 37 | } |
39 | 38 | } |
40 | 39 |
|
41 | | -# To run the get operations by ID, an ID is required. |
42 | | -lsg_id = "ff94bbb5-c5a6-4f10-ac20-11ecf4cd4ecb" |
43 | | - |
44 | 40 | # To run the scope patch operations in this example, a scope name is required. |
45 | | -scope_name = "scope1" |
| 41 | +scope_name = "test" |
46 | 42 |
|
47 | 43 | options = { |
48 | 44 | "name": "OneView Test Logical Switch Group", |
|
61 | 57 |
|
62 | 58 | # Try load config from a file (if there is a config file) |
63 | 59 | config = try_load_from_file(config) |
64 | | - |
65 | 60 | oneview_client = OneViewClient(config) |
66 | | - |
67 | | -# Get switch type to use in creation of logical switch group |
68 | | -print("\nGet switch type to use in creation of logical switch group") |
69 | | -switch_type = oneview_client.switch_types.get_all()[0] |
70 | | -print(" Found switch type at uri: '{}'".format(switch_type['uri'])) |
71 | | - |
72 | | -# Create a logical switch group |
73 | | -print("\nCreate a logical switch group") |
74 | | -options['switchMapTemplate']['switchMapEntryTemplates'][0]['permittedSwitchTypeUri'] = switch_type['uri'] |
75 | | -created_lsg = oneview_client.logical_switch_groups.create(options) |
76 | | -print(" Created logical switch group '{name}' at uri: '{uri}'".format(**created_lsg)) |
| 61 | +logical_switch_groups = oneview_client.logical_switch_groups |
| 62 | +switch_types = oneview_client.switch_types |
77 | 63 |
|
78 | 64 | # Get all, with defaults |
79 | 65 | print("\nGet all Logical Switch Groups") |
80 | | -lsgs = oneview_client.logical_switch_groups.get_all() |
| 66 | +lsgs = logical_switch_groups.get_all() |
81 | 67 | for lsg in lsgs: |
82 | 68 | print(" '{name}' at uri: '{uri}'".format(**lsg)) |
83 | 69 |
|
84 | 70 | # Get the first 10 records, sorting by name descending, filtering by name |
85 | 71 | print("\nGet the first Logical Switch Groups, sorting by name descending, filtering by name") |
86 | | -lsgs = oneview_client.logical_switch_groups.get_all( |
| 72 | +lsgs = logical_switch_groups.get_all( |
87 | 73 | 0, 10, sort='name:descending', filter="\"'name'='OneView Test Logical Switch Group'\"") |
88 | 74 | for lsg in lsgs: |
89 | 75 | print(" '{name}' at uri: '{uri}'".format(**lsg)) |
90 | 76 |
|
91 | 77 | # Get Logical Switch by property |
92 | | -lsg_getby = oneview_client.logical_switch_groups.get_by('name', 'OneView Test Logical Switch Group')[0] |
93 | | -print("\nFound logical switch group by name: '{name}' at uri = '{uri}'".format(**lsg_getby)) |
| 78 | +lsg_getby = logical_switch_groups.get_by('name', 'OneView Test Logical Switch Group') |
| 79 | +if lsg_getby: |
| 80 | + print("\nFound logical switch group by name: '{name}' at uri = '{uri}'".format(**lsg_getby[0])) |
| 81 | + |
| 82 | + print("\nGet a Logical Switch Group by uri") |
| 83 | + lsg_byuri = logical_switch_groups.get_by_uri(lsg_getby[0]["uri"]) |
| 84 | + print(" Found logical switch group '{name}' by uri '{uri}'".format(**lsg_byuri.data)) |
| 85 | + |
| 86 | +# Get switch type to use in creation of logical switch group |
| 87 | +print("\nGet switch type to use in creation of logical switch group") |
| 88 | +switch_type = switch_types.get_by_name("Arista 7060X") |
| 89 | +print(" Found switch type at uri: '{}'".format(switch_type.data['uri'])) |
| 90 | + |
| 91 | +lsg = logical_switch_groups.get_by_name(options["name"]) |
| 92 | +if not lsg: |
| 93 | + # Create a logical switch group |
| 94 | + print("\nCreate a logical switch group") |
| 95 | + options['switchMapTemplate']['switchMapEntryTemplates'][0]['permittedSwitchTypeUri'] = switch_type.data['uri'] |
| 96 | + lsg = oneview_client.logical_switch_groups.create(options) |
| 97 | + print(" Created logical switch group '{name}' at uri: '{uri}'".format(**lsg.data)) |
94 | 98 |
|
95 | 99 | # Update a logical switch group |
96 | 100 | print("\nUpdate the name of a logical switch group") |
97 | | -lsg_to_update = created_lsg.copy() |
| 101 | +lsg_to_update = lsg.data.copy() |
98 | 102 | lsg_to_update["name"] = "Renamed Logical Switch Group" |
99 | | -updated_lsg = oneview_client.logical_switch_groups.update(lsg_to_update) |
100 | | -print(" Successfully updated logical switch group with name '{name}'".format(**updated_lsg)) |
| 103 | +lsg.update(lsg_to_update) |
| 104 | +print(" Successfully updated logical switch group with name '{name}'".format(**lsg.data)) |
101 | 105 |
|
102 | 106 | # Update a logical switch group by adding another switch with a relative value of 2 |
103 | 107 | print("\nUpdate a logical switch group by adding another switch with a relative value of 2") |
104 | | -lsg_to_update = updated_lsg.copy() |
| 108 | +lsg_to_update = lsg.data.copy() |
105 | 109 | switch_options = { |
106 | 110 | "logicalLocation": { |
107 | 111 | "locationEntries": [{ |
108 | 112 | "relativeValue": 2, |
109 | 113 | "type": "StackingMemberId", |
110 | 114 | }] |
111 | 115 | }, |
112 | | - "permittedSwitchTypeUri": switch_type['uri'] |
| 116 | + "permittedSwitchTypeUri": switch_type.data['uri'] |
113 | 117 | } |
114 | 118 | lsg_to_update['switchMapTemplate']['switchMapEntryTemplates'].append(switch_options) |
115 | | -updated_lsg = oneview_client.logical_switch_groups.update(lsg_to_update) |
116 | | -pprint(updated_lsg) |
| 119 | +lsg.update(lsg_to_update) |
| 120 | +pprint(lsg.data) |
117 | 121 |
|
118 | 122 | # Get scope to be added |
119 | 123 | print("\nGet the scope named '%s'." % scope_name) |
120 | 124 | scope = oneview_client.scopes.get_by_name(scope_name) |
121 | 125 |
|
122 | 126 | # Performs a patch operation on the Logical Switch Group |
123 | | -if scope: |
| 127 | +if scope and oneview_client.api_version <= 500: |
124 | 128 | print("\nPatches the logical switch group assigning the '%s' scope to it." % scope_name) |
125 | | - updated_lsg = oneview_client.logical_switch_groups.patch(updated_lsg['uri'], |
126 | | - 'replace', |
127 | | - '/scopeUris', |
128 | | - [scope['uri']]) |
129 | | - pprint(updated_lsg) |
130 | | - |
131 | | -# Get by ID |
132 | | -try: |
133 | | - print("\nGet a Logical Switch Group by ID '{}'".format(lsg_id)) |
134 | | - lsg_byid = oneview_client.logical_switch_groups.get(lsg_id) |
135 | | - print(" Found logical switch group '{name}' by ID at uri '{uri}'".format(**lsg_byid)) |
136 | | -except HPOneViewException as e: |
137 | | - print(e.msg) |
138 | | - |
139 | | -# Get by uri |
140 | | -try: |
141 | | - print("\nGet a Logical Switch Group by uri") |
142 | | - lsg_byuri = oneview_client.logical_switch_groups.get(created_lsg["uri"]) |
143 | | - print(" Found logical switch group '{name}' by uri '{uri}'".format(**lsg_byuri)) |
144 | | -except HPOneViewException as e: |
145 | | - print(e.msg) |
| 129 | + lsg.patch('replace', |
| 130 | + '/scopeUris', |
| 131 | + [scope['uri']]) |
| 132 | + pprint(lsg.data) |
146 | 133 |
|
147 | 134 | # Delete a logical switch group |
148 | 135 | print("\nDelete the created logical switch group") |
149 | | -oneview_client.logical_switch_groups.delete(updated_lsg) |
| 136 | +lsg.delete() |
150 | 137 | print(" Successfully deleted logical switch group") |
0 commit comments