datapath-windows: Fix warning from the powershell module
[cascardo/ovs.git] / datapath-windows / misc / OVS.psm1
1 <#
2 Copyright 2014, 2015 Cloudbase Solutions Srl
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 #>
16
17 $WMI_JOB_STATUS_STARTED = 4096
18 $WMI_JOB_STATE_RUNNING = 4
19 $WMI_JOB_STATE_COMPLETED = 7
20
21 $hvassembly = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.HyperV.PowerShell")
22
23 function Set-VMNetworkAdapterOVSPort
24 {
25     [CmdletBinding()]
26     param
27     (
28         [parameter(Mandatory=$true, ValueFromPipeline=$true)]
29         [Microsoft.HyperV.PowerShell.VMNetworkAdapter]$VMNetworkAdapter,
30
31         [parameter(Mandatory=$true)]
32         [ValidateLength(1, 48)]
33         [string]$OVSPortName
34     )
35     process
36     {
37         $ns = "root\virtualization\v2"
38         $EscapedId = $VMNetworkAdapter.Id.Replace('\', '\\')
39
40         $sd = gwmi -namespace $ns -class Msvm_EthernetPortAllocationSettingData -Filter "ElementName = '$OVSPortName'"
41         if($sd)
42         {
43             if($sd.InstanceId.Contains($VMNetworkAdapter.Id))
44             {
45                 throw "The OVS port name '$OVSPortName' is already assigned to this port."
46             }
47             throw "Cannot assign the OVS port name '$OVSPortName' as it is already assigned to an other port."
48         }
49
50         $sd = gwmi -namespace $ns -class Msvm_EthernetPortAllocationSettingData -Filter "InstanceId like '$EscapedId%'"
51
52         if($sd)
53         {
54             $sd.ElementName = $OVSPortName
55
56             $vsms = gwmi -namespace $ns -class Msvm_VirtualSystemManagementService
57             $retVal = $vsms.ModifyResourceSettings(@($sd.GetText(1)))
58             try
59             {
60                 CheckWMIReturnValue $retVal
61             }
62             catch
63             {
64                 throw "Assigning OVS port '$OVSPortName' failed"
65             }
66         }
67     }
68 }
69
70 function Get-VMNetworkAdapterByOVSPort
71 {
72     [CmdletBinding()]
73     param
74     (
75
76         [parameter(Mandatory=$true)]
77         [ValidateLength(1, 48)]
78         [string]$OVSPortName
79     )
80     process
81     {
82         $ns = "root\virtualization\v2"
83
84         $sd = gwmi -namespace $ns -class Msvm_EthernetPortAllocationSettingData -Filter "ElementName = '$OVSPortName'"
85         if($sd)
86         {
87             return $sd
88         }
89     }
90 }
91
92 function Get-VMByOVSPort
93 {
94     [CmdletBinding()]
95     param
96     (
97         [parameter(Mandatory=$true)]
98         [ValidateLength(1, 48)]
99         [string]$OVSPortName
100     )
101     process
102     {
103         $ns = "root\virtualization\v2"
104
105         $vms = gwmi -namespace $ns -class Msvm_VirtualSystemSettingData
106         ForEach($vm in $vms)
107         {
108             $ports = gwmi -Namespace $ns -Query "
109                 Associators of {$vm} Where
110                 ResultClass = Msvm_EthernetPortAllocationSettingData"
111             if ($ports.ElementName -eq $OVSPortName)
112             {
113                 return $vm
114             }
115         }
116     }
117 }
118
119 #This function returns the Msvm_VirtualSystemSettingData given a VMName
120 function Get-VMNetworkAdapterWithOVSPort
121 {
122     [CmdletBinding()]
123     param
124     (
125         [parameter(Mandatory=$true)]
126         [ValidateLength(1, 1024)]
127         [string]$vmName
128     )
129     process
130     {
131         $ns = "root\virtualization\v2"
132         $vm = {}
133         $ports = {}
134
135         $vm = gwmi -namespace $ns -class Msvm_VirtualSystemSettingData -Filter "ElementName = '$VMName'"
136
137         $ports = gwmi -Namespace $ns -Query "
138                  Associators of {$vm} Where
139                  ResultClass = Msvm_EthernetPortAllocationSettingData"
140
141         return $ports
142     }
143 }
144
145 function CheckWMIReturnValue($retVal)
146 {
147     if ($retVal.ReturnValue -ne 0)
148     {
149         if ($retVal.ReturnValue -eq $WMI_JOB_STATUS_STARTED)
150         {
151             do
152             {
153                 $job = [wmi]$retVal.Job
154             }
155             while ($job.JobState -eq $WMI_JOB_STATE_RUNNING)
156
157             if ($job.JobState -ne $WMI_JOB_STATE_COMPLETED)
158             {
159                 echo $job.ReturnValue
160                 $errorString = "Job Failed. Job State: " + $job.JobState.ToString()
161                 if ($job.__CLASS -eq "Msvm_ConcreteJob")
162                 {
163                     $errorString += " Error Code: " + $job.ErrorCode.ToString()
164                     $errorString += " Error Details: " + $job.ErrorDescription
165                 }
166                 else
167                 {
168                     $error = $job.GetError()
169                     if ($error.Error)
170                     {
171                         $errorString += " Error:" + $error.Error
172                     }
173                 }
174                 throw $errorString
175             }
176         }
177         else
178         {
179             throw "Job Failed. Return Value: {0}" -f $job.ReturnValue
180         }
181     }
182 }
183
184 function Set-VMNetworkAdapterOVSPortDirect
185 {
186     [CmdletBinding()]
187     param
188     (
189         [parameter(Mandatory=$true)]
190         [ValidateLength(1, 1024)]
191         [string]$vmName,
192
193         [parameter(Mandatory=$true)]
194         [ValidateLength(1, 48)]
195         [string]$OVSPortName
196     )
197     process
198     {
199         $vnic = 0
200
201         if ($vmName)
202         {
203             $vnic = Get-VMNetworkAdapter -VMName $vmName
204         }
205         # XXX the vnic index should be provided by the caller
206         $vnic[0] | Set-VMNetworkAdapterOVSPort -OVSPortName $OVSPortName
207     }
208 }
209
210 Export-ModuleMember -function Set-*, Get-*