pulumi/pkg/backend/httpstate/stack.go

204 lines
6.4 KiB
Go
Raw Normal View History

2018-05-22 21:43:36 +02:00
// Copyright 2016-2018, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package httpstate
import (
"context"
"fmt"
"time"
2018-01-11 00:04:55 +01:00
"github.com/pulumi/pulumi/pkg/apitype"
"github.com/pulumi/pulumi/pkg/backend"
"github.com/pulumi/pulumi/pkg/backend/httpstate/client"
"github.com/pulumi/pulumi/pkg/engine"
"github.com/pulumi/pulumi/pkg/operations"
"github.com/pulumi/pulumi/pkg/resource/deploy"
"github.com/pulumi/pulumi/pkg/tokens"
"github.com/pulumi/pulumi/pkg/util/contract"
"github.com/pulumi/pulumi/pkg/util/result"
)
// Stack is a cloud stack. This simply adds some cloud-specific properties atop the standard backend stack interface.
type Stack interface {
backend.Stack
Consolidate some `new` and `up` functionality (#1884) Previously `new` was operating under the assumption that it was always going to be creating a new project/stack, and would always prompt for these values. However, we want to be able to use `new` to pull down the source for an existing stack. This change adds a `--stack` flag to `new` that can be used to specify an existing stack. If the specified stack already exists, we won't prompt for the project name/description, and instead just use the existing stack's values. If `--stack` is specified, but doesn't already exist, it will just use that as the stack name (instead of prompting) when creating the stack. `new` also now handles configuration like `up <url>`: if the stack is a preconfigured empty stack (i.e. it was created/configured in the Pulumi Console via Pulumi Button or New Project), we will use the existing stack's config without prompting. Otherwise we will prompt for config, and just like `up <url>`, we'll use the existing stack's config values as defaults when prompting, or if the stack didn't exist, use the defaults from the template. Previously `up <url>`'s handling of the project name/description wasn't correct: it would always automatically use the values from the template without prompting. Now, just like `new`: - When updating an existing stack, it will not prompt, and just use the existing stack's values. - When creating a new stack, it will prompt for the project name/description, using the defaults from the template. This PR consolidates some of the `new`/`up` implementation so it shares code for this functionality. There's definitely opportunities for a lot more code reuse, but that cleanup can happen down the line if/when we have the cycles.
2018-09-06 21:45:56 +02:00
CloudURL() string // the URL to the cloud containing this stack.
OrgName() string // the organization that owns this stack.
ConsoleURL() (string, error) // the URL to view the stack's information on Pulumi.com
Tags() map[apitype.StackTagName]string // the stack's tags.
StackIdentifier() client.StackIdentifier
}
type cloudBackendReference struct {
name tokens.QName
project string
owner string
b *cloudBackend
}
func (c cloudBackendReference) String() string {
curUser, err := c.b.CurrentUser()
if err != nil {
curUser = ""
}
2019-01-25 18:48:08 +01:00
// If the project names match, we can elide them.
if c.b.currentProject != nil && c.project == string(c.b.currentProject.Name) {
2019-01-25 18:48:08 +01:00
if c.owner == curUser {
return string(c.name) // Elide owner too, if it is the current user.
}
return fmt.Sprintf("%s/%s", c.owner, c.name)
}
return fmt.Sprintf("%s/%s/%s", c.owner, c.project, c.name)
}
func (c cloudBackendReference) Name() tokens.QName {
return c.name
}
// cloudStack is a cloud stack descriptor.
type cloudStack struct {
// ref is the stack's unique name.
ref cloudBackendReference
// cloudURL is the URl to the cloud containing this stack.
cloudURL string
// orgName is the organization that owns this stack.
orgName string
// snapshot contains the latest deployment state, allocated on first use.
snapshot **deploy.Snapshot
// b is a pointer to the backend that this stack belongs to.
b *cloudBackend
// tags contains metadata tags describing additional, extensible properties about this stack.
tags map[apitype.StackTagName]string
}
func newStack(apistack apitype.Stack, b *cloudBackend) Stack {
// Now assemble all the pieces into a stack structure.
return &cloudStack{
ref: cloudBackendReference{
owner: apistack.OrgName,
project: apistack.ProjectName,
name: apistack.StackName,
b: b,
},
2018-08-09 04:26:51 +02:00
cloudURL: b.CloudURL(),
orgName: apistack.OrgName,
snapshot: nil, // We explicitly allocate the snapshot on first use, since it is expensive to compute.
Consolidate some `new` and `up` functionality (#1884) Previously `new` was operating under the assumption that it was always going to be creating a new project/stack, and would always prompt for these values. However, we want to be able to use `new` to pull down the source for an existing stack. This change adds a `--stack` flag to `new` that can be used to specify an existing stack. If the specified stack already exists, we won't prompt for the project name/description, and instead just use the existing stack's values. If `--stack` is specified, but doesn't already exist, it will just use that as the stack name (instead of prompting) when creating the stack. `new` also now handles configuration like `up <url>`: if the stack is a preconfigured empty stack (i.e. it was created/configured in the Pulumi Console via Pulumi Button or New Project), we will use the existing stack's config without prompting. Otherwise we will prompt for config, and just like `up <url>`, we'll use the existing stack's config values as defaults when prompting, or if the stack didn't exist, use the defaults from the template. Previously `up <url>`'s handling of the project name/description wasn't correct: it would always automatically use the values from the template without prompting. Now, just like `new`: - When updating an existing stack, it will not prompt, and just use the existing stack's values. - When creating a new stack, it will prompt for the project name/description, using the defaults from the template. This PR consolidates some of the `new`/`up` implementation so it shares code for this functionality. There's definitely opportunities for a lot more code reuse, but that cleanup can happen down the line if/when we have the cycles.
2018-09-06 21:45:56 +02:00
tags: apistack.Tags,
2018-08-09 04:26:51 +02:00
b: b,
}
}
Consolidate some `new` and `up` functionality (#1884) Previously `new` was operating under the assumption that it was always going to be creating a new project/stack, and would always prompt for these values. However, we want to be able to use `new` to pull down the source for an existing stack. This change adds a `--stack` flag to `new` that can be used to specify an existing stack. If the specified stack already exists, we won't prompt for the project name/description, and instead just use the existing stack's values. If `--stack` is specified, but doesn't already exist, it will just use that as the stack name (instead of prompting) when creating the stack. `new` also now handles configuration like `up <url>`: if the stack is a preconfigured empty stack (i.e. it was created/configured in the Pulumi Console via Pulumi Button or New Project), we will use the existing stack's config without prompting. Otherwise we will prompt for config, and just like `up <url>`, we'll use the existing stack's config values as defaults when prompting, or if the stack didn't exist, use the defaults from the template. Previously `up <url>`'s handling of the project name/description wasn't correct: it would always automatically use the values from the template without prompting. Now, just like `new`: - When updating an existing stack, it will not prompt, and just use the existing stack's values. - When creating a new stack, it will prompt for the project name/description, using the defaults from the template. This PR consolidates some of the `new`/`up` implementation so it shares code for this functionality. There's definitely opportunities for a lot more code reuse, but that cleanup can happen down the line if/when we have the cycles.
2018-09-06 21:45:56 +02:00
func (s *cloudStack) Ref() backend.StackReference { return s.ref }
func (s *cloudStack) Backend() backend.Backend { return s.b }
func (s *cloudStack) CloudURL() string { return s.cloudURL }
func (s *cloudStack) OrgName() string { return s.orgName }
func (s *cloudStack) Tags() map[apitype.StackTagName]string { return s.tags }
func (s *cloudStack) StackIdentifier() client.StackIdentifier {
si, err := s.b.getCloudStackIdentifier(s.ref)
contract.AssertNoError(err) // the above only fails when ref is of the wrong type.
return si
}
Show manifest information for stacks This change supports displaying manifest information for a stack and changes the way we handle Snapshots in our backend. Previously, every call to GetStack would synthesize a Snapshot by taking the set of resources returned from the `/api/stacks/<owner>/<name>` endpoint, combined with an empty manfiest (since the service was not returning the manifest). This wasn't great for two reasons: 1. We didn't have manifest information, so we couldn't display any of its information (most important the last updated time). 2. This strategy required that the service return all the resources for a stack anytime GetStack was called. While the CLI did not often need this detailed information the fact that we forced the Service to produce it (which in the case of stack managed PPC would require the service to talk to yet another service) creates a bunch of work that we end up ignoring. I've refactored the code such that `backend.Stack`'s `Snapshot()` method now lazily requests the information from the service such that we can construct a `Snapshot()` on demand and only pay the cost when we actually need it. I think making more of this stuff lazy is the long term direction we want to follow. Unfortunately, right now, it means in cases where we do need this data we end up fetching it twice. The service does it once when we call GetStack and then we do it again when we actually need to get at the Snapshot. However, once we land this change, we can update the service to no longer return resources on the apistack.Stack type. The CLI no longer needs this property. We'll likely want to continue in a direction where `apistack.Stack` can be created quickly by the service (without expensive database queries or fetching remote resources) and just add additional endpoints that let us get at the specific information we want in the specific cases when we want it instead of forcing us to return a bunch of data that we often ignore. Fixes pulumi/pulumi-service#371
2018-05-23 00:39:13 +02:00
func (s *cloudStack) Snapshot(ctx context.Context) (*deploy.Snapshot, error) {
if s.snapshot != nil {
return *s.snapshot, nil
}
snap, err := s.b.getSnapshot(ctx, s.ref)
Show manifest information for stacks This change supports displaying manifest information for a stack and changes the way we handle Snapshots in our backend. Previously, every call to GetStack would synthesize a Snapshot by taking the set of resources returned from the `/api/stacks/<owner>/<name>` endpoint, combined with an empty manfiest (since the service was not returning the manifest). This wasn't great for two reasons: 1. We didn't have manifest information, so we couldn't display any of its information (most important the last updated time). 2. This strategy required that the service return all the resources for a stack anytime GetStack was called. While the CLI did not often need this detailed information the fact that we forced the Service to produce it (which in the case of stack managed PPC would require the service to talk to yet another service) creates a bunch of work that we end up ignoring. I've refactored the code such that `backend.Stack`'s `Snapshot()` method now lazily requests the information from the service such that we can construct a `Snapshot()` on demand and only pay the cost when we actually need it. I think making more of this stuff lazy is the long term direction we want to follow. Unfortunately, right now, it means in cases where we do need this data we end up fetching it twice. The service does it once when we call GetStack and then we do it again when we actually need to get at the Snapshot. However, once we land this change, we can update the service to no longer return resources on the apistack.Stack type. The CLI no longer needs this property. We'll likely want to continue in a direction where `apistack.Stack` can be created quickly by the service (without expensive database queries or fetching remote resources) and just add additional endpoints that let us get at the specific information we want in the specific cases when we want it instead of forcing us to return a bunch of data that we often ignore. Fixes pulumi/pulumi-service#371
2018-05-23 00:39:13 +02:00
if err != nil {
return nil, err
}
s.snapshot = &snap
return *s.snapshot, nil
}
func (s *cloudStack) Remove(ctx context.Context, force bool) (bool, error) {
return backend.RemoveStack(ctx, s, force)
}
func (s *cloudStack) Rename(ctx context.Context, newName tokens.QName) error {
return backend.RenameStack(ctx, s, newName)
}
func (s *cloudStack) Preview(ctx context.Context, op backend.UpdateOperation) (engine.ResourceChanges, result.Result) {
return backend.PreviewStack(ctx, s, op)
}
func (s *cloudStack) Update(ctx context.Context, op backend.UpdateOperation) (engine.ResourceChanges, result.Result) {
return backend.UpdateStack(ctx, s, op)
}
func (s *cloudStack) Refresh(ctx context.Context, op backend.UpdateOperation) (engine.ResourceChanges, result.Result) {
return backend.RefreshStack(ctx, s, op)
}
func (s *cloudStack) Destroy(ctx context.Context, op backend.UpdateOperation) (engine.ResourceChanges, result.Result) {
return backend.DestroyStack(ctx, s, op)
}
func (s *cloudStack) Watch(ctx context.Context, op backend.UpdateOperation) result.Result {
return backend.WatchStack(ctx, s, op)
}
func (s *cloudStack) GetLogs(ctx context.Context, cfg backend.StackConfiguration,
query operations.LogQuery) ([]operations.LogEntry, error) {
return backend.GetStackLogs(ctx, s, cfg, query)
}
func (s *cloudStack) ExportDeployment(ctx context.Context) (*apitype.UntypedDeployment, error) {
return backend.ExportStackDeployment(ctx, s)
}
func (s *cloudStack) ImportDeployment(ctx context.Context, deployment *apitype.UntypedDeployment) error {
return backend.ImportStackDeployment(ctx, s, deployment)
}
func (s *cloudStack) ConsoleURL() (string, error) {
return s.b.StackConsoleURL(s.ref)
}
// cloudStackSummary implements the backend.StackSummary interface, by wrapping
// an apitype.StackSummary struct.
type cloudStackSummary struct {
summary apitype.StackSummary
b *cloudBackend
}
func (css cloudStackSummary) Name() backend.StackReference {
contract.Assert(css.summary.ProjectName != "")
return cloudBackendReference{
owner: css.summary.OrgName,
project: css.summary.ProjectName,
name: tokens.QName(css.summary.StackName),
b: css.b,
}
}
func (css cloudStackSummary) LastUpdate() *time.Time {
if css.summary.LastUpdate == nil {
return nil
}
t := time.Unix(*css.summary.LastUpdate, 0)
return &t
}
func (css cloudStackSummary) ResourceCount() *int {
return css.summary.ResourceCount
}