-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
squashing all commits into one (#216)
- Loading branch information
1 parent
2bc199c
commit 4bbef3b
Showing
27 changed files
with
1,487 additions
and
296 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
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,76 @@ | ||
// Copyright © 2023 Dell Inc., or its subsidiaries. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"karavi-authorization/pb" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// NewTenantUpdateCmd creates a new update command for tenant | ||
func NewTenantUpdateCmd() *cobra.Command { | ||
tenantUpdateCmd := &cobra.Command{ | ||
Use: "update", | ||
TraverseChildren: true, | ||
Short: "Update a tenant resource within CSM Authorization", | ||
Long: `Updates a tenant resource within CSM Authorization`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
addr, err := cmd.Flags().GetString("addr") | ||
if err != nil { | ||
reportErrorAndExit(JSONOutput, cmd.ErrOrStderr(), err) | ||
} | ||
|
||
insecure, err := cmd.Flags().GetBool("insecure") | ||
if err != nil { | ||
reportErrorAndExit(JSONOutput, cmd.ErrOrStderr(), err) | ||
} | ||
|
||
tenantClient, conn, err := CreateTenantServiceClient(addr, insecure) | ||
if err != nil { | ||
reportErrorAndExit(JSONOutput, cmd.ErrOrStderr(), err) | ||
} | ||
defer conn.Close() | ||
|
||
name, err := cmd.Flags().GetString("name") | ||
if err != nil { | ||
reportErrorAndExit(JSONOutput, cmd.ErrOrStderr(), err) | ||
} | ||
if strings.TrimSpace(name) == "" { | ||
reportErrorAndExit(JSONOutput, cmd.ErrOrStderr(), errors.New("empty name not allowed")) | ||
} | ||
|
||
approveSdc, err := cmd.Flags().GetBool("approvesdc") | ||
if err != nil { | ||
reportErrorAndExit(JSONOutput, cmd.ErrOrStderr(), err) | ||
} | ||
|
||
_, err = tenantClient.UpdateTenant(context.Background(), &pb.UpdateTenantRequest{ | ||
TenantName: name, | ||
Approvesdc: approveSdc, | ||
}) | ||
if err != nil { | ||
reportErrorAndExit(JSONOutput, cmd.ErrOrStderr(), err) | ||
} | ||
}, | ||
} | ||
|
||
tenantUpdateCmd.Flags().StringP("name", "n", "", "Tenant name") | ||
tenantUpdateCmd.Flags().Bool("approvesdc", true, "To allow/deny SDC approval requests") | ||
return tenantUpdateCmd | ||
} |
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,165 @@ | ||
// Copyright © 2023 Dell Inc., or its subsidiaries. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
"io/ioutil" | ||
"karavi-authorization/pb" | ||
"os" | ||
"testing" | ||
|
||
"google.golang.org/grpc" | ||
) | ||
|
||
func TestTenantUpdate(t *testing.T) { | ||
afterFn := func() { | ||
CreateTenantServiceClient = createTenantServiceClient | ||
JSONOutput = jsonOutput | ||
osExit = os.Exit | ||
} | ||
|
||
t.Run("it requests updation of a tenant", func(t *testing.T) { | ||
defer afterFn() | ||
CreateTenantServiceClient = func(_ string, _ bool) (pb.TenantServiceClient, io.Closer, error) { | ||
return &fakeTenantServiceClient{}, ioutil.NopCloser(nil), nil | ||
} | ||
JSONOutput = func(w io.Writer, _ interface{}) error { | ||
return nil | ||
} | ||
osExit = func(code int) { | ||
} | ||
var gotOutput bytes.Buffer | ||
|
||
cmd := NewRootCmd() | ||
cmd.SetOutput(&gotOutput) | ||
cmd.SetArgs([]string{"tenant", "update", "-n", "testname", "--approvesdc", "true"}) | ||
cmd.Execute() | ||
|
||
if len(gotOutput.Bytes()) != 0 { | ||
t.Errorf("expected zero output but got %q", string(gotOutput.Bytes())) | ||
} | ||
}) | ||
t.Run("it requires a valid tenant server connection", func(t *testing.T) { | ||
defer afterFn() | ||
CreateTenantServiceClient = func(_ string, _ bool) (pb.TenantServiceClient, io.Closer, error) { | ||
return nil, ioutil.NopCloser(nil), errors.New("test error") | ||
} | ||
var gotCode int | ||
done := make(chan struct{}) | ||
osExit = func(code int) { | ||
gotCode = code | ||
done <- struct{}{} | ||
done <- struct{}{} // we can't let this function return | ||
} | ||
var gotOutput bytes.Buffer | ||
|
||
cmd := NewRootCmd() | ||
cmd.SetErr(&gotOutput) | ||
cmd.SetArgs([]string{"tenant", "update", "-n", "testname", "--approvesdc", "true"}) | ||
go cmd.Execute() | ||
<-done | ||
|
||
wantCode := 1 | ||
if gotCode != wantCode { | ||
t.Errorf("got exit code %d, want %d", gotCode, wantCode) | ||
} | ||
var gotErr CommandError | ||
if err := json.NewDecoder(&gotOutput).Decode(&gotErr); err != nil { | ||
t.Fatal(err) | ||
} | ||
wantErrMsg := "test error" | ||
if gotErr.ErrorMsg != wantErrMsg { | ||
t.Errorf("got err %q, want %q", gotErr.ErrorMsg, wantErrMsg) | ||
} | ||
}) | ||
t.Run("it requires a valid name argument", func(t *testing.T) { | ||
defer afterFn() | ||
CreateTenantServiceClient = func(_ string, _ bool) (pb.TenantServiceClient, io.Closer, error) { | ||
return &fakeTenantServiceClient{}, ioutil.NopCloser(nil), nil | ||
} | ||
var gotCode int | ||
done := make(chan struct{}) | ||
osExit = func(code int) { | ||
gotCode = code | ||
done <- struct{}{} | ||
done <- struct{}{} // we can't let this function return | ||
} | ||
|
||
var gotOutput bytes.Buffer | ||
|
||
rootCmd := NewRootCmd() | ||
rootCmd.SetErr(&gotOutput) | ||
rootCmd.SetArgs([]string{"tenant", "update"}) | ||
|
||
go rootCmd.Execute() | ||
<-done | ||
|
||
wantCode := 1 | ||
if gotCode != wantCode { | ||
t.Errorf("got exit code %d, want %d", gotCode, wantCode) | ||
} | ||
var gotErr CommandError | ||
if err := json.NewDecoder(&gotOutput).Decode(&gotErr); err != nil { | ||
t.Fatal(err) | ||
} | ||
wantErrMsg := "empty name not allowed" | ||
if gotErr.ErrorMsg != wantErrMsg { | ||
t.Errorf("got err %q, want %q", gotErr.ErrorMsg, wantErrMsg) | ||
} | ||
}) | ||
t.Run("it handles server errors", func(t *testing.T) { | ||
defer afterFn() | ||
CreateTenantServiceClient = func(_ string, _ bool) (pb.TenantServiceClient, io.Closer, error) { | ||
return &fakeTenantServiceClient{ | ||
UpdateTenantFn: func(_ context.Context, _ *pb.UpdateTenantRequest, _ ...grpc.CallOption) (*pb.Tenant, error) { | ||
return nil, errors.New("test error") | ||
}, | ||
}, ioutil.NopCloser(nil), nil | ||
} | ||
var gotCode int | ||
done := make(chan struct{}) | ||
osExit = func(code int) { | ||
gotCode = code | ||
done <- struct{}{} | ||
done <- struct{}{} // we can't let this function return | ||
} | ||
var gotOutput bytes.Buffer | ||
|
||
rootCmd := NewRootCmd() | ||
rootCmd.SetErr(&gotOutput) | ||
rootCmd.SetArgs([]string{"tenant", "update", "-n", "test", "--approvesdc", "true"}) | ||
|
||
go rootCmd.Execute() | ||
<-done | ||
|
||
wantCode := 1 | ||
if gotCode != wantCode { | ||
t.Errorf("got exit code %d, want %d", gotCode, wantCode) | ||
} | ||
var gotErr CommandError | ||
if err := json.NewDecoder(&gotOutput).Decode(&gotErr); err != nil { | ||
t.Fatal(err) | ||
} | ||
wantErrMsg := "test error" | ||
if gotErr.ErrorMsg != wantErrMsg { | ||
t.Errorf("got err %q, want %q", gotErr.ErrorMsg, wantErrMsg) | ||
} | ||
}) | ||
} |
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
Oops, something went wrong.