Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions cmd/common-input.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"strconv"
Expand Down Expand Up @@ -91,6 +92,10 @@ func initInputFlags(cmd *cobra.Command, typeName string, fields ...inputField) {
cmd.Flags().IntP(field.FlagName, field.FlagShorthand, field.DefaultValue.(int), field.FlagDescription)
case "string":
cmd.Flags().StringP(field.FlagName, field.FlagShorthand, field.DefaultValue.(string), field.FlagDescription)
case "string+file":
cmd.Flags().StringP(field.FlagName, field.FlagShorthand, field.DefaultValue.(string), field.FlagDescription)
// special case to read input from file, appends -file to argument
cmd.Flags().StringP(wrapFlagForFile(field.FlagName), field.FlagShorthand, field.DefaultValue.(string), field.FlagDescription+" (from file path)")
case "[]int":
// see https://github.com/spf13/pflag/issues/222
// cmd.Flags().IntSliceP(field.FlagName, field.FlagShorthand, field.DefaultValue.([]int), field.FlagDescription)
Expand Down Expand Up @@ -144,19 +149,46 @@ func preRunFlagCheckInput(cmd *cobra.Command, args []string) error {
return nil
}

func getFlagValue(cmd *cobra.Command, varType, flagName string) (interface{}, error) {
if !cmd.Flags().Changed(flagName) {
return nil, fmt.Errorf("user did not specify %s", flagName)
func wrapFlagForFile(flagName string) string {
return flagName + "-file"
}

func getFlagValueChanged(cmd *cobra.Command, varType, flagName string) bool {
changed := cmd.Flags().Changed(flagName)
if !changed && varType == "string+file" {
changed = cmd.Flags().Changed(wrapFlagForFile(flagName))
}
var value interface{}
return changed
}

func getFlagValue(cmd *cobra.Command, varType, flagName string) (interface{}, error) {
var err error
var value interface{}
switch varType {
case "bool":
value, err = cmd.Flags().GetBool(flagName)
case "int":
value, err = cmd.Flags().GetInt(flagName)
case "string":
value, err = cmd.Flags().GetString(flagName)
case "string+file":
// If string value was passed, return it, otherwise, attempt to read from file
value, err = cmd.Flags().GetString(flagName)
if err != nil {
return nil, err
}
if value != "" {
return value, nil
}
filename, err := cmd.Flags().GetString(wrapFlagForFile(flagName))
if err != nil {
return nil, err
}
contents, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
value = string(contents)
case "[]int":
// see https://github.com/spf13/pflag/issues/222
// we accepted a string slice instead, and will now convert to a int slice
Expand Down Expand Up @@ -219,7 +251,8 @@ func forAllInput(cmd *cobra.Command,
for i, field := range data.fields {
var d interface{}

if !cmd.Flags().Changed(field.FlagName) && field.Mandatory {
flagChanged := getFlagValueChanged(cmd, field.VarType, field.FlagName)
if !flagChanged && field.Mandatory {
// user did not supply the field value in a flag
fieldInArgs := false
if field.MainField {
Expand All @@ -230,7 +263,7 @@ func forAllInput(cmd *cobra.Command,
// must ask interactively
d = interactivelyReadField(cmd, field)
}
} else if !cmd.Flags().Changed(field.FlagName) && !writeDefaultValues {
} else if !flagChanged && !writeDefaultValues {
// in placeInputValues, we only call the respective function for this field
// if the value for the entry is not nil. so, we just leave it nil, to indicate this value is not provided
continue
Expand Down
6 changes: 6 additions & 0 deletions cmd/root-settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ var settingsAnalyticsCmd = &cobra.Command{
Short: "Operations on analytics",
}

var settingsIdentityProviderConfigCmd = &cobra.Command{
Use: "idp",
Short: "Configure Identity Provider settings",
}

func init() {
rootCmd.AddCommand(settingsCmd)

settingsCmd.AddCommand(settingsAgentConfigCmd)
settingsCmd.AddCommand(settingsEnrollmentCmd)
settingsCmd.AddCommand(settingsAnalyticsCmd)
settingsCmd.AddCommand(settingsIdentityProviderConfigCmd)

// Here you will define your flags and configuration settings.

Expand Down
105 changes: 105 additions & 0 deletions cmd/settings-idp-delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Package cmd implements access-cli commands
package cmd

/*
Copyright © 2020 Barracuda Networks, Inc.

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.
*/

import (
"fmt"

api "github.com/barracuda-cloudgen-access/access-cli/client/identity_providers"

"github.com/spf13/cobra"
)

// deleteIdpCmd represents the get command
var deleteIdpCmd = &cobra.Command{
Use: "delete [idp ID]...",
Aliases: []string{"remove", "rm"},
Short: "Delete idps",
PreRunE: func(cmd *cobra.Command, args []string) error {
err := preRunCheckAuth(cmd, args)
if err != nil {
return err
}

err = preRunFlagChecks(cmd, args)
if err != nil {
return err
}

if !multiOpCheckArgsPresent(cmd, args) {
return fmt.Errorf("missing idp ID argument")
}

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
adminIDs, err := multiOpParseInt64Args(cmd, args, "id")
if err != nil {
return err
}

delete := func(ids []int64) error {
params := api.NewDeleteIdentityProviderParams()
setTenant(cmd, params)
params.SetID(ids)

_, err = global.Client.IdentityProviders.DeleteIdentityProvider(params, global.AuthWriter)
if err != nil {
return processErrorResponse(err)
}
return nil
}

tw, j := multiOpBuildTableWriter()

if loopControlContinueOnError(cmd) {
// then we must delete individually, because on a request for multiple deletions,
// the server does nothing if one fails

for _, id := range adminIDs {
err = delete([]int64{id})
var result interface{}
result = "success"
if err != nil {
result = err
}
multiOpTableWriterAppend(tw, &j, id, result)
}
err = nil
} else {
err = delete(adminIDs)
var result interface{}
result = "success"
if err != nil {
result = err
}
multiOpTableWriterAppend(tw, &j, "*", result)
}

return printListOutputAndError(cmd, j, tw, len(adminIDs), err)
},
}

func init() {
settingsIdentityProviderConfigCmd.AddCommand(deleteIdpCmd)

initMultiOpArgFlags(deleteIdpCmd, "idp", "delete", "id", "[]int64")
initOutputFlags(deleteIdpCmd)
initLoopControlFlags(deleteIdpCmd)
initTenantFlags(deleteIdpCmd)
}
139 changes: 139 additions & 0 deletions cmd/settings-idp-get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// Package cmd implements access-cli commands
package cmd

/*
Copyright © 2020 Barracuda Networks, Inc.

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.
*/

import (
"fmt"
"strconv"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/spf13/cobra"

api "github.com/barracuda-cloudgen-access/access-cli/client/identity_providers"
"github.com/barracuda-cloudgen-access/access-cli/models"
)

// getIdentityProviderCmd represents the get command
var getIdentityProviderCmd = &cobra.Command{
Use: "get [idp ID]",
Short: "Get IdP configuration",
PreRunE: func(cmd *cobra.Command, args []string) error {
err := preRunCheckAuth(cmd, args)
if err != nil {
return err
}

err = preRunFlagChecks(cmd, args)
if err != nil {
return err
}

if len(args) == 0 && !cmd.Flags().Changed("id") {
return fmt.Errorf("missing user ID argument")
}

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var id int64
var err error
if cmd.Flags().Changed("id") {
var d int
d, err = cmd.Flags().GetInt("id")
id = int64(d)
} else {
id, err = strconv.ParseInt(args[0], 10, 64)
}
if err != nil {
return err
}

cmd.SilenceUsage = true // errors beyond this point are no longer due to malformed input

params := api.NewGetIdentityProviderParams()
setTenant(cmd, params)
params.SetID(id)

resp, err := global.Client.IdentityProviders.GetIdentityProvider(params, global.AuthWriter)
if err != nil {
return processErrorResponse(err)
}

tw := identityProviderConfigBuildTableWriter()
if resp.Payload.IdentityProvider.ID > 0 {
identityProviderTableWriterAppend(tw, resp.Payload.IdentityProvider)
}

return printListOutputAndError(cmd, resp.Payload, tw, 1, err)
},
}

func identityProviderConfigBuildTableWriter() table.Writer {
tw := table.NewWriter()
tw.Style().Format.Header = text.FormatDefault
tw.AppendHeader(table.Row{
"ID",
"Type",
"Name",
"CreatedAt",
"UpdatedAt",
})

return tw
}

func identityProviderTableWriterAppend(tw table.Writer, idp models.IdentityProvider) table.Writer {
tw.AppendRow(table.Row{
idp.ID,
idp.IdpType,
idp.Name,
idp.CreatedAt,
idp.UpdatedAt,
})
return tw
}

func identityProviderTableWriterAppendError(tw table.Writer, err error, id interface{}) {
tw.AppendRow(table.Row{
"[ERR]",
processErrorResponse(err),
"-",
"-",
"-",
})
}

func init() {
settingsIdentityProviderConfigCmd.AddCommand(getIdentityProviderCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// getIdentityProviderCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// getIdentityProviderCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

initOutputFlags(getIdentityProviderCmd)
initTenantFlags(getIdentityProviderCmd)

getIdentityProviderCmd.Flags().Int("id", 0, "id of user to get")
}
Loading