Exemplo n.º 1
0
        private async Task <Workspace> OperationDoWork(Workspace workspace, ResourceOperation operation, string[] addresses)
        {
            var workingDir = workspace.GetPath(_terraformOptions.RootWorkingDirectory);
            var files      = await _db.GetWorkspaceFiles(workspace, workspace.Directory);

            await workspace.PrepareFileSystem(workingDir, files);

            var initResult = _terraformService.InitializeWorkspace(
                workingDir, workspace.Name, workspace.IsDefault, null);

            var statePath = string.Empty;

            if (!workspace.IsDefault)
            {
                statePath = workspace.GetStatePath(workingDir, backupState: false);
            }

            if (!initResult.IsError)
            {
                switch (operation)
                {
                case ResourceOperation.taint:
                case ResourceOperation.untaint:
                    foreach (string address in addresses)
                    {
                        TerraformResult taintResult = null;

                        switch (operation)
                        {
                        case ResourceOperation.taint:
                            taintResult = _terraformService.Taint(workingDir, address, statePath);
                            break;

                        case ResourceOperation.untaint:
                            taintResult = _terraformService.Untaint(workingDir, address, statePath);
                            break;
                        }

                        if (taintResult != null && taintResult.IsError)
                        {
                            _logger.LogError(taintResult.Output);
                        }
                    }
                    break;

                case ResourceOperation.refresh:
                    TerraformResult refreshResult = _terraformService.Refresh(workingDir, statePath);
                    if (refreshResult.IsError)
                    {
                        _logger.LogError(refreshResult.Output);
                    }
                    break;
                }

                await workspace.RetrieveState(workingDir);

                await _db.SaveChangesAsync();

                workspace.CleanupFileSystem(_terraformOptions.RootWorkingDirectory);
            }

            return(workspace);
        }
Exemplo n.º 2
0
        private async Task <ResourceCommandResult> OperationDoWork(Workspace workspace, ResourceOperation operation, string[] addresses, string args)
        {
            var         errors     = new List <string>();
            JsonElement?outputs    = null;
            var         workingDir = workspace.GetPath(_terraformOptions.RootWorkingDirectory);
            var         files      = await _db.GetWorkspaceFiles(workspace, workspace.Directory);

            await workspace.PrepareFileSystem(workingDir, files);

            var initResult = _terraformService.InitializeWorkspace(workspace, null);

            var statePath = string.Empty;

            if (!workspace.IsDefault)
            {
                statePath = workspace.GetStatePath(workingDir, backupState: false);
            }

            if (!initResult.IsError)
            {
                TerraformResult result = null;

                switch (operation)
                {
                case ResourceOperation.taint:
                case ResourceOperation.untaint:
                    foreach (string address in addresses)
                    {
                        TerraformResult taintResult = null;

                        switch (operation)
                        {
                        case ResourceOperation.taint:
                            taintResult = _terraformService.Taint(workspace, address, statePath);
                            break;

                        case ResourceOperation.untaint:
                            taintResult = _terraformService.Untaint(workspace, address, statePath);
                            break;
                        }

                        if (taintResult != null && taintResult.IsError)
                        {
                            errors.Add(taintResult.Output);
                        }
                    }
                    break;

                case ResourceOperation.refresh:
                    result = _terraformService.Refresh(workspace, statePath);
                    break;

                case ResourceOperation.remove:
                    result = _terraformService.RemoveResources(workspace, addresses, statePath);
                    break;

                case ResourceOperation.import:
                    result = _terraformService.Import(workspace, addresses[0], args, statePath);
                    break;

                case ResourceOperation.output:
                    result  = _terraformService.GetOutputs(workspace, statePath);
                    outputs = JsonDocument.Parse(result.Output).RootElement;
                    break;
                }

                if (result != null && result.IsError)
                {
                    errors.Add(result.Output);
                }

                await workspace.RetrieveState(workingDir);

                await _db.SaveChangesAsync();

                workspace.CleanupFileSystem(_terraformOptions.RootWorkingDirectory);
            }

            return(new ResourceCommandResult
            {
                Resources = _mapper.Map <Resource[]>(workspace.GetState().GetResources(), opts => opts.ExcludeMembers(nameof(Resource.Attributes))),
                Errors = errors.ToArray(),
                Outputs = outputs
            });
        }