Skip to content

Commit a30786a

Browse files
author
Tobias Fuhrimann
committed
Add listing of invitations per entity
1 parent f9e5889 commit a30786a

4 files changed

Lines changed: 207 additions & 0 deletions

File tree

billing_account_invitations.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"strings"
8+
9+
"code.cloudfoundry.org/cli/plugin"
10+
)
11+
12+
// BillingAccountInvitations retrieves all invitations for a billing account.
13+
func (p *AppCloudPlugin) BillingAccountInvitations(c plugin.CliConnection, billingAccountName string) error {
14+
username, err := c.Username()
15+
if err != nil {
16+
username = "you"
17+
}
18+
19+
fmt.Printf("Gettings invitations to billing account %s as %s...\n", cyanBold(billingAccountName), cyanBold(username))
20+
21+
ba, err := getBillingAccount(c, billingAccountName)
22+
if err != nil {
23+
return fmt.Errorf("Billing account %s not found", billingAccountName)
24+
}
25+
26+
url := fmt.Sprintf("/custom/accounts/%s/invitations", ba.Metadata.GUID)
27+
resLines, err := c.CliCommandWithoutTerminalOutput("curl", url)
28+
if err != nil {
29+
return fmt.Errorf("Couldn't get invitations for billing account %s", billingAccountName)
30+
}
31+
32+
resString := strings.Join(resLines, "")
33+
var res InvitationsResponse
34+
err = json.Unmarshal([]byte(resString), &res)
35+
if err != nil {
36+
return errors.New("Couldn't read JSON response from server")
37+
}
38+
39+
if res.ErrorCode != "" {
40+
return errors.New(res.Description)
41+
}
42+
43+
fmt.Println(greenBold("OK\n\n"))
44+
45+
if len(res.Resources) == 0 {
46+
fmt.Println("No invitations found")
47+
return nil
48+
}
49+
50+
fmt.Println(bold("Invitee roles status"))
51+
for _, inv := range res.Resources {
52+
fmt.Printf("%s %s %s\n", inv.Entity.Invitee, inv.Entity.Roles, inv.Entity.Status)
53+
}
54+
return nil
55+
}

main.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,27 @@ func (p *AppCloudPlugin) GetMetadata() plugin.PluginMetadata {
7575
},
7676

