Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ MAINTAINER = plotly
REPOSITORY = dds-client
HARDWARE = $(shell uname -m)
SYSTEM_NAME = $(shell uname -s | tr '[:upper:]' '[:lower:]')
BASE_VERSION ?= 0.2.0
BASE_VERSION ?= 0.3.0
IMAGE_NAME ?= $(MAINTAINER)/$(REPOSITORY)

ifeq ($(CIRCLE_BRANCH),release)
Expand Down
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ Delete a postgres service
dds-client postgres:delete --name dopsa
```

#### `postgres:link`

Link a postgres service to an app

```shell
dds-client postgres:link --name dopsa --app dopsa
```

#### `postgres:unlink`

Unlink a postgres service from an app

```shell
dds-client postgres:unlink --name dopsa --app dopsa
```

#### `redis:list`

List all redis services
Expand Down Expand Up @@ -122,3 +138,19 @@ Delete a redis service
```shell
dds-client redis:delete --name dopsa
```

#### `redis:link`

Link a redis service to an app

```shell
dds-client redis:link --name dopsa --app dopsa
```

#### `redis:unlink`

Unlink a redis service from an app

```shell
dds-client redis:unlink --name dopsa --app dopsa
```
146 changes: 146 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ type DeleteServiceResponse struct {
DeleteService DeleteService `json:"deleteService"`
}

type LinkServiceResponse struct {
LinkService LinkService `json:"linkService"`
}

type UnlinkServiceResponse struct {
UnlinkService UnlinkService `json:"linkService"`
}

type AddService struct {
Service Service `json:"app"`
Error string `json:"error"`
Expand All @@ -74,6 +82,16 @@ type DeleteService struct {
Error string `json:"error"`
}

type LinkService struct {
Ok bool `json:"ok"`
Error string `json:"error"`
}

type UnlinkService struct {
Ok bool `json:"ok"`
Error string `json:"error"`
}

var ApiKey = os.Getenv("DASH_ENTERPRISE_API_KEY")
var DashEnterpriseURL = os.Getenv("DASH_ENTERPRISE_URL")
var Username = os.Getenv("DASH_ENTERPRISE_USERNAME")
Expand All @@ -95,10 +113,18 @@ func postgresExists(name string) {
serviceExists("postgres", name)
}

func postgresLink(serviceName string, appName string) {
serviceLink("postgres", serviceName, appName)
}

func postgresList() {
serviceList("postgres")
}

func postgresUnlink(serviceName string, appName string) {
serviceUnlink("postgres", serviceName, appName)
}

func redisCreate(name string) {
serviceCreate("redis", name)
}
Expand All @@ -111,10 +137,18 @@ func redisExists(name string) {
serviceExists("redis", name)
}

func redisLink(serviceName string, appName string) {
serviceLink("redis", serviceName, appName)
}

func redisList() {
serviceList("redis")
}

func redisUnlink(serviceName string, appName string) {
serviceUnlink("redis", serviceName, appName)
}

func serviceCreate(serviceType string, name string) {
if name == "" {
log.Fatal(errors.New("No name specified"))
Expand Down Expand Up @@ -232,6 +266,94 @@ func serviceList(serviceType string) {
}
}

func serviceLink(serviceType string, serviceName string, appName string) {
if serviceName == "" {
log.Fatal(errors.New("No name specified"))
}

if appName == "" {
log.Fatal(errors.New("No app specified"))
}

mutation := `
mutation LinkService($appname: String!, $serviceName: String!, $serviceType: ServiceType = %s) {
linkService(appname: $appname, serviceType: $serviceType, serviceName: $serviceName) {
ok
error
}
}
`
req := graphql.NewRequest(fmt.Sprintf(mutation, serviceType))

req.Var("appname", appName)
req.Var("serviceName", serviceName)

req.Header.Set("Cache-Control", "no-cache")
req.Header.Set("Authorization", "Basic "+basicAuth(Username, ApiKey))

ctx := context.Background()

var respData LinkServiceResponse

client, err := graphqlClient()
if err != nil {
log.Fatal(err)
}
if err := client.Run(ctx, req, &respData); err != nil {
log.Fatal(err)
}

if respData.LinkService.Error != "" {
fmt.Printf(" ! %v\n", respData.LinkService.Error)
} else {
fmt.Printf("====> %v linked!\n", appName)
}
}

func serviceUnlink(serviceType string, serviceName string, appName string) {
if serviceName == "" {
log.Fatal(errors.New("No name specified"))
}

if appName == "" {
log.Fatal(errors.New("No app specified"))
}

mutation := `
mutation UnlinkService($appname: String!, $serviceName: String!, $serviceType: ServiceType = %s) {
unlinkService(appname: $appname, serviceType: $serviceType, serviceName: $serviceName) {
ok
error
}
}
`
req := graphql.NewRequest(fmt.Sprintf(mutation, serviceType))

req.Var("appname", appName)
req.Var("serviceName", serviceName)

req.Header.Set("Cache-Control", "no-cache")
req.Header.Set("Authorization", "Basic "+basicAuth(Username, ApiKey))

ctx := context.Background()

var respData UnlinkServiceResponse

client, err := graphqlClient()
if err != nil {
log.Fatal(err)
}
if err := client.Run(ctx, req, &respData); err != nil {
log.Fatal(err)
}

if respData.UnlinkService.Error != "" {
fmt.Printf(" ! %v\n", respData.UnlinkService.Error)
} else {
fmt.Printf("====> %v unlinked!\n", appName)
}
}

func fetchServices() (ServicesResponse, error) {
req := graphql.NewRequest(`
{
Expand Down Expand Up @@ -477,8 +599,16 @@ func main() {
postgresExistsCmd := parser.NewCommand("postgres:exists", "Check if a postgres service exists")
postgresExistsCmdName := postgresExistsCmd.String("", "name", &argparse.Options{Help: "Name of service"})

postgresLinkCmd := parser.NewCommand("postgres:link", "Link a postgres service to an app")
postgresLinkCmdName := postgresLinkCmd.String("", "name", &argparse.Options{Help: "Name of service"})
postgresLinkCmdApp := postgresLinkCmd.String("", "app", &argparse.Options{Help: "Name of app"})

postgresListCmd := parser.NewCommand("postgres:list", "List all postgres services")

postgresUnlinkCmd := parser.NewCommand("postgres:unlink", "Unlink a postgres service to an app")
postgresUnlinkCmdName := postgresUnlinkCmd.String("", "name", &argparse.Options{Help: "Name of service"})
postgresUnlinkCmdApp := postgresUnlinkCmd.String("", "app", &argparse.Options{Help: "Name of app"})

redisCreateCmd := parser.NewCommand("redis:create", "Create a redis service")
redisCreateCmdName := redisCreateCmd.String("", "name", &argparse.Options{Help: "Name of service"})

Expand All @@ -488,8 +618,16 @@ func main() {
redisExistsCmd := parser.NewCommand("redis:exists", "Check if a redis service exists")
redisExistsCmdName := redisExistsCmd.String("", "name", &argparse.Options{Help: "Name of service"})

redisLinkCmd := parser.NewCommand("redis:link", "Link a redis service to an app")
redisLinkCmdName := redisLinkCmd.String("", "name", &argparse.Options{Help: "Name of service"})
redisLinkCmdApp := redisLinkCmd.String("", "app", &argparse.Options{Help: "Name of app"})

redisListCmd := parser.NewCommand("redis:list", "List all redis services")

redisUnlinkCmd := parser.NewCommand("redis:unlink", "Unlink a redis service to an app")
redisUnlinkCmdName := redisUnlinkCmd.String("", "name", &argparse.Options{Help: "Name of service"})
redisUnlinkCmdApp := redisUnlinkCmd.String("", "app", &argparse.Options{Help: "Name of app"})

err := parser.Parse(os.Args)
if err != nil {
fmt.Print(parser.Usage(err))
Expand All @@ -510,16 +648,24 @@ func main() {
postgresDelete(*postgresDeleteCmdName)
} else if postgresExistsCmd.Happened() {
postgresExists(*postgresExistsCmdName)
} else if postgresLinkCmd.Happened() {
postgresLink(*postgresLinkCmdName, *postgresLinkCmdApp)
} else if postgresListCmd.Happened() {
postgresList()
} else if postgresUnlinkCmd.Happened() {
postgresUnlink(*postgresUnlinkCmdName, *postgresUnlinkCmdApp)
} else if redisCreateCmd.Happened() {
redisCreate(*redisCreateCmdName)
} else if redisDeleteCmd.Happened() {
redisDelete(*redisDeleteCmdName)
} else if redisExistsCmd.Happened() {
redisExists(*redisExistsCmdName)
} else if redisLinkCmd.Happened() {
redisLink(*redisLinkCmdName, *redisLinkCmdApp)
} else if redisListCmd.Happened() {
redisList()
} else if redisUnlinkCmd.Happened() {
redisUnlink(*redisUnlinkCmdName, *redisUnlinkCmdApp)
} else {
err := fmt.Errorf("bad arguments, please check usage")
fmt.Print(parser.Usage(err))
Expand Down