Skip to content

Commit

Permalink
Update get methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lblackstone committed May 5, 2020
1 parent ec2759b commit c963966
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 9 deletions.
11 changes: 10 additions & 1 deletion provider/pkg/gen/go-templates/configFile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package yaml

import (
"fmt"

"github.com/pkg/errors"
"github.com/pulumi/pulumi/sdk/v2/go/pulumi"
)
Expand Down Expand Up @@ -87,6 +89,13 @@ func NewConfigFile(ctx *pulumi.Context,
return configFile, nil
}

func (cf *ConfigFile) GetResource(key string) pulumi.Output {
// GetResource returns a resource defined by a built-in Kubernetes group/version/kind, name and namespace.
// For example, GetResource("v1/Pod", "foo", "") would return a Pod called "foo" from the "default" namespace.
func (cf *ConfigFile) GetResource(gvk, name, namespace string) pulumi.Output {
id := name
if len(namespace) > 0 && namespace != "default" {
id = fmt.Sprintf("%s/%s", namespace, name)
}
key := fmt.Sprintf("%s::%s", gvk, id)
return cf.Resources.MapIndex(pulumi.String(key))
}
11 changes: 10 additions & 1 deletion provider/pkg/gen/go-templates/configGroup.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package yaml

import (
"fmt"

"github.com/pkg/errors"
"github.com/pulumi/pulumi/sdk/v2/go/pulumi"
)
Expand Down Expand Up @@ -87,6 +89,13 @@ func NewConfigGroup(ctx *pulumi.Context,
return configGroup, nil
}

func (cf *ConfigGroup) GetResource(key string) pulumi.Output {
// GetResource returns a resource defined by a built-in Kubernetes group/version/kind, name and namespace.
// For example, GetResource("v1/Pod", "foo", "") would return a Pod called "foo" from the "default" namespace.
func (cf *ConfigGroup) GetResource(gvk, name, namespace string) pulumi.Output {
id := name
if len(namespace) > 0 && namespace != "default" {
id = fmt.Sprintf("%s/%s", namespace, name)
}
key := fmt.Sprintf("%s::%s", gvk, id)
return cf.Resources.MapIndex(pulumi.String(key))
}
2 changes: 0 additions & 2 deletions provider/pkg/gen/go-templates/yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ func yamlDecode(ctx *pulumi.Context, text string, opts ...pulumi.ResourceOption)
var ret struct {
Result []map[string]interface{} `pulumi:"result"`
}
// TODO(joe): not clear how to translate ResourceOptions to InvokeOptions (or even get individual
// settings, e.g. opts.Parent).
if err := ctx.Invoke("kubernetes:yaml:decode", &args, &ret); err != nil {
return nil, err
}
Expand Down
11 changes: 10 additions & 1 deletion sdk/go/kubernetes/yaml/configFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package yaml

import (
"fmt"

"github.com/pkg/errors"
"github.com/pulumi/pulumi/sdk/v2/go/pulumi"
)
Expand Down Expand Up @@ -87,6 +89,13 @@ func NewConfigFile(ctx *pulumi.Context,
return configFile, nil
}

func (cf *ConfigFile) GetResource(key string) pulumi.Output {
// GetResource returns a resource defined by a built-in Kubernetes group/version/kind, name and namespace.
// For example, GetResource("v1/Pod", "foo", "") would return a Pod called "foo" from the "default" namespace.
func (cf *ConfigFile) GetResource(gvk, name, namespace string) pulumi.Output {
id := name
if len(namespace) > 0 && namespace != "default" {
id = fmt.Sprintf("%s/%s", namespace, name)
}
key := fmt.Sprintf("%s::%s", gvk, id)
return cf.Resources.MapIndex(pulumi.String(key))
}
11 changes: 10 additions & 1 deletion sdk/go/kubernetes/yaml/configGroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package yaml

import (
"fmt"

"github.com/pkg/errors"
"github.com/pulumi/pulumi/sdk/v2/go/pulumi"
)
Expand Down Expand Up @@ -87,6 +89,13 @@ func NewConfigGroup(ctx *pulumi.Context,
return configGroup, nil
}

func (cf *ConfigGroup) GetResource(key string) pulumi.Output {
// GetResource returns a resource defined by a built-in Kubernetes group/version/kind, name and namespace.
// For example, GetResource("v1/Pod", "foo", "") would return a Pod called "foo" from the "default" namespace.
func (cf *ConfigGroup) GetResource(gvk, name, namespace string) pulumi.Output {
id := name
if len(namespace) > 0 && namespace != "default" {
id = fmt.Sprintf("%s/%s", namespace, name)
}
key := fmt.Sprintf("%s::%s", gvk, id)
return cf.Resources.MapIndex(pulumi.String(key))
}
2 changes: 0 additions & 2 deletions sdk/go/kubernetes/yaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,6 @@ func yamlDecode(ctx *pulumi.Context, text string, opts ...pulumi.ResourceOption)
var ret struct {
Result []map[string]interface{} `pulumi:"result"`
}
// TODO(joe): not clear how to translate ResourceOptions to InvokeOptions (or even get individual
// settings, e.g. opts.Parent).
if err := ctx.Invoke("kubernetes:yaml:decode", &args, &ret); err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/go/yaml/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func main() {
}

if resources != nil {
hostIP := resources.GetResource("v1/Pod::foo").Apply(func(r interface{}) (interface{}, error) {
hostIP := resources.GetResource("v1/Pod", "foo", "").Apply(func(r interface{}) (interface{}, error) {
pod := r.(*corev1.Pod)
return pod.Status.HostIP(), nil
})
Expand Down

0 comments on commit c963966

Please sign in to comment.