Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Bootstrapping and resources #357

Merged
merged 38 commits into from
Mar 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
3227736
Add resource plugin API and example
nwt Jan 11, 2017
f589c2a
Add bootstrap plugin
nwt Jan 11, 2017
7199b61
Merge branch 'master' into bootstrapping-and-resources
Jan 12, 2017
90d662e
Merge branch 'master' into bootstrapping-and-resources
nwt Jan 12, 2017
c88e0f1
Vendor github.com/twmb/algoimpl
nwt Jan 13, 2017
0b338e9
Rename bootstrap plugin to resource plugin
nwt Jan 13, 2017
6034f0e
Merge branch 'master' into bootstrapping-and-resources
nwt Jan 13, 2017
a6861b9
Use the instance SPI now that instance RPC supports types
nwt Jan 13, 2017
290be1b
Merge branch 'master' into bootstrapping-and-resources
nwt Jan 17, 2017
5a52ab3
Merge branch 'master' into bootstrapping-and-resources
nwt Jan 23, 2017
fbab575
Add resource SPI
nwt Jan 25, 2017
186b01f
Fix typos
nwt Jan 25, 2017
116f29c
Move template parsing into validate method
nwt Jan 25, 2017
f829033
Merge branch 'master' into bootstrapping-and-resources
nwt Feb 17, 2017
aa68dff
Update pkg/plugin/resource/resource.go after merge
nwt Feb 17, 2017
c4af50f
Use github.com/docker/infrakit/pkg/template
nwt Feb 23, 2017
4af2adf
Merge 'master' into bootstrapping-and-resources
nwt Mar 2, 2017
c1caa0d
Merge branch 'master' into bootstrapping-and-resources
nwt Mar 8, 2017
23503e2
Switch syntax to `{{ resource "key" }}`
nwt Mar 8, 2017
94aeb6c
Remove stray fmt.Println
nwt Mar 8, 2017
e3196bc
Remove resourceIdentityFunc
nwt Mar 11, 2017
0b1e083
Improve logging and output
nwt Mar 11, 2017
4754039
Merge branch 'master' into bootstrapping-and-resources
nwt Mar 14, 2017
4489d2a
Refactor
nwt Mar 14, 2017
1949765
Add tests
nwt Mar 14, 2017
7b74eeb
Revert "Remove resourceIdentityFunc"
nwt Mar 15, 2017
83c8302
Merge branch 'master' into bootstrapping-and-resources
Mar 15, 2017
457bd1d
Merge branch 'master' into bootstrapping-and-resources
Mar 16, 2017
a3036b2
Merge branch 'master' into bootstrapping-and-resources
Mar 18, 2017
6ce9423
Update DescribeResources signature
nwt Mar 16, 2017
91b597f
Add resource plugin executable
nwt Mar 20, 2017
8f07a1d
Remove "ID" from resource plugin output
nwt Mar 20, 2017
a2014c8
Rename resource.go to plugin.go
nwt Mar 20, 2017
faa771a
Merge branch 'master' into bootstrapping-and-resources
nwt Mar 20, 2017
b7311d3
Merge branch 'master' into bootstrapping-and-resources
nwt Mar 22, 2017
c3cb29b
Change syntax from map to list
nwt Mar 22, 2017
69a10ad
Update resource plugin tag names
nwt Mar 22, 2017
a6a2eba
Merge branch 'master' into bootstrapping-and-resources
nwt Mar 22, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update DescribeResources signature
Signed-off-by: Noah Treuhaft <noah.treuhaft@docker.com>
  • Loading branch information
