Skip to content
This repository has been archived by the owner on May 6, 2022. It is now read-only.

Commit

Permalink
support detach env via name
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan committed Feb 13, 2021
1 parent 2a91698 commit 48dda66
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
53 changes: 53 additions & 0 deletions client/instances/attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package instances

import (
"encoding/json"
"fmt"
"net/url"
"path"
"strings"
Expand All @@ -35,6 +37,57 @@ func Attach(name string, environment string) (respBody []byte, err error) {
return respBody, err
}

//DetachEnv
func DetachEnv(instance string) (respBody []byte, err error) {

var attachmentName string

type instanceAttachment struct {
Name string `json:"name,omitempty"`
Environment string `json:"environment,omitempty"`
CreatedAt string `json:"createdAt,omitempty"`
}

type instanceAttachments struct {
Attachments []instanceAttachment `json:"attachments,omitempty"`
}

instAttach := instanceAttachments{}

u, _ := url.Parse(apiclient.BaseURL)

apiclient.SetPrintOutput(false)
listAttachments, err := ListAttach(instance)
if err != nil {
return nil, err
}
apiclient.SetPrintOutput(true)

err = json.Unmarshal(listAttachments, &instAttach)
if err != nil {
return nil, err
}

if len(instAttach.Attachments) < 1 {
return nil, fmt.Errorf("no environments attached to the instance")
}

for _, attachedEnv := range instAttach.Attachments {
if attachedEnv.Environment == apiclient.GetApigeeEnv() {
attachmentName = attachedEnv.Name
break
}
}

if attachmentName == "" {
return nil, fmt.Errorf("The environment %s, does not appear to be attached to the instance %s", apiclient.GetApigeeEnv(), instance)
}

u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "instances", instance, "attachments", attachmentName)
respBody, err = apiclient.HttpClient(apiclient.GetPrintOutput(), u.String(), "", "DELETE")
return respBody, err
}

//Detach
func Detach(name string, attachment string) (respBody []byte, err error) {
u, _ := url.Parse(apiclient.BaseURL)
Expand Down
2 changes: 1 addition & 1 deletion cmd/instances/delattachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var DeleteAttachCmd = &cobra.Command{
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
_, err = instances.Detach(name, environment)
_, err = instances.DetachEnv(name)
return
},
}
Expand Down

0 comments on commit 48dda66

Please sign in to comment.