public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();
            ExecuteClientAction(() =>
            {
                string resourceGroupName;
                string sshKeyName;
                string publicKey = this.PublicKey;

                switch (this.ParameterSetName)
                {
                case ResourceIDParameterSet:
                    resourceGroupName = GetResourceGroupName(this.ResourceId);
                    sshKeyName        = GetResourceName(this.ResourceId, "Microsoft.Compute/SshPublicKeys");
                    break;

                case InputObjectParameterSet:
                    resourceGroupName = GetResourceGroupName(this.InputObject.Id);
                    sshKeyName        = GetResourceName(this.InputObject.Id, "Microsoft.Compute/SshPublicKeys");
                    break;

                default:
                    resourceGroupName = this.ResourceGroupName;
                    sshKeyName        = this.Name;
                    break;
                }

                SshPublicKeyUpdateResource sshkeyUpdateResource = new SshPublicKeyUpdateResource();
                sshkeyUpdateResource.PublicKey = publicKey;
                var result   = SshPublicKeyClient.Update(resourceGroupName, sshKeyName, sshkeyUpdateResource);
                var psObject = new PSSshPublicKeyResource();
                ComputeAutomationAutoMapperProfile.Mapper.Map <SshPublicKeyResource, PSSshPublicKeyResource>(result, psObject);
                WriteObject(psObject);
            });
        }
Exemplo n.º 2
0
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();
            ExecuteClientAction(() =>
            {
                string resourceGroupName = this.ResourceGroupName;
                string sshKeyName        = this.Name;
                SshPublicKeyResource result;
                SshPublicKeyResource sshkey = new SshPublicKeyResource();
                ResourceGroup rg            = ArmClient.ResourceGroups.Get(resourceGroupName);
                sshkey.Location             = rg.Location;


                if (this.IsParameterBound(c => c.PublicKey))
                {
                    sshkey.PublicKey = this.PublicKey;
                    result           = SshPublicKeyClient.Create(resourceGroupName, sshKeyName, sshkey);
                }
                else
                {
                    WriteDebug("No public key is provided. A key pair is being generated for you.");

                    result = SshPublicKeyClient.Create(resourceGroupName, sshKeyName, sshkey);
                    SshPublicKeyGenerateKeyPairResult keypair = SshPublicKeyClient.GenerateKeyPair(resourceGroupName, sshKeyName);
                    result.PublicKey = keypair.PublicKey;

                    string sshFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".ssh");
                    if (!Directory.Exists(sshFolder))
                    {
                        Directory.CreateDirectory(sshFolder);
                    }

                    DateTimeOffset now        = DateTimeOffset.UtcNow;
                    string privateKeyFileName = now.ToUnixTimeSeconds().ToString();
                    string publicKeyFileName  = now.ToUnixTimeSeconds().ToString() + ".pub";
                    string privateKeyFilePath = Path.Combine(sshFolder, privateKeyFileName);
                    string publicKeyFilePath  = Path.Combine(sshFolder, publicKeyFileName);
                    using (StreamWriter writer = new StreamWriter(privateKeyFilePath))
                    {
                        writer.WriteLine(keypair.PrivateKey);
                    }
                    WriteWarning("Private key is saved to " + privateKeyFilePath);

                    using (StreamWriter writer = new StreamWriter(publicKeyFilePath))
                    {
                        writer.WriteLine(keypair.PublicKey);
                    }
                    WriteWarning("Public key is saved to " + publicKeyFilePath);
                }

                var psObject = new PSSshPublicKeyResource();
                ComputeAutomationAutoMapperProfile.Mapper.Map <SshPublicKeyResource, PSSshPublicKeyResource>(result, psObject);
                WriteObject(psObject);
            });
        }
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();
            ExecuteClientAction(() =>
            {
                string resourceGroupName;
                string sshKeyName;

                if (this.ParameterSetName == ResourceIDParameterSet)
                {
                    resourceGroupName = GetResourceGroupName(this.ResourceId);
                    sshKeyName        = GetResourceName(this.ResourceId, "Microsoft.Compute/sshPublicKeys");
                }
                else
                {
                    resourceGroupName = this.ResourceGroupName;
                    sshKeyName        = this.Name;
                }

                if (ShouldGetByName(resourceGroupName, sshKeyName))
                {
                    var result   = SshPublicKeyClient.Get(resourceGroupName, sshKeyName);
                    var psObject = new PSSshPublicKeyResource();
                    ComputeAutomationAutoMapperProfile.Mapper.Map <SshPublicKeyResource, PSSshPublicKeyResource>(result, psObject);
                    WriteObject(psObject);
                }
                else if (ShouldListByResourceGroup(resourceGroupName, sshKeyName))
                {
                    var result       = SshPublicKeyClient.ListByResourceGroup(resourceGroupName);
                    var resultList   = result.ToList();
                    var nextPageLink = result.NextPageLink;
                    while (!string.IsNullOrEmpty(nextPageLink))
                    {
                        var pageResult = SshPublicKeyClient.ListByResourceGroupNext(nextPageLink);
                        foreach (var pageItem in pageResult)
                        {
                            resultList.Add(pageItem);
                        }
                        nextPageLink = pageResult.NextPageLink;
                    }
                    var psObject = new List <PSSshPublicKeyResourceList>();
                    foreach (var r in resultList)
                    {
                        psObject.Add(ComputeAutomationAutoMapperProfile.Mapper.Map <SshPublicKeyResource, PSSshPublicKeyResourceList>(r));
                    }
                    WriteObject(TopLevelWildcardFilter(resourceGroupName, sshKeyName, psObject), true);
                }
                else
                {
                    var result       = SshPublicKeyClient.ListBySubscription();
                    var resultList   = result.ToList();
                    var nextPageLink = result.NextPageLink;
                    while (!string.IsNullOrEmpty(nextPageLink))
                    {
                        var pageResult = SshPublicKeyClient.ListBySubscriptionNext(nextPageLink);
                        foreach (var pageItem in pageResult)
                        {
                            resultList.Add(pageItem);
                        }
                        nextPageLink = pageResult.NextPageLink;
                    }
                    var psObject = new List <PSSshPublicKeyResourceList>();
                    foreach (var r in resultList)
                    {
                        psObject.Add(ComputeAutomationAutoMapperProfile.Mapper.Map <SshPublicKeyResource, PSSshPublicKeyResourceList>(r));
                    }
                    WriteObject(TopLevelWildcardFilter(resourceGroupName, sshKeyName, psObject), true);
                }
            });
        }