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

[CLOUDGA-22581] : CLI changes for Connection Pooling #240

Merged
merged 7 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions cmd/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
pitrconfig "github.com/yugabyte/ybm-cli/cmd/cluster/pitr-config"
readreplica "github.com/yugabyte/ybm-cli/cmd/cluster/read-replica"
"github.com/yugabyte/ybm-cli/cmd/util"
connectionpooling "github.com/yugabyte/ybm-cli/cmd/cluster/connection-pooling"
)

// getCmd represents the list command
Expand Down Expand Up @@ -63,4 +64,8 @@ func init() {
util.AddCommandIfFeatureFlag(ClusterCmd, pitrconfig.PitrConfigCmd, util.PITR_CONFIG)
pitrconfig.PitrConfigCmd.PersistentFlags().StringVarP(&pitrconfig.ClusterName, "cluster-name", "c", "", "[REQUIRED] The name of the cluster.")
pitrconfig.PitrConfigCmd.MarkPersistentFlagRequired("cluster-name")

util.AddCommandIfFeatureFlag(ClusterCmd, connectionpooling.ConnectionPoolingCmd, util.CONNECTION_POOLING)
connectionpooling.ConnectionPoolingCmd.PersistentFlags().StringVarP(&connectionpooling.ClusterName, "cluster-name", "c", "", "[REQUIRED] The name of the cluster.")
connectionpooling.ConnectionPoolingCmd.MarkPersistentFlagRequired("cluster-name")
}
129 changes: 129 additions & 0 deletions cmd/cluster/connection-pooling/connection_pooling.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Licensed to Yugabyte, Inc. under one or more contributor license
// agreements. See the NOTICE file distributed with this work for
// additional information regarding copyright ownership. Yugabyte
// licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package connection_pooling

import (
"fmt"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/yugabyte/ybm-cli/cmd/util"
ybmAuthClient "github.com/yugabyte/ybm-cli/internal/client"
"github.com/yugabyte/ybm-cli/internal/formatter"
ybmclient "github.com/yugabyte/yugabytedb-managed-go-client-internal"
)

var ClusterName string

var ConnectionPoolingCmd = &cobra.Command{
Use: "connection-pooling",
Short: "Manage Connection Pooling",
Long: "Manage Connection Pooling",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}

var enableConnectionPoolingCmd = &cobra.Command{
Use: "enable",
Short: "Enable Connection Pooling",
Long: "Enable Connection Pooling",
Run: func(cmd *cobra.Command, args []string) {
authApi, err := ybmAuthClient.NewAuthApiClient()
if err != nil {
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err))
}
authApi.GetInfo("", "")

clusterName, _ := cmd.Flags().GetString("cluster-name")
clusterId, err := authApi.GetClusterIdByName(clusterName)
if err != nil {
logrus.Fatalf("%s", ybmAuthClient.GetApiErrorDetails(err))
}

connectionPoolingOpSpec := ybmclient.NewConnectionPoolingOpSpec(ybmclient.CONNECTIONPOOLINGOPENUM_ENABLE)

resp, err := authApi.PerformConnectionPoolingOperation(clusterId).ConnectionPoolingOpSpec(*connectionPoolingOpSpec).Execute()

if err != nil {
logrus.Debugf("Full HTTP response: %v", resp)
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err))
}

msg := fmt.Sprintf("Connection Pooling for cluster %s is being enabled", formatter.Colorize(clusterName, formatter.GREEN_COLOR))
if viper.GetBool("wait") {
returnStatus, err := authApi.WaitForTaskCompletion(clusterId, ybmclient.ENTITYTYPEENUM_CLUSTER, ybmclient.TASKTYPEENUM_ENABLE_CONNECTION_POOLING, []string{"FAILED", "SUCCEEDED"}, msg)
if err != nil {
logrus.Fatalf("error when getting task status: %s", err)
}
if returnStatus != "SUCCEEDED" {
logrus.Fatalf("Operation failed with error: %s", returnStatus)
}
fmt.Printf("Connection Pooling has been enabled on cluster %s\n", formatter.Colorize(clusterName, formatter.GREEN_COLOR))
} else {
fmt.Println(msg)
}
},
}

