-
Notifications
You must be signed in to change notification settings - Fork 41
/
plugin_info.go
243 lines (201 loc) · 8.05 KB
/
plugin_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// 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 il
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"os/exec"
"sync"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
"github.com/pulumi/pulumi/pkg/v3/codegen/convert"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge"
)
// ProviderInfoSource abstracts the ability to fetch tfbridge information for a Terraform provider. This is abstracted
// primarily for testing purposes.
type ProviderInfoSource interface {
// GetProviderInfo returns the tfbridge information for the indicated Terraform provider.
GetProviderInfo(registry, namespace, name, version string) (*tfbridge.ProviderInfo, error)
}
// mapperProviderInfoSource wraps a convert.Mapper to return tfbridge.ProviderInfo
type mapperProviderInfoSource struct {
mapper convert.Mapper
}
func NewMapperProviderInfoSource(mapper convert.Mapper) ProviderInfoSource {
return &mapperProviderInfoSource{mapper: mapper}
}
func (mapper *mapperProviderInfoSource) GetProviderInfo(
registryName, namespace, name, version string) (*tfbridge.ProviderInfo, error) {
data, err := mapper.mapper.GetMapping(name)
if err != nil {
return nil, err
}
// Might be nil or []
if len(data) == 0 {
message := fmt.Sprintf("could not find mapping information for provider %s", name)
message += "; try installing a pulumi plugin that supports this terraform provider"
return nil, fmt.Errorf(message)
}
var info *tfbridge.MarshallableProviderInfo
err = json.Unmarshal(data, &info)
if err != nil {
return nil, fmt.Errorf("could not decode schema information for provider %s: %w", name, err)
}
return info.Unmarshal(), nil
}
// CachingProviderInfoSource wraps a ProviderInfoSource in a cache for faster access.
type CachingProviderInfoSource struct {
m sync.RWMutex
source ProviderInfoSource
entries map[string]*tfbridge.ProviderInfo
}
func (cache *CachingProviderInfoSource) cacheKey(registry, namespace, name, version string) string {
return fmt.Sprintf("%s/%s/%s@%s",
url.PathEscape(registry), url.PathEscape(namespace), url.PathEscape(name), url.PathEscape(version))
}
func (cache *CachingProviderInfoSource) getProviderInfo(key string) (*tfbridge.ProviderInfo, bool) {
cache.m.RLock()
defer cache.m.RUnlock()
info, ok := cache.entries[key]
return info, ok
}
// GetProviderInfo returns the tfbridge information for the indicated Terraform provider as well as the name of the
// corresponding Pulumi resource provider.
func (cache *CachingProviderInfoSource) GetProviderInfo(
registryName, namespace, name, version string) (*tfbridge.ProviderInfo, error) {
key := cache.cacheKey(registryName, namespace, name, version)
if info, ok := cache.getProviderInfo(key); ok {
return info, nil
}
cache.m.Lock()
defer cache.m.Unlock()
info, err := cache.source.GetProviderInfo(registryName, namespace, name, version)
if err != nil {
return nil, err
}
cache.entries[key] = info
return info, nil
}
// NewCachingProviderInfoSource creates a new CachingProviderInfoSource that wraps the given ProviderInfoSource.
func NewCachingProviderInfoSource(source ProviderInfoSource) *CachingProviderInfoSource {
return &CachingProviderInfoSource{
source: source,
entries: map[string]*tfbridge.ProviderInfo{},
}
}
type multiProviderInfoSource []ProviderInfoSource
func NewMultiProviderInfoSource(sources ...ProviderInfoSource) ProviderInfoSource {
return multiProviderInfoSource(sources)
}
func (s multiProviderInfoSource) GetProviderInfo(
registryName, namespace, name, version string) (*tfbridge.ProviderInfo, error) {
for _, s := range s {
if s != nil {
if info, err := s.GetProviderInfo(registryName, namespace, name, version); err == nil && info != nil {
return info, nil
}
}
}
return nil, getMissingPluginError(name)
}
type pluginProviderInfoSource struct{}
// PluginProviderInfoSource is the ProviderInfoSource that retrieves tfbridge information by loading and interrogating
// the Pulumi resource provider that corresponds to a Terraform provider.
var PluginProviderInfoSource = ProviderInfoSource(pluginProviderInfoSource{})
var pulumiNames = map[string]string{
"azurerm": "azure",
"bigip": "f5bigip",
"google": "gcp",
"template": "terraform-template",
}
// GetPulumiProviderName returns the Pulumi name for the given Terraform provider. In most cases the two names will be
// identical.
func GetPulumiProviderName(terraformProviderName string) string {
if pulumiName, hasPulumiName := pulumiNames[terraformProviderName]; hasPulumiName {
return pulumiName
}
return terraformProviderName
}
// GetTerraformProviderName returns the canonical Terraform provider name for the given provider info.
func GetTerraformProviderName(info tfbridge.ProviderInfo) string {
if info.Name == "google-beta" {
return "google"
}
return info.Name
}
// GetProviderInfo returns the tfbridge information for the indicated Terraform provider as well as the name of the
// corresponding Pulumi resource provider.
func (pluginProviderInfoSource) GetProviderInfo(
registryName, namespace, name, version string) (*tfbridge.ProviderInfo, error) {
tfProviderName := name
pluginName := GetPulumiProviderName(tfProviderName)
path, err := workspace.GetPluginPath(workspace.ResourcePlugin, pluginName, nil, nil)
if err != nil {
return nil, err
} else if path == "" {
return nil, getMissingPluginError(name)
}
// Run the plugin and decode its provider config.
// nolint: gas
cmd := exec.Command(path, "-get-provider-info")
out, err := cmd.StdoutPipe()
if err != nil {
return nil, errors.Wrapf(err, "failed to load plugin %s for provider %s", pluginName, tfProviderName)
}
if err = cmd.Start(); err != nil {
return nil, errors.Wrapf(err, "failed to load plugin %s for provider %s", pluginName, tfProviderName)
}
var info *tfbridge.MarshallableProviderInfo
err = jsoniter.NewDecoder(out).Decode(&info)
if cErr := cmd.Wait(); cErr != nil {
return nil, errors.Wrapf(err, "failed to run plugin %s for provider %s", pluginName, tfProviderName)
}
if err != nil {
return nil, errors.Wrapf(err, "could not decode schema information for provider %s", tfProviderName)
}
return info.Unmarshal(), nil
}
// getMissingPluginError returns an error that informs the user that a plugin for a Terraform provider cannot be found,
// and how to go about acquiring it if it is hosted on Pulumi.com.
func getMissingPluginError(providerName string) error {
pluginName := GetPulumiProviderName(providerName)
message := fmt.Sprintf("could not find plugin %s for provider %s", pluginName, providerName)
latest := getLatestPluginVersion(pluginName)
if latest != "" {
message += fmt.Sprintf("; try running 'pulumi plugin install resource %s %s'", pluginName, latest)
}
return errors.New(message)
}
// getLatestPluginVersion returns the version number for the latest released version of the indicated plugin by
// querying the value of the `latest` tag in the plugin's corresponding NPM package.
func getLatestPluginVersion(pluginName string) string {
resp, err := http.Get("https://registry.npmjs.org/@pulumi/" + pluginName)
if err != nil {
return ""
}
defer contract.IgnoreClose(resp.Body)
// The structure of the response to the above call is documented here:
// - https://github.com/npm/registry/blob/master/docs/responses/package-metadata.md
var packument struct {
DistTags map[string]string `json:"dist-tags"`
}
if err = jsoniter.NewDecoder(resp.Body).Decode(&packument); err != nil {
return ""
}
return packument.DistTags["latest"]
}