Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
Add support for stateless actions (#630)
Browse files Browse the repository at this point in the history
This implements stateless actions as defined in cnabio/cnab-spec#85.
  • Loading branch information
simonferquel authored and radu-matei committed Mar 14, 2019
1 parent 779c1d3 commit 178fe2d
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 11 deletions.
7 changes: 5 additions & 2 deletions pkg/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
"github.com/deislabs/duffle/pkg/driver"
)

// notStateless is there just to make callers of opFromClaims more readable
const notStateless = false

// Action describes one of the primary actions that can be executed in CNAB.
//
// The actions are:
Expand Down Expand Up @@ -48,8 +51,8 @@ func getImageMap(b *bundle.Bundle) ([]byte, error) {
return json.Marshal(imgs)
}

func opFromClaim(action string, c *claim.Claim, ii bundle.InvocationImage, creds credentials.Set, w io.Writer) (*driver.Operation, error) {
env, files, err := creds.Expand(c.Bundle)
func opFromClaim(action string, stateless bool, c *claim.Claim, ii bundle.InvocationImage, creds credentials.Set, w io.Writer) (*driver.Operation, error) {
env, files, err := creds.Expand(c.Bundle, stateless)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/action/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestOpFromClaim(t *testing.T) {
}
invocImage := c.Bundle.InvocationImages[0]

op, err := opFromClaim(claim.ActionInstall, c, invocImage, mockSet, os.Stdout)
op, err := opFromClaim(claim.ActionInstall, notStateless, c, invocImage, mockSet, os.Stdout)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -140,7 +140,7 @@ func TestOpFromClaim_UndefinedParams(t *testing.T) {
}
invocImage := c.Bundle.InvocationImages[0]

_, err := opFromClaim(claim.ActionInstall, c, invocImage, mockSet, os.Stdout)
_, err := opFromClaim(claim.ActionInstall, notStateless, c, invocImage, mockSet, os.Stdout)
assert.Error(t, err)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/action/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (i *Install) Run(c *claim.Claim, creds credentials.Set, w io.Writer) error
return err
}

op, err := opFromClaim(claim.ActionInstall, c, invocImage, creds, w)
op, err := opFromClaim(claim.ActionInstall, notStateless, c, invocImage, creds, w)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/action/run_custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (i *RunCustom) Run(c *claim.Claim, creds credentials.Set, w io.Writer) erro
return err
}

op, err := opFromClaim(i.Action, c, invocImage, creds, w)
op, err := opFromClaim(i.Action, actionDef.Stateless, c, invocImage, creds, w)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/action/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (i *Status) Run(c *claim.Claim, creds credentials.Set, w io.Writer) error {
return err
}

op, err := opFromClaim(claim.ActionStatus, c, invocImage, creds, w)
op, err := opFromClaim(claim.ActionStatus, notStateless, c, invocImage, creds, w)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/action/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (u *Uninstall) Run(c *claim.Claim, creds credentials.Set, w io.Writer) erro
return err
}

op, err := opFromClaim(claim.ActionUninstall, c, invocImage, creds, w)
op, err := opFromClaim(claim.ActionUninstall, notStateless, c, invocImage, creds, w)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/action/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (u *Upgrade) Run(c *claim.Claim, creds credentials.Set, w io.Writer) error
return err
}

op, err := opFromClaim(claim.ActionUpgrade, c, invocImage, creds, w)
op, err := opFromClaim(claim.ActionUpgrade, notStateless, c, invocImage, creds, w)
if err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ type Action struct {
//
// If it is possible that an action modify a release, this must be set to true.
Modifies bool `json:"modifies" mapstructure:"modifies"`
// Stateless indicates that the action is purely informational, that credentials are not required, and that the runtime should not keep track of its invocation
Stateless bool `json:"stateless,omitempty" mapstructure:"stateless"`
// Description describes the action as a user-readable string
Description string `json:"description,omitempty" mapstructure:"description,omitempty"`
}

// ValuesOrDefaults returns parameter values or the default parameter values
Expand Down
5 changes: 4 additions & 1 deletion pkg/credentials/credentialset.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ type Set map[string]string
//
// This matches the credentials required by the bundle to the credentials present
// in the credentialset, and then expands them per the definition in the Bundle.
func (s Set) Expand(b *bundle.Bundle) (env, files map[string]string, err error) {
func (s Set) Expand(b *bundle.Bundle, stateless bool) (env, files map[string]string, err error) {
env, files = map[string]string{}, map[string]string{}
for name, val := range b.Credentials {
src, ok := s[name]
if !ok {
if stateless {
continue
}
err = fmt.Errorf("credential %q is missing from the user-supplied credentials", name)
return
}
Expand Down
18 changes: 17 additions & 1 deletion pkg/credentials/credentialset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestCredentialSet_Expand(t *testing.T) {
"third": "third",
}

env, path, err := cs.Expand(b)
env, path, err := cs.Expand(b, false)
is := assert.New(t)
is.NoError(err)
for k, v := range b.Credentials {
Expand All @@ -85,3 +85,19 @@ func TestCredentialSet_Expand(t *testing.T) {
}
}
}

func TestCredentialSetMissingCred(t *testing.T) {
b := &bundle.Bundle{
Name: "knapsack",
Credentials: map[string]bundle.Location{
"first": {
EnvironmentVariable: "FIRST_VAR",
},
},
}
cs := Set{}
_, _, err := cs.Expand(b, false)
assert.EqualError(t, err, `credential "first" is missing from the user-supplied credentials`)
_, _, err = cs.Expand(b, true)
assert.NoError(t, err)
}

0 comments on commit 178fe2d

Please sign in to comment.