Skip to content

Commit

Permalink
Fix comments from PR #106 (#108)
Browse files Browse the repository at this point in the history
* Fix comments from PR #106

* Comments from testing

* fix error messages
  • Loading branch information
cdavid authored Mar 23, 2023
1 parent 3793a9a commit 9ae5ec7
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 94 deletions.
15 changes: 10 additions & 5 deletions cmd/cluster/network/endpoint/create_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ var createEndpointCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
authApi, err := ybmAuthClient.NewAuthApiClient()
if err != nil {
logrus.Fatalf("Could not initiate api client: %s", err.Error())
logrus.Fatalf("Could not initiate api client: %s", ybmAuthClient.GetApiErrorDetails(err))
}
authApi.GetInfo("", "")

clusterData := getCluster(cmd, authApi)
clusterName, _ := cmd.Flags().GetString("cluster-name")
clusterData, err := authApi.GetClusterByName(clusterName)
if err != nil {
logrus.Fatalf("Could not get cluster data: %s", ybmAuthClient.GetApiErrorDetails(err))
}

accessibilityType, _ := cmd.Flags().GetString("accessibility-type")
reg, _ := cmd.Flags().GetString("region")

Expand Down Expand Up @@ -75,7 +80,7 @@ var createEndpointCmd = &cobra.Command{
createResp, r, err := authApi.CreatePrivateServiceEndpoint(clusterData.Info.Id).PrivateServiceEndpointSpec(createPseSpec[0]).Execute()
if err != nil {
logrus.Debugf("Full HTTP response: %v", r)
logrus.Fatalf("Could not create private service endpoint: %s", err.Error())
logrus.Fatalf("Could not create private service endpoint: %s", ybmAuthClient.GetApiErrorDetails(err))
}

psEps := util.Filter(createResp.GetData(), func(ep ybmclient.PrivateServiceEndpointRegionData) bool {
Expand All @@ -99,9 +104,9 @@ var createEndpointCmd = &cobra.Command{

func init() {
EndpointCmd.AddCommand(createEndpointCmd)
createEndpointCmd.Flags().String("accessibility-type", "", "[REQUIRED] The accessibility of the endpoint.")
createEndpointCmd.Flags().String("accessibility-type", "", "[REQUIRED] The accessibility of the endpoint. Valid options are PUBLIC, PRIVATE and PRIVATE_SERVICE_ENDPOINT.")
createEndpointCmd.MarkFlagRequired("accessibility-type")
createEndpointCmd.Flags().String("region", "", "[REQUIRED] The region of the endpoint.")
createEndpointCmd.MarkFlagRequired("region")
createEndpointCmd.Flags().String("security-principals", "", "[OPTIONAL] The security principals of the endpoint.")
createEndpointCmd.Flags().String("security-principals", "", "[OPTIONAL] The list of security principals that have access to this endpoint. Required for private service endpoints. Accepts a comma separated list. E.g.: `arn:aws:iam::account_id1:root,arn:aws:iam::account_id2:root`")
}
11 changes: 8 additions & 3 deletions cmd/cluster/network/endpoint/delete_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,19 @@ var deleteEndpointCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
authApi, err := ybmAuthClient.NewAuthApiClient()
if err != nil {
logrus.Fatalf("Could not initiate api client: %s\n", err.Error())
logrus.Fatalf("Could not initiate api client: %s\n", ybmAuthClient.GetApiErrorDetails(err))
}
authApi.GetInfo("", "")

clusterEndpoints, clusterId, endpointId := getEndpointById(cmd, authApi)
clusterName, _ := cmd.Flags().GetString("cluster-name")
endpointId, _ := cmd.Flags().GetString("endpoint-id")
clusterEndpoint, clusterId, err := authApi.GetEndpointByIdForClusterByName(clusterName, endpointId)
if err != nil {
logrus.Fatalf("Error when calling `ClusterApi.GetEndpointByIdForClusterByName`: %s\n", ybmAuthClient.GetApiErrorDetails(err))
}

// We currently support fetching just Private Service Endpoints
switch clusterEndpoints[0].GetAccessibilityType() {
switch clusterEndpoint.GetAccessibilityType() {

case ybmclient.ACCESSIBILITYTYPE_PRIVATE_SERVICE_ENDPOINT:
logrus.Debugln("Endpoint is a private service endpoint, attempting to delete")
Expand Down
15 changes: 10 additions & 5 deletions cmd/cluster/network/endpoint/describe_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,19 @@ var describeEndpointCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
authApi, err := ybmAuthClient.NewAuthApiClient()
if err != nil {
logrus.Fatalf("Could not initiate api client: %s\n", err.Error())
logrus.Fatalf("Could not initiate api client: %s\n", ybmAuthClient.GetApiErrorDetails(err))
}
authApi.GetInfo("", "")

clusterEndpoints, clusterId, endpointId := getEndpointById(cmd, authApi)
clusterName, _ := cmd.Flags().GetString("cluster-name")
endpointId, _ := cmd.Flags().GetString("endpoint-id")
clusterEndpoint, clusterId, err := authApi.GetEndpointByIdForClusterByName(clusterName, endpointId)
if err != nil {
logrus.Fatalf("Error when calling `ClusterApi.GetEndpointByIdForClusterByName`: %s\n", ybmAuthClient.GetApiErrorDetails(err))
}

// We currently support fetching just Private Service Endpoints
switch clusterEndpoints[0].GetAccessibilityType() {
switch clusterEndpoint.GetAccessibilityType() {

case ybmclient.ACCESSIBILITYTYPE_PRIVATE_SERVICE_ENDPOINT:
logrus.Debugln("Endpoint is a private service endpoint, getting more data")
Expand All @@ -53,7 +58,7 @@ var describeEndpointCmd = &cobra.Command{
psEndpointContext := *formatter.NewPSEndpointContext()
psEndpointContext.Output = os.Stdout
psEndpointContext.Format = formatter.NewPSEndpointFormat(viper.GetString("output"))
psEndpointContext.SetFullPSEndpoint(*authApi, pseGetResponse.GetData(), clusterEndpoints[0])
psEndpointContext.SetFullPSEndpoint(*authApi, pseGetResponse.GetData(), clusterEndpoint)
psEndpointContext.Write()
return
}
Expand All @@ -62,7 +67,7 @@ var describeEndpointCmd = &cobra.Command{
Output: os.Stdout,
Format: formatter.NewPSEndpointFormat(viper.GetString("output")),
}
formatter.PSEndpointWrite(psEndpointContext, pseGetResponse.GetData(), clusterEndpoints[0])
formatter.PSEndpointWrite(psEndpointContext, pseGetResponse.GetData(), clusterEndpoint)

default:
logrus.Fatalf("Endpoint is not a private service endpoint. Only private service endpoints are currently supported.\n")
Expand Down
12 changes: 8 additions & 4 deletions cmd/cluster/network/endpoint/list_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ var listEndpointCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
authApi, err := ybmAuthClient.NewAuthApiClient()
if err != nil {
logrus.Fatalf("Could not initiate api client: %s\n", err.Error())
logrus.Fatalf("Could not initiate api client: %s\n", ybmAuthClient.GetApiErrorDetails(err))
}
authApi.GetInfo("", "")

clusterEndpoints, _ := getEndpoints(cmd, authApi)
clusterName, _ := cmd.Flags().GetString("cluster-name")
clusterEndpoints, _, err := authApi.GetEndpointsForClusterByName(clusterName)
if err != nil {
logrus.Fatalf("Could not get cluster data: %s\n", ybmAuthClient.GetApiErrorDetails(err))
}

region, _ := cmd.Flags().GetString("region")
if region != "" {
Expand Down Expand Up @@ -69,6 +73,6 @@ var listEndpointCmd = &cobra.Command{

func init() {
EndpointCmd.AddCommand(listEndpointCmd)
listEndpointCmd.Flags().String("accessibility", "", "[OPTIONAL] Accessibility of the endpoint")
listEndpointCmd.Flags().String("region", "", "[OPTIONAL] Region of the endpoint")
listEndpointCmd.Flags().String("accessibility-type", "", "[OPTIONAL] Accessibility of the endpoint. Valid options are PUBLIC, PRIVATE and PRIVATE_SERVICE_ENDPOINT.")
listEndpointCmd.Flags().String("region", "", "[OPTIONAL] The region of the endpoint.")
}
13 changes: 9 additions & 4 deletions cmd/cluster/network/endpoint/update_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@ var updateEndpointCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
authApi, err := ybmAuthClient.NewAuthApiClient()
if err != nil {
logrus.Fatalf("Could not initiate api client: %s\n", err.Error())
logrus.Fatalf("Could not initiate api client: %s\n", ybmAuthClient.GetApiErrorDetails(err))
}
authApi.GetInfo("", "")

clusterEndpoints, clusterId, endpointId := getEndpointById(cmd, authApi)
clusterName, _ := cmd.Flags().GetString("cluster-name")
endpointId, _ := cmd.Flags().GetString("endpoint-id")
clusterEndpoint, clusterId, err := authApi.GetEndpointByIdForClusterByName(clusterName, endpointId)
if err != nil {
logrus.Fatalf("Error when calling `ClusterApi.GetEndpointByIdForClusterByName`: %s\n", ybmAuthClient.GetApiErrorDetails(err))
}

// We currently support fetching just Private Service Endpoints
switch clusterEndpoints[0].GetAccessibilityType() {
switch clusterEndpoint.GetAccessibilityType() {

case ybmclient.ACCESSIBILITYTYPE_PRIVATE_SERVICE_ENDPOINT:
if !cmd.Flags().Changed("security-principals") {
Expand Down Expand Up @@ -82,5 +87,5 @@ func init() {
EndpointCmd.AddCommand(updateEndpointCmd)
updateEndpointCmd.Flags().String("endpoint-id", "", "[REQUIRED] The ID of the endpoint")
updateEndpointCmd.MarkFlagRequired("endpoint-id")
updateEndpointCmd.Flags().String("security-principals", "", "[OPTIONAL] The list of security principals that have access to this endpoint (comma separated). Required for private service endpoints")
updateEndpointCmd.Flags().String("security-principals", "", "[OPTIONAL] The list of security principals that have access to this endpoint. Required for private service endpoints. Accepts a comma separated list. E.g.: `arn:aws:iam::account_id1:root,arn:aws:iam::account_id2:root`")
}
70 changes: 0 additions & 70 deletions cmd/cluster/network/endpoint/util_endpoint.go

This file was deleted.

49 changes: 46 additions & 3 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,17 +312,60 @@ func (a *AuthApiClient) GetInfo(providedAccountID string, providedProjectID stri
}
}

func (a *AuthApiClient) GetClusterIdByName(clusterName string) (string, error) {
func (a *AuthApiClient) GetClusterByName(clusterName string) (ybmclient.ClusterData, error) {
clusterResp, resp, err := a.ListClusters().Name(clusterName).Execute()
if err != nil {
b, _ := httputil.DumpResponse(resp, true)
logrus.Debug(string(b))
return "", err
return ybmclient.ClusterData{}, err
}
clusterData := clusterResp.GetData()

if len(clusterData) != 0 {
return clusterData[0].Info.GetId(), nil
return clusterData[0], nil
}

return ybmclient.ClusterData{}, fmt.Errorf("could not get cluster data for cluster name: %s", clusterName)
}

func (a *AuthApiClient) GetEndpointsForClusterByName(clusterName string) ([]ybmclient.Endpoint, string, error) {
clusterData, err := a.GetClusterByName(clusterName)
if err != nil {
return nil, "", err
}
clusterId := clusterData.Info.GetId()
clusterEndpoints := clusterData.Info.GetClusterEndpoints()
jsonEndpoints, _ := json.Marshal(clusterEndpoints)
logrus.Debugf("Found endpoints: %v\n", string(jsonEndpoints))

return clusterEndpoints, clusterId, nil
}

func (a *AuthApiClient) GetEndpointByIdForClusterByName(clusterName string, endpointId string) (ybmclient.Endpoint, string, error) {
endpoints, clusterId, err := a.GetEndpointsForClusterByName(clusterName)
if err != nil {
// return the error
return ybmclient.Endpoint{}, "", err
}

endpoints = util.Filter(endpoints, func(endpoint ybmclient.Endpoint) bool {
return endpoint.GetId() == endpointId || endpoint.GetPseId() == endpointId
})

if len(endpoints) == 0 {
logrus.Fatalf("Endpoint not found\n")
}
if len(endpoints) > 1 {
logrus.Fatalf("Multiple endpoints found\n")
}

return endpoints[0], clusterId, nil
}

func (a *AuthApiClient) GetClusterIdByName(clusterName string) (string, error) {
clusterData, err := a.GetClusterByName(clusterName)
if err == nil {
return clusterData.Info.GetId(), nil
}

return "", fmt.Errorf("could not get cluster data for cluster name: %s", clusterName)
Expand Down

0 comments on commit 9ae5ec7

Please sign in to comment.