var disableConnectionPoolingCmd = &cobra.Command{
Use: "disable",
Short: "Disable Connection Pooling",
Long: "Disable Connection Pooling",
Run: func(cmd *cobra.Command, args []string) {
authApi, err := ybmAuthClient.NewAuthApiClient()
adityakaushik99-yb marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err))
}
authApi.GetInfo("", "")

clusterName, _ := cmd.Flags().GetString("cluster-name")
clusterId, err := authApi.GetClusterIdByName(clusterName)
if err != nil {
logrus.Fatalf("%s", ybmAuthClient.GetApiErrorDetails(err))
}

connectionPoolingOpSpec := ybmclient.NewConnectionPoolingOpSpec(ybmclient.CONNECTIONPOOLINGOPENUM_DISABLE)

resp, err := authApi.PerformConnectionPoolingOperation(clusterId).ConnectionPoolingOpSpec(*connectionPoolingOpSpec).Execute()

if err != nil {
logrus.Debugf("Full HTTP response: %v", resp)
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err))
}

msg := fmt.Sprintf("Connection Pooling for cluster %s is being disabled", formatter.Colorize(clusterName, formatter.GREEN_COLOR))
if viper.GetBool("wait") {
returnStatus, err := authApi.WaitForTaskCompletion(clusterId, ybmclient.ENTITYTYPEENUM_CLUSTER, ybmclient.TASKTYPEENUM_DISABLE_CONNECTION_POOLING, []string{"FAILED", "SUCCEEDED"}, msg)
adityakaushik99-yb marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
logrus.Fatalf("error when getting task status: %s", err)
}
if returnStatus != "SUCCEEDED" {
logrus.Fatalf("Operation failed with error: %s", returnStatus)
}
fmt.Printf("Connection Pooling has been disabled on cluster %s\n", formatter.Colorize(clusterName, formatter.GREEN_COLOR))
} else {
fmt.Println(msg)
}
},
}

func init() {
util.AddCommandIfFeatureFlag(ConnectionPoolingCmd, enableConnectionPoolingCmd, util.CONNECTION_POOLING)

util.AddCommandIfFeatureFlag(ConnectionPoolingCmd, disableConnectionPoolingCmd, util.CONNECTION_POOLING)
}
98 changes: 98 additions & 0 deletions cmd/connection_pooling_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Licensed to Yugabyte, Inc. under one or more contributor license
// agreements. See the NOTICE file distributed with this work for
// additional information regarding copyright ownership. Yugabyte
// licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package cmd_test

import (
"fmt"
"net/http"
"os"
"os/exec"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
"github.com/onsi/gomega/ghttp"
openapi "github.com/yugabyte/yugabytedb-managed-go-client-internal"
)

var _ = Describe("Connection Pooling", func() {

var (
server *ghttp.Server
statusCode int
responseAccount openapi.AccountListResponse
responseProject openapi.AccountListResponse
responseListClusters openapi.ClusterListResponse
)

BeforeEach(func() {
var err error
server, err = newGhttpServer(responseAccount, responseProject)
Expect(err).ToNot(HaveOccurred())
os.Setenv("YBM_HOST", fmt.Sprintf("http://%s", server.Addr()))
os.Setenv("YBM_APIKEY", "test-token")
os.Setenv("YBM_FF_CONNECTION_POOLING", "true")
statusCode = 200
err = loadJson("./test/fixtures/list-clusters.json", &responseListClusters)
Expect(err).ToNot(HaveOccurred())
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest(http.MethodGet, "/api/public/v1/accounts/340af43a-8a7c-4659-9258-4876fd6a207b/projects/78d4459c-0f45-47a5-899a-45ddf43eba6e/clusters"),
ghttp.RespondWithJSONEncodedPtr(&statusCode, responseListClusters),
),
)
})

