-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More Stacks, StackPlans support #934
Merged
brandonc
merged 6 commits into
main
from
TF-18387-stacks-configuration-deployment-support-in-go-tfe
Jul 19, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f73f833
More Stacks, StackPlans support
brandonc 79e2f37
Adds additional godoc comments and CHANGELOG
brandonc d780907
Add Stack Plans actions
brandonc ed5a42a
lint fix
brandonc 0654890
Remove references to stack-diagnostics
brandonc 30cc1ac
adds Stack godocs
brandonc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package tfe | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
) | ||
|
||
// StackConfigurations describes all the stacks configurations-related methods that the | ||
// HCP Terraform API supports. | ||
// NOTE WELL: This is a beta feature and is subject to change until noted otherwise in the | ||
// release notes. | ||
type StackConfigurations interface { | ||
// ReadConfiguration returns a stack configuration by its ID. | ||
Read(ctx context.Context, id string) (*StackConfiguration, error) | ||
} | ||
|
||
type stackConfigurations struct { | ||
client *Client | ||
} | ||
|
||
var _ StackConfigurations = &stackConfigurations{} | ||
|
||
func (s stackConfigurations) Read(ctx context.Context, id string) (*StackConfiguration, error) { | ||
brandonc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
req, err := s.client.NewRequest("GET", fmt.Sprintf("stack-configurations/%s", url.PathEscape(id)), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
stackConfiguration := &StackConfiguration{} | ||
err = req.Do(ctx, stackConfiguration) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return stackConfiguration, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package tfe | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
) | ||
|
||
// StackDeployments describes all the stacks deployments-related methods that the | ||
// HCP Terraform API supports. | ||
// NOTE WELL: This is a beta feature and is subject to change until noted otherwise in the | ||
// release notes. | ||
type StackDeployments interface { | ||
// Read returns a stack deployment by its name. | ||
Read(ctx context.Context, stackID, deployment string) (*StackDeployment, error) | ||
} | ||
|
||
type stackDeployments struct { | ||
client *Client | ||
} | ||
|
||
func (s stackDeployments) Read(ctx context.Context, stackID, deploymentName string) (*StackDeployment, error) { | ||
req, err := s.client.NewRequest("GET", fmt.Sprintf("stacks/%s/stack-deployments/%s", url.PathEscape(stackID), url.PathEscape(deploymentName)), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
deployment := &StackDeployment{} | ||
err = req.Do(ctx, deployment) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return deployment, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ package tfe | |
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
@@ -166,10 +167,156 @@ func TestStackReadUpdateDelete(t *testing.T) { | |
require.NoError(t, err) | ||
require.Equal(t, "updated description", stackUpdated.Description) | ||
|
||
stackUpdatedConfig, err := client.Stacks.UpdateConfiguration(ctx, stack.ID) | ||
require.NoError(t, err) | ||
require.Equal(t, stack.Name, stackUpdatedConfig.Name) | ||
|
||
err = client.Stacks.Delete(ctx, stack.ID) | ||
require.NoError(t, err) | ||
|
||
stackReadAfterDelete, err := client.Stacks.Read(ctx, stack.ID) | ||
require.ErrorIs(t, err, ErrResourceNotFound) | ||
require.Nil(t, stackReadAfterDelete) | ||
} | ||
|
||
func pollStackDeployments(t *testing.T, ctx context.Context, client *Client, stackID string) (stack *Stack) { | ||
t.Helper() | ||
|
||
// pollStackDeployments will poll the given stack until it has deployments or the deadline is reached. | ||
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(5*time.Minute)) | ||
defer cancel() | ||
|
||
deadline, _ := ctx.Deadline() | ||
t.Logf("Polling stack %q for deployments with deadline of %s", stackID, deadline) | ||
|
||
ticker := time.NewTicker(2 * time.Second) | ||
defer ticker.Stop() | ||
|
||
for finished := false; !finished; { | ||
t.Log("...") | ||
select { | ||
case <-ctx.Done(): | ||
t.Fatalf("Stack %q had no deployments at deadline", stackID) | ||
case <-ticker.C: | ||
var err error | ||
stack, err = client.Stacks.Read(ctx, stackID) | ||
if err != nil { | ||
t.Fatalf("Failed to read stack %q: %s", stackID, err) | ||
} | ||
|
||
t.Logf("Stack %q had %d deployments", stack.ID, len(stack.DeploymentNames)) | ||
if len(stack.DeploymentNames) > 0 { | ||
finished = true | ||
} | ||
} | ||
} | ||
|
||
return | ||
} | ||
|
||
func pollStackDeploymentStatus(t *testing.T, ctx context.Context, client *Client, stackID, deploymentName, status string) { | ||
// pollStackDeployments will poll the given stack until it has deployments or the deadline is reached. | ||
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(5*time.Minute)) | ||
defer cancel() | ||
|
||
deadline, _ := ctx.Deadline() | ||
t.Logf("Polling stack %q for deployments with deadline of %s", stackID, deadline) | ||
|
||
ticker := time.NewTicker(2 * time.Second) | ||
defer ticker.Stop() | ||
|
||
for finished := false; !finished; { | ||
t.Log("...") | ||
select { | ||
case <-ctx.Done(): | ||
t.Fatalf("Stack deployment %s/%s did not have status %q at deadline", stackID, deploymentName, status) | ||
case <-ticker.C: | ||
var err error | ||
deployment, err := client.StackDeployments.Read(ctx, stackID, deploymentName) | ||
if err != nil { | ||
t.Fatalf("Failed to read stack deployment %s/%s: %s", stackID, deploymentName, err) | ||
} | ||
|
||
t.Logf("Stack deployment %s/%s had status %q", stackID, deploymentName, deployment.Status) | ||
if deployment.Status == status { | ||
finished = true | ||
} | ||
} | ||
} | ||
} | ||
|
||
func pollStackConfigurationStatus(t *testing.T, ctx context.Context, client *Client, stackConfigID, status string) (stackConfig *StackConfiguration) { | ||
// pollStackDeployments will poll the given stack until it has deployments or the deadline is reached. | ||
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(5*time.Minute)) | ||
defer cancel() | ||
|
||
deadline, _ := ctx.Deadline() | ||
t.Logf("Polling stack configuration %q for status %q with deadline of %s", stackConfigID, status, deadline) | ||
|
||
ticker := time.NewTicker(2 * time.Second) | ||
defer ticker.Stop() | ||
|
||
var err error | ||
for finished := false; !finished; { | ||
t.Log("...") | ||
select { | ||
case <-ctx.Done(): | ||
t.Fatalf("Stack configuration %q did not have status %q at deadline", stackConfigID, status) | ||
case <-ticker.C: | ||
stackConfig, err = client.StackConfigurations.Read(ctx, stackConfigID) | ||
if err != nil { | ||
t.Fatalf("Failed to read stack configuration %q: %s", stackConfigID, err) | ||
} | ||
|
||
t.Logf("Stack configuration %q had status %q", stackConfigID, stackConfig.Status) | ||
if stackConfig.Status == status { | ||
finished = true | ||
} | ||
} | ||
} | ||
|
||
return | ||
} | ||
|
||
func TestStackConverged(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎉 |
||
skipUnlessBeta(t) | ||
|
||
client := testClient(t) | ||
ctx := context.Background() | ||
|
||
orgTest, orgTestCleanup := createOrganization(t, client) | ||
t.Cleanup(orgTestCleanup) | ||
|
||
oauthClient, cleanup := createOAuthClient(t, client, orgTest, nil) | ||
t.Cleanup(cleanup) | ||
|
||
stack, err := client.Stacks.Create(ctx, StackCreateOptions{ | ||
Name: "test-stack", | ||
VCSRepo: &StackVCSRepo{ | ||
Identifier: "brandonc/pet-nulls-stack", | ||
OAuthTokenID: oauthClient.OAuthTokens[0].ID, | ||
}, | ||
Project: &Project{ | ||
ID: orgTest.DefaultProject.ID, | ||
}, | ||
}) | ||
|
||
require.NoError(t, err) | ||
require.NotNil(t, stack) | ||
|
||
stackUpdated, err := client.Stacks.UpdateConfiguration(ctx, stack.ID) | ||
require.NoError(t, err) | ||
require.NotNil(t, stackUpdated) | ||
|
||
deployments := []string{"production", "staging"} | ||
|
||
stack = pollStackDeployments(t, ctx, client, stackUpdated.ID) | ||
require.ElementsMatch(t, deployments, stack.DeploymentNames) | ||
require.NotNil(t, stack.LatestStackConfiguration) | ||
|
||
for _, deployment := range deployments { | ||
pollStackDeploymentStatus(t, ctx, client, stack.ID, deployment, "paused") | ||
} | ||
|
||
pollStackConfigurationStatus(t, ctx, client, stack.LatestStackConfiguration.ID, "converged") | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the more constrained format
rfc3339
and notiso8601
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For all practical purposes, the API emits dates that fits both formats. For the purposes of parsing dates from HCP Terraform, they should be equivalent. RFC3339 is the slightly more stringent version so I usually choose it