protected override void BeginProcessing()
        {
            Guid runspId;
            var  runsp = PowerShell.Create(RunspaceMode.CurrentRunspace);

            runspId    = runsp.Runspace.InstanceId;
            Connection = (Connection)TokenCollection.Get(runspId);


            if (Connection == null)
            {
                throw new ArgumentException("No Active Connection");
            }

            if (string.IsNullOrEmpty(Connection.AccessToken))
            {
                throw new ArgumentException("No Valid Connection");
            }

            //   checkcon = new CompanyService(Connection);
            //   checkcon.OnRefreshConnectionRaised += Service_OnRefreshConnectionRaised;
            //   var currentcompany = Task.Run(() => checkcon.GetCurrentAsync()).Result;

            if (Connection.TenantId == Guid.Empty)
            {
                throw new ArgumentException("No Tenant Selected");
            }


            var datenow    = DateTime.Now.ToUniversalTime();
            var expiredate = Connection.ExpiresAt;

            if (DateTime.Compare(datenow, expiredate) == 1)
            {
                TokenService.Refresh(Connection.LogonUrl, Connection.RefreshToken);
                Connection.RefreshToken = TokenService.RefreshToken;

                TokenCollection.Replace(runspId, Connection);


                // throw new ArgumentException("Connection Token Refresh needed");
            }
        }
        protected override void ProcessRecord()
        {
            string password = new System.Net.NetworkCredential(string.Empty, Credential.Password).Password;

            TokenService.Connect(_loginUrl, Credential.UserName, password);

            var con = new Connection {
                UserName = Credential.UserName, PassWord = password, LogonUrl = _loginUrl, AccessToken = TokenService.AccessToken, ApiUrl = _apiUrl, ExpiresAt = TokenService.ExpiresAt, RefreshToken = TokenService.RefreshToken
            };



            //if (Id == Guid.Empty)
            //{
            //    // No Platform selected
            //    PlatformService platformService = new PlatformService(con);

            //    try
            //    {

            //        Task<Result<List<Platform>>> callTask = Task.Run(() => platformService.AllAsync());

            //        callTask.Wait();
            //        platforms = callTask.Result.Object;

            //        platforms.ToList().ForEach(WriteObject);
            //    }
            //    catch (Exception e)
            //    {
            //        Console.WriteLine("Error: " + e.Message);
            //    }
            //}
            //else
            //{
            TenantService tenantService = new TenantService(con);
            JobService    jobService    = new JobService(con);

            try
            {
                Task <Result <List <Tenant> > > callTask = Task.Run(() => tenantService.GetByPlatformAsync(PlatformId));

                callTask.Wait();

                if (callTask.Result.Object != null)
                {
                    if (callTask.Result.Object.Count == 0)
                    {
                        Console.WriteLine("No Tenant existing");
                    }
                    else if (callTask.Result.Object.Count == 1)
                    {
                        tenant       = callTask.Result.Object.First();
                        con.TenantId = tenant.Id;


                        WriteObject(new ConnectionResult {
                            UserName = con.UserName, ApiUrl = con.ApiUrl, LogonUrl = con.LogonUrl, TenantId = con.TenantId
                        });
                    }
                    else
                    {
                        if (TenantId != Guid.Empty & callTask.Result.Object.Any(x => x.Id == TenantId))
                        {
                            tenant       = callTask.Result.Object.First(x => x.Id == TenantId);
                            con.TenantId = tenant.Id;


                            WriteObject(new ConnectionResult {
                                UserName = con.UserName, ApiUrl = con.ApiUrl, LogonUrl = con.LogonUrl, TenantId = con.TenantId
                            });
                        }
                        else
                        {
                            Console.WriteLine("As multiple Tenant exists please Set Tenant focus.");

                            WriteObject(callTask.Result.Object);
                        }
                    }

                    Guid runspId;
                    using (var runsp = PowerShell.Create(RunspaceMode.CurrentRunspace))
                    {
                        runspId = runsp.Runspace.InstanceId;
                        TokenCollection.Replace(runspId, con);
                    }
                }
                else if (callTask.Result.Error != null)
                {
                    throw new RemoteException("Conflict Error: " + callTask.Result.Error.ErrorType + "\r\n" + callTask.Result.Error.FaultyValues);
                }
                else
                {
                    throw new RemoteException("API returns: " + callTask.Result.Code.ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
            }
        }
Пример #3
0
        protected override void ProcessRecord()
        {
            Guid runspId;
            var  runsp = PowerShell.Create(RunspaceMode.CurrentRunspace);

            runspId = runsp.Runspace.InstanceId;
            var con = (Connection)TokenCollection.Get(runspId);


            if (con == null)
            {
                throw new ArgumentException("No Active Connection");
            }

            if (string.IsNullOrEmpty(con.AccessToken))
            {
                throw new ArgumentException("No Valid Connection");
            }

            TenantService tenantService = new TenantService(con);



            try
            {
                Task <Result <List <Tenant> > > callTask = Task.Run(() => tenantService.GetByPlatformAsync(PlatformId));

                callTask.Wait();

                if (callTask.Result.Object != null)
                {
                    if (callTask.Result.Object.Count == 0)
                    {
                        Console.WriteLine("No Tenant existing");
                    }
                    else if (callTask.Result.Object.Count == 1)
                    {
                        tenant       = callTask.Result.Object.First();
                        con.TenantId = tenant.Id;


                        WriteObject(new ConnectionResult {
                            UserName = con.UserName, ApiUrl = con.ApiUrl, LogonUrl = con.LogonUrl, TenantId = con.TenantId
                        });
                    }
                    else
                    {
                        if (callTask.Result.Object.Any(x => x.Id == Id))
                        {
                            tenant       = callTask.Result.Object.First(x => x.Id == Id);
                            con.TenantId = tenant.Id;


                            WriteObject(new ConnectionResult {
                                UserName = con.UserName, ApiUrl = con.ApiUrl, LogonUrl = con.LogonUrl, TenantId = con.TenantId
                            });
                        }
                        else
                        {
                            Console.WriteLine("As multiple Tenant exists please Set Tenant focus.");

                            WriteObject(callTask.Result.Object);
                        }
                    }


                    using (runsp = PowerShell.Create(RunspaceMode.CurrentRunspace))
                    {
                        runspId = runsp.Runspace.InstanceId;
                        TokenCollection.Replace(runspId, con);
                    }
                }
                else if (callTask.Result.Error != null)
                {
                    throw new RemoteException("Conflict Error: " + callTask.Result.Error.ErrorType + "\r\n" + callTask.Result.Error.FaultyValues);
                }
                else
                {
                    throw new RemoteException("API returns: " + callTask.Result.Code.ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
            }
        }