nwt committed Mar 20, 2017
commit 6ce942394fc68bb3489625ea2205140f26f028f3
26 changes: 22 additions & 4 deletions cmd/cli/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ func resourceCommand(plugins func() discovery.Plugins) *cobra.Command {
Short: "Access resource plugin",
}
cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error {
if err := upTree(c, func(x *cobra.Command, argv []string) error {
if x.PersistentPreRunE != nil {
return x.PersistentPreRunE(x, argv)
}
return nil
}); err != nil {
return err
}

plugins, err := discovery.NewPluginDiscovery()
if err != nil {
Expand Down Expand Up @@ -65,7 +73,12 @@ func resourceCommand(plugins func() discovery.Plugins) *cobra.Command {
if err != nil {
return err
}
view, err := engine.AddFunc("resource", resourceIdentityFunc).Render(nil)

engine.WithFunctions(func() []template.Function {
return []template.Function{{Name: "resource", Func: resourceFunc}}
})

view, err := engine.Render(nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -112,7 +125,12 @@ func resourceCommand(plugins func() discovery.Plugins) *cobra.Command {
if err != nil {
return err
}
view, err := engine.AddFunc("resource", resourceIdentityFunc).Render(nil)

engine.WithFunctions(func() []template.Function {
return []template.Function{{Name: "resource", Func: resourceFunc}}
})

view, err := engine.Render(nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -140,6 +158,6 @@ func resourceCommand(plugins func() discovery.Plugins) *cobra.Command {
return cmd
}

func resourceIdentityFunc(name string) string {
return fmt.Sprintf("{{ resource `%s` }}", name)
func resourceFunc(s string) string {
return fmt.Sprintf("{{ resource `%s` }}", s)
}
21 changes: 19 additions & 2 deletions pkg/plugin/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"sort"
"strings"

log "github.com/Sirupsen/logrus"
"github.com/docker/infrakit/pkg/discovery"
Expand Down Expand Up @@ -157,8 +158,24 @@ func (p *plugin) Destroy(config resource.Spec, pretend bool) (string, error) {
return desc, nil
}

func (p *plugin) DescribeResources() ([]instance.Description, error) {
return nil, errors.New("unimplemented")
func (p *plugin) DescribeResources(config resource.Spec) (string, error) {
spec, _, err := validate(config, p.instancePluginLookup)
if err != nil {
return "", err
}

ids, err := describe(config.ID, *spec)
if err != nil {
return "", err
}

details := []string{}
for name, id := range ids {
details = append(details, fmt.Sprintf("Found %s (ID %s)", name, id))
}
sort.Strings(details)

return strings.Join(details, "\n"), nil
}

func describe(configID resource.ID, spec Spec) (map[string]instance.ID, error) {
Expand Down
37 changes: 27 additions & 10 deletions pkg/plugin/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ func newTestInstancePlugin() *testing_instance.Plugin {
DoDescribeInstances: func(tags map[string]string) ([]instance.Description, error) {
descriptions := []instance.Description{}
Loop:
for id, instnc := range instances {
for id, inst := range instances {
for k, v := range tags {
if v2, ok := instnc.Tags[k]; !ok || v2 != v {
if v2, ok := inst.Tags[k]; !ok || v2 != v {
continue Loop
}
}
descriptions = append(descriptions, instance.Description{
ID: id,
LogicalID: instnc.LogicalID,
Tags: instnc.Tags,
LogicalID: inst.LogicalID,
Tags: inst.Tags,
})
}
return descriptions, nil
Expand All @@ -75,9 +75,9 @@ func TestCommitAndDestroy(t *testing.T) {
properties := types.AnyString(
`{"Resources": {"a": {"Plugin": "pluginA", "Properties": "{{ resource ` + "`b`" + ` }}"}, "b": {"Plugin": "pluginB", "Properties": ""}}}`)

const specID = "config"
const configID = "config"
spec := resource.Spec{
ID: specID,
ID: configID,
Properties: properties,
}

Expand All @@ -103,15 +103,15 @@ func TestCommitAndDestroy(t *testing.T) {

require.NotEqual(t, "", descriptions[0].ID)
require.Nil(t, descriptions[0].LogicalID)
require.Equal(t, map[string]string{resourceGroupTag: specID, resourceNameTag: "a"}, descriptions[0].Tags)
require.Equal(t, map[string]string{resourceGroupTag: configID, resourceNameTag: "a"}, descriptions[0].Tags)

descriptions, err = instancePluginB.DescribeInstances(nil)
require.NoError(t, err)
require.Len(t, descriptions, 1)

require.NotEqual(t, "", descriptions[0].ID)
require.Nil(t, descriptions[0].LogicalID)
require.Equal(t, map[string]string{resourceGroupTag: specID, resourceNameTag: "b"}, descriptions[0].Tags)
require.Equal(t, map[string]string{resourceGroupTag: configID, resourceNameTag: "b"}, descriptions[0].Tags)

// Commit with the same specification should create no additional resources.
_, err = p.Commit(spec, false)
Expand Down Expand Up @@ -155,8 +155,25 @@ func TestCommitAndDestroy(t *testing.T) {
}

func TestDescribeResources(t *testing.T) {
_, err := NewResourcePlugin(nil).DescribeResources()
require.Error(t, err)
const configID = "config"
instancePlugin := newTestInstancePlugin()
p := NewResourcePlugin(func(name plugin_base.Name) (instance.Plugin, error) {
return instancePlugin, nil
})

aID, err := instancePlugin.Provision(instance.Spec{Tags: map[string]string{resourceGroupTag: configID, resourceNameTag: "a"}})
require.NoError(t, err)
bID, err := instancePlugin.Provision(instance.Spec{Tags: map[string]string{resourceGroupTag: configID, resourceNameTag: "b"}})
require.NoError(t, err)

properties := types.AnyString(`{"Resources": {"a": {"Plugin": "p", "Properties": ""}, "b": {"Plugin": "p", "Properties": ""}}}`)
spec := resource.Spec{
ID: configID,
Properties: properties,
}
details, err := p.DescribeResources(spec)
require.NoError(t, err)
require.Equal(t, fmt.Sprintf("Found a (ID %s)\nFound b (ID %s)", string(*aID), string(*bID)), details)
}

func TestDescribe(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions pkg/spi/resource/spi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package resource

import (
"github.com/docker/infrakit/pkg/spi"
"github.com/docker/infrakit/pkg/spi/instance"
"github.com/docker/infrakit/pkg/types"
)

Expand All @@ -12,11 +11,12 @@ var InterfaceSpec = spi.InterfaceSpec{
Version: "0.1.0",
}

// ID is the unique identifier for a collection of resources.
// ID is a unique identifier for a collection of resources.
type ID string

// Spec is a specification of resources to provision.
type Spec struct {

// ID is the unique identifier for the collection of resources.
ID ID

Expand All @@ -26,7 +26,7 @@ type Spec struct {

// Plugin defines the functions for a Resource plugin.
type Plugin interface {
Commit(spec Spec, pretend bool) (plan string, err error)
Destroy(spec Spec, pretend bool) (plan string, err error)
DescribeResources() ([]instance.Description, error)
Commit(spec Spec, pretend bool) (string, error)
Destroy(spec Spec, pretend bool) (string, error)
DescribeResources(spec Spec) (string, error)
}