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
|
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfconfig
import (
"fmt"
"strconv"
"strings"
)
// Resource represents a single "resource" or "data" block within a module.
type Resource struct {
Mode ResourceMode `json:"mode"`
Type string `json:"type"`
Name string `json:"name"`
Provider ProviderRef `json:"provider"`
Pos SourcePos `json:"pos"`
}
// MapKey returns a string that can be used to uniquely identify the receiver
// in a map[string]*Resource.
func (r *Resource) MapKey() string {
switch r.Mode {
case ManagedResourceMode:
return fmt.Sprintf("%s.%s", r.Type, r.Name)
case DataResourceMode:
return fmt.Sprintf("data.%s.%s", r.Type, r.Name)
default:
// should never happen
return fmt.Sprintf("[invalid_mode!].%s.%s", r.Type, r.Name)
}
}
// ResourceMode represents the "mode" of a resource, which is used to
// distinguish between managed resources ("resource" blocks in config) and
// data resources ("data" blocks in config).
type ResourceMode rune
const InvalidResourceMode ResourceMode = 0
const ManagedResourceMode ResourceMode = 'M'
const DataResourceMode ResourceMode = 'D'
func (m ResourceMode) String() string {
switch m {
case ManagedResourceMode:
return "managed"
case DataResourceMode:
return "data"
default:
return ""
}
}
// MarshalJSON implements encoding/json.Marshaler.
func (m ResourceMode) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(m.String())), nil
}
func resourceTypeDefaultProviderName(typeName string) string {
if underPos := strings.IndexByte(typeName, '_'); underPos != -1 {
return typeName[:underPos]
}
return typeName
}
|