~travis-plummer/openstack-cli-powershell/master

« back to all changes in this revision

Viewing changes to Openstack.Client.Powershell/Cmdlets/Compute/Security/ResetPasswordCmdlet.cs

  • Committer: Monty Taylor
  • Date: 2015-10-17 20:03:56 UTC
  • Revision ID: git-v1:803406e7e09f6f4199f7a92f11f2904ad1d6cc15
Retire stackforge/openstack-cli-powershell

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* ============================================================================
2
 
Copyright 2014 Hewlett Packard
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
 
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;
23
 
using System;
24
 
using Openstack.Client.Powershell.Providers.Compute;
25
 
 
26
 
namespace Openstack.Client.Powershell.Cmdlets.Compute.Server
27
 
{
28
 
    [Cmdlet(VerbsCommon.Reset, "Password", SupportsShouldProcess = true)]
29
 
    [RequiredServiceIdentifierAttribute(Openstack.Objects.Domain.Admin.Services.Compute)]
30
 
    public class ResetPasswordCmdlet : BasePSCmdlet
31
 
    {
32
 
        private string _administratorPassword;
33
 
        private string _serverId;      
34
 
 
35
 
        #region Parameters
36
 
//=========================================================================================
37
 
/// <summary>
38
 
/// 
39
 
/// </summary>
40
 
//=========================================================================================
41
 
        [Parameter(Position = 1, ParameterSetName = "ResetPassword", Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Help Text")]
42
 
        [Alias("id")]
43
 
        [ValidateNotNullOrEmpty]
44
 
        public string ServerId
45
 
        {
46
 
            get { return _serverId; }
47
 
            set { _serverId = value; }
48
 
        }
49
 
//=========================================================================================
50
 
/// <summary>
51
 
/// 
52
 
/// </summary>
53
 
//=========================================================================================
54
 
        [Parameter(Position = 0, ParameterSetName = "ResetPassword", Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Help Text")]
55
 
        [Alias("p")]
56
 
        [ValidateNotNullOrEmpty]
57
 
        public string AdministratorPassword
58
 
        {
59
 
            get { return _administratorPassword; }
60
 
            set { _administratorPassword = value; }
61
 
        }
62
 
        #endregion
63
 
        #region Methods
64
 
//=========================================================================================
65
 
/// <summary>
66
 
/// 
67
 
/// </summary>
68
 
//=========================================================================================
69
 
        private void ChangeWindowsImagePW()
70
 
        {
71
 
            CredentialListManager manager = new CredentialListManager(this.Settings);
72
 
            if (this.Settings.EnableCredentialTracking == true)
73
 
            {
74
 
                manager.SetPassword(this.ServerId, this.AdministratorPassword);
75
 
                Console.WriteLine("");
76
 
                Console.WriteLine("Password Updated.");
77
 
                Console.WriteLine("");
78
 
            }
79
 
            else
80
 
            {
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("");
84
 
            }
85
 
        }
86
 
//=========================================================================================
87
 
/// <summary>
88
 
/// 
89
 
/// </summary>
90
 
//=========================================================================================
91
 
        private void ChangeNonWindowsImagePW()
92
 
        {
93
 
            ChangePasswordAction action  = new ChangePasswordAction();
94
 
            action.AdministratorPassword = this.AdministratorPassword;
95
 
            action.ServerId              = this.ServerId;
96
 
 
97
 
            this.RepositoryFactory.CreateServerRepository().ChangePassword(action);
98
 
            Console.WriteLine("");
99
 
            Console.WriteLine("Password Changed.");
100
 
            Console.WriteLine("");
101
 
        }
102
 
//=========================================================================================
103
 
/// <summary>
104
 
/// 
105
 
/// </summary>
106
 
/// <returns></returns>
107
 
//=========================================================================================
108
 
        private string GetServerId()
109
 
        {
110
 
            BaseUIContainer currentContainer = this.SessionState.PSVariable.Get("CurrentContainer").Value as BaseUIContainer;
111
 
 
112
 
            if (currentContainer.Name == "Metadata")
113
 
            {
114
 
                ServerUIContainer serverContainer = currentContainer.Parent as ServerUIContainer;
115
 
                if (serverContainer != null)
116
 
                    return serverContainer.Entity.Id;
117
 
            }
118
 
            else
119
 
            {              
120
 
                return currentContainer.Entity.Id;
121
 
            }
122
 
            return null;
123
 
        }
124
 
//=========================================================================================
125
 
/// <summary>
126
 
/// 
127
 
/// </summary>
128
 
/// <returns></returns>
129
 
//=========================================================================================
130
 
        private bool IsWindowsImage(string imageId)
131
 
        {
132
 
            Image image = this.RepositoryFactory.CreateImageRepository().GetImage(imageId);
133
 
            return image.IsWindowsImage;
134
 
        }
135
 
//=========================================================================================
136
 
/// <summary>
137
 
/// 
138
 
/// </summary>
139
 
//=========================================================================================
140
 
        protected override void ProcessRecord()
141
 
        {
142
 
            if (this.ServerId == null)
143
 
                this.ServerId = this.GetServerId();
144
 
 
145
 
            Openstack.Objects.Domain.Compute.Server server = this.RepositoryFactory.CreateServerRepository().GetServer(this.ServerId);
146
 
            
147
 
            if (this.IsWindowsImage(server.Image.Id)) {
148
 
                ChangeWindowsImagePW();
149
 
            }
150
 
            else
151
 
            {
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("");
155
 
            }         
156
 
        }
157
 
        #endregion
158
 
    }
159
 
}