|
14 | 14 |
|
15 | 15 | package tfbridge |
16 | 16 |
|
17 | | -import ( |
18 | | - "io" |
19 | | - "os" |
20 | | - "path/filepath" |
| 17 | +// This file provides backward compatibility for moving [info.AssetTranslation] and |
| 18 | +// [info.AssetTranslationKind] to [info]. |
21 | 19 |
|
22 | | - "github.com/pulumi/pulumi/sdk/v3/go/common/resource" |
23 | | - "github.com/pulumi/pulumi/sdk/v3/go/common/util/contract" |
24 | | -) |
| 20 | +import "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge/info" |
25 | 21 |
|
26 | 22 | // AssetTranslation instructs the bridge how to translate assets into something Terraform can use. |
27 | | -type AssetTranslation struct { |
28 | | - Kind AssetTranslationKind // the kind of translation to perform. |
29 | | - Format resource.ArchiveFormat // an archive format, required if this is an archive. |
30 | | - HashField string // a field to store the hash into, if any. |
31 | | -} |
| 23 | +type AssetTranslation = info.AssetTranslation |
32 | 24 |
|
33 | 25 | // AssetTranslationKind may be used to choose from various source and dest translation targets. |
34 | | -type AssetTranslationKind int |
| 26 | +type AssetTranslationKind = info.AssetTranslationKind |
35 | 27 |
|
36 | 28 | const ( |
37 | 29 | // FileAsset turns the asset into a file on disk and passes the filename in its place. |
38 | | - FileAsset AssetTranslationKind = iota |
| 30 | + FileAsset = info.FileAsset |
39 | 31 | // BytesAsset turns the asset into a []byte and passes it directly in-memory. |
40 | | - BytesAsset |
| 32 | + BytesAsset = info.BytesAsset |
41 | 33 | // FileArchive turns the archive into a file on disk and passes the filename in its place. |
42 | | - FileArchive |
| 34 | + FileArchive = info.FileArchive |
43 | 35 | // BytesArchive turns the asset into a []byte and passes that directly in-memory. |
44 | | - BytesArchive |
| 36 | + BytesArchive = info.BytesArchive |
45 | 37 | ) |
46 | | - |
47 | | -// Type fetches the Pulumi runtime type corresponding to values of this asset kind. |
48 | | -func (a *AssetTranslation) Type() string { |
49 | | - switch a.Kind { |
50 | | - case FileAsset, BytesAsset: |
51 | | - return "Asset" |
52 | | - case FileArchive, BytesArchive: |
53 | | - return "Archive" |
54 | | - default: |
55 | | - contract.Failf("Unrecognized asset translation kind: %v", a.Kind) |
56 | | - return "" |
57 | | - } |
58 | | -} |
59 | | - |
60 | | -// writeToTempFile creates a temporary file and passes it to the provided function, which will fill in the file's |
61 | | -// contents. Upon success, this function returns the path of the temporary file and a nil error. |
62 | | -func writeToTempFile(writeFunc func(w io.Writer) error) (string, error) { |
63 | | - f, err := os.CreateTemp("", "pulumi-temp-asset") |
64 | | - if err != nil { |
65 | | - return "", err |
66 | | - } |
67 | | - defer contract.IgnoreClose(f) |
68 | | - |
69 | | - if err := writeFunc(f); err != nil { |
70 | | - return "", err |
71 | | - } |
72 | | - |
73 | | - return f.Name(), nil |
74 | | -} |
75 | | - |
76 | | -// translateToFile translates an asset or archive to a filename. If possible, it attempts to reuse previously spilled |
77 | | -// assets/archives with the same identity. |
78 | | -func translateToFile(hash string, hasContents bool, writeFunc func(w io.Writer) error) (string, error) { |
79 | | - // If possible, we want to produce a predictable filename in order to avoid spurious diffs and spilling the same |
80 | | - // asset multiple times. |
81 | | - memoPath := "" |
82 | | - if hash != "" { |
83 | | - memoPath = filepath.Join(os.TempDir(), "pulumi-asset-"+hash) |
84 | | - } |
85 | | - |
86 | | - // If we have no contents, just return the file path. Note that this may be the empty string if we were also |
87 | | - // missing a hash. |
88 | | - if !hasContents { |
89 | | - return memoPath, nil |
90 | | - } |
91 | | - |
92 | | - // If we have no translation path, just write the asset to a temporary file and return. |
93 | | - if memoPath == "" { |
94 | | - return writeToTempFile(writeFunc) |
95 | | - } |
96 | | - |
97 | | - // If the translation file already exists, assume it has the appropriate contents and return the file path. |
98 | | - info, err := os.Stat(memoPath) |
99 | | - if err == nil && info.Mode().IsRegular() && info.Size() > 0 { |
100 | | - return memoPath, nil |
101 | | - } |
102 | | - |
103 | | - // Otherwise, write the asset to a temporary file, then attempt to move the temp file to the expected path. |
104 | | - // If the move fails, we'll use the temp file name. |
105 | | - tempName, err := writeToTempFile(writeFunc) |
106 | | - if err != nil { |
107 | | - return "", err |
108 | | - } |
109 | | - if err := os.Rename(tempName, memoPath); err != nil && !os.IsExist(err) { |
110 | | - return tempName, nil |
111 | | - } |
112 | | - return memoPath, nil |
113 | | -} |
114 | | - |
115 | | -// IsAsset returns true if the translation deals with an asset (rather than archive). |
116 | | -func (a *AssetTranslation) IsAsset() bool { |
117 | | - return a.Kind == FileAsset || a.Kind == BytesAsset |
118 | | -} |
119 | | - |
120 | | -// IsArchive returns true if the translation deals with an archive (rather than asset). |
121 | | -func (a *AssetTranslation) IsArchive() bool { |
122 | | - return a.Kind == FileArchive || a.Kind == BytesArchive |
123 | | -} |
124 | | - |
125 | | -// TranslateAsset translates the given asset using the directives provided by the translation info. |
126 | | -func (a *AssetTranslation) TranslateAsset(asset *resource.Asset) (interface{}, error) { |
127 | | - contract.Assertf(a.IsAsset(), "a.IsAsset()") |
128 | | - |
129 | | - // TODO[pulumi/pulumi#153]: support HashField. |
130 | | - |
131 | | - // Now produce either a temp file or a binary blob, as requested. |
132 | | - switch a.Kind { |
133 | | - case FileAsset: |
134 | | - path, err := translateToFile(asset.Hash, asset.HasContents(), func(w io.Writer) error { |
135 | | - blob, err := asset.Read() |
136 | | - if err != nil { |
137 | | - return err |
138 | | - } |
139 | | - defer contract.IgnoreClose(blob) |
140 | | - |
141 | | - _, err = io.Copy(w, blob) |
142 | | - return err |
143 | | - }) |
144 | | - return path, err |
145 | | - case BytesAsset: |
146 | | - if !asset.HasContents() { |
147 | | - return []byte{}, nil |
148 | | - } |
149 | | - return asset.Bytes() |
150 | | - default: |
151 | | - contract.Failf("Unrecognized asset translation kind: %v", a.Kind) |
152 | | - return nil, nil |
153 | | - } |
154 | | -} |
155 | | - |
156 | | -// TranslateArchive translates the given archive using the directives provided by the translation info. |
157 | | -func (a *AssetTranslation) TranslateArchive(archive *resource.Archive) (interface{}, error) { |
158 | | - // TODO[pulumi/pulumi#153]: support HashField. |
159 | | - |
160 | | - // Produce either a temp file or an in-memory representation, as requested. |
161 | | - format := a.Format |
162 | | - if format == resource.NotArchive { |
163 | | - format = resource.ZIPArchive |
164 | | - } |
165 | | - switch a.Kind { |
166 | | - case FileArchive, FileAsset: |
167 | | - path, err := translateToFile(archive.Hash, archive.HasContents(), func(w io.Writer) error { |
168 | | - return archive.Archive(format, w) |
169 | | - }) |
170 | | - return path, err |
171 | | - case BytesArchive, BytesAsset: |
172 | | - if !archive.HasContents() { |
173 | | - return []byte{}, nil |
174 | | - } |
175 | | - return archive.Bytes(format) |
176 | | - default: |
177 | | - contract.Failf("Unrecognized asset translation kind: %v", a.Kind) |
178 | | - return nil, nil |
179 | | - } |
180 | | -} |
0 commit comments