Skip to content

Commit

Permalink
feat: adds wait option to multiple cmds #518 (#519)
Browse files Browse the repository at this point in the history
* feat: adds wait option to multiple cmds #518

* feat: adds wait option to multiple cmds #518

* bug: fixes state field #518

* bug: fixes err check #518

* feat: wait for operations for all lro #518

* feat: wait for operations for all lro #518

* feat: wait for operations for all lro #518
  • Loading branch information
srinandan authored Aug 8, 2024
1 parent ac030f9 commit 5ecb2db
Show file tree
Hide file tree
Showing 18 changed files with 300 additions and 20 deletions.
8 changes: 8 additions & 0 deletions internal/client/envgroups/envgroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ func ListAttach(name string) (respBody []byte, err error) {
return respBody, err
}

// GetAttach
func GetAttach(name string, attachment string) (respBody []byte, err error) {
u, _ := url.Parse(apiclient.GetApigeeBaseURL())
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "envgroups", name, "attachments", attachment)
respBody, err = apiclient.HttpClient(u.String())
return respBody, err
}

func getArrayStr(str []string) string {
tmp := strings.Join(str, ",")
tmp = strings.ReplaceAll(tmp, ",", "\",\"")
Expand Down
2 changes: 2 additions & 0 deletions internal/client/instances/attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"internal/apiclient"
)

const interval = 10

// Attach
func Attach(name string, environment string) (respBody []byte, err error) {
envgroup := []string{}
Expand Down
44 changes: 44 additions & 0 deletions internal/client/operations/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (
"encoding/json"
"net/url"
"path"
"time"

"internal/apiclient"
"internal/clilog"
)

type ops struct {
Expand Down Expand Up @@ -59,6 +61,8 @@ const (
Both OperationCompleteState = "Both"
)

const interval = 10

// Get
func Get(name string) (respBody []byte, err error) {
u, _ := url.Parse(apiclient.GetApigeeBaseURL())
Expand Down Expand Up @@ -116,3 +120,43 @@ func filterOperation(respBody []byte, state string, completeState OperationCompl
}
return operationsRespBody, nil
}

// WaitForOperation
func WaitForOperation(operationName string) error {
var err error

clilog.Info.Printf("Checking operation status in %d seconds\n", interval)

apiclient.DisableCmdPrintHttpResponse()

stop := apiclient.Every(interval*time.Second, func(time.Time) bool {
var respBody []byte
respMap := make(map[string]interface{})
if respBody, err = Get(operationName); err != nil {
clilog.Error.Printf("Error fetching operation status: %v", err)
return false
}

if err = json.Unmarshal(respBody, &respMap); err != nil {
return true
}

if respMap["done"] == true {
if respMap["error"] != nil {
clilog.Info.Printf("Operation failed with status: %v\n", respMap["error"])
}
clilog.Info.Println("Operation completed")
} else {
metadata, _ := respMap["metadata"].(map[string]interface{})
state, _ := metadata["state"].(string)
clilog.Info.Printf("Operation status is: %s. Waiting %d seconds.\n", state, interval)
return true
}

return false
})

<-stop

return err
}
18 changes: 16 additions & 2 deletions internal/cmd/env/crtenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
package env

import (
"encoding/json"
"fmt"
"net/url"
"path/filepath"

"internal/apiclient"

"internal/client/env"
"internal/client/operations"

"github.com/spf13/cobra"
)
Expand All @@ -42,7 +45,17 @@ var CreateCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) (err error) {
cmd.SilenceUsage = true

_, err = env.Create(envType, deploymentType, fwdProxyURI)
respBody, err := env.Create(envType, deploymentType, fwdProxyURI)
if err != nil {
return
}
if wait {
respMap := make(map[string]interface{})
if err = json.Unmarshal(respBody, &respMap); err != nil {
return err
}
err = operations.WaitForOperation(filepath.Base(respMap["name"].(string)))
}
return
},
}
Expand All @@ -58,6 +71,7 @@ func init() {
"", "Deployment type - must be PROXY or ARCHIVE")
CreateCmd.Flags().StringVarP(&fwdProxyURI, "fwdproxyuri", "f",
"", "URL of the forward proxy to be applied to the runtime instances in this env")

CreateCmd.Flags().BoolVarP(&wait, "wait", "",
false, "Waits for the create to finish, with success or error")
_ = CreateCmd.MarkFlagRequired("env")
}
17 changes: 16 additions & 1 deletion internal/cmd/env/delenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
package env

import (
"encoding/json"
"internal/apiclient"
"path/filepath"

"internal/client/env"
"internal/client/operations"

"github.com/spf13/cobra"
)
Expand All @@ -35,14 +38,26 @@ var DelCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) (err error) {
cmd.SilenceUsage = true

_, err = env.Delete()
respBody, err := env.Delete()
if err != nil {
return
}
if wait {
respMap := make(map[string]interface{})
if err = json.Unmarshal(respBody, &respMap); err != nil {
return err
}
err = operations.WaitForOperation(filepath.Base(respMap["name"].(string)))
}
return
},
}

func init() {
DelCmd.Flags().StringVarP(&environment, "env", "e",
"", "Apigee environment name")
DelCmd.Flags().BoolVarP(&wait, "wait", "",
false, "Waits for the deletion to finish, with success or error")

_ = DelCmd.MarkFlagRequired("env")
}
19 changes: 16 additions & 3 deletions internal/cmd/envgroup/attachenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
package envgroup