7777
// Send invitations
78+
{
79+
Name: "billing-account-invitations",
80+
HelpText: "List all invitations for a billing account",
81+
UsageDetails: plugin.Usage{
82+
Usage: "billing-account-invitations BILLING_ACCOUNT",
83+
},
84+
},
85+
{
86+
Name: "org-invitations",
87+
HelpText: "List all invitations for an org",
88+
UsageDetails: plugin.Usage{
89+
Usage: "org-invitations ORG",
90+
},
91+
},
92+
{
93+
Name: "space-invitations",
94+
HelpText: "List all invitations for a space",
95+
UsageDetails: plugin.Usage{
96+
Usage: "space-invitations SPACE",
97+
},
98+
},
7899
{
79100
Name: "invite-billing-account-user",
80101
HelpText: "Invite a user to a billing account as an 'accountOwner'",
@@ -256,6 +277,27 @@ func (p *AppCloudPlugin) Run(cliConnection plugin.CliConnection, args []string)
256277
err = p.DeclineInvitation(cliConnection, args[1])
257278

258279
// Send invitations
280+
case "billing-account-invitations":
281+
if len(args) != 2 {
282+
fmt.Println("Incorrect Usage: the required argument BILLING_ACCOUNT was not provided")
283+
return
284+
}
285+
286+
err = p.BillingAccountInvitations(cliConnection, args[1])
287+
case "org-invitations":
288+
if len(args) != 2 {
289+
fmt.Println("Incorrect Usage: the required argument ORG was not provided")
290+
return
291+
}
292+
293+
err = p.OrgInvitations(cliConnection, args[1])
294+
case "space-invitations":
295+
if len(args) != 2 {
296+
fmt.Println("Incorrect Usage: the required argument SPACE was not provided")
297+
return
298+
}
299+
300+
err = p.SpaceInvitations(cliConnection, args[1])
259301
case "invite-billing-account-user":
260302
if len(args) != 3 {
261303
fmt.Println("Incorrect Usage: the required arguments USERNAME and/or BILLING_ACCOUNT were not provided")

org_invitations.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"strings"
8+
9+
"code.cloudfoundry.org/cli/plugin"
10+
)
11+
12+
// OrgInvitations retrieves all invitations for an org.
13+
func (p *AppCloudPlugin) OrgInvitations(c plugin.CliConnection, orgName string) error {
14+
username, err := c.Username()
15+
if err != nil {
16+
username = "you"
17+
}
18+
19+
fmt.Printf("Gettings invitations to org %s as %s...\n", cyanBold(orgName), cyanBold(username))
20+
21+
o, err := c.GetOrg(orgName)
22+
if err != nil {
23+
return fmt.Errorf("Org %s not found", orgName)
24+
}
25+
26+
url := fmt.Sprintf("/custom/organizations/%s/invitations", o.Guid)
27+
resLines, err := c.CliCommandWithoutTerminalOutput("curl", url)
28+
if err != nil {
29+
return fmt.Errorf("Couldn't get invitations for org %s", orgName)
30+
}
31+
32+
resString := strings.Join(resLines, "")
33+
var res InvitationsResponse
34+
err = json.Unmarshal([]byte(resString), &res)
35+
if err != nil {
36+
return errors.New("Couldn't read JSON response from server")
37+
}
38+
39+
if res.ErrorCode != "" {
40+
return errors.New(res.Description)
41+
}
42+
43+
fmt.Println(greenBold("OK\n\n"))
44+
45+
if len(res.Resources) == 0 {
46+
fmt.Println("No invitations found")
47+
return nil
48+
}
49+
50+
fmt.Println(bold("Invitee status roles"))
51+
for _, inv := range res.Resources {
52+
fmt.Printf("%s %s %s\n", inv.Entity.Invitee, inv.Entity.Status, inv.Entity.Roles)
53+
}
54+
return nil
55+
}

space_invitations.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"strings"
8+
9+
"code.cloudfoundry.org/cli/plugin"
10+
)
11+
12+
// SpaceInvitations retrieves all invitations for a space.
13+
func (p *AppCloudPlugin) SpaceInvitations(c plugin.CliConnection, spaceName string) error {
14+
username, err := c.Username()
15+
if err != nil {
16+
username = "you"
17+
}
18+
19+
fmt.Printf("Gettings invitations to space %s as %s...\n", cyanBold(spaceName), cyanBold(username))
20+
21+
s, err := c.GetSpace(spaceName)
22+
if err != nil {
23+
return fmt.Errorf("Space %s not found", spaceName)
24+
}
25+
26+
url := fmt.Sprintf("/custom/spaces/%s/invitations", s.Guid)
27+
resLines, err := c.CliCommandWithoutTerminalOutput("curl", url)
28+
if err != nil {
29+
return fmt.Errorf("Couldn't get invitations for space %s", spaceName)
30+
}
31+
32+
resString := strings.Join(resLines, "")
33+
var res InvitationsResponse
34+
err = json.Unmarshal([]byte(resString), &res)
35+
if err != nil {
36+
return errors.New("Couldn't read JSON response from server")
37+
}
38+
39+
if res.ErrorCode != "" {
40+
return errors.New(res.Description)
41+
}
42+
43+
fmt.Println(greenBold("OK\n\n"))
44+
45+
if len(res.Resources) == 0 {
46+
fmt.Println("No invitations found")
47+
return nil
48+
}
49+
50+
fmt.Println(bold("Invitee roles status"))
51+
for _, inv := range res.Resources {
52+
fmt.Printf("%s %s %s\n", inv.Entity.Invitee, inv.Entity.Roles, inv.Entity.Status)
53+
}
54+
return nil
55+
}

0 commit comments

Comments
 (0)