diff --git a/README.md b/README.md index b31c9946..c0e84628 100644 --- a/README.md +++ b/README.md @@ -947,6 +947,7 @@ The following parameters are supported. See Common Reference for a list of addit ## org +* [create](#createorg) * [list](#listorgs) * [get](#getorg) * [setmart](#setmart) diff --git a/cmd/org/createorg/createorg.go b/cmd/org/createorg/createorg.go new file mode 100644 index 00000000..7c96d28a --- /dev/null +++ b/cmd/org/createorg/createorg.go @@ -0,0 +1,71 @@ +package createorg + +import ( + "fmt" + "net/url" + "path" + "strings" + + "github.com/spf13/cobra" + "github.com/srinandan/apigeecli/cmd/shared" +) + +//Cmd to get org details +var Cmd = &cobra.Command{ + Use: "create", + Short: "Create a new Apigee Org", + Long: "Create a new Apigee Org; Your GCP project must be whitelist for this operation", + Args: func(cmd *cobra.Command, args []string) error { + if !validRegion(region) { + return fmt.Errorf("Invalid analytics region."+ + " Analytics region must be one of : %v", analyticsRegions) + } + return nil + }, + RunE: func(cmd *cobra.Command, args []string) (err error) { + u, _ := url.Parse(baseURL) + u.Path = path.Join(u.Path) + q := u.Query() + q.Set("parent", "projects/"+shared.RootArgs.ProjectID) + u.RawQuery = q.Encode() + + orgPayload := []string{} + orgPayload = append(orgPayload, "\"name\":\""+shared.RootArgs.Org+"\"") + orgPayload = append(orgPayload, "\"analyticsRegion\":\""+region+"\"") + + payload := "{" + strings.Join(orgPayload, ",") + "}" + _, err = shared.HttpClient(true, u.String(), payload) + return + }, +} + +var analyticsRegions = [...]string{"asia-east1", "asia-east1", "asia-northeast1", "asia-southeast1", + "europe-west1", "us-central1", "us-east1", "us-east4", "us-west1", "australia-southeast1", + "europe-west2"} + +func validRegion(region string) bool { + for _, r := range analyticsRegions { + if region == r { + return true + } + } + return false +} + +const baseURL = "https://apigee.googleapis.com/v1/organizations" + +var region string + +func init() { + + Cmd.Flags().StringVarP(&shared.RootArgs.Org, "org", "o", + "", "Apigee organization name") + Cmd.Flags().StringVarP(®ion, "reg", "r", + "", "Analytics region name") + Cmd.Flags().StringVarP(&shared.RootArgs.ProjectID, "prj", "p", + "", "GCP Project ID") + + _ = Cmd.MarkFlagRequired("prj") + _ = Cmd.MarkFlagRequired("org") + _ = Cmd.MarkFlagRequired("reg") +} diff --git a/cmd/org/org.go b/cmd/org/org.go index 64a52ee3..3fe7e4cd 100644 --- a/cmd/org/org.go +++ b/cmd/org/org.go @@ -2,6 +2,7 @@ package org import ( "github.com/spf13/cobra" + "github.com/srinandan/apigeecli/cmd/org/createorg" "github.com/srinandan/apigeecli/cmd/org/getorg" "github.com/srinandan/apigeecli/cmd/org/listorgs" "github.com/srinandan/apigeecli/cmd/org/setmart" @@ -15,7 +16,7 @@ var Cmd = &cobra.Command{ } func init() { - + Cmd.AddCommand(createorg.Cmd) Cmd.AddCommand(listorgs.Cmd) Cmd.AddCommand(getorg.Cmd) Cmd.AddCommand(setmart.Cmd)