Context("When enabling connection pooling", func() {
It("should return ", func() {
statusCode = 200

server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest(http.MethodPut, "/api/public/v1/accounts/340af43a-8a7c-4659-9258-4876fd6a207b/projects/78d4459c-0f45-47a5-899a-45ddf43eba6e/clusters/5f80730f-ba3f-4f7e-8c01-f8fa4c90dad8/connection-pooling"),
ghttp.RespondWithJSONEncodedPtr(&statusCode, "Successfully submitted cluster connection pooling operation request"),
),
)

cmd := exec.Command(compiledCLIPath, "cluster", "connection-pooling", "enable", "--cluster-name", "stunning-sole")
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
session.Wait(2)
Expect(session.Out).Should(gbytes.Say("Connection Pooling for cluster stunning-sole is being enabled"))
session.Kill()
})
It("should return required field name and type when not set", func() {
adityakaushik99-yb marked this conversation as resolved.
Show resolved Hide resolved
statusCode = 200

server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest(http.MethodPut, "/api/public/v1/accounts/340af43a-8a7c-4659-9258-4876fd6a207b/projects/78d4459c-0f45-47a5-899a-45ddf43eba6e/clusters/5f80730f-ba3f-4f7e-8c01-f8fa4c90dad8/connection-pooling"),
ghttp.RespondWithJSONEncodedPtr(&statusCode, "Successfully submitted cluster connection pooling operation request"),
),
)

cmd := exec.Command(compiledCLIPath, "cluster", "connection-pooling", "disable", "--cluster-name", "stunning-sole")
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
exec.Command(compiledCLIPath, "y")
Expect(err).NotTo(HaveOccurred())
session.Wait(2)
Expect(session.Out).Should(gbytes.Say("Connection Pooling for cluster stunning-sole is being disabled"))
session.Kill()
})

})
})
1 change: 1 addition & 0 deletions cmd/util/feature_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
ENTERPRISE_SECURITY FeatureFlag = "ENTERPRISE_SECURITY"
DB_AUDIT_LOGS FeatureFlag = "DB_AUDIT_LOGS"
PITR_CONFIG FeatureFlag = "PITR_CONFIG"
CONNECTION_POOLING FeatureFlag = "CONNECTION_POOLING"
)

func (f FeatureFlag) String() string {
Expand Down
30 changes: 15 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,24 @@ require (
github.com/inhies/go-bytesize v0.0.0-20220417184213-4913239db9cf
github.com/jayco/go-emoji-flag v0.0.0-20190810054606-01604da018da
github.com/mattn/go-runewidth v0.0.14
github.com/onsi/ginkgo/v2 v2.15.0
github.com/onsi/gomega v1.32.0
github.com/onsi/ginkgo/v2 v2.19.0
github.com/onsi/gomega v1.34.1
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
github.com/pkg/errors v0.9.1
github.com/robfig/cron v1.2.0
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.17.0
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816
github.com/yugabyte/yugabytedb-managed-go-client-internal v0.0.0-20240824013428-0810f4c8c85e
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
golang.org/x/mod v0.14.0
golang.org/x/term v0.16.0
github.com/yugabyte/yugabytedb-managed-go-client-internal v0.0.0-20240821184408-0227fa6030a5
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56
golang.org/x/mod v0.19.0
golang.org/x/term v0.22.0
gotest.tools/v3 v3.4.0
)

require (
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
Expand All @@ -44,13 +45,12 @@ require (
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/go-github/v58 v58.0.0
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
Expand All @@ -70,14 +70,14 @@ require (
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/oauth2 v0.12.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.16.1 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/tools v0.23.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.33.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Expand Down
Loading