diff --git a/cmd/iamctl/const.go b/cmd/iamctl/const.go index 6d5589ed..a5d815c7 100644 --- a/cmd/iamctl/const.go +++ b/cmd/iamctl/const.go @@ -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 }} diff --git a/cmd/iamctl/main.go b/cmd/iamctl/main.go index 6e358b88..de146cae 100644 --- a/cmd/iamctl/main.go +++ b/cmd/iamctl/main.go @@ -6,6 +6,10 @@ import ( "github.com/spf13/cobra" ) +const ( + defaultFunction = "GetIAMPolicy" +) + var ( // input file specifies the location for the input json. inputFile string @@ -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 @@ -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) }, } @@ -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.") diff --git a/cmd/iamctl/policy.go b/cmd/iamctl/policy.go index 00eddcb4..03ebc275 100644 --- a/cmd/iamctl/policy.go +++ b/cmd/iamctl/policy.go @@ -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 { @@ -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) }