|
4 | 4 | "bytes"
|
5 | 5 | "context"
|
6 | 6 | "encoding/base64"
|
| 7 | + "encoding/json" |
7 | 8 | "errors"
|
8 | 9 | "fmt"
|
9 | 10 | "io"
|
@@ -714,6 +715,43 @@ func (b *ByocAws) BootstrapList(ctx context.Context) ([]string, error) {
|
714 | 715 | }
|
715 | 716 | // Cut off the prefix and the .json suffix
|
716 | 717 | stack := (*obj.Key)[len(prefix) : len(*obj.Key)-5]
|
| 718 | + // Check the contents of the JSON file, because the size is not a reliable indicator of a valid stack |
| 719 | + objOutput, err := s3client.GetObject(ctx, &s3.GetObjectInput{ |
| 720 | + Bucket: &bucketName, |
| 721 | + Key: obj.Key, |
| 722 | + }) |
| 723 | + if err != nil { |
| 724 | + term.Debugf("Failed to get Pulumi state object %q: %v", *obj.Key, err) |
| 725 | + } else { |
| 726 | + defer objOutput.Body.Close() |
| 727 | + var state struct { |
| 728 | + Version int `json:"version"` |
| 729 | + Checkpoint struct { |
| 730 | + // Stack string `json:"stack"` TODO: could use this instead of deriving the stack name from the key |
| 731 | + Latest struct { |
| 732 | + Resources []struct{} `json:"resources,omitempty"` |
| 733 | + PendingOperations []struct { |
| 734 | + Resource struct { |
| 735 | + Urn string `json:"urn"` |
| 736 | + } |
| 737 | + } `json:"pending_operations,omitempty"` |
| 738 | + } |
| 739 | + } |
| 740 | + } |
| 741 | + if err := json.NewDecoder(objOutput.Body).Decode(&state); err != nil { |
| 742 | + term.Debugf("Failed to decode Pulumi state %q: %v", *obj.Key, err) |
| 743 | + } else if state.Version != 3 { |
| 744 | + term.Debug("Skipping Pulumi state with version", state.Version) |
| 745 | + } else if len(state.Checkpoint.Latest.PendingOperations) > 0 { |
| 746 | + for _, op := range state.Checkpoint.Latest.PendingOperations { |
| 747 | + parts := strings.Split(op.Resource.Urn, "::") // prefix::project::type::resource => urn:provider:stack::project::plugin:file:class::name |
| 748 | + stack += fmt.Sprintf(" (pending %q)", parts[3]) |
| 749 | + } |
| 750 | + } else if len(state.Checkpoint.Latest.Resources) == 0 { |
| 751 | + continue // skip: no resources and no pending operations |
| 752 | + } |
| 753 | + } |
| 754 | + |
717 | 755 | stacks = append(stacks, stack)
|
718 | 756 | }
|
719 | 757 | return stacks, nil
|
|
0 commit comments