|
| 1 | +/* |
| 2 | +Copyright © 2020 Portworx |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package guestaccess |
| 17 | + |
| 18 | +import ( |
| 19 | + "github.com/portworx/pxc/pkg/cliops" |
| 20 | + "github.com/portworx/pxc/pkg/commander" |
| 21 | + "github.com/portworx/pxc/pkg/portworx" |
| 22 | + "github.com/portworx/pxc/pkg/util" |
| 23 | + "github.com/spf13/cobra" |
| 24 | +) |
| 25 | + |
| 26 | +var showGuestAccessCmd *cobra.Command |
| 27 | + |
| 28 | +var _ = commander.RegisterCommandVar(func() { |
| 29 | + showGuestAccessCmd = &cobra.Command{ |
| 30 | + Use: "show", |
| 31 | + Short: "show guest access role", |
| 32 | + RunE: showGuestAccessExec, |
| 33 | + } |
| 34 | +}) |
| 35 | + |
| 36 | +var _ = commander.RegisterCommandInit(func() { |
| 37 | + GuestAccessAddCommand(showGuestAccessCmd) |
| 38 | + |
| 39 | + showGuestAccessCmd.Flags().StringP("output", "o", "", "Output in yaml|json|wide") |
| 40 | +}) |
| 41 | + |
| 42 | +func showGuestAccessExec(cmd *cobra.Command, args []string) error { |
| 43 | + ctx, conn, err := portworx.PxConnectDefault() |
| 44 | + _ = ctx |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + defer conn.Close() |
| 49 | + // Parse out all of the common cli volume flags |
| 50 | + cai := cliops.GetCliAuthInputs(cmd, args) |
| 51 | + |
| 52 | + // Create a cliVolumeOps object |
| 53 | + authOps := cliops.NewCliAuthOps(cai) |
| 54 | + |
| 55 | + // initialize alertOP interface |
| 56 | + authOps.AuthOps = portworx.NewAuthOps() |
| 57 | + |
| 58 | + // Create the parser object |
| 59 | + authgf := NewGuestAccessShowFormatter(authOps) |
| 60 | + return util.PrintFormatted(authgf) |
| 61 | +} |
| 62 | + |
| 63 | +type guestAccessShowFormatter struct { |
| 64 | + cliops.CliAuthOps |
| 65 | +} |
| 66 | + |
| 67 | +// NewGuestAccessShowFormatter creates a new guest access formatter |
| 68 | +func NewGuestAccessShowFormatter(cvOps *cliops.CliAuthOps) *guestAccessShowFormatter { |
| 69 | + return &guestAccessShowFormatter{ |
| 70 | + CliAuthOps: *cvOps, |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// YamlFormat returns the yaml representation of the object |
| 75 | +func (f *guestAccessShowFormatter) YamlFormat() (string, error) { |
| 76 | + role, err := f.AuthOps.GetRole("system.guest") |
| 77 | + if err != nil { |
| 78 | + return "", err |
| 79 | + } |
| 80 | + return util.ToYaml(role) |
| 81 | +} |
| 82 | + |
| 83 | +// JsonFormat returns the json representation of the object |
| 84 | +func (f *guestAccessShowFormatter) JsonFormat() (string, error) { |
| 85 | + role, err := f.AuthOps.GetRole("system.guest") |
| 86 | + if err != nil { |
| 87 | + return "", err |
| 88 | + } |
| 89 | + return util.ToJson(role) |
| 90 | +} |
| 91 | + |
| 92 | +// WideFormat returns the wide string representation of the object |
| 93 | +func (f *guestAccessShowFormatter) WideFormat() (string, error) { |
| 94 | + f.Wide = true |
| 95 | + return f.DefaultFormat() |
| 96 | +} |
| 97 | + |
| 98 | +// DefaultFormat returns the default string representation of the object |
| 99 | +func (f *guestAccessShowFormatter) DefaultFormat() (string, error) { |
| 100 | + role, err := f.AuthOps.GetRole("system.guest") |
| 101 | + if err != nil { |
| 102 | + return "", err |
| 103 | + } |
| 104 | + |
| 105 | + if role.String() == portworx.RoleGuestDisabled.String() { |
| 106 | + util.Printf("Guest access disabled\n") |
| 107 | + |
| 108 | + } else { |
| 109 | + util.Printf("Guest access enabled\n") |
| 110 | + } |
| 111 | + |
| 112 | + return "", nil |
| 113 | +} |
0 commit comments