Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor endpoint operations #56

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
301 changes: 108 additions & 193 deletions cmd/endpoint.go
Original file line number Diff line number Diff line change
@@ -1,235 +1,150 @@
package cmd

import (
"bytes"
"encoding/json"
"fmt"
"os"

"github.com/degica/barcelona-cli/api"
"github.com/degica/barcelona-cli/operations"
"github.com/degica/barcelona-cli/utils"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli"
)

var EndpointCommand = cli.Command{
Name: "endpoint",
Usage: "Endpoint operations",
Subcommands: []cli.Command{
{
func getEndpointSubcommands() []cli.Command {
districtFlag := cli.StringFlag{
Name: "district, d",
Value: "default",
Usage: "AWS region",
}

publicFlag := cli.BoolFlag{
Name: "public",
Usage: "Public facing endpoint",
}

certificateFlag := cli.StringFlag{
Name: "certificate-arn",
Usage: "ACM Certificate ARN",
}

sslPolicyFlag := cli.StringFlag{
Name: "ssl-policy",
Usage: "HTTPS SSL Policy",
}

noConfirmFlag := cli.BoolFlag{
Name: "no-confirmation",
}

endpointSubcommands := map[operations.OperationType]cli.Command{
operations.Create: cli.Command{
Name: "create",
Usage: "Create a new endpoint",
ArgsUsage: "ENDPOINT_NAME",
Flags: []cli.Flag{
cli.StringFlag{
Name: "district, d",
Value: "default",
Usage: "AWS region",
},
cli.BoolFlag{
Name: "public",
Usage: "Public facing endpoint",
},
cli.StringFlag{
Name: "certificate-arn",
Usage: "ACM Certificate ARN",
},
cli.StringFlag{
Name: "ssl-policy",
Usage: "HTTPS SSL Policy",
},
districtFlag,
publicFlag,
certificateFlag,
sslPolicyFlag,
},
Action: func(c *cli.Context) error {
endpointName := c.Args().Get(0)
if len(endpointName) == 0 {
return cli.NewExitError("endpoint name is required", 1)
}

if len(c.Args()) != 1 {
return cli.NewExitError("please place options and flags before the endpoint name.", 1)
}

public := c.Bool("public")
request := api.Endpoint{
Name: endpointName,
Public: &public,
CertificateID: c.String("certificate-arn"),
SslPolicy: c.String("ssl-policy"),
}

b, err := json.Marshal(&request)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
},

resp, err := api.DefaultClient.Request("POST", fmt.Sprintf("/districts/%s/endpoints", c.String("district")), bytes.NewBuffer(b))
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
var eResp api.EndpointResponse
err = json.Unmarshal(resp, &eResp)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
printEndpoint(eResp.Endpoint)
operations.Update: cli.Command{
Name: "update",
Usage: "Update an endpoint",
ArgsUsage: "ENDPOINT_NAME",
Flags: []cli.Flag{
districtFlag,
certificateFlag,
sslPolicyFlag,
},
},

return nil
operations.Delete: cli.Command{
Name: "delete",
Usage: "Delete an endpoint",
ArgsUsage: "ENDPOINT_NAME",
Flags: []cli.Flag{
districtFlag,
noConfirmFlag,
},
},
{

operations.Show: cli.Command{
Name: "show",
Usage: "Show endpoint information",
ArgsUsage: "ENDPOINT_NAME",
Flags: []cli.Flag{
cli.StringFlag{
Name: "district, d",
Value: "default",
Usage: "District name",
},
},
Action: func(c *cli.Context) error {
endpointName := c.Args().Get(0)
if len(endpointName) == 0 {
return cli.NewExitError("endpoint name is required", 1)
}

resp, err := api.DefaultClient.Request("GET", fmt.Sprintf("/districts/%s/endpoints/%s", c.String("district"), endpointName), nil)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
var eResp api.EndpointResponse
err = json.Unmarshal(resp, &eResp)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
printEndpoint(eResp.Endpoint)

return nil
districtFlag,
},
},
{

operations.List: cli.Command{
Name: "list",
Usage: "List endpoints",
Flags: []cli.Flag{
cli.StringFlag{
Name: "district, d",
Value: "default",
Usage: "District name",
},
},
Action: func(c *cli.Context) error {
resp, err := api.DefaultClient.Request("GET", fmt.Sprintf("/districts/%s/endpoints", c.String("district")), nil)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
var eResp api.EndpointResponse
err = json.Unmarshal(resp, &eResp)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
printEndpoints(eResp.Endpoints)

return nil
districtFlag,
},
},
{
Name: "update",
Usage: "Update an endpoint",
ArgsUsage: "ENDPOINT_NAME",
Flags: []cli.Flag{
cli.StringFlag{
Name: "district, d",
Value: "default",
Usage: "District name",
},
cli.StringFlag{
Name: "certificate-arn",
Usage: "ACM Certificate ARN",
},
cli.StringFlag{

Usage: "HTTPS SSL Policy",
},
},
Action: func(c *cli.Context) error {
endpointName := c.Args().Get(0)
if len(endpointName) == 0 {
return cli.NewExitError("endpoint name is required", 1)
}
}

request := api.Endpoint{
CertificateID: c.String("certificate-arn"),
SslPolicy: c.String("ssl-policy"),
}
var array []cli.Command

b, err := json.Marshal(&request)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
for opname, command := range endpointSubcommands {

resp, err := api.DefaultClient.Request("PATCH", fmt.Sprintf("/districts/%s/endpoints/%s", c.String("district"), endpointName), bytes.NewBuffer(b))
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
var eResp api.EndpointResponse
err = json.Unmarshal(resp, &eResp)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
printEndpoint(eResp.Endpoint)
captured_opname := opname
captured_command := command

return nil
},
},
{
Name: "delete",
Usage: "Delete an endpoint",
ArgsUsage: "ENDPOINT_NAME",
Flags: []cli.Flag{
cli.StringFlag{
Name: "district, d",
Value: "default",
Usage: "District name",
},
cli.BoolFlag{
Name: "no-confirmation",
},
},
subcommand := cli.Command{
Name: command.Name,
Usage: command.Usage,
ArgsUsage: command.ArgsUsage,
Flags: command.Flags,
Action: func(c *cli.Context) error {

endpointName := c.Args().Get(0)
if len(endpointName) == 0 {
return cli.NewExitError("endpoint name is required", 1)
}
if captured_command.ArgsUsage == "ENDPOINT_NAME" {
if len(endpointName) == 0 {
return cli.NewExitError("endpoint name is required", 1)
}

fmt.Printf("You are attempting to delete /%s/endpoints/%s\n", c.String("district"), endpointName)
if !c.Bool("no-confirmation") && !utils.AreYouSure("This operation cannot be undone. Are you sure?", utils.NewStdinInputReader()) {
return nil
if len(c.Args()) != 1 {
return cli.NewExitError("please place options and flags before the endpoint name.", 1)
}
}

_, err := api.DefaultClient.Request("DELETE", fmt.Sprintf("/districts/%s/endpoints/%s", c.String("district"), endpointName), nil)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
return nil
public := c.Bool("public")
cert_arn := c.String("certificate-arn")
policy := c.String("ssl-policy")
districtName := c.String("district")
noconfirm := c.Bool("no-confirmation")

client := struct {
*api.Client
utils.UserInputReader
}{
api.DefaultClient,
utils.NewStdinInputReader(),
}

oper := operations.NewEndpointOperation(
districtName,
endpointName,
public,
cert_arn,
policy,
noconfirm,
captured_opname,
client,
)
return operations.Execute(oper)
},
},
},
}
}
array = append(array, subcommand)
}

func printEndpoint(e *api.Endpoint) {
fmt.Printf("Name: %s\n", e.Name)
fmt.Printf("Public: %t\n", *e.Public)
fmt.Printf("SSL Policy: %s\n", e.SslPolicy)
fmt.Printf("Certificate ARN: %s\n", e.CertificateID)
fmt.Printf("DNS Name: %s\n", e.DNSName)
return array
}

func printEndpoints(es []*api.Endpoint) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "District", "Public", "SSL Policy", "Cert ID"})
table.SetBorder(false)
for _, e := range es {
table.Append([]string{e.Name, e.District.Name, fmt.Sprintf("%t", *e.Public), e.SslPolicy, e.CertificateID})
}
table.Render()
var EndpointCommand = cli.Command{
Name: "endpoint",
Usage: "Endpoint operations",
Subcommands: getEndpointSubcommands(),
}
Loading