Skip to content

Commit

Permalink
iamctl: function flag
Browse files Browse the repository at this point in the history
  • Loading branch information
alebedev87 committed Oct 17, 2023
1 parent 6a7fb19 commit 3d0e252
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
4 changes: 3 additions & 1 deletion cmd/iamctl/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ package {{.Package}}
import cco "github.com/openshift/cloud-credential-operator/pkg/apis/cloudcredential/v1"
{{- with .Definition }}
type IAMPolicy struct {
Version string
Statement []cco.StatementEntry
}
{{- end }}
func GetIAMPolicy() IAMPolicy {
func {{ .Function }}() IAMPolicy {
return IAMPolicy{
Statement: []cco.StatementEntry{
{{- range .Statement }}
Expand Down
13 changes: 11 additions & 2 deletions cmd/iamctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
"github.com/spf13/cobra"
)

const (
defaultFunction = "GetIAMPolicy"
)

var (
// input file specifies the location for the input json.
inputFile string
Expand All @@ -19,6 +23,9 @@ var (
// pkg specifies the package with which the code is generated.
pkg string

// function specifies the function name with which the code is generated.
function string

// skipMinify specifies whether the minification of the AWS policy has to be skipped.
skipMinify bool

Expand All @@ -35,7 +42,7 @@ var rootCmd = &cobra.Command{
Also it can produce a CredentialsRequest YAML file which can provision the secret for the controller.
`,
Run: func(cmd *cobra.Command, args []string) {
generateIAMPolicy(inputFile, outputFile, outputCRFile, pkg)
generateIAMPolicy(inputFile, outputFile, outputCRFile, pkg, function)
},
}

Expand All @@ -59,9 +66,11 @@ func init() {

rootCmd.PersistentFlags().StringVarP(&outputCRFile, "output-cr-file", "c", "", "Used to specify output CredentialsRequest YAML file path.")

rootCmd.PersistentFlags().StringVarP(&pkg, "package", "p", "main", "Used to specify output Go file path.")
rootCmd.PersistentFlags().StringVarP(&pkg, "package", "p", "main", "Used to specify the Go package in the output file.")
_ = rootCmd.MarkPersistentFlagRequired("package")

rootCmd.PersistentFlags().StringVarP(&function, "function", "f", defaultFunction, "Used to specify the Go function name in the output file.")

rootCmd.PersistentFlags().BoolVarP(&skipMinify, "no-minify", "n", false, "Used to skip the minification of the output AWS policy.")

rootCmd.PersistentFlags().BoolVarP(&splitResource, "split-resource", "s", false, "Used to split AWS policy's statement into many with one resource per statement.")
Expand Down
21 changes: 14 additions & 7 deletions cmd/iamctl/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ var compressionPrefixes = map[string]string{
"elasticloadbalancing:Describe": "elasticloadbalancing:Describe*",
}

func generateIAMPolicy(inputFile, output, outputCR, pkg string) {
generateIAMPolicyFromTemplate(filetemplate, inputFile, output, pkg)
func generateIAMPolicy(inputFile, output, outputCR, pkg, function string) {
generateIAMPolicyFromTemplate(filetemplate, inputFile, output, pkg, function)
if outputCR != "" {
generateIAMPolicyFromTemplate(credentialsRequestTemplate, inputFile, outputCR, pkg)
generateIAMPolicyFromTemplate(credentialsRequestTemplate, inputFile, outputCR, pkg, function)
}
}

func generateIAMPolicyFromTemplate(filetemplate string, inputFile, output, pkg string) {
func generateIAMPolicyFromTemplate(filetemplate string, inputFile, output, pkg, function string) {
funcMap := template.FuncMap{
"stringOrSlice": func(value interface{}, yaml bool) string {
if values, slice := value.([]interface{}); slice {
Expand Down Expand Up @@ -117,9 +117,16 @@ func generateIAMPolicyFromTemplate(filetemplate string, inputFile, output, pkg s

var in bytes.Buffer
err = tmpl.Execute(&in, struct {
Package string
Statement []policyStatement
}{Package: pkg, Statement: policy.Statement})
Package string
Function string
Definition bool
Statement []policyStatement
}{
Package: pkg,
Function: function,
Definition: function == defaultFunction,
Statement: policy.Statement,
})
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 3d0e252

Please sign in to comment.