/// <summary>
        /// Add firewall rule to unblock the port to the GCE instance.
        /// </summary>
        public async Task EnablePortAsync()
        {
            // Get a refreshed list of firewall rules.
            // If not refreshed, UpdateInstancePorts may fail.
            _gceInstance = await DataSource.RefreshInstance(_gceInstance);

            string portTag = PortInfo.GetTag(_gceInstance.Name);

            var toEnable = new List <FirewallPort> {
                new FirewallPort(portTag, PortInfo.Port)
            };
            GceOperation operation = DataSource.UpdateInstancePorts(
                _gceInstance,
                portsToEnable: toEnable,
                portsToDisable: new List <FirewallPort>());
            await operation.OperationTask;

            _portEnabledTime = DateTime.UtcNow;
        }
Example #2
0
        private async void UpdateInstanceState(GceOperation pendingOperation)
        {
            while (pendingOperation != null && !pendingOperation.OperationTask.IsCompleted)
            {
                // Since there's a pending operation the loading state needs to be set to show
                // progress ui.
                IsLoading = true;

                // Setting the content according to the operation type.
                switch (pendingOperation.OperationType)
                {
                case OperationType.StartInstance:
                    Caption = String.Format(Resources.CloudExplorerGceInstanceStartingCaption, Instance.Name);
                    break;

                case OperationType.StopInstance:
                    Caption = String.Format(Resources.CloudExplorerGceInstanceStoppingCaption, Instance.Name);
                    break;

                case OperationType.SettingTags:
                    Caption = String.Format(Resources.CloudExplorerGceInstanceSettingTagsCaption, Instance.Name);
                    break;

                case OperationType.ModifyingFirewall:
                    Caption = String.Format(Resources.CloudExplorerGceInstanceUpdatingFirewallCaption, Instance.Name);
                    break;
                }

                // Update the context menu to reflect the state.
                UpdateContextMenu();

                try
                {
                    // Await the end of the task. We can also get here if the task is faulted,
                    // in which case we need to handle that case.
                    while (true)
                    {
                        // Refresh the instance before waiting for the operation to finish.
                        Instance = await _owner.DataSource.RefreshInstance(Instance);

                        // Wait for the operation to finish up to the timeout, which we will use to refresh the
                        // state of the instance.
                        var result = await Task.WhenAny(pendingOperation.OperationTask, Task.Delay(s_pollTimeout));

                        if (result == pendingOperation.OperationTask)
                        {
                            // Await the task again to get any possible exception.
                            await pendingOperation.OperationTask;
                            break;
                        }
                    }

                    // Refresh the instance state after the operation is finished.
                    Instance = await _owner.DataSource.RefreshInstance(Instance);
                }
                catch (DataSourceException ex)
                {
                    Caption   = Instance.Name;
                    IsLoading = false;
                    IsError   = true;
                    UpdateContextMenu();

                    Debug.WriteLine($"Previous operation failed.");
                    switch (pendingOperation.OperationType)
                    {
                    case OperationType.StartInstance:
                        GcpOutputWindow.OutputLine(String.Format(Resources.CloudExplorerGceStartOperationFailedMessage, Instance.Name, ex.Message));
                        break;

                    case OperationType.StopInstance:
                        GcpOutputWindow.OutputLine(String.Format(Resources.CloudExplorerGceStopOperationFailedMessage, Instance.Name, ex.Message));
                        break;
                    }

                    // Permanent error.
                    return;
                }

                // See if there are more operations.
                pendingOperation = _owner.DataSource.GetPendingOperation(Instance);
            }

            // Normal state, no pending operations.
            IsLoading = false;
            Caption   = Instance.Name;
            UpdateContextMenu();
        }
Example #3
0
        private void UpdateInstanceState()
        {
            GceOperation pendingOperation = _owner.DataSource.GetPendingOperation(Instance);

            UpdateInstanceState(pendingOperation);
        }
Example #4
0
        private async Task UpdateInstanceStateAsync()
        {
            GceOperation pendingOperation = _owner.DataSource.GetPendingOperation(Instance);

            await UpdateInstanceStateAsync(pendingOperation);
        }