@@ -64,6 +64,14 @@ type DeleteServiceResponse struct {
6464 DeleteService DeleteService `json:"deleteService"`
6565}
6666
67+ type LinkServiceResponse struct {
68+ LinkService LinkService `json:"linkService"`
69+ }
70+
71+ type UnlinkServiceResponse struct {
72+ UnlinkService UnlinkService `json:"linkService"`
73+ }
74+
6775type AddService struct {
6876 Service Service `json:"app"`
6977 Error string `json:"error"`
@@ -74,6 +82,16 @@ type DeleteService struct {
7482 Error string `json:"error"`
7583}
7684
85+ type LinkService struct {
86+ Ok bool `json:"ok"`
87+ Error string `json:"error"`
88+ }
89+
90+ type UnlinkService struct {
91+ Ok bool `json:"ok"`
92+ Error string `json:"error"`
93+ }
94+
7795var ApiKey = os .Getenv ("DASH_ENTERPRISE_API_KEY" )
7896var DashEnterpriseURL = os .Getenv ("DASH_ENTERPRISE_URL" )
7997var Username = os .Getenv ("DASH_ENTERPRISE_USERNAME" )
@@ -95,10 +113,18 @@ func postgresExists(name string) {
95113 serviceExists ("postgres" , name )
96114}
97115
116+ func postgresLink (serviceName string , appName string ) {
117+ serviceLink ("postgres" , serviceName , appName )
118+ }
119+
98120func postgresList () {
99121 serviceList ("postgres" )
100122}
101123
124+ func postgresUnlink (serviceName string , appName string ) {
125+ serviceUnlink ("postgres" , serviceName , appName )
126+ }
127+
102128func redisCreate (name string ) {
103129 serviceCreate ("redis" , name )
104130}
@@ -111,10 +137,18 @@ func redisExists(name string) {
111137 serviceExists ("redis" , name )
112138}
113139
140+ func redisLink (serviceName string , appName string ) {
141+ serviceLink ("redis" , serviceName , appName )
142+ }
143+
114144func redisList () {
115145 serviceList ("redis" )
116146}
117147
148+ func redisUnlink (serviceName string , appName string ) {
149+ serviceUnlink ("redis" , serviceName , appName )
150+ }
151+
118152func serviceCreate (serviceType string , name string ) {
119153 if name == "" {
120154 log .Fatal (errors .New ("No name specified" ))
@@ -232,6 +266,94 @@ func serviceList(serviceType string) {
232266 }
233267}
234268
269+ func serviceLink (serviceType string , serviceName string , appName string ) {
270+ if serviceName == "" {
271+ log .Fatal (errors .New ("No name specified" ))
272+ }
273+
274+ if appName == "" {
275+ log .Fatal (errors .New ("No app specified" ))
276+ }
277+
278+ mutation := `
279+ mutation LinkService($appname: String!, $serviceName: String!, $serviceType: ServiceType = %s) {
280+ linkService(appname: $appname, serviceType: $serviceType, serviceName: $serviceName) {
281+ ok
282+ error
283+ }
284+ }
285+ `
286+ req := graphql .NewRequest (fmt .Sprintf (mutation , serviceType ))
287+
288+ req .Var ("appname" , appName )
289+ req .Var ("serviceName" , serviceName )
290+
291+ req .Header .Set ("Cache-Control" , "no-cache" )
292+ req .Header .Set ("Authorization" , "Basic " + basicAuth (Username , ApiKey ))
293+
294+ ctx := context .Background ()
295+
296+ var respData LinkServiceResponse
297+
298+ client , err := graphqlClient ()
299+ if err != nil {
300+ log .Fatal (err )
301+ }
302+ if err := client .Run (ctx , req , & respData ); err != nil {
303+ log .Fatal (err )
304+ }
305+
306+ if respData .LinkService .Error != "" {
307+ fmt .Printf (" ! %v\n " , respData .LinkService .Error )
308+ } else {
309+ fmt .Printf ("====> %v linked!\n " , appName )
310+ }
311+ }
312+
313+ func serviceUnlink (serviceType string , serviceName string , appName string ) {
314+ if serviceName == "" {
315+ log .Fatal (errors .New ("No name specified" ))
316+ }
317+
318+ if appName == "" {
319+ log .Fatal (errors .New ("No app specified" ))
320+ }
321+
322+ mutation := `
323+ mutation UnlinkService($appname: String!, $serviceName: String!, $serviceType: ServiceType = %s) {
324+ unlinkService(appname: $appname, serviceType: $serviceType, serviceName: $serviceName) {
325+ ok
326+ error
327+ }
328+ }
329+ `
330+ req := graphql .NewRequest (fmt .Sprintf (mutation , serviceType ))
331+
332+ req .Var ("appname" , appName )
333+ req .Var ("serviceName" , serviceName )
334+
335+ req .Header .Set ("Cache-Control" , "no-cache" )
336+ req .Header .Set ("Authorization" , "Basic " + basicAuth (Username , ApiKey ))
337+
338+ ctx := context .Background ()
339+
340+ var respData UnlinkServiceResponse
341+
342+ client , err := graphqlClient ()
343+ if err != nil {
344+ log .Fatal (err )
345+ }
346+ if err := client .Run (ctx , req , & respData ); err != nil {
347+ log .Fatal (err )
348+ }
349+
350+ if respData .UnlinkService .Error != "" {
351+ fmt .Printf (" ! %v\n " , respData .UnlinkService .Error )
352+ } else {
353+ fmt .Printf ("====> %v unlinked!\n " , appName )
354+ }
355+ }
356+
235357func fetchServices () (ServicesResponse , error ) {
236358 req := graphql .NewRequest (`
237359{
@@ -477,8 +599,16 @@ func main() {
477599 postgresExistsCmd := parser .NewCommand ("postgres:exists" , "Check if a postgres service exists" )
478600 postgresExistsCmdName := postgresExistsCmd .String ("" , "name" , & argparse.Options {Help : "Name of service" })
479601
602+ postgresLinkCmd := parser .NewCommand ("postgres:link" , "Link a postgres service to an app" )
603+ postgresLinkCmdName := postgresLinkCmd .String ("" , "name" , & argparse.Options {Help : "Name of service" })
604+ postgresLinkCmdApp := postgresLinkCmd .String ("" , "app" , & argparse.Options {Help : "Name of app" })
605+
480606 postgresListCmd := parser .NewCommand ("postgres:list" , "List all postgres services" )
481607
608+ postgresUnlinkCmd := parser .NewCommand ("postgres:unlink" , "Unlink a postgres service to an app" )
609+ postgresUnlinkCmdName := postgresUnlinkCmd .String ("" , "name" , & argparse.Options {Help : "Name of service" })
610+ postgresUnlinkCmdApp := postgresUnlinkCmd .String ("" , "app" , & argparse.Options {Help : "Name of app" })
611+
482612 redisCreateCmd := parser .NewCommand ("redis:create" , "Create a redis service" )
483613 redisCreateCmdName := redisCreateCmd .String ("" , "name" , & argparse.Options {Help : "Name of service" })
484614
@@ -488,8 +618,16 @@ func main() {
488618 redisExistsCmd := parser .NewCommand ("redis:exists" , "Check if a redis service exists" )
489619 redisExistsCmdName := redisExistsCmd .String ("" , "name" , & argparse.Options {Help : "Name of service" })
490620
621+ redisLinkCmd := parser .NewCommand ("redis:link" , "Link a redis service to an app" )
622+ redisLinkCmdName := redisLinkCmd .String ("" , "name" , & argparse.Options {Help : "Name of service" })
623+ redisLinkCmdApp := redisLinkCmd .String ("" , "app" , & argparse.Options {Help : "Name of app" })
624+
491625 redisListCmd := parser .NewCommand ("redis:list" , "List all redis services" )
492626
627+ redisUnlinkCmd := parser .NewCommand ("redis:unlink" , "Unlink a redis service to an app" )
628+ redisUnlinkCmdName := redisUnlinkCmd .String ("" , "name" , & argparse.Options {Help : "Name of service" })
629+ redisUnlinkCmdApp := redisUnlinkCmd .String ("" , "app" , & argparse.Options {Help : "Name of app" })
630+
493631 err := parser .Parse (os .Args )
494632 if err != nil {
495633 fmt .Print (parser .Usage (err ))
@@ -510,16 +648,24 @@ func main() {
510648 postgresDelete (* postgresDeleteCmdName )
511649 } else if postgresExistsCmd .Happened () {
512650 postgresExists (* postgresExistsCmdName )
651+ } else if postgresLinkCmd .Happened () {
652+ postgresLink (* postgresLinkCmdName , * postgresLinkCmdApp )
513653 } else if postgresListCmd .Happened () {
514654 postgresList ()
655+ } else if postgresUnlinkCmd .Happened () {
656+ postgresUnlink (* postgresUnlinkCmdName , * postgresUnlinkCmdApp )
515657 } else if redisCreateCmd .Happened () {
516658 redisCreate (* redisCreateCmdName )
517659 } else if redisDeleteCmd .Happened () {
518660 redisDelete (* redisDeleteCmdName )
519661 } else if redisExistsCmd .Happened () {
520662 redisExists (* redisExistsCmdName )
663+ } else if redisLinkCmd .Happened () {
664+ redisLink (* redisLinkCmdName , * redisLinkCmdApp )
521665 } else if redisListCmd .Happened () {
522666 redisList ()
667+ } else if redisUnlinkCmd .Happened () {
668+ redisUnlink (* redisUnlinkCmdName , * redisUnlinkCmdApp )
523669 } else {
524670 err := fmt .Errorf ("bad arguments, please check usage" )
525671 fmt .Print (parser .Usage (err ))
0 commit comments