|
4 | 4 | "fmt" |
5 | 5 |
|
6 | 6 | "github.com/hashicorp/terraform/configs/configschema" |
| 7 | + "github.com/mcuadros/ascode/terraform" |
7 | 8 | "go.starlark.net/starlark" |
8 | 9 | "go.starlark.net/syntax" |
9 | 10 | ) |
@@ -67,7 +68,9 @@ func (c *ResourceCollection) Truth() starlark.Bool { |
67 | 68 | func (c *ResourceCollection) Freeze() {} |
68 | 69 |
|
69 | 70 | // Hash honors the starlark.Value interface. |
70 | | -func (c *ResourceCollection) Hash() (uint32, error) { return 42, nil } |
| 71 | +func (c *ResourceCollection) Hash() (uint32, error) { |
| 72 | + return 0, fmt.Errorf("unhashable type: ResourceCollection") |
| 73 | +} |
71 | 74 |
|
72 | 75 | // Name honors the starlark.Callable interface. |
73 | 76 | func (c *ResourceCollection) Name() string { |
@@ -248,3 +251,107 @@ func getValue(r *Resource, key string) starlark.Value { |
248 | 251 |
|
249 | 252 | return r.values.Get(key).Starlark() |
250 | 253 | } |
| 254 | + |
| 255 | +type ProviderCollection struct { |
| 256 | + pm *terraform.PluginManager |
| 257 | + *AttrDict |
| 258 | +} |
| 259 | + |
| 260 | +func NewProviderCollection(pm *terraform.PluginManager) *ProviderCollection { |
| 261 | + return &ProviderCollection{ |
| 262 | + pm: pm, |
| 263 | + AttrDict: NewAttrDict(), |
| 264 | + } |
| 265 | +} |
| 266 | + |
| 267 | +// String honors the starlark.Value interface. |
| 268 | +func (c *ProviderCollection) String() string { |
| 269 | + return "foo" |
| 270 | +} |
| 271 | + |
| 272 | +// Type honors the starlark.Value interface. |
| 273 | +func (c *ProviderCollection) Type() string { |
| 274 | + return "ProviderCollection" |
| 275 | +} |
| 276 | + |
| 277 | +// Truth honors the starlark.Value interface. |
| 278 | +func (c *ProviderCollection) Truth() starlark.Bool { |
| 279 | + return true // even when empty |
| 280 | +} |
| 281 | + |
| 282 | +// Freeze honors the starlark.Value interface. |
| 283 | +func (c *ProviderCollection) Freeze() {} |
| 284 | + |
| 285 | +// Hash honors the starlark.Value interface. |
| 286 | +func (c *ProviderCollection) Hash() (uint32, error) { |
| 287 | + return 0, fmt.Errorf("unhashable type: ProviderCollection") |
| 288 | +} |
| 289 | + |
| 290 | +// Name honors the starlark.Callable interface. |
| 291 | +func (c *ProviderCollection) Name() string { |
| 292 | + return "foo" |
| 293 | +} |
| 294 | + |
| 295 | +// CallInternal honors the starlark.Callable interface. |
| 296 | +func (c *ProviderCollection) CallInternal(thread *starlark.Thread, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { |
| 297 | + name, version, alias, err := c.unpackArgs(args) |
| 298 | + if err != nil { |
| 299 | + return nil, err |
| 300 | + } |
| 301 | + |
| 302 | + return c.MakeProvider(name, version, alias, kwargs) |
| 303 | +} |
| 304 | + |
| 305 | +func (c *ProviderCollection) MakeProvider(name, version, alias string, kwargs []starlark.Tuple) (*Provider, error) { |
| 306 | + n := starlark.String(name) |
| 307 | + a := starlark.String(alias) |
| 308 | + |
| 309 | + if _, ok, _ := c.Get(n); !ok { |
| 310 | + c.SetKey(n, NewAttrDict()) |
| 311 | + } |
| 312 | + providers, _, _ := c.Get(n) |
| 313 | + if _, ok, _ := providers.(*AttrDict).Get(a); ok { |
| 314 | + return nil, fmt.Errorf("already exists a provider %q with the alias %q", name, alias) |
| 315 | + } |
| 316 | + |
| 317 | + p, err := MakeProvider(c.pm, name, version, alias) |
| 318 | + if err != nil { |
| 319 | + return nil, err |
| 320 | + } |
| 321 | + |
| 322 | + if err := providers.(*AttrDict).SetKey(starlark.String(p.alias), p); err != nil { |
| 323 | + return nil, err |
| 324 | + } |
| 325 | + |
| 326 | + return p, p.loadKeywordArgs(kwargs) |
| 327 | +} |
| 328 | + |
| 329 | +func (c *ProviderCollection) unpackArgs(args starlark.Tuple) (string, string, string, error) { |
| 330 | + var name, version, alias starlark.String |
| 331 | + switch len(args) { |
| 332 | + case 3: |
| 333 | + var ok bool |
| 334 | + alias, ok = args.Index(2).(starlark.String) |
| 335 | + if !ok { |
| 336 | + return "", "", "", fmt.Errorf("expected string, got %s", args.Index(2).Type()) |
| 337 | + } |
| 338 | + fallthrough |
| 339 | + case 2: |
| 340 | + var ok bool |
| 341 | + version, ok = args.Index(1).(starlark.String) |
| 342 | + if !ok { |
| 343 | + return "", "", "", fmt.Errorf("expected string, got %s", args.Index(1).Type()) |
| 344 | + } |
| 345 | + fallthrough |
| 346 | + case 1: |
| 347 | + var ok bool |
| 348 | + name, ok = args.Index(0).(starlark.String) |
| 349 | + if !ok { |
| 350 | + return "", "", "", fmt.Errorf("expected string, got %s", args.Index(0).Type()) |
| 351 | + } |
| 352 | + default: |
| 353 | + return "", "", "", fmt.Errorf("unexpected positional arguments count") |
| 354 | + } |
| 355 | + |
| 356 | + return string(name), string(version), string(alias), nil |
| 357 | +} |
0 commit comments