-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
cpi-config
and update-cpi-config
commands
[#132264755](https://www.pivotaltracker.com/story/show/132264755) Signed-off-by: Ming Xiao <[email protected]>
- Loading branch information
1 parent
56c2abe
commit 49a28f8
Showing
12 changed files
with
507 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package cmd | ||
|
||
import ( | ||
boshdir "github.com/cloudfoundry/bosh-cli/director" | ||
boshui "github.com/cloudfoundry/bosh-cli/ui" | ||
) | ||
|
||
type CPIConfigCmd struct { | ||
ui boshui.UI | ||
director boshdir.Director | ||
} | ||
|
||
func NewCPIConfigCmd(ui boshui.UI, director boshdir.Director) CPIConfigCmd { | ||
return CPIConfigCmd{ui: ui, director: director} | ||
} | ||
|
||
func (c CPIConfigCmd) Run() error { | ||
cpiConfig, err := c.director.LatestCPIConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c.ui.PrintBlock(cpiConfig.Properties) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cmd_test | ||
|
||
import ( | ||
"errors" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
. "github.com/cloudfoundry/bosh-cli/cmd" | ||
boshdir "github.com/cloudfoundry/bosh-cli/director" | ||
fakedir "github.com/cloudfoundry/bosh-cli/director/directorfakes" | ||
fakeui "github.com/cloudfoundry/bosh-cli/ui/fakes" | ||
) | ||
|
||
var _ = Describe("CPIConfigCmd", func() { | ||
var ( | ||
ui *fakeui.FakeUI | ||
director *fakedir.FakeDirector | ||
command CPIConfigCmd | ||
) | ||
|
||
BeforeEach(func() { | ||
ui = &fakeui.FakeUI{} | ||
director = &fakedir.FakeDirector{} | ||
command = NewCPIConfigCmd(ui, director) | ||
}) | ||
|
||
Describe("Run", func() { | ||
act := func() error { return command.Run() } | ||
|
||
It("shows cpi config", func() { | ||
cpiConfig := boshdir.CPIConfig{ | ||
Properties: "some-properties", | ||
} | ||
|
||
director.LatestCPIConfigReturns(cpiConfig, nil) | ||
|
||
err := act() | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
Expect(ui.Blocks).To(Equal([]string{"some-properties"})) | ||
}) | ||
|
||
It("returns error if cpi config cannot be retrieved", func() { | ||
director.LatestCPIConfigReturns(boshdir.CPIConfig{}, errors.New("fake-err")) | ||
|
||
err := act() | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(ContainSubstring("fake-err")) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package cmd | ||
|
||
import ( | ||
bosherr "github.com/cloudfoundry/bosh-utils/errors" | ||
|
||
boshdir "github.com/cloudfoundry/bosh-cli/director" | ||
boshtpl "github.com/cloudfoundry/bosh-cli/director/template" | ||
boshui "github.com/cloudfoundry/bosh-cli/ui" | ||
) | ||
|
||
type UpdateCPIConfigCmd struct { | ||
ui boshui.UI | ||
director boshdir.Director | ||
} | ||
|
||
func NewUpdateCPIConfigCmd(ui boshui.UI, director boshdir.Director) UpdateCPIConfigCmd { | ||
return UpdateCPIConfigCmd{ui: ui, director: director} | ||
} | ||
|
||
func (c UpdateCPIConfigCmd) Run(opts UpdateCPIConfigOpts) error { | ||
tpl := boshtpl.NewTemplate(opts.Args.CPIConfig.Bytes) | ||
|
||
bytes, err := tpl.Evaluate(opts.VarFlags.AsVariables(), opts.OpsFlags.AsOps(), boshtpl.EvaluateOpts{}) | ||
if err != nil { | ||
return bosherr.WrapErrorf(err, "Evaluating cpi config") | ||
} | ||
|
||
err = c.ui.AskForConfirmation() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return c.director.UpdateCPIConfig(bytes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package cmd_test | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/cppforlife/go-patch/patch" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
. "github.com/cloudfoundry/bosh-cli/cmd" | ||
fakedir "github.com/cloudfoundry/bosh-cli/director/directorfakes" | ||
boshtpl "github.com/cloudfoundry/bosh-cli/director/template" | ||
fakeui "github.com/cloudfoundry/bosh-cli/ui/fakes" | ||
) | ||
|
||
var _ = Describe("UpdateCPIConfigCmd", func() { | ||
var ( | ||
ui *fakeui.FakeUI | ||
director *fakedir.FakeDirector | ||
command UpdateCPIConfigCmd | ||
) | ||
|
||
BeforeEach(func() { | ||
ui = &fakeui.FakeUI{} | ||
director = &fakedir.FakeDirector{} | ||
command = NewUpdateCPIConfigCmd(ui, director) | ||
}) | ||
|
||
Describe("Run", func() { | ||
var ( | ||
opts UpdateCPIConfigOpts | ||
) | ||
|
||
BeforeEach(func() { | ||
opts = UpdateCPIConfigOpts{ | ||
Args: UpdateCPIConfigArgs{ | ||
CPIConfig: FileBytesArg{Bytes: []byte("cpi-config")}, | ||
}, | ||
} | ||
}) | ||
|
||
act := func() error { return command.Run(opts) } | ||
|
||
It("updates cpi config", func() { | ||
err := act() | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
Expect(director.UpdateCPIConfigCallCount()).To(Equal(1)) | ||
|
||
bytes := director.UpdateCPIConfigArgsForCall(0) | ||
Expect(bytes).To(Equal([]byte("cpi-config\n"))) | ||
}) | ||
|
||
It("updates templated cpi config", func() { | ||
opts.Args.CPIConfig = FileBytesArg{ | ||
Bytes: []byte("name: ((name))\ntype: ((type))"), | ||
} | ||
|
||
opts.VarKVs = []boshtpl.VarKV{ | ||
{Name: "name", Value: "val1-from-kv"}, | ||
} | ||
|
||
opts.VarsFiles = []boshtpl.VarsFileArg{ | ||
{Vars: boshtpl.Variables(map[string]interface{}{"name": "val1-from-file"})}, | ||
{Vars: boshtpl.Variables(map[string]interface{}{"type": "val2-from-file"})}, | ||
} | ||
|
||
opts.OpsFiles = []OpsFileArg{ | ||
{ | ||
Ops: patch.Ops([]patch.Op{ | ||
patch.ReplaceOp{Path: patch.MustNewPointerFromString("/xyz?"), Value: "val"}, | ||
}), | ||
}, | ||
} | ||
|
||
err := act() | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
Expect(director.UpdateCPIConfigCallCount()).To(Equal(1)) | ||
|
||
bytes := director.UpdateCPIConfigArgsForCall(0) | ||
Expect(bytes).To(Equal([]byte("name: val1-from-kv\ntype: val2-from-file\nxyz: val\n"))) | ||
}) | ||
|
||
It("does not stop if confirmation is rejected", func() { | ||
ui.AskedConfirmationErr = errors.New("stop") | ||
|
||
err := act() | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(ContainSubstring("stop")) | ||
|
||
Expect(director.UpdateCPIConfigCallCount()).To(Equal(0)) | ||
}) | ||
|
||
It("returns error if updating failed", func() { | ||
director.UpdateCPIConfigReturns(errors.New("fake-err")) | ||
|
||
err := act() | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(ContainSubstring("fake-err")) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package director | ||
|
||
import ( | ||
"net/http" | ||
|
||
bosherr "github.com/cloudfoundry/bosh-utils/errors" | ||
) | ||
|
||
type CPIConfig struct { | ||
Properties string | ||
} | ||
|
||
func (d DirectorImpl) LatestCPIConfig() (CPIConfig, error) { | ||
resps, err := d.client.CPIConfigs() | ||
if err != nil { | ||
return CPIConfig{}, err | ||
} | ||
|
||
if len(resps) == 0 { | ||
return CPIConfig{}, bosherr.Error("No CPI config") | ||
} | ||
|
||
return resps[0], nil | ||
} | ||
|
||
func (d DirectorImpl) UpdateCPIConfig(manifest []byte) error { | ||
return d.client.UpdateCPIConfig(manifest) | ||
} | ||
|
||
func (c Client) CPIConfigs() ([]CPIConfig, error) { | ||
var resps []CPIConfig | ||
|
||
err := c.clientRequest.Get("/cpi_configs?limit=1", &resps) | ||
if err != nil { | ||
return resps, bosherr.WrapErrorf(err, "Finding CPI configs") | ||
} | ||
|
||
return resps, nil | ||
} | ||
|
||
func (c Client) UpdateCPIConfig(manifest []byte) error { | ||
path := "/cpi_configs" | ||
|
||
setHeaders := func(req *http.Request) { | ||
req.Header.Add("Content-Type", "text/yaml") | ||
} | ||
|
||
_, _, err := c.clientRequest.RawPost(path, manifest, setHeaders) | ||
if err != nil { | ||
return bosherr.WrapErrorf(err, "Updating CPI config") | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.