2023-05-02 11:33:06 -04:00
|
|
|
// Copyright IBM Corp. 2014, 2026
|
2023-08-10 18:43:27 -04:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-05-02 11:33:06 -04:00
|
|
|
|
2021-12-07 18:35:04 -05:00
|
|
|
package cloud
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
|
2023-12-20 06:04:10 -05:00
|
|
|
"github.com/hashicorp/cli"
|
2021-12-07 18:35:04 -05:00
|
|
|
"github.com/hashicorp/go-tfe"
|
backendrun: Separate the types/etc for backends that support operations
We previously had all of the types and helpers for all kinds of backends
together in package backend. That kept things relatively simple, but it
also meant that the majority of backends that only deal with remote state
storage ended up still indirectly depending on the entire Terraform modules
runtime, configuration loader, etc, etc, which brings into scope a bunch
of external dependencies that the remote state backends don't really need.
Since backends that support operations are a rare exception, we'll move the
types and helpers for those into a separate package "backendrun", and
then the main package backend can have a much more modest set of types and,
more importantly, a modest set of dependencies on other packages in this
codebase.
This is part of an ongoing effort to reduce the exposure of Terraform Core
and CLI code to the remote backends and vice-versa, so that in the long
run we can more often treat them as separate for dependency maintenance
purposes.
2024-03-11 19:27:44 -04:00
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/internal/backend/backendrun"
|
2021-12-07 18:35:04 -05:00
|
|
|
)
|
|
|
|
|
|
2021-12-15 16:55:46 -05:00
|
|
|
// IntegrationOutputWriter is an interface used to to write output tailored for
|
2024-04-22 15:21:52 -04:00
|
|
|
// HCP Terraform integrations
|
2021-12-07 18:35:04 -05:00
|
|
|
type IntegrationOutputWriter interface {
|
|
|
|
|
End()
|
|
|
|
|
OutputElapsed(message string, maxMessage int)
|
|
|
|
|
Output(str string)
|
|
|
|
|
SubOutput(str string)
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-22 15:21:52 -04:00
|
|
|
// IntegrationContext is a set of data that is useful when performing HCP Terraform integration operations
|
2021-12-07 18:35:04 -05:00
|
|
|
type IntegrationContext struct {
|
|
|
|
|
B *Cloud
|
|
|
|
|
StopContext context.Context
|
|
|
|
|
CancelContext context.Context
|
backendrun: Separate the types/etc for backends that support operations
We previously had all of the types and helpers for all kinds of backends
together in package backend. That kept things relatively simple, but it
also meant that the majority of backends that only deal with remote state
storage ended up still indirectly depending on the entire Terraform modules
runtime, configuration loader, etc, etc, which brings into scope a bunch
of external dependencies that the remote state backends don't really need.
Since backends that support operations are a rare exception, we'll move the
types and helpers for those into a separate package "backendrun", and
then the main package backend can have a much more modest set of types and,
more importantly, a modest set of dependencies on other packages in this
codebase.
This is part of an ongoing effort to reduce the exposure of Terraform Core
and CLI code to the remote backends and vice-versa, so that in the long
run we can more often treat them as separate for dependency maintenance
purposes.
2024-03-11 19:27:44 -04:00
|
|
|
Op *backendrun.Operation
|
2021-12-07 18:35:04 -05:00
|
|
|
Run *tfe.Run
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 16:55:46 -05:00
|
|
|
// integrationCLIOutput implements IntegrationOutputWriter
|
2021-12-07 18:35:04 -05:00
|
|
|
type integrationCLIOutput struct {
|
2021-12-15 16:55:46 -05:00
|
|
|
CLI cli.Ui
|
|
|
|
|
Colorizer Colorer
|
|
|
|
|
started time.Time
|
2021-12-07 18:35:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _ IntegrationOutputWriter = (*integrationCLIOutput)(nil) // Compile time check
|
|
|
|
|
|
2022-11-30 20:08:32 -05:00
|
|
|
func (s *IntegrationContext) Poll(backoffMinInterval float64, backoffMaxInterval float64, every func(i int) (bool, error)) error {
|
2021-12-07 18:35:04 -05:00
|
|
|
for i := 0; ; i++ {
|
|
|
|
|
select {
|
|
|
|
|
case <-s.StopContext.Done():
|
|
|
|
|
return s.StopContext.Err()
|
|
|
|
|
case <-s.CancelContext.Done():
|
|
|
|
|
return s.CancelContext.Err()
|
2022-11-30 20:08:32 -05:00
|
|
|
case <-time.After(backoff(backoffMinInterval, backoffMaxInterval, i)):
|
2021-12-07 18:35:04 -05:00
|
|
|
// blocks for a time between min and max
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cont, err := every(i)
|
|
|
|
|
if !cont {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 16:55:46 -05:00
|
|
|
// BeginOutput writes a preamble to the CLI and creates a new IntegrationOutputWriter interface
|
|
|
|
|
// to write the remaining CLI output to. Use IntegrationOutputWriter.End() to complete integration
|
|
|
|
|
// output
|
2021-12-07 18:35:04 -05:00
|
|
|
func (s *IntegrationContext) BeginOutput(name string) IntegrationOutputWriter {
|
|
|
|
|
var result IntegrationOutputWriter = &integrationCLIOutput{
|
2021-12-15 16:55:46 -05:00
|
|
|
CLI: s.B.CLI,
|
|
|
|
|
Colorizer: s.B.Colorize(),
|
|
|
|
|
started: time.Now(),
|
2021-12-07 18:35:04 -05:00
|
|
|
}
|
|
|
|
|
|
2021-12-15 16:55:46 -05:00
|
|
|
result.Output("\n[bold]" + name + ":\n")
|
2021-12-07 18:35:04 -05:00
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 16:55:46 -05:00
|
|
|
// End writes the termination output for the integration
|
2021-12-07 18:35:04 -05:00
|
|
|
func (s *integrationCLIOutput) End() {
|
2021-12-15 16:55:46 -05:00
|
|
|
if s.CLI == nil {
|
2021-12-07 18:35:04 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 16:55:46 -05:00
|
|
|
s.CLI.Output("\n------------------------------------------------------------------------\n")
|
2021-12-07 18:35:04 -05:00
|
|
|
}
|
|
|
|
|
|
2021-12-15 16:55:46 -05:00
|
|
|
// Output writes a string after colorizing it using any [colorstrings](https://github.com/mitchellh/colorstring) it contains
|
2021-12-07 18:35:04 -05:00
|
|
|
func (s *integrationCLIOutput) Output(str string) {
|
2021-12-15 16:55:46 -05:00
|
|
|
if s.CLI == nil {
|
2021-12-07 18:35:04 -05:00
|
|
|
return
|
|
|
|
|
}
|
2021-12-15 16:55:46 -05:00
|
|
|
s.CLI.Output(s.Colorizer.Color(str))
|
2021-12-07 18:35:04 -05:00
|
|
|
}
|
|
|
|
|
|
2021-12-15 16:55:46 -05:00
|
|
|
// SubOutput writes a string prefixed by a "│ " after colorizing it using any [colorstrings](https://github.com/mitchellh/colorstring) it contains
|
2021-12-07 18:35:04 -05:00
|
|
|
func (s *integrationCLIOutput) SubOutput(str string) {
|
2021-12-15 16:55:46 -05:00
|
|
|
if s.CLI == nil {
|
2021-12-07 18:35:04 -05:00
|
|
|
return
|
|
|
|
|
}
|
2021-12-15 16:55:46 -05:00
|
|
|
s.CLI.Output(s.Colorizer.Color(fmt.Sprintf("[reset]│ %s", str)))
|
2021-12-07 18:35:04 -05:00
|
|
|
}
|
|
|
|
|
|
2021-12-15 16:55:46 -05:00
|
|
|
// OutputElapsed writes a string followed by the amount of time that has elapsed since calling BeginOutput.
|
2021-12-07 18:35:04 -05:00
|
|
|
// Example pending output; the variable spacing (50 chars) allows up to 99 tasks (two digits) in each category:
|
|
|
|
|
// ---------------
|
|
|
|
|
// 13 tasks still pending, 0 passed, 0 failed ...
|
|
|
|
|
// 13 tasks still pending, 0 passed, 0 failed ... (8s elapsed)
|
|
|
|
|
// 13 tasks still pending, 0 passed, 0 failed ... (19s elapsed)
|
|
|
|
|
// 13 tasks still pending, 0 passed, 0 failed ... (33s elapsed)
|
|
|
|
|
func (s *integrationCLIOutput) OutputElapsed(message string, maxMessage int) {
|
2021-12-15 16:55:46 -05:00
|
|
|
if s.CLI == nil {
|
2021-12-07 18:35:04 -05:00
|
|
|
return
|
|
|
|
|
}
|
2021-12-15 16:55:46 -05:00
|
|
|
elapsed := time.Since(s.started).Truncate(1 * time.Second)
|
|
|
|
|
s.CLI.Output(fmt.Sprintf("%-"+strconv.FormatInt(int64(maxMessage), 10)+"s", message) + s.Colorizer.Color(fmt.Sprintf("[dim](%s elapsed)", elapsed)))
|
2021-12-07 18:35:04 -05:00
|
|
|
}
|