/// <summary>Constructs a new resource.</summary>
 public UsersResource(Google.Apis.Services.IClientService service)
 {
     this.service  = service;
     Projects      = new ProjectsResource(service);
     SshPublicKey  = new SshPublicKeyResource(service);
     SshPublicKeys = new SshPublicKeysResource(service);
 }
Пример #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 virtual Response <SshPublicKeyResource> Create(string resourceGroupName, string sshPublicKeyName, SshPublicKeyResource parameters, CancellationToken cancellationToken = default)
 {
     using var scope = _clientDiagnostics.CreateScope("SshPublicKeysClient.Create");
     scope.Start();
     try
     {
         return(RestClient.Create(resourceGroupName, sshPublicKeyName, parameters, cancellationToken));
     }
     catch (Exception e)
     {
         scope.Failed(e);
         throw;
     }
 }
 public virtual async Task <Response <SshPublicKeyResource> > CreateAsync(string resourceGroupName, string sshPublicKeyName, SshPublicKeyResource parameters, CancellationToken cancellationToken = default)
 {
     using var scope = _clientDiagnostics.CreateScope("SshPublicKeysClient.Create");
     scope.Start();
     try
     {
         return(await RestClient.CreateAsync(resourceGroupName, sshPublicKeyName, parameters, cancellationToken).ConfigureAwait(false));
     }
     catch (Exception e)
     {
         scope.Failed(e);
         throw;
     }
 }
 /// <summary>
 /// Creates a new SSH public key resource.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='resourceGroupName'>
 /// The name of the resource group.
 /// </param>
 /// <param name='sshPublicKeyName'>
 /// The name of the SSH public key.
 /// </param>
 /// <param name='parameters'>
 /// Parameters supplied to create the SSH public key.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <SshPublicKeyResource> CreateAsync(this ISshPublicKeysOperations operations, string resourceGroupName, string sshPublicKeyName, SshPublicKeyResource parameters, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.CreateWithHttpMessagesAsync(resourceGroupName, sshPublicKeyName, parameters, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
 /// <summary>
 /// Creates a new SSH public key resource.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='resourceGroupName'>
 /// The name of the resource group.
 /// </param>
 /// <param name='sshPublicKeyName'>
 /// The name of the SSH public key.
 /// </param>
 /// <param name='parameters'>
 /// Parameters supplied to create the SSH public key.
 /// </param>
 public static SshPublicKeyResource Create(this ISshPublicKeysOperations operations, string resourceGroupName, string sshPublicKeyName, SshPublicKeyResource parameters)
 {
     return(operations.CreateAsync(resourceGroupName, sshPublicKeyName, parameters).GetAwaiter().GetResult());
 }