/
alias.go
45 lines (40 loc) 路 1.06 KB
/
alias.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
package resource
import (
"fmt"
"strings"
)
type Alias struct {
URN URN
Name string
Type string
Project string
Stack string
Parent URN
}
func (a *Alias) GetURN() URN {
if a.URN != "" {
return a.URN
}
return CreateURN(a.Name, a.Type, a.Parent, a.Project, a.Stack)
}
func (a *Alias) NoParent() bool {
return a.Parent == ""
}
// CreateURN computes a URN from the combination of a resource name, resource type, and optional parent,
func CreateURN(name string, t string, parent URN, project string, stack string) URN {
createURN := func(parent URN, stack string, project string, t string, name string) URN {
parentString := string(parent)
var parentPrefix string
if parent == "" {
parentPrefix = "urn:pulumi:" + stack + "::" + project + "::"
} else {
ix := strings.LastIndex(parentString, "::")
if ix == -1 {
panic(fmt.Sprintf("Expected 'parent' string '%s' to contain '::'", parent))
}
parentPrefix = parentString[0:ix] + "$"
}
return URN(parentPrefix + t + "::" + name)
}
return createURN(parent, stack, project, t, name)
}