Skip to content

Commit

Permalink
Added attach cluster with config command
Browse files Browse the repository at this point in the history
  • Loading branch information
adityakaushik99-yb committed Aug 23, 2023
1 parent 9d1d910 commit cd01bde
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
59 changes: 59 additions & 0 deletions cmd/metrics_exporter/metrics_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,59 @@ var removeMetricsExporterFromClusterCmd = &cobra.Command{
},
}

var associateMetricsExporterWithClusterCmd = &cobra.Command{
Use: "attach",
Short: "Associate Metrics Exporter Config with Cluster",
Long: "Associate Metrics Exporter Config with Cluster",
Run: func(cmd *cobra.Command, args []string) {
authApi, err := ybmAuthClient.NewAuthApiClient()
if err != nil {
logrus.Fatalf("could not initiate api client: %s", err.Error())
}
authApi.GetInfo("", "")

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

configName, _ := cmd.Flags().GetString("config-name")

resp, r, err := authApi.ListMetricsExporterConfigs().Execute()

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

configId := ""

for _, metricsExporter := range resp.Data {
if metricsExporter.GetSpec().Name == configName {
configId = metricsExporter.GetInfo().Id
break
}
}

if configId == "" {
logrus.Fatalf("Could not find config with name %s", configName)
}

metricsExporterClusterConfigSpec := ybmclient.NewMetricsExporterClusterConfigurationSpec(configId)

resp1, r, err := authApi.AssociateMetricsExporterWithCluster(clusterId).MetricsExporterClusterConfigurationSpec(*metricsExporterClusterConfigSpec).Execute()

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

fmt.Printf("Attaching Metrics Exporter Config %s with cluster %s", resp1.Data.Spec.Name, clusterName)
fmt.Println()
},
}

func init() {
MetricsExporterCmd.AddCommand(createMetricsExporterCmd)
createMetricsExporterCmd.Flags().String("name", "", "[REQUIRED] The name of the cluster.")
Expand All @@ -208,4 +261,10 @@ func init() {
MetricsExporterCmd.AddCommand(removeMetricsExporterFromClusterCmd)
removeMetricsExporterFromClusterCmd.Flags().String("cluster-name", "", "[REQUIRED] The name of the cluster.")
removeMetricsExporterFromClusterCmd.MarkFlagRequired("cluster-name")

MetricsExporterCmd.AddCommand(associateMetricsExporterWithClusterCmd)
associateMetricsExporterWithClusterCmd.Flags().String("cluster-name", "", "[REQUIRED] The name of the cluster.")
associateMetricsExporterWithClusterCmd.MarkFlagRequired("cluster-name")
associateMetricsExporterWithClusterCmd.Flags().String("config-name", "", "[REQUIRED] The name of the metrics exporter config")
associateMetricsExporterWithClusterCmd.MarkFlagRequired("config-name")
}
4 changes: 4 additions & 0 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1301,3 +1301,7 @@ func (a *AuthApiClient) DeleteMetricsExporterConfig(configId string) ybmclient.A
func (a *AuthApiClient) RemoveMetricsExporterConfigFromCluster(clusterId string) ybmclient.ApiRemoveMetricsExporterConfigFromClusterRequest {
return a.ApiClient.MetricsExporterConfigApi.RemoveMetricsExporterConfigFromCluster(a.ctx, a.AccountID, a.ProjectID, clusterId)
}

func (a *AuthApiClient) AssociateMetricsExporterWithCluster(clusterId string) ybmclient.ApiAddMetricsExporterConfigToClusterRequest {
return a.ApiClient.MetricsExporterConfigApi.AddMetricsExporterConfigToCluster(a.ctx, a.AccountID, a.ProjectID, clusterId)
}

0 comments on commit cd01bde

Please sign in to comment.