Skip to content

Commit

Permalink
feat: b3lbctl apply -f localhost.tenant.yaml command #41
Browse files Browse the repository at this point in the history
  • Loading branch information
SLedunois committed Apr 21, 2022
1 parent 62a2f5c commit ecd2a78
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pkg/cmd/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func (cmd *Cmd) ApplyFlags() {
}

func (cmd *Cmd) printApplyMessage(kind string, resource *interface{}) {
if kind == "InstanceList" {
cmd.Command.Println("InstanceList created")
if kind == "InstanceList" || kind == "Tenant" {
cmd.Command.Println(fmt.Sprintf("%s resource created", kind))
}
}

Expand Down
32 changes: 30 additions & 2 deletions pkg/cmd/apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestNewCmd(t *testing.T) {
},
},
{
Name: "applying an InstanceList file should print a valid `InstanceList created` message",
Name: "applying an InstanceList file should print a valid `InstanceList resource created` message",
Args: []string{"-f", "/tmp/instances.yml"},
Mock: func() {
mock.ApplyFunc = func(kind string, resource *interface{}) error {
Expand All @@ -62,7 +62,35 @@ func TestNewCmd(t *testing.T) {
},
Validator: func(t *testing.T, output *bytes.Buffer, err error) {
assert.Nil(t, err)
assert.Equal(t, "InstanceList created", strings.TrimSpace(string(output.Bytes())))
assert.Equal(t, "InstanceList resource created", strings.TrimSpace(string(output.Bytes())))
},
},
{
Name: "applying a Tenant file should print `Tenant resource created`",
Args: []string{"-f", "/tmp/localhost.tenant.yml"},
Mock: func() {
instances := &admin.Tenant{
Kind: "Tenant",
Spec: map[string]string{},
Instances: []string{},
}

out, err := yaml.Marshal(instances)
if err != nil {
t.Fatal(err)
}

if err := os.WriteFile("/tmp/localhost.tenant.yml", out, os.FileMode(0644)); err != nil {
t.Fatal(err)
}

mock.ApplyFunc = func(kind string, resource *interface{}) error {
return nil
}
},
Validator: func(t *testing.T, output *bytes.Buffer, err error) {
assert.Nil(t, err)
assert.Equal(t, "Tenant resource created", strings.TrimSpace(string(output.Bytes())))
},
},
}
Expand Down
11 changes: 10 additions & 1 deletion pkg/cmd/apply/manager.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package apply

import (
"errors"

"github.com/SLedunois/b3lb/v2/pkg/admin"
"gopkg.in/yaml.v3"
)
Expand All @@ -11,5 +13,12 @@ func toResource(in []byte) (string, interface{}, error) {
return "", nil, err
}

return "InstanceList", admin.InstanceList{}, nil
switch resource.Kind {
case "InstanceList":
return "InstanceList", admin.InstanceList{}, nil
case "Tenant":
return "Tenant", admin.Tenant{}, nil
default:
return "", nil, errors.New("unknown resource kind")
}
}
40 changes: 40 additions & 0 deletions pkg/cmd/apply/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,46 @@ func TestToResource(t *testing.T) {
assert.Equal(t, "admin.InstanceList", reflect.ValueOf(resource).Type().String())
},
},
{
name: "passing a kind Tenant should return a Tenant struct",
mock: func() {
tenant := &admin.Tenant{
Kind: "Tenant",
Instances: []string{},
Spec: map[string]string{},
}

out, err := yaml.Marshal(tenant)
if err != nil {
t.Fatal(err)
}

in = out
},
validator: func(t *testing.T, kind string, resource interface{}, err error) {
assert.Nil(t, err)
assert.Equal(t, "Tenant", kind)
assert.Equal(t, "admin.Tenant", reflect.ValueOf(resource).Type().String())
},
},
{
name: "passing an unknown kind should return an error",
mock: func() {
resource := &Resource{
Kind: "FakeKind",
}

out, err := yaml.Marshal(resource)
if err != nil {
t.Fatal(err)
}

in = out
},
validator: func(t *testing.T, kind string, resource interface{}, err error) {
assert.NotNil(t, err)
},
},
}

for _, test := range tests {
Expand Down

0 comments on commit ecd2a78

Please sign in to comment.