vaguerent/internal/client/operation.go
Chris Roberts e958c6183a Adds initial HCP config support
Adds initial basic support for HCP based configuration in vagrant-go.
The initalization process has been updated to remove Vagrantfile parsing
from the client, moving it to the runner using init jobs for the basis
and the project (if there is one). Detection is done on the file based
on extension for Ruby based parsing or HCP based parsing.

Current HCP parsing is extremely simple and currently just a base to
build off. Config components will be able to implement an `Init`
function to handle receiving configuration data from a non-native source
file. This will be extended to include a default approach for injecting
defined data in the future.

Some cleanup was done in the state around validations. Some logging
adjustments were applied on the Ruby side for better behavior
consistency.

VirtualBox provider now caches locale detection to prevent multiple
checks every time the driver is initialized.
2023-09-07 17:26:10 -07:00

200 lines
3.7 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package client
import (
"context"
"github.com/hashicorp/vagrant-plugin-sdk/component"
"github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk"
"github.com/hashicorp/vagrant/internal/server/logviewer"
"github.com/hashicorp/vagrant/internal/server/proto/vagrant_server"
)
func (c *Client) Validate(
ctx context.Context,
op *vagrant_server.Job_ValidateOp,
mod JobModifier,
) (*vagrant_server.Job_ValidateResult, error) {
if op == nil {
op = &vagrant_server.Job_ValidateOp{}
}
// Validate our job
job := c.job()
job.Operation = &vagrant_server.Job_Validate{
Validate: op,
}
if mod != nil {
mod(job)
}
// Execute it
result, err := c.doJob(ctx, job, c.ui)
if err != nil {
return nil, err
}
return result.Validate, nil
}
func (c *Client) Commands(
ctx context.Context,
op *vagrant_server.Job_InitOp,
mod JobModifier,
) (*vagrant_server.Job_InitResult, error) {
if op == nil {
op = &vagrant_server.Job_InitOp{}
}
job := c.job()
job.Operation = &vagrant_server.Job_Init{
Init: op,
}
if mod != nil {
mod(job)
}
result, err := c.doJob(ctx, job, c.ui)
if err != nil {
return nil, err
}
return result.Init, nil
}
func (c *Client) BasisInit(
ctx context.Context,
mod JobModifier,
) (*vagrant_plugin_sdk.Ref_Basis, error) {
job := c.job()
job.Operation = &vagrant_server.Job_InitBasis{
InitBasis: &vagrant_server.Job_InitBasisOp{},
}
// Apply scoping to job
mod(job)
result, err := c.doJob(ctx, job, c.ui)
if err != nil {
return nil, err
}
return result.Basis.Basis, nil
}
func (c *Client) ProjectInit(
ctx context.Context,
mod JobModifier,
) (*vagrant_plugin_sdk.Ref_Project, error) {
job := c.job()
job.Operation = &vagrant_server.Job_InitProject{
InitProject: &vagrant_server.Job_InitProjectOp{},
}
// Apply scoping to job
mod(job)
result, err := c.doJob(ctx, job, c.ui)
if err != nil {
return nil, err
}
return result.Project.Project, nil
}
func (c *Client) Command(
ctx context.Context,
op *vagrant_server.Job_CommandOp,
mod JobModifier,
) (*vagrant_server.Job_CommandResult, error) {
if op == nil {
op = &vagrant_server.Job_CommandOp{}
}
job := c.job()
job.Operation = &vagrant_server.Job_Command{
Command: op,
}
if mod != nil {
mod(job)
}
result, err := c.doJob(ctx, job, c.ui)
if err != nil {
return nil, err
}
return result.Run, nil
}
func (c *Client) Auth(
ctx context.Context,
op *vagrant_server.Job_AuthOp,
mod JobModifier,
) (*vagrant_server.Job_AuthResult, error) {
if op == nil {
op = &vagrant_server.Job_AuthOp{}
}
// Auth our job
job := c.job()
job.Operation = &vagrant_server.Job_Auth{
Auth: op,
}
if mod != nil {
mod(job)
}
// Execute it
result, err := c.doJob(ctx, job, c.ui)
if err != nil {
return nil, err
}
return result.Auth, nil
}
func (c *Client) Docs(
ctx context.Context,
op *vagrant_server.Job_DocsOp,
mod JobModifier,
) (*vagrant_server.Job_DocsResult, error) {
if op == nil {
op = &vagrant_server.Job_DocsOp{}
}
job := c.job()
job.Operation = &vagrant_server.Job_Docs{
Docs: op,
}
if mod != nil {
mod(job)
}
// Execute it
result, err := c.doJob(ctx, job, c.ui)
if err != nil {
return nil, err
}
return result.Docs, nil
}
// TODO(spox): need to think about how to apply this
func (c *Client) Logs(ctx context.Context) (component.LogViewer, error) {
log := c.logger.Named("logs")
// First we attempt to query the server for logs for this deployment.
log.Info("requesting log stream")
client, err := c.client.GetLogStream(ctx, &vagrant_server.GetLogStreamRequest{
Scope: &vagrant_server.GetLogStreamRequest_Basis{
// Basis: b.Ref(),
},
})
if err != nil {
return nil, err
}
// Build our log viewer
return &logviewer.Viewer{Stream: client}, nil
}