refactor

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: Apache-2.0 Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package refactor detects resources moved or renamed between the deployed stacks and the synthesized ones, as upstream's api/refactoring does: the read-only half of its refactor engine, which diff's moves use (J.8-D1, #78).

The refactor action (M.4, #96) builds its execution on top: the stack definitions, the StackRefactor calls and their waits, and the overrides.

Detection

Digests gives each resource a digest: a SHA-256 of its type, its properties with references replaced by markers, and the digests of the resources it depends on, so that a resource keeps its digest when it, or anything it depends on, is renamed or moved to another stack. A Context groups an environment's deployed and local resources by digest: a digest at different locations before and after is a move, a mapping when it has one location on each side, and ambiguous when it has several on a side, unless the digests of the opposite graph, which cover the resources depending on each, settle it. Group reads, for each environment the local stacks resolve to, the deployed stacks of the same names.

Digests

Which resources count as moved depends only on which digests are equal, so the digests are upstream's, JavaScript's semantics included, and feed the hasher upstream's bytes (J.8-D4):

  • an object's keys in the default sort's order, by UTF-16 code units (cfntemplate.CompareStrings), each followed by its value;
  • an array's elements and nothing else, so that [] and {} hash alike;
  • null as the string "null", so that null and "null" hash alike;
  • any other value as its JavaScript type name followed by String(value) (cfntemplate.ToString), so that 5 and "5" differ, and numbers are written as JavaScript writes them;
  • the dependencies' digests in the graph's topological order, by Kahn's algorithm over the reverse edges, whose order is that of the templates' members, which cfntemplate.Object keeps.

Ref, Fn::GetAtt and an Fn::ImportValue of a known export are references, and an Fn::ImportValue of an export whose value isn't an object is that value; DependsOn members, the Metadata's aws:cdk:path, and resources of type AWS::CDK::Metadata are left out. So are __proto__ members, at any depth, because upstream copies each object by assigning its members to a new {}, where assigning __proto__ sets the copy's prototype, or does nothing, rather than adding a member; the dependencies still count those members' references, as upstream's graph reads the parsed template itself. testdata/digest/digest.mjs runs upstream's own digest and graph code over the fixture corpus and a table of edge cases, and the tests compare every digest, in order, with the table it writes.

Where upstream fails with a TypeError on a template CloudFormation would reject, detection carries on, as J.4-D10 does for the diff: a member of null, such as a null resource's Type, reads as missing, and a Fn::GetAtt that is neither a list nor a string names no resource. A Resources or Outputs section that isn't an object is read as empty, where upstream reads an array's or a string's elements as resources or outputs; so is a deployed template that isn't an object. An Fn::ImportValue of a name only Object.prototype has, such as "toString", is not an export, where upstream's lookup finds the prototype's member and hashes the import as null.

Reading the deployed stacks

Group reads the deployed stacks with the base credentials of each environment, through awsauth.Provider.ForEnvironment with no role, as upstream does, so detection in an account the base credentials aren't for fails with IncorrectDefaultCredentials, unless a credential plugin gives that account's (J.8-D5). Any failure fails the whole detection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Digests

func Digests(stacks []*Stack, opposite bool) map[string]string

Digests returns the digest of each resource in stacks, keyed by "<stack name>.<logical ID>", as hex: upstream's computeResourceDigests. With opposite set, a resource's digest covers the resources that depend on it, rather than those it depends on.

A resource of type AWS::CDK::Metadata has no digest, nor has a resource in a dependency cycle, or depending on one.

Types

type Context

type Context struct {
	// contains filtered or unexported fields
}

Context is the detection of moves and renames between the deployed stacks and the local ones of one environment: upstream's RefactoringContext, without its execution.

func NewContext

func NewContext(env cloudassembly.Environment, deployed, local []*Stack, ignoreModifications bool) (*Context, error)

NewContext detects the moves and renames in env from the deployed stacks to the local ones.

Unless ignoreModifications is set, a resource may only move or be renamed: when a resource is added, removed or changed, NewContext fails with a cdkerr.KindToolkit error with Code "RefactorResourceMismatch", whose message lists the resources only deployed, only local, and different in the two.

func (*Context) Ambiguous

func (c *Context) Ambiguous() [][2][]string

Ambiguous returns the moves the context couldn't settle, each as the paths of the resource's deployed locations and of its local ones: upstream's ambiguousPaths.

func (*Context) Env

Env returns the environment the context is for.

func (*Context) Mappings

func (c *Context) Mappings() []Mapping

Mappings returns the resources that moved or were renamed unambiguously, each from its deployed location to its local one.

type EnvGroup

type EnvGroup struct {
	// Env is the resolved environment.
	Env cloudassembly.Environment

	Local, Deployed []*Stack
}

EnvGroup is the local stacks of one environment and the deployed stacks they are compared with: upstream's StackGroup.

func Group

func Group(ctx context.Context, p *awsauth.Provider, local []*Stack, additional []string) ([]EnvGroup, error)

Group groups the local stacks by the environment each resolves to, in the order the environments first appear, and reads each environment's deployed stacks that have a local stack's name, or one of additional: upstream's groupStacks.

The deployed stacks are those ListStacks gives in CREATE_COMPLETE, UPDATE_COMPLETE, UPDATE_ROLLBACK_COMPLETE, IMPORT_COMPLETE or ROLLBACK_COMPLETE, in its order, with their original templates, read 20 at a time. They are read with the environment's base credentials, through p.ForEnvironment with no role (J.8-D5), so Group fails with ForEnvironment's error, such as IncorrectDefaultCredentials, for an environment the base credentials aren't for.

type EnvMappings

type EnvMappings struct {
	// Env is the resolved environment.
	Env cloudassembly.Environment

	// Mappings maps each moved resource's deployed location to its local one, both as "<stack name>.<logical ID>".
	Mappings map[string]string
}

EnvMappings is the moves detected in one environment, by location: upstream's EnvironmentSpecificMappings.

func MappingsByEnvironment

func MappingsByEnvironment(ctx context.Context, p *awsauth.Provider, local []*Stack, ignoreModifications bool) ([]EnvMappings, error)

MappingsByEnvironment detects the moves of the local stacks, by the environment they resolve to: upstream's mappingsByEnvironment, which diff calls with ignoreModifications set. It fails as Group and NewContext do.

type Location

type Location struct {
	Stack     *Stack
	LogicalID string
}

Location is a resource in a stack: upstream's ResourceLocation.

func (Location) Path

func (l Location) Path() string

Path returns the resource's construct path, from its Metadata's aws:cdk:path, or else its location: upstream's toPath.

func (Location) String

func (l Location) String() string

String returns the location as "<stack name>.<logical ID>": upstream's toLocationString.

type Mapping

type Mapping struct {
	Source, Destination Location
}

Mapping is a resource's move, from its deployed location to its local one: upstream's ResourceMapping.

type Stack

type Stack struct {
	// Env is the stack's environment: as the manifest gives it for a local stack, and resolved for a deployed one.
	Env cloudassembly.Environment

	// Name is the stack's name in CloudFormation.
	Name string

	Template *cfntemplate.Object

	// ID is a deployed stack's ARN, and empty for a local one.
	ID string
}

Stack is a stack as detection sees it: upstream's CloudFormationStack.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL