1
/* ============================================================================
2
Copyright 2014 Hewlett Packard
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
8
http://www.apache.org/licenses/LICENSE-2.0
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
using System.Management.Automation;
17
using Openstack.Client.Powershell.Cmdlets.Common;
18
using Openstack.Objects.Domain.Compute;
19
using Openstack.Objects.Domain.Compute.Servers;
20
using Openstack.Client.Powershell.Providers.Common;
21
using Openstack.Client.Powershell.Providers.Security;
22
using Openstack.Objects.DataAccess.Security;
24
using Openstack.Client.Powershell.Providers.Compute;
26
namespace Openstack.Client.Powershell.Cmdlets.Compute.Server
28
[Cmdlet(VerbsCommon.Reset, "Password", SupportsShouldProcess = true)]
29
[RequiredServiceIdentifierAttribute(Openstack.Objects.Domain.Admin.Services.Compute)]
30
public class ResetPasswordCmdlet : BasePSCmdlet
32
private string _administratorPassword;
33
private string _serverId;
36
//=========================================================================================
40
//=========================================================================================
41
[Parameter(Position = 1, ParameterSetName = "ResetPassword", Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Help Text")]
43
[ValidateNotNullOrEmpty]
44
public string ServerId
46
get { return _serverId; }
47
set { _serverId = value; }
49
//=========================================================================================
53
//=========================================================================================
54
[Parameter(Position = 0, ParameterSetName = "ResetPassword", Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Help Text")]
56
[ValidateNotNullOrEmpty]
57
public string AdministratorPassword
59
get { return _administratorPassword; }
60
set { _administratorPassword = value; }
64
//=========================================================================================
68
//=========================================================================================
69
private void ChangeWindowsImagePW()
71
CredentialListManager manager = new CredentialListManager(this.Settings);
72
if (this.Settings.EnableCredentialTracking == true)
74
manager.SetPassword(this.ServerId, this.AdministratorPassword);
75
Console.WriteLine("");
76
Console.WriteLine("Password Updated.");
77
Console.WriteLine("");
81
Console.WriteLine("");
82
Console.WriteLine("You're attempting to update the password for a Windows Image but EnableCredentialTracking is currently turned off. To turn this on please use the Get-config and Set-config cmdlets. Operation failed.");
83
Console.WriteLine("");
86
//=========================================================================================
90
//=========================================================================================
91
private void ChangeNonWindowsImagePW()
93
ChangePasswordAction action = new ChangePasswordAction();
94
action.AdministratorPassword = this.AdministratorPassword;
95
action.ServerId = this.ServerId;
97
this.RepositoryFactory.CreateServerRepository().ChangePassword(action);
98
Console.WriteLine("");
99
Console.WriteLine("Password Changed.");
100
Console.WriteLine("");
102
//=========================================================================================
106
/// <returns></returns>
107
//=========================================================================================
108
private string GetServerId()
110
BaseUIContainer currentContainer = this.SessionState.PSVariable.Get("CurrentContainer").Value as BaseUIContainer;
112
if (currentContainer.Name == "Metadata")
114
ServerUIContainer serverContainer = currentContainer.Parent as ServerUIContainer;
115
if (serverContainer != null)
116
return serverContainer.Entity.Id;
120
return currentContainer.Entity.Id;
124
//=========================================================================================
128
/// <returns></returns>
129
//=========================================================================================
130
private bool IsWindowsImage(string imageId)
132
Image image = this.RepositoryFactory.CreateImageRepository().GetImage(imageId);
133
return image.IsWindowsImage;
135
//=========================================================================================
139
//=========================================================================================
140
protected override void ProcessRecord()
142
if (this.ServerId == null)
143
this.ServerId = this.GetServerId();
145
Openstack.Objects.Domain.Compute.Server server = this.RepositoryFactory.CreateServerRepository().GetServer(this.ServerId);
147
if (this.IsWindowsImage(server.Image.Id)) {
148
ChangeWindowsImagePW();
152
Console.WriteLine("");
153
Console.WriteLine("Invalid Server : The Server you supplied is not a based on a Windows image. We currently only support key based authentication for non-Windows images.");
154
Console.WriteLine("");