import (
"encoding/json"
"internal/apiclient"
"path/filepath"

"internal/client/envgroups"
"internal/client/operations"

"github.com/spf13/cobra"
)
Expand All @@ -34,20 +37,30 @@ var AttachCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) (err error) {
cmd.SilenceUsage = true

_, err = envgroups.Attach(name, environment)
respBody, err := envgroups.Attach(name, environment)
if err != nil {
return
}
if wait {
respMap := make(map[string]interface{})
if err = json.Unmarshal(respBody, &respMap); err != nil {
return err
}
err = operations.WaitForOperation(filepath.Base(respMap["name"].(string)))
}
return
},
}

func init() {
AttachCmd.Flags().StringVarP(&org, "org", "o",
"", "Apigee organization name")

AttachCmd.Flags().StringVarP(&name, "name", "n",
"", "Name of the environment group")

AttachCmd.Flags().StringVarP(&environment, "env", "e",
"", "Name of the environment")
AttachCmd.Flags().BoolVarP(&wait, "wait", "",
false, "Waits for the attachment to finish, with success or error")

_ = AttachCmd.MarkFlagRequired("name")
_ = AttachCmd.MarkFlagRequired("env")
Expand Down
19 changes: 18 additions & 1 deletion internal/cmd/envgroup/crtenvgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
package envgroup

import (
"encoding/json"
"internal/apiclient"
"path/filepath"

"internal/client/envgroups"
"internal/client/operations"

"github.com/spf13/cobra"
)
Expand All @@ -34,16 +37,30 @@ var CreateCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) (err error) {
cmd.SilenceUsage = true

_, err = envgroups.Create(name, hostnames)
respBody, err := envgroups.Create(name, hostnames)
if err != nil {
return
}
if wait {
respMap := make(map[string]interface{})
if err = json.Unmarshal(respBody, &respMap); err != nil {
return err
}
err = operations.WaitForOperation(filepath.Base(respMap["name"].(string)))
}
return
},
}

var wait bool

func init() {
CreateCmd.Flags().StringVarP(&name, "name", "n",
"", "Name of the Environment Group")
CreateCmd.Flags().StringArrayVarP(&hostnames, "hosts", "d",
[]string{}, "A list of hostnames")
CreateCmd.Flags().BoolVarP(&wait, "wait", "",
false, "Waits for the create to finish, with success or error")

_ = CreateCmd.MarkFlagRequired("name")
_ = CreateCmd.MarkFlagRequired("hosts")
Expand Down
15 changes: 14 additions & 1 deletion internal/cmd/envgroup/delenvgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
package envgroup

import (
"encoding/json"
"internal/apiclient"
"path/filepath"

"internal/client/envgroups"
"internal/client/operations"

"github.com/spf13/cobra"
)
Expand All @@ -34,7 +37,17 @@ var DelCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) (err error) {
cmd.SilenceUsage = true

_, err = envgroups.Delete(name)
respBody, err := envgroups.Delete(name)
if err != nil {
return
}
if wait {
respMap := make(map[string]interface{})
if err = json.Unmarshal(respBody, &respMap); err != nil {
return err
}
err = operations.WaitForOperation(filepath.Base(respMap["name"].(string)))
}
return
},
}
Expand Down
18 changes: 17 additions & 1 deletion internal/cmd/eptattachment/crteptattachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
package eptattachment

import (
"encoding/json"
"fmt"
"path/filepath"
"regexp"

"internal/apiclient"

"internal/client/eptattachment"
"internal/client/operations"

"github.com/spf13/cobra"
)
Expand All @@ -44,12 +47,23 @@ var CreateCmd = &cobra.Command{
return fmt.Errorf("disk encryption key must be of the format " +
"projects/{project-id}/regions/{location}/serviceAttachments/{sa-name}")
}
_, err = eptattachment.Create(name, serviceAttachment, location)
respBody, err := eptattachment.Create(name, serviceAttachment, location)
if err != nil {
return
}
if wait {
respMap := make(map[string]interface{})
if err = json.Unmarshal(respBody, &respMap); err != nil {
return err
}
err = operations.WaitForOperation(filepath.Base(respMap["name"].(string)))
}
return err
},
}

var location, serviceAttachment string
var wait bool

func init() {
CreateCmd.Flags().StringVarP(&name, "name", "n",
Expand All @@ -58,6 +72,8 @@ func init() {
"", "Location of the service endpoint")
CreateCmd.Flags().StringVarP(&serviceAttachment, "service-attachment", "s",
"", "Service attachment url: projects/{project-id}/regions/{location}/serviceAttachments/{sa-name}")
CreateCmd.Flags().BoolVarP(&wait, "wait", "",
false, "Waits for the create to finish, with success or error")

_ = CreateCmd.MarkFlagRequired("name")
_ = CreateCmd.MarkFlagRequired("service-attachment")
Expand Down
17 changes: 16 additions & 1 deletion internal/cmd/eptattachment/deleptattachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
package eptattachment

import (
"encoding/json"
"internal/apiclient"
"path/filepath"

"internal/client/eptattachment"
"internal/client/operations"

"github.com/spf13/cobra"
)
Expand All @@ -34,14 +37,26 @@ var RemoveCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) (err error) {
cmd.SilenceUsage = true

_, err = eptattachment.Delete(name)
respBody, err := eptattachment.Delete(name)
if err != nil {
return
}
if wait {
respMap := make(map[string]interface{})
if err = json.Unmarshal(respBody, &respMap); err != nil {
return err
}
err = operations.WaitForOperation(filepath.Base(respMap["name"].(string)))
}
return
},
}

func init() {
RemoveCmd.Flags().StringVarP(&name, "name", "n",
"", "Name of the service endpoint")
RemoveCmd.Flags().BoolVarP(&wait, "wait", "",
false, "Waits for the delete to finish, with success or error")

_ = RemoveCmd.MarkFlagRequired("name")
}
Loading

0 comments on commit 5ecb2db

Please sign in